-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
302 lines (249 loc) · 10.8 KB
/
api.py
File metadata and controls
302 lines (249 loc) · 10.8 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import requests
import os
import base64
from datetime import datetime
from gamuLogger import Printer, deep_debug, debug, info, warning, error, critical, deep_debug_func, debug_func, COLORS
type Commit_sha = str
type Tree_sha = str
type Blob_sha = str
class RequestError(Exception):
def __init__(self, code, message):
msg = ""
if(code == 400):
msg = "Bad Request: Invalid request"
elif(code == 401):
msg = "Unauthorized: Invalid token"
elif(code == 402):
msg = "Payment Required: Rate limit exceeded"
elif(code == 403):
msg = "Forbidden: Insufficient permissions"
elif(code == 404):
msg = "Not Found: Repository not found"
elif(code == 405):
msg = "Method Not Allowed: Invalid method"
elif(code == 406):
msg = "Not Acceptable: Invalid Accept header"
elif(code == 422):
msg = "Unprocessable Entity: Invalid request"
elif(code == 429):
msg = "Too Many Requests: Rate limit exceeded"
elif(code == 500):
msg = "Internal Server Error: Server error"
elif(code == 501):
msg = "Not Implemented: Invalid request"
elif(code == 502):
msg = "Bad Gateway: Server error"
elif(code == 503):
msg = "Service Unavailable: Server error"
elif(code == 504):
msg = "Gateway Timeout: Server error"
elif(code == 505):
msg = "HTTP Version Not Supported: Invalid request"
elif(code == 506):
msg = "Variant Also Negotiates: Invalid request"
elif(code == 507):
msg = "Insufficient Storage: Server error"
elif(code == 508):
msg = "Loop Detected: Server error"
elif(code == 510):
msg = "Not Extended: Invalid request"
elif(code == 511):
msg = "Network Authentication Required: Invalid request"
else:
msg = "Unknown Error"
super().__init__(f"Error {code}: {msg} - {message}")
@staticmethod
def from_response(response):
"""Raise a RequestError from a response object if the status code is not 200."""
if response.status_code < 200 or response.status_code >= 300:
raise RequestError(response.status_code, response.json()['message'])
class API:
def __init__(self, token, repository, branch='main', simulate=False):
self.simulate = simulate
self.headers = {
'Authorization': f'Bearer {token}',
'Accept': 'application/vnd.github+json'
}
debug(f'API headers: {self.headers}')
self.base_url = 'https://api.github.com'
self.repository_url = f'{self.base_url}/repos/{repository}'
self.repository = repository
self.branch = branch
self.fileList = {} # type: dict[str, datetime] # path : edit datetime
debug(f'Repository URL: {self.repository_url}')
cwd = os.getcwd()
debug(f'Current working directory: {cwd}')
name = self.repository.split('/')[-1]
self._path = f'{cwd}/{name}'
debug(f'Path: {self._path}')
def abs(self, path):
return f'{self._path}/{path}'
def __get(self, url):
deep_debug(f'GET {url}')
response = requests.get(url, headers=self.headers)
deep_debug(f'Response: {response.status_code}\n response keys: {response.json().keys()}')
RequestError.from_response(response)
return response.json()
def __post(self, url, data):
deep_debug(f'POST {url}\n{data if len(str(data)) < 200 else "data too long to be displayed"}')
response = requests.post(url, headers=self.headers, json=data)
deep_debug(f'Response: {response.status_code}\n response keys: {response.json().keys()}')
RequestError.from_response(response)
return response.json()
def __download_file(self, url, path):
debug(f'Downloading {path} ...')
content = self.__get(url)['content']
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, 'wb') as f:
f.write(base64.b64decode(content))
debug('Downloaded')
def __download_dir(self, url, path):
tree = self.__get(url)['tree']
for item in tree:
if item['type'] == 'blob':
self.__download_file(item['url'], f'{path}/{item["path"]}')
else:
self.__download_dir(item['url'], f'{path}/{item["path"]}')
def clone(self) -> bool:
info(f"Looking for cloning {self.repository} ...")
cwd = os.getcwd()
name = self.repository.split('/')[-1]
if os.path.exists(name):
error(f"Directory {name} already exists")
return False
os.mkdir(name)
url = f'{self.repository_url}/git/refs/heads/{self.branch}'
sha = self.__get(url)['object']['sha']
url = f'{self.repository_url}/git/trees/{sha}'
tree = self.__get(url)['tree']
info(f"Cloning {self.repository}:{self.branch} into {self.path()} ...")
for item in tree:
if item['type'] == 'blob':
self.__download_file(item['url'], f'{self.path()}/{item["path"]}')
else:
self.__download_dir(item['url'], f'{self.path()}/{item["path"]}')
info('Repository cloned')
self.fileList = self.__scan_files()
return True
def __scan_files(self) -> dict[str, datetime]:
debug(f'Scanning files in {self.path()} ...')
data = {}
for root, dirs, files in os.walk(self.path()):
for file in files:
path = f'{root}/{file}'
data[path] = datetime.fromtimestamp(os.path.getmtime(path))
debug(f"{len(data)} files found")
return data
def __create_blob(self, filepath) -> Blob_sha:
deep_debug(f'Creating blob for {filepath} ...')
url = f'{self.repository_url}/git/blobs'
content = open(filepath, 'r', encoding='utf-8').read()
content64 = base64.b64encode(content.encode()).decode()
result = self.__post(url, {'content': content64, 'encoding': 'base64'})['sha']
deep_debug(f'Blob created: {result}')
return result
def getRelPath(self, path):
return path.replace(self.path() + "/", "")
def __create_tree(self, fileTree) -> Tree_sha:
tree = []
for file in fileTree["files"]:
path = self.getRelPath(file)
filename = os.path.basename(path)
debug(f"Adding {path} to tree...")
tree.append({
'path': filename,
'mode': '100644',
'type': 'blob',
'sha': self.__create_blob(file)
})
for dir in fileTree["dirs"]:
tree.append({
'path': dir["name"],
'mode': '040000',
'type': 'tree',
'sha': self.__create_tree(dir)
})
if self.simulate:
debug('Simulation mode enabled; skipping tree creation on GitHub')
info("created tree :\n"+str(tree))
return self.__post(f'{self.repository_url}/git/trees', {'tree': tree})['sha']
@deep_debug_func
def __get_commit_sha(self) -> Commit_sha:
return self.__get(f'{self.repository_url}/git/refs/heads/{self.branch}')['object']['sha']
@deep_debug_func
def __create_commit(self, message, tree_sha) -> Commit_sha:
json={
'message': message,
'tree': tree_sha,
'parents': [self.__get_commit_sha()]
}
return self.__post(f'{self.repository_url}/git/commits', json)['sha']
@deep_debug_func
def __update_ref(self, commit_sha):
url = f'{self.repository_url}/git/refs/heads/{self.branch}'
self.__post(url, {'sha': commit_sha})
def push(self, message):
info("Looking for changes ...")
newFileList = self.__scan_files()
changes = []
for path in newFileList:
if path not in self.fileList or newFileList[path] != self.fileList[path]:
changes.append(path)
if len(changes) == 0:
info("No changes found; doing nothing")
return
info(f"Pushing {len(changes)} changes to {self.repository} in {self.branch} ...")
tree = self.__build_fileTree()
debug(f"Committing changes in {self.repository} ...")
if self.simulate:
debug('Simulation mode enabled; skipping commit creation and ref update on GitHub')
else:
tree_sha = self.__create_tree(tree)
commit_sha = self.__create_commit(message, tree_sha)
self.__update_ref(commit_sha)
info('Changes pushed')
def __build_fileTree(self):
return self.__recurse_build_fileTree(self.path())
def __recurse_build_fileTree(self, path):
deep_debug(f"Building file tree for {path} ...")
fileTree = {"files": [], "dirs": []}
for filepath in os.listdir(path):
fullpath = f'{path}/{filepath}'
if os.path.isfile(fullpath):
fileTree["files"].append(fullpath)
else:
fileTree["dirs"].append({
"name": filepath,
"files": self.__recurse_build_fileTree(fullpath)["files"],
"dirs": self.__recurse_build_fileTree(fullpath)["dirs"]
})
deep_debug(f"File tree for {path} built")
return fileTree
def clean(self):
debug('Cleaning up ...')
cwd = os.getcwd()
name = self.repository.split('/')[-1]
os.system(f'rm -rf {name}')
info(f'Directory {name} removed')
def __enter__(self):
"""Usage:\n
```
with API(token, 'gamunetwork/gamunetwork.github.io') as (api, path):
# modify files in path
with open(f'{path}/path/to/file', 'w', encoding='utf-8') as f:
f.write('Hello World!')
# push changes
api.push('Updating files')
```"""
self.auto_clean = True
debug('auto clean enabled; use `api.auto_clean = False` to disable')
if not self.clone():
raise Exception(f"Failed to clone repository {self.repository}")
return self, self.path()
def __exit__(self, exc_type, exc_value, traceback):
if self.auto_clean:
self.clean()
else:
warning(f'auto clean disabled; the folder {self.path()} was not removed')
def path(self):
return self._path