forked from raspberrypi/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_auto_ninjabuild.py
More file actions
executable file
·195 lines (175 loc) · 7.69 KB
/
Copy pathcreate_auto_ninjabuild.py
File metadata and controls
executable file
·195 lines (175 loc) · 7.69 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python3
import os.path
import sys
import json
import re
import yaml
import ninja_syntax
def resolve_url(filename, relative_link):
return os.path.normpath(os.path.join(os.path.dirname(filename), relative_link))
def scan_adoc(adoc_filename, apparent_filename, parents):
parents.add(adoc_filename)
# look for image files
with open(os.path.join(input_dir, adoc_filename)) as fh:
contents = fh.read()
join_files[adoc_filename] = set()
joinee_dir = os.path.dirname(adoc_filename)
# look for includes
for include in re.findall(r'(?:^|\n)include::(.+?)\[\](?:\n|$)', contents):
include_adoc = os.path.join(joinee_dir, include)
if include_adoc in parents:
raise Exception("{} includes {} which creates an infinite loop".format(adoc_filename, include_adoc))
join_files[adoc_filename].add(include_adoc)
scan_adoc(include_adoc, apparent_filename, parents.copy())
# look for image files
for image in re.findall(r'image::?(.+?)\[.*\]', contents):
if not (image.startswith('http:') or image.startswith('https:')):
image_filename = resolve_url(adoc_filename, image)
dest_image = resolve_url(apparent_filename, image)
if dest_image in destimages2srcimages and destimages2srcimages[dest_image] != image_filename:
raise Exception("{} and {} would both end up as {}".format(destimages2srcimages[dest_image], image_filename, dest_image))
srcimages2destimages[image_filename] = dest_image
destimages2srcimages[dest_image] = image_filename
if __name__ == "__main__":
index_json = sys.argv[1]
config_yaml = sys.argv[2]
input_dir = sys.argv[3]
if not os.path.exists(input_dir):
raise Exception("{} doesn't exist".format(input_dir))
scripts_dir = sys.argv[4]
output_dir = sys.argv[5]
adoc_includes_dir = sys.argv[6]
assets_dir = sys.argv[7]
redirects_dir = sys.argv[8]
output_ninjabuild = sys.argv[9]
# Read _config.yml
with open(config_yaml) as config_fh:
site_config = yaml.safe_load(config_fh)
category_pages = set([
('index.adoc', site_config['title']),
('404.adoc', site_config['title'])
])
doc_pages = set()
page_images = set()
# Read index.json
with open(index_json) as json_fh:
data = json.load(json_fh)
for tab in data['tabs']:
# either both present, or both missing
assert ('path' in tab) == ('subitems' in tab)
if 'path' in tab:
# category (boxes) page
category_pages.add((os.path.join(tab['path'], 'index.adoc'), '{} - {}'.format(site_config['title'], tab['title'])))
# build_adoc
for subitem in tab['subitems']:
if 'subpath' in subitem:
doc_pages.add(os.path.join(tab['path'], subitem['subpath']))
if 'image' in subitem:
page_images.add(subitem['image'])
# Write rules to autogenerate files and copy adoc files
with open(output_ninjabuild, 'w') as fh:
ninja = ninja_syntax.Writer(fh, width=0)
ninja.comment("This file is autogenerated, do not edit.")
ninja.newline()
ninja.variable('src_dir', input_dir)
ninja.variable('out_dir', output_dir)
ninja.variable('inc_dir', adoc_includes_dir)
ninja.variable('scripts_dir', scripts_dir)
ninja.variable('redirects_dir', redirects_dir)
ninja.variable('documentation_index', index_json)
ninja.variable('site_config', config_yaml)
ninja.newline()
targets = []
for page, title in sorted(category_pages):
dest = os.path.join('$out_dir', page)
ninja.build(dest, 'create_categories_page', variables={'title': title})
targets.append(dest)
if targets:
ninja.default(targets)
targets = []
ninja.newline()
all_doc_sources = []
srcimages2destimages = {}
destimages2srcimages = {} # used for detecting filename conflicts
join_files = dict() # of sets
# documentation pages
for page in doc_pages:
# find includes and images
scan_adoc(page, page, set())
#print(join_files)
for page in sorted(doc_pages):
for include in sorted(join_files[page]):
dest = os.path.join('$inc_dir', include)
source = os.path.join('$src_dir', include)
extra_sources = ['$scripts_dir/create_build_adoc_include.py', '$site_config', '$GITHUB_EDIT_TEMPLATE']
if source not in all_doc_sources:
all_doc_sources.append(source)
ninja.build(dest, 'create_build_adoc_include', source, extra_sources)
targets.append(dest)
dest = os.path.join('$out_dir', page)
source = os.path.join('$src_dir', page)
extra_sources = ['$scripts_dir/create_build_adoc.py', '$documentation_index', '$site_config', '$GITHUB_EDIT_TEMPLATE']
if source not in all_doc_sources:
all_doc_sources.append(source)
ninja.build(dest, 'create_build_adoc', source, extra_sources)
targets.append(dest)
if targets:
ninja.default(targets)
targets = []
ninja.newline()
# images used on documentation pages
for source in sorted(srcimages2destimages):
dest = os.path.join('$out_dir', srcimages2destimages[source])
source = os.path.join('$src_dir', source)
ninja.build(dest, 'copy', source)
targets.append(dest)
if targets:
ninja.default(targets)
targets = []
ninja.newline()
# ToC data
dest = os.path.join('$out_dir', '_data', 'nav.json')
source = '$documentation_index'
extra_sources = ['$scripts_dir/create_nav.py']
extra_sources.extend(all_doc_sources)
ninja.build(dest, 'create_toc', source, extra_sources)
targets.append(dest)
if targets:
ninja.default(targets)
targets = []
ninja.newline()
# Images on boxes
for image in sorted(page_images):
dest = os.path.join('$out_dir', 'images', image)
source = os.path.join('$DOCUMENTATION_IMAGES_DIR', image)
ninja.build(dest, 'copy', source)
targets.append(dest)
if targets:
ninja.default(targets)
targets = []
ninja.newline()
# Jekyll-assets
for root, dirs, files in os.walk(assets_dir):
for asset in sorted(files):
asset_filepath = os.path.relpath(os.path.join(root, asset), assets_dir)
dest = os.path.join('$out_dir', asset_filepath)
source = os.path.join(assets_dir, asset_filepath)
ninja.build(dest, 'copy', source)
targets.append(dest)
if targets:
ninja.default(targets)
targets = []
ninja.newline()
# Redirects & htaccess
dest = os.path.join('$out_dir', '.htaccess')
source = '$HTACCESS_EXTRA'
extra_sources = ['$scripts_dir/create_htaccess.py']
for file in sorted(os.listdir(redirects_dir)):
if os.path.splitext(file)[1] == '.csv':
extra_sources.append(os.path.join('$redirects_dir', file))
ninja.build(dest, 'create_htaccess', source, extra_sources)
targets.append(dest)
if targets:
ninja.default(targets)
targets = []
ninja.newline()