-
Notifications
You must be signed in to change notification settings - Fork 19
/
generate_index.py
62 lines (47 loc) · 1.62 KB
/
generate_index.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
#!/usr/bin/env python3
import re
import os
from jinja2 import Environment, FileSystemLoader
PATH = os.path.dirname(os.path.abspath(__file__))
TEMPLATE_ENVIRONMENT = Environment(
autoescape=False,
loader=FileSystemLoader(PATH),
trim_blocks=False)
def render_template(template_filename, context):
return TEMPLATE_ENVIRONMENT.get_template(template_filename).render(context)
def version_part_parse(part):
if not part:
return 0
return int(part)
def version_parse(version):
# packaging.version.parse does not parse some of the existing version numbers
return tuple([version_part_parse(v)
for v in version.replace("post", ".").replace("p", ".").split(".")])
def create_index_html():
fname = "docs/index.html"
versions = []
for fn in os.listdir('docs'):
m = re.match(r'^\d\..*', fn)
if m is not None:
v = version_parse(fn)
if v > version_parse("0.8.5"):
versions.append((v, fn))
versions.sort(reverse=True)
current_version = versions[0][1]
os.system("find docs/{} -name '*.html' | xargs python3 add-tracking.py".format(current_version))
os.system("rm -f docs/current")
os.system("ln -sf {} docs/current".format(current_version))
older_versions = versions[1:]
context = {
'current_version': current_version,
'older_versions': older_versions,
}
#
with open(fname, 'w') as f:
html = render_template('index.html.j2', context)
f.write(html)
def main():
create_index_html()
########################################
if __name__ == "__main__":
main()