-
Notifications
You must be signed in to change notification settings - Fork 13
/
shared.py
61 lines (45 loc) · 1.31 KB
/
shared.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
#!/usr/bin/env python3
r"""
shared.py
-John Taylor
May-14-2020
Functions shared between build.py and version_compare.py
"""
import re
import time
import urllib.request
LESSURL = "http://greenwoodsoftware.com/less/download.html"
version_url_re = re.compile(r"""Download <strong>RECOMMENDED</strong> version (.*?) """, re.M | re.S | re.I)
NEWFILE = "new.txt"
def download_less_web_page() -> str:
"""Download LESSURL and save the contents to fname
Returns:
An in-memory version of the downloaded web page
"""
fname = "download.html"
try:
urllib.request.urlretrieve(LESSURL, fname)
time.sleep(1)
except:
return False
try:
with open(fname) as fp:
page = fp.read()
except:
return False
return page
def get_latest_version_url(page: str) -> tuple:
"""Return the URL for the "RECOMMENDED version"
Args:
page: an HTML web page, provided in LESSURL
Returns:
A tuple containing: (version number, zip archive URL)
Ex: 551, http://greenwoodsoftware.com/less/less-551.zip
"""
match = version_url_re.findall(page)
if not len(match):
return (None, None)
version = match[0]
archive = "less-%s.zip" % version
url = LESSURL.replace("download.html", archive)
return version, url