-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_sync.py
73 lines (62 loc) · 2.04 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
65
66
67
68
69
70
71
72
73
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
from pathlib import Path
# git root path for files to push to remote
DIR_FOR_GIT = os.path.dirname(os.path.abspath(__file__))
# 忽略目录
IGNORE_DIR_LIST = [
'.git',
]
global cnt
class FileChangeHandler(FileSystemEventHandler):
def on_modified(self, event):
# print('文件发生变化')
cnt++
if cnt % 10 != 0: # 十次变化一次提交
continue
src_path = event.src_path.replace('\\', '/')
base_name = os.path.basename(src_path)
if base_name not in IGNORE_DIR_LIST:
print('base_name:', base_name)
print('src_path:', src_path)
os.chdir(DIR_FOR_GIT)
git_add_cmd = "git add -A"
git_commit_cmd = "git commit -m " + re.escape("Update " + base_name)
if platform.system() == "Windows":
git_commit_cmd = "git commit -m Update."
git_pull_cmd = "git pull origin main"
git_push_cmd = "git push origin main"
call(
git_add_cmd + "&&" +
git_commit_cmd + "&&" +
git_pull_cmd + "&&" +
git_push_cmd,
shell=True
)
if __name__ == "__main__":
observer = Observer()
event_handler = FileChangeHandler()
current_dir = Path.cwd()
# 遍历当前目录
for item in current_dir.iterdir():
if item.is_dir() and item.name not in IGNORE_DIR_LIST:
print(item.name)
file_path = os.path.join(DIR_FOR_GIT, item.name)
print('当前文件路径', file_path)
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:
print('服务中断')
observer.stop()
observer.join()