-
Notifications
You must be signed in to change notification settings - Fork 13
/
version_compare.py
99 lines (77 loc) · 2.65 KB
/
version_compare.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
#!/usr/bin/env python3
r"""
version_compare.py
-John Taylor
May-14-2020
Compare local github version with less web site
"""
import json
import urllib.request
import re
import sys
from shared import download_less_web_page, get_latest_version_url, LESSURL, NEWFILE
LOCALURL = "https://api.github.com/repos/jftuga/less-Windows/releases"
def download_local_web_page() -> str:
"""Download and return JSON from LOCALURL
"""
try:
with urllib.request.urlopen(LOCALURL) as f:
page = f.read()
except:
return False
page = page.decode("utf-8")
return page
def get_latest_local_version(page: str) -> str:
"""Extract and return the lastest release version from a JSON page
Ex: 561
"""
try:
j = json.loads(page)
except:
return False
if not len(j):
return "500"
newest = j[0]
print(f'{newest["tag_name"]=}')
# The initial version is different than future versions
if "v560" == newest["tag_name"]:
return "560"
# given less-v561.17, return 561
release_version = newest["tag_name"][6:11]
if release_version.endswith(".0"):
release_version = re.sub("\.0$", "", release_version)
print(f'{release_version=}')
return release_version
def main():
"""Write to new.txt when a new version needs to be downloaded
"""
if not (page := download_local_web_page()):
print("Unable to download URL: %s" % (LOCALURL))
sys.exit(10)
if not (local_version := get_latest_local_version(page)):
print("Unable to extract version from URL: %s" % (LOCALURL))
sys.exit(20)
if not (page := download_less_web_page()):
print("Unable to download URL: %s" % (LESSURL))
sys.exit(30)
return
remote_version, _ = get_latest_version_url(page)
if remote_version is None:
print("Unable to extract version from: %s" % (LESSURL), file=sys.stderr)
sys.exit(40)
if remote_version == local_version:
print(f"Versions are the same: remote_version: {remote_version} local_version: {local_version}")
return
if float(local_version) >= float(remote_version):
print(f"Local version is newer: local_version: {local_version} remote_version: {remote_version}")
sys.exit(120)
print(f"Remote version is newer: remote_version: {remote_version} local_version: {local_version}")
print(f"Saving new version to file: {NEWFILE}")
try:
with open(NEWFILE, mode="w") as fp:
fp.write("%s\n" % remote_version)
except:
print(f"Unable able to open file for writing: {NEWFILE}")
sys.exit(50)
if "__main__" == __name__:
main()