/
build.py
executable file
·129 lines (98 loc) · 3.54 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python3
# coding: utf-8
import argparse
import json
import os
import platform
import shutil
from datetime import datetime
from lxml import etree
###############################################################################
# Options
###############################################################################
PARSER = argparse.ArgumentParser()
PARSER.add_argument(
'-C', '--clean-all', dest='clean_all', action='store_true',
help='''Cleans all the Node & Bower related tools / libraries and
updates them to their latest versions''',
)
ARGS = PARSER.parse_args()
###############################################################################
# Globals
###############################################################################
BAD_ENDINGS = ['~']
###############################################################################
# Directories
###############################################################################
DIR_MAIN = '.'
DIR_APACHE = os.path.join(DIR_MAIN, 'apache')
DIR_TEMPLATES = os.path.join(DIR_MAIN, 'templates')
DIR_TEMPLATES_APACHE = os.path.join(DIR_TEMPLATES, 'apache')
class FileResolver(etree.Resolver):
def resolve(self, url, identifier, context):
return self.resolve_filename(url, context)
###############################################################################
# Helpers
###############################################################################
def print_out(script, filename=''):
timestamp = datetime.now().strftime('%H:%M:%S')
if not filename:
filename = '-' * 46
script = script.rjust(12, '-')
print('[%s] %12s %s', timestamp, script, filename)
def make_dirs(directory):
if not os.path.exists(directory):
os.makedirs(directory)
def remove_file_dir(file_dir):
if isinstance(file_dir, list) or isinstance(file_dir, tuple):
for file_ in file_dir:
remove_file_dir(file_)
else:
if not os.path.exists(file_dir):
return
if os.path.isdir(file_dir):
shutil.rmtree(file_dir, ignore_errors=True)
else:
os.remove(file_dir)
def read_json(path):
with open(path) as f:
return json.load(f)
def create_apache_docs():
conf = read_json(os.path.join(DIR_APACHE, "config.json"))
result = []
for item in conf:
r = {
"file": item["id"],
"xml": os.path.join(DIR_APACHE, item["id"] + ".xml"),
"stylesheet": os.path.join(DIR_APACHE, item["stylesheet"] + ".xsl"),
}
result.append(r)
return result
def compile_xslt():
docs = create_apache_docs()
make_dirs(DIR_TEMPLATES_APACHE)
for d in docs:
parser = etree.XMLParser(load_dtd=False, dtd_validation=False)
parser.resolvers.add(FileResolver())
xml_input = etree.parse(d["xml"], parser)
stylesheet = d["stylesheet"]
xslt_root = etree.parse(stylesheet, parser)
transform = etree.XSLT(xslt_root)
content = transform(xml_input)
with open(os.path.join(DIR_TEMPLATES_APACHE, d["file"] + ".html"), 'wb') as fout:
fout.write(content)
###############################################################################
# Main
###############################################################################
def run_clean_all():
print_out('CLEAN ALL')
remove_file_dir([
DIR_TEMPLATES_APACHE
])
def run():
os.chdir(os.path.dirname(os.path.realpath(__file__)))
if ARGS.clean_all:
run_clean_all()
compile_xslt()
if __name__ == '__main__':
run()