-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
187 lines (168 loc) · 6.51 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import os
import requests
import sys
import base64
import json, time
from threading import Thread
import json
import os
import win32file
import win32con
# 打开文件并读取内容
with open('config.json', 'r', encoding='utf-8') as file:
# 使用json.load()方法解析JSON数据
configdata = json.load(file)
token = configdata['token']
logfile = configdata['logfile']
apiurl = configdata['apiurl']
committername = configdata['committername']
committeremail = configdata['committeremail']
folder = configdata['folder']
pathtowatch = configdata['pathtowatch']
headers = {"Authorization": "token " + token}
requests.packages.urllib3.disable_warnings()
sys.stdout=open(logfile, "a+")
# 读取文件
def open_file(file_path):
with open(file_path, 'rb') as f:
return f.read()
# 将文件转换为base64编码,上传文件必须将文件以base64格式上传
def file_base64(data):
data_b64 = base64.b64encode(data).decode('utf-8')
return data_b64
def upload(file_data, file_name):
url = apiurl+file_name.replace("\\","/").replace(folder,"")
req = requests.get(url=url, headers=headers, verify=False)
req.encoding = "utf-8"
re_data = json.loads(req.text)
upload_file(file_data, file_name, re_data["sha"])
# 上传文件
def upload_file(file_data, file_name, sha=""):
url = apiurl+file_name.replace("\\","/").replace(folder,"")
content = file_base64(file_data)
data = {
"message": file_name,
"committer": {
"name": committername,
"email": committeremail
},
"content": content
}
if sha:
data["sha"] = sha
data = json.dumps(data)
req = requests.put(url=url, data=data, headers=headers, verify=False)
req.encoding = "utf-8"
re_data = json.loads(req.text)
if "content" not in re_data:
return upload(file_data, file_name)
print(re_data['content']['sha'])
def rm(file_name):
url = apiurl+file_name.replace("\\","/").replace(folder,"")
req = requests.get(url=url, headers=headers, verify=False)
req.encoding = "utf-8"
re_data = json.loads(req.text)
data = {
"message": file_name,
"committer": {
"name": committername,
"email": committeremail
},
"sha": re_data["sha"]
}
data = json.dumps(data)
req = requests.put(url=url, data=data, headers=headers, verify=False)
print("<span class="">Deleted", file_name,"</span>")
ACTIONS = {
1 : "Created",
2 : "Deleted",
3 : "Updated",
4 : "Renamed from something",
5 : "Renamed to something"
}
FILE_LIST_DIRECTORY = win32con.GENERIC_READ | win32con.GENERIC_WRITE
path_to_watch = pathtowatch
hDir = win32file.CreateFile (
path_to_watch,
FILE_LIST_DIRECTORY,
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
None,
win32con.OPEN_EXISTING,
win32con.FILE_FLAG_BACKUP_SEMANTICS,
None
)
def tryup(action, file):
full_filename = os.path.join(path_to_watch, file)
print (full_filename, ACTIONS.get (action, "Unknown"))
if ACTIONS.get (action, "Unknown") in ["Renamed from something", "Renamed to something","Deleted"]:
try:
rm(full_filename)
except requests.exceptions.HTTPError as errh:
print("HTTP错误:", errh)
time.sleep(30)
return tryup(action, file)
except requests.exceptions.ConnectionError as errc:
print("连接错误:", errc)
time.sleep(30)
return tryup(action, file)
except requests.exceptions.Timeout as errt:
print("超时错误:", errt)
time.sleep(30)
return tryup(action, file)
except requests.exceptions.RequestException as err:
print("其他错误:", err)
time.sleep(30)
return tryup(action, file)
except:
time.sleep(30)
return tryup(action, file)
if ACTIONS.get (action, "Unknown")!="Deleted":
while 1:
try:
fdata = open_file(full_filename)
break
except FileNotFoundError:
return
except:
pass
try:
upload_file(fdata, full_filename)
except KeyError:
time.sleep(30)
return tryup(action, file)
except requests.exceptions.HTTPError as errh:
print("HTTP错误:", errh)
time.sleep(30)
return tryup(action, file)
except requests.exceptions.ConnectionError as errc:
print("连接错误:", errc)
time.sleep(30)
return tryup(action, file)
except requests.exceptions.Timeout as errt:
print("超时错误:", errt)
time.sleep(30)
return tryup(action, file)
except requests.exceptions.RequestException as err:
print("其他错误:", err)
time.sleep(30)
return tryup(action, file)
except:
time.sleep(30)
return tryup(action, file)
if __name__ == '__main__':
while 1:
results = win32file.ReadDirectoryChangesW (
hDir, #handle: Handle to the directory to be monitored. This directory must be opened with the FILE_LIST_DIRECTORY access right.
1024, #size: Size of the buffer to allocate for the results.
True, #bWatchSubtree: Specifies whether the ReadDirectoryChangesW function will monitor the directory or the directory tree.
win32con.FILE_NOTIFY_CHANGE_FILE_NAME |
win32con.FILE_NOTIFY_CHANGE_DIR_NAME |
win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES |
win32con.FILE_NOTIFY_CHANGE_SIZE |
win32con.FILE_NOTIFY_CHANGE_LAST_WRITE |
win32con.FILE_NOTIFY_CHANGE_SECURITY,
None,
None)
for action, file in results:
sys.stdout.flush()
Thread(target=tryup, args=(action, file)).start()