-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathfile_sync.py
executable file
·64 lines (55 loc) · 1.84 KB
/
file_sync.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
63
64
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import time
import ntpath
import os
import re
import platform
from subprocess import call
from shutil import copy
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
# git root path for files to push to remote
DIR_FOR_GIT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# files to synchronize
SYNC_FILE_LIST = []
f = open(os.path.join(DIR_FOR_GIT, "file_list.txt"), "r")
try:
SYNC_FILE_LIST = [line.strip().replace('\\','/') for line in f if os.path.isfile(line.strip().replace('\\','/'))]
except Exception as e:
raise e
finally:
f.close()
class FileChangeHandler(FileSystemEventHandler):
def on_modified(self, event):
src_path = event.src_path.replace('\\','/')
if src_path in SYNC_FILE_LIST:
copy(src_path, DIR_FOR_GIT)
os.chdir(DIR_FOR_GIT)
git_add_cmd = "git add -A"
git_commit_cmd = "git commit -m " + re.escape("Update "+os.path.basename(src_path))
if platform.system() == "Windows":
git_commit_cmd = "git commit -m Update."
git_pull_cmd = "git pull origin master"
git_push_cmd = "git push origin master"
call(
git_add_cmd + "&&" +
git_commit_cmd + "&&" +
git_pull_cmd + "&&" +
git_push_cmd,
shell=True
)
if __name__ == "__main__":
observer = Observer()
event_handler = FileChangeHandler()
for file_path in SYNC_FILE_LIST:
copy(file_path, DIR_FOR_GIT)
observer.schedule(event_handler, path=os.path.dirname(os.path.realpath(file_path)), recursive=False)
observer.start()
try:
while True:
time.sleep(10)
except KeyboardInterrupt:
observer.stop()
observer.join()