forked from cc004/autopcr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_download_web.py
104 lines (91 loc) · 3.57 KB
/
_download_web.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
import asyncio
import aiohttp
import os
import shutil
async def get_latest_release_info(owner, repo):
url = f"https://api.github.com/repos/{owner}/{repo}/releases/latest"
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
if response.ok:
release_info = await response.json()
tag_name = release_info['tag_name']
assets = release_info['assets']
asset_download_urls = [asset['browser_download_url'] for asset in assets]
return tag_name, asset_download_urls
else:
print(response.status)
print(await response.json())
return None, None
path = os.path.dirname(os.path.abspath(__file__))
async def check_version(version):
now_version = None
web_version = os.path.join(path, "autopcr", "http_server", "client_version")
if os.path.exists(web_version):
with open(web_version, "r") as f:
now_version = f.read().strip()
version = version.strip()
if not now_version or now_version != version:
return True
return False
async def save_version(version):
web_version = os.path.join(path, "autopcr", "http_server", "client_version")
with open(web_version, "w") as f:
f.write(version)
async def download_assets(asset_download_urls):
ret = []
async with aiohttp.ClientSession() as session:
for url in asset_download_urls:
async with session.get(url) as response:
if response.status == 200:
filename = url.split('/')[-1]
filepath = os.path.join(path, filename)
print(f"Downloading {filename} -> {filepath}")
with open(filepath, 'wb') as f:
while True:
chunk = await response.content.read(1024)
if not chunk:
break
f.write(chunk)
ret.append(filepath)
else:
print(f"Failed to download {url}")
raise Exception(f"Failed to download {url}")
return ret
async def extract_web(filepaths):
web_path = os.path.join(path, "autopcr", "http_server", "ClientApp")
if not os.path.exists(web_path):
os.makedirs(web_path)
print(f"Removed old web file")
shutil.rmtree(web_path)
for filepath in filepaths:
print(f"Unzipped {filepath}")
import zipfile
with zipfile.ZipFile(filepath, 'r') as zip_ref:
zip_ref.extractall(web_path)
print(f"Delete {filepath}")
os.remove(filepath)
async def do_download():
owner = 'Lanly109'
repo = 'AutoPCR_Web'
latest_release_tag, asset_download_urls = await get_latest_release_info(owner, repo)
if latest_release_tag and asset_download_urls:
print(f"Latest release tag: {latest_release_tag}")
if await check_version(latest_release_tag):
filepaths = await download_assets(asset_download_urls)
await extract_web(filepaths)
await save_version(latest_release_tag)
else:
print("Already up to date")
else:
print("Failed to fetch latest release info")
raise Exception("Failed to fetch latest release info")
async def main(zips):
if zips:
await extract_web(zips)
else:
await do_download()
if __name__ == "__main__":
import sys
zips = sys.argv[1:]
loop = asyncio.get_event_loop()
loop.run_until_complete(main(zips))