-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_front.py
137 lines (103 loc) · 4.15 KB
/
get_front.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
# _ _ _ _ _ __ __ ____
# | | (_) | | | | (_) | \/ |/ __ \
# | | _ | |_ | |__ _ _ _ _ __ ___ | \ / | | | |
# | | | | | __| | "_ \ | | | | | | | "_ ` _ \ | |\/| | | | |
# | |____ | | | |_ | | | | | | | |_| | | | | | | | | | | | |__| |
# |______| |_| \__| |_| |_| |_| \__,_| |_| |_| |_| |_| |_|\___\_\
from zipfile import ZipFile
from requests import get
from os import path, remove
class ReleaseNotFound(Exception):
...
class AssetsNotFound(Exception):
...
class BadAssetError(Exception):
...
extract_config = {
'templates': ['index.html'],
}
def get_zip() -> (str, bytes):
releases_content = get('https://api.github.com/repos/'
'tikovka72/limq-front/releases/latest')
if releases_content.status_code != 200:
raise ReleaseNotFound("can't find release in limq-front")
latest_release = releases_content.json()
assets = latest_release.get('assets')
if not assets:
raise AssetsNotFound("can't find assets in limq-front")
first_asset = assets[0]
asset_url = first_asset.get('browser_download_url')
zip_name = first_asset.get('name')
if not asset_url or not zip_name:
raise BadAssetError("can't find url in asset")
file = get(asset_url).content
return zip_name, file
def write_zip(zip_name: str, file: bytes):
with open(zip_name, 'wb') as zip_file:
zip_file.write(file)
def delete_old_front(rules: dict):
if path.exists('.old_front.txt'):
with open('.old_front.txt', encoding='utf8') as f:
old_files = f.readlines()
with open('.gitignore', 'r', encoding='utf8') as gr:
if (old_strings := '\n\n' + ''.join(old_files)) \
in (gitignore := gr.read()):
with open('.gitignore', 'w', encoding='utf8') as gw:
print(gitignore.replace(old_strings, ''))
gw.write(gitignore.replace(old_strings, ''))
for rule in rules.keys():
for rule_file in rules[rule]:
if rule_file in ''.join(old_files):
file = path.join(rule, rule_file)
try:
remove(path.abspath(file))
except FileNotFoundError:
...
except PermissionError:
print(f"\tCan't delete: {file}")
except IsADirectoryError:
print(f"\tDirectory: {file}")
for old_file in old_files:
file = old_file.replace('\n', '')
try:
remove(path.abspath(file))
except FileNotFoundError:
...
except PermissionError:
print(f"\tCan't delete: {file}")
except IsADirectoryError:
print(f"\tDirectory: {file}")
def unzip_files(name: str, rules: dict) -> list:
with ZipFile(name, 'r') as zip_file:
all_files = list(zip_file.namelist())
for rule_path in rules.keys():
for member in rules[rule_path]:
if member in all_files:
all_files.remove(member)
zip_file.extract(member, rule_path)
for other_member in all_files:
zip_file.extract(other_member)
return list(zip_file.namelist())
def write_current_files(all_files: list):
with open('.old_front.txt', 'w', encoding='utf8') as f:
f.write('\n'.join(all_files))
with open('.gitignore', 'a', encoding='utf8') as g:
g.write('\n\n' + '\n'.join(all_files))
def remove_zip(zip_name: str):
remove(zip_name)
def main():
zip_name, zip_file = get_zip()
print(f'file downloaded: {zip_name}')
write_zip(zip_name, zip_file)
print('file written')
delete_old_front(extract_config)
print('old front files deleted')
all_files = unzip_files(zip_name, extract_config)
print('all files unzipped')
write_current_files(all_files)
print('new files saved in .old_front.txt')
remove_zip(zip_name)
print('zip file deleted')
print('all done!')
if __name__ == '__main__':
main()