-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyncfiles.py
62 lines (49 loc) · 1.5 KB
/
syncfiles.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
from dirsync import sync
from envparse import env
from pushover import Client
"""
* Syncs the SOURCE_DIR to the TARGET_DIR
* Sends notifications as needed via Pushover
"""
env.read_envfile()
SOURCE_DIR = env('SOURCE_DIR')
TARGET_DIR = env('TARGET_DIR')
PUSHOVER_API_TOKEN = env('PUSHOVER_API_TOKEN')
PUSHOVER_USER_KEY = env('PUSHOVER_USER_KEY')
APP_NAME = env('APP_NAME')
def send_notification(title, msg):
client = Client(PUSHOVER_USER_KEY, api_token=PUSHOVER_API_TOKEN)
client.send_message(msg, title=title)
def run():
# make sure the source dir exists, exit if it doesn't
if not os.path.exists(SOURCE_DIR):
msg = "SOURCE_DIR {} doesn't exist!".format(SOURCE_DIR)
print(msg)
send_notification(
'{} - ERROR'.format(APP_NAME),
msg
)
exit(1)
# make sure the target dir exists, create if it doesn't
if not os.path.exists(TARGET_DIR):
print("TARGET_DIR {} doesn't exist. Creating ...".format(TARGET_DIR))
os.mkdir(TARGET_DIR)
print('Syncing files ...')
try:
sync(SOURCE_DIR, TARGET_DIR, 'sync', purge=True)
msg = 'Successful sync!'
print(msg)
send_notification(
'{} - SYNC OK'.format(APP_NAME),
msg
)
except Exception as e:
msg = 'ERROR: {}'.format(str(e))
print(msg)
send_notification(
'{} - SYNC FAILED'.format(APP_NAME),
msg
)
if __name__ == '__main__':
run()