-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync_ondemand.py
31 lines (28 loc) · 1.04 KB
/
sync_ondemand.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import sys
import json
import datetime
import traceback
import smtplib
from email.mime.text import MIMEText
import main
# Attempt a sync; send failure email with traceback if error.
# Name of configuration is file passed via command-line
try:
print('Start sync at ' + str(datetime.datetime.now()))
main.init_config(sys.argv[1])
main.main_sync()
print('Done at ' + str(datetime.datetime.now()))
except Exception as e:
with open(sys.argv[1]) as config_file:
smtp_config = json.load(config_file)['smtp']
print('Exception at ' + str(datetime.datetime.now()) +
'! Check notification email.')
msg = MIMEText('Sync failed at ' + str(datetime.datetime.now()) + '\n\nError: '
+ str(traceback.format_exc()))
msg['Subject'] = smtp_config['subject']
msg['From'] = smtp_config['from']
msg['To'] = smtp_config['to']
with smtplib.SMTP(smtp_config['server']) as smtp:
smtp.starttls()
smtp.login(smtp_config['username'], smtp_config['password'])
smtp.send_message(msg)