-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdater.py
108 lines (90 loc) · 2.32 KB
/
updater.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
"""
Updater for krpg
"""
import glob
import json
import os
import zlib
import urllib3
LINK = "https://raw.githubusercontent.com/kotazzz/krpg/master/"
FILES = ["krpg/**/*.py", "krpg/**/*.krpg", "krpg/*.*", "updater.py"]
def local_hashes() -> dict[str, int]:
"""Get local hashes of files
Returns
-------
dict[str, int]
dict with hashes
"""
# find all files
hashes = {}
for i in FILES:
# get all files by mask
for j in glob.glob(i, recursive=True):
# get hash
with open(j, "rb") as f:
j = j.replace("\\", "/")
# CRLF -> LF
fixed = f.read().replace(b"\r\n", b"\n")
hashes[j] = zlib.crc32(fixed)
return hashes
def get_hashes() -> dict[str, int]:
"""Get hashes from github
Returns
-------
dict[str, int]
dict with hashes
Raises
------
ValueError
Can't get hashes
"""
http = urllib3.PoolManager()
r = http.request("GET", LINK + "hashes.json")
if r.status == 200:
return json.loads(r.data)
raise ValueError("Can't get hashes")
def update():
"""Update krpg
Raises
------
ValueError
Can't download file
"""
# get hashes from both sides
local = local_hashes()
remote = get_hashes()
queue = []
for i in remote:
if i not in local:
print(f"New file: {i}")
queue.append(i)
elif local[i] != remote[i]:
print(f"Updated: {i}")
queue.append(i)
for i in local:
if i not in remote:
print(f"Deleted: {i}")
os.remove(i)
if not queue:
print("Nothing to update")
return
for i in queue:
print(f"Downloading: {i}")
# download
url = LINK + i
http = urllib3.PoolManager()
r = http.request("GET", url)
if r.status != 200:
raise ValueError(f"Can't download {url}: {r.status}, {r.reason}")
# create folders
try:
os.makedirs(os.path.dirname(i), exist_ok=True)
except FileNotFoundError:
print("Skipping folder creation")
with open(i, "wb") as f:
f.write(r.data.replace(b"\r\n", b"\n"))
def main():
"""Main function"""
update()
if __name__ == "__main__":
main()