代码拉取完成,页面将自动刷新
#!/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()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。