-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
executable file
·74 lines (62 loc) · 2.28 KB
/
generate.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
#!/usr/bin/env python3
import os, sys, subprocess, configparser
try:
import jinja2 as jinja
except ImportError:
print("To compile this website, you must install jinja")
os.exit(1)
cp = configparser.ConfigParser(
interpolation = configparser.ExtendedInterpolation()
)
cp.optionxform = str # be case sensitive in keys
cp.read(sys.argv[1])
conf = cp['main']
pages = cp['pages']
cp['autogen'] = {}
# Get the Git version and populate 'Version' with it.
try:
cp['autogen']['FullVersion'] = subprocess.check_output(
["git", "rev-parse", "HEAD"],
universal_newlines=True)
cp['autogen']['VersionSummary'] = subprocess.check_output(
["git", "describe", "--always", "--dirty=+"],
universal_newlines=True)
cp['autogen']['LastChanged'] = subprocess.check_output(
["git", "log", "-1", "--pretty=format:%ad", "--date=short"],
universal_newlines=True)
except (subprocess.CalledProcessError, FileNotFoundError) as e:
print("Could not get Git version: %s" % e)
print("Continuing...")
env = jinja.Environment(loader=jinja.FileSystemLoader(conf.get('TemplatePath')))
for pagename, page in pages.items():
# Handle the root page in a sensible way.
if page == "/" or page == "":
page = "index.html"
# Get the filename of the page.
pagepath = os.path.join(conf.get('PagePath'), page)
outputpath = os.path.join(conf.get('OutputPath'), page)
print("%s... " % page, end='')
if pagename in cp:
data = cp[pagename]
else:
data = {}
try:
with open(pagepath, 'r') as fi:
paget = env.from_string(fi.read())
with open(outputpath, 'w') as fo:
fo.write( paget.render(
root = conf.get('RootURL'),
pages = pages,
links = cp['links'],
info = cp['info'],
repository = conf.get('Repository'),
versionlink = conf.get('RepositoryVersion'),
GA_ID = conf.get('GA_ID'),
versionsum = cp['autogen'].get('VersionSummary'),
lastchanged = cp['autogen'].get('LastChanged'),
data = data,
))
print("OK")
except IOError as e:
print("Failed!")
print(" %s" % e)