Skip to content

Commit

Permalink
The email will now send the complete execution log
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Fernandes Vieira committed Dec 11, 2018
1 parent 6ebfb0f commit 432458a
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 31 deletions.
11 changes: 2 additions & 9 deletions 1dsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

while True:
log = _log.Log(root) # logs all program operations
email = _email.Email() # email sent to the user, if asked
config = _config.Config(root) # main configuration file
control = _control.Control(root) # controls the sync schedule
email = _email.Email() # email sent to the user, if asked

if not config.load(log): # loads the main config file
raise SystemExit # force close if an error occurred
Expand All @@ -45,21 +45,14 @@
sync = _sync.Sync(os.path.join(dirpath, file)) # initializes the sync

if (not sync.load(log)) or (not sync.run(control, log)): # loads and runs the sync
# will enter here if any of the above operations returns an error
#log.error_occurred = True

if not sync.disable(log): # an error in trying to disable the sync will force close the program
raise SystemExit

email.append_message("[ERROR] " + os.path.join(dirpath, file)) # reports the error in the email

else:
email.append_message("[ OK ] " + os.path.join(dirpath, file)) # reports the sync success in the email

log.report("") # indentation of the log file

config.run_post_sync_script(log) # runs the post sync script

email.append_message(log.get_content())
config.send_email(email, log) # sends the email, if necessary

if not control.write(log): # writes the schedule file
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

## Português
### Aviso
**Cuidado ao ler este readme, pois ele difere entre versões do programa. Veja sempre o arquivo readme incluído na release que você baixou. Este readme, em particular, é referente à versão 2.5-alpha7**
**Cuidado ao ler este readme, pois ele difere entre versões do programa. Veja sempre o arquivo readme incluído na release que você baixou. Este readme, em particular, é referente à versão 2.5-alpha9**

**A versão atual é incompatível com o arquivo de configuração usado até a versão 2.4. Você terá que atualizar sua configuração manualmente, caso o arquivo seja de versões mais antigas. Veja o novo modelo em [configuração geral](#configuração-geral)**

Expand Down Expand Up @@ -318,7 +318,7 @@ Uma boa combinação é utilizar `run_continuously` juntamente com a inicializa
## English
### Warning
**Be careful when reading this readme because it differs between versions. Always read the readme file included in the release you downloaded. This particular readme refers to the version 2.5-alpha7**
**Be careful when reading this readme because it differs between versions. Always read the readme file included in the release you downloaded. This particular readme refers to the version 2.5-alpha9**
**The current version is incompatible with the configuration file used until 2.4. You will need to update your configuration manually, if the file were from older versions. See the new model in [general configuration](#general-configuration)**
Expand Down
4 changes: 2 additions & 2 deletions _about.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
'''

name = "1D-Sync"
version = "2.5-alpha8"
build_date = "2018/12/10"
version = "2.5-alpha9"
build_date = "2018/12/11"
codename = "Ghost River"
6 changes: 3 additions & 3 deletions _email.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def send(self, sender, password, addressee, log): # sends the email

try:
email_msg = EmailMessage()
email_msg.set_content(_strings.strings["email_header"] + self.message)
email_msg.set_content(self.message)

email_msg['Subject'] = _strings.strings["email_subject"]
email_msg['From'] = sender
Expand All @@ -40,5 +40,5 @@ def send(self, sender, password, addressee, log): # sends the email
log.report("error_email_send", detail=addressee)
return False

def append_message(self, text): # appends some text after the current message
self.message += text + "\n"
def append_message(self, text):
self.message += text
28 changes: 14 additions & 14 deletions _log.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
class Log:
def __init__(self, root): # initializes an empty log
self.path = os.path.join(root, _paths.logs_folder, time.strftime("%Y-%m-%d %H-%M-%S") + ".txt")

self.error_occurred = False
self.warning_occurred = False
self.sync_occurred = False

self.content = []
self.summary = []

self.repeated_line = ""
self.repeated_count = 0

def report(self, id, detail="", critical=False): # reports events in the log
# writes the string associated to the received id
# detail is used to include extra information in the message
# critical is used to make sure the log is written right away when a serious error occurred
if id.startswith("error"):
self.error_occurred = True

Expand All @@ -38,11 +38,7 @@ def report(self, id, detail="", critical=False): # reports events in the log
self.repeated_count += 1

if self.repeated_count <= _defaults.default_log_repeated_lines_threshold:
try:
self.insert(id, detail)
except:
# log the raw string if it was not identified
self.insert(id, detail)
self.insert(id, detail)

else:
if self.repeated_count > _defaults.default_log_repeated_lines_threshold:
Expand All @@ -53,7 +49,6 @@ def report(self, id, detail="", critical=False): # reports events in the log

self.insert(id, detail)


if critical:
return self.write()

Expand All @@ -75,18 +70,23 @@ def insert(self, id, detail):
# skip timestamp in this case but include some spacing for indentation
self.content.append(_strings.strings["prefix_timestamp_spacing"] + _strings.strings["prefix_spacing"] + id.replace("\n", "\n" + _strings.strings["prefix_timestamp_spacing"] + _strings.strings["prefix_spacing"]) + detail)

def get_content(self):
string = ""

string += _strings.strings["prefix_timestamp_spacing"] + _strings.strings["prefix_spacing"] + _about.name + " " + _about.version + " \"" + _about.codename + "\" build date:" + _about.build_date + "\n\n"
for c in self.content:
string += c + "\n"

return string

def write(self): # writes the message to the file system
try:
self.file = open(self.path, "w")
self.file.write(_strings.strings["prefix_timestamp_spacing"] + _strings.strings["prefix_spacing"] + _about.name + " " + _about.version + " \"" + _about.codename + "\" build date:" + _about.build_date + "\n\n")

for c in self.content:
self.file.write(c + "\n")

self.file.write(self.get_content())
self.file.close()

return True

except:
return False

Expand Down
1 change: 0 additions & 1 deletion _strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

# email
"email_subject": "1D-Sync Alert Service",
"email_header": "Last sync run status\n\n",

# Config
"ok_config_json_load": "config .json loaded",
Expand Down
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Options tied to malfunctions in config.json are now inverted, making them easier to understand while also removing the need to use precedence of options
* Add option to use a faster but inaccurate comparison technique, it will compare filenames only (like the early versions)
* Fixed a bug when the program would try to validate a disabled sync, possibly logging the failure multiple times
* The email now contains the complete execution log

2.4 - Fear of the Dark
This release focuses on making selection conditions more versatile by using a logical expression analyzer (a very simple one, don't expect too much) available as a separate project in https://github.com/1deterministic/Simple-Logical-Expression-Analyzer
Expand Down

0 comments on commit 432458a

Please sign in to comment.