-
Notifications
You must be signed in to change notification settings - Fork 16
/
update.py
executable file
·69 lines (54 loc) · 2 KB
/
update.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
#!/usr/bin/env python
"""
Update versionwarning.js and link it to docs
"""
import re
import os
import argparse
import html.parser
from packaging.version import parse as parse_version
class Scaper(html.parser.HTMLParser):
def __init__(self):
super().__init__()
self.numpy_versions = set()
def handle_starttag(self, tag, attrs):
if tag == "a":
href = dict(attrs).get("href", "")
if href:
m = re.match("^[0-9.]+$", href)
if m:
self.numpy_versions.add(parse_version(href))
def main():
p = argparse.ArgumentParser(description=__doc__.strip())
args = p.parse_args()
scraper = Scaper()
with open("index.html", "r", encoding="utf-8") as f:
scraper.feed(f.read())
scraper.close()
# Update versionwarning.js
with open("_static/versionwarning.js_t", "r", encoding="utf-8") as f:
text = f.read()
latest_version = str(max(scraper.numpy_versions))
print("Latest NumPy version:", latest_version)
text = text.replace("{{ NUMPY_LATEST_VERSION }}", latest_version)
with open("_static/versionwarning.js", "w", encoding="utf-8") as f:
f.write(text)
# Update doctools.js
for dirname in os.listdir():
if not os.path.isdir(dirname):
continue
doctools_js = os.path.join(dirname, "_static", "doctools.js")
if os.path.isfile(doctools_js):
with open(doctools_js, "r", encoding="utf-8") as f:
text = f.read()
if "versionwarning.js" not in text:
with open(doctools_js, "a", encoding="utf-8") as f:
f.write(
"\n"
'var script = document.createElement("script"); '
'script.type = "text/javascript"; '
'script.src = "/doc/_static/versionwarning.js"; '
"document.head.appendChild(script);"
)
if __name__ == "__main__":
main()