-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupdate_msi.py
73 lines (59 loc) · 2.14 KB
/
update_msi.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
import codecs
import os
from typing import Any, Dict, Optional
import requests
import wget
def read(rel_path: str) -> str:
"""
Read a file.
"""
here = os.path.abspath(os.path.dirname(__file__))
with codecs.open(os.path.join(here, rel_path), "r") as fp:
return fp.read()
def get_version() -> Any:
"""
Read app version from a file.
"""
file_path = "version.txt"
if not os.path.exists(file_path):
file_path = "i8_terminal/version.txt"
for line in read(file_path).splitlines():
if line.startswith("__version__"):
delim = '"' if '"' in line else "'"
return line.split(delim)[1]
else:
raise RuntimeError("Unable to find version string.")
def is_latest_version(latest_version: str) -> Any:
if latest_version:
app_version = get_version()
return app_version == latest_version
return None
def get_latest_release_metadata() -> Optional[Dict[str, Any]]:
resp = requests.get("https://api.github.com/repos/investoreight/i8-terminal/releases/latest")
if resp.status_code != 200:
print("No new release found.")
return None
msi_url = None
release_metadata = resp.json()
if release_metadata.get("assets"):
msi_url = release_metadata.get("assets")[0].get("browser_download_url")
return {"version": release_metadata["tag_name"][1:], "msi_url": msi_url}
def run_update() -> Any:
release_metadata = get_latest_release_metadata()
if not release_metadata:
return None
if not is_latest_version(latest_version=release_metadata["version"]):
if not release_metadata["msi_url"]:
print("Error while finding latest release!")
return
local_file = f"i8-terminal-{release_metadata['version']}-win64.msi"
try:
downloaded_msi = wget.download(release_metadata["msi_url"], local_file)
if downloaded_msi:
os.system(local_file) # Open downloaded MSI
except:
print("No new release found.")
else:
print("Latest version of i8-terminal is already installed.")
if __name__ == "__main__":
run_update()