-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.py
70 lines (56 loc) · 1.89 KB
/
build.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
import datetime
import hashlib
import os
import pathlib
import re
import sys
import tempfile
import idna
import urllib3
import psl
PACKAGE_PATH = pathlib.Path(__file__).parent / "psl" / "__init__.py"
def main() -> int:
http = urllib3.PoolManager()
resp = http.request("GET", psl.PUBLIC_SUFFIX_URL, preload_content=True)
sha1 = hashlib.sha1()
tmp = tempfile.mkstemp()[1]
with open(tmp, "w") as f:
for line in "".join(resp.data.decode("utf-8")).split("\n"):
line = line.strip()
if not line or (line.startswith("//") and "===" not in line):
continue
if any(ord(c) > 0x7F for c in line):
line = idna.encode(line, strict=True, std3_rules=True).decode()
sha1.update(line.encode("utf-8"))
f.write(line + "\n")
checksum = sha1.hexdigest()
if psl.__checksum__ == checksum:
print("Current package has latest Public Suffix list")
return 1
print("Updating psl.txt")
os.rename(tmp, psl._PUBLIC_SUFFIX_PATH)
print("Updating package metadata")
lines = []
today = datetime.date.today()
with open(PACKAGE_PATH, "r") as f:
for line in f:
if line.startswith("__version__"):
line = re.sub(
r"__version__\s*=\s*['\"][\d.]+['\"]",
f'__version__ = "{today.year}.{today.month}.{today.day}"',
line,
)
elif line.startswith("__checksum__"):
line = re.sub(
r"__checksum__\s*=\s*['\"][a-f0-9]+['\"]",
f'__checksum__ = "{checksum}"',
line,
)
lines.append(line)
with open(PACKAGE_PATH, "w") as f:
f.truncate()
f.write("".join(lines))
print("Successfully updated psl")
return 0
if __name__ == "__main__":
sys.exit(main())