1 Star 0 Fork 0

nsyouran/file_sync

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
file_sync.py 1.93 KB
一键复制 编辑 原始数据 按行查看 历史
Tim 提交于 2016-11-25 10:59 . Remove print.
#!/usr/bin/python
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() for line in f]
except Exception as e:
raise e
finally:
f.close()
# get filename without upper directory
def path_leaf(path):
head, tail = ntpath.split(path)
return tail or ntpath.basename(head)
class FileChangeHandler(FileSystemEventHandler):
def on_modified(self, event):
if event.src_path in SYNC_FILE_LIST:
copy(event.src_path, DIR_FOR_GIT)
cd_cmd = "cd "+DIR_FOR_GIT
git_add_cmd = "git add -A"
git_commit_cmd = "git commit -m " + re.escape("Update "+path_leaf(event.src_path))
if platform.system() == "Windows":
git_commit_cmd = "git commit -m " + re.escape("Update_"+path_leaf(event.src_path))
git_pull_cmd = "git pull origin master"
git_push_cmd = "git push origin master"
call(
cd_cmd + "&&" +
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()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/nsyouran/file_sync.git
git@gitee.com:nsyouran/file_sync.git
nsyouran
file_sync
file_sync
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385