-
Notifications
You must be signed in to change notification settings - Fork 198
/
Copy pathman_pages.py
executable file
·276 lines (226 loc) · 8.61 KB
/
man_pages.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python3
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright 2020 George Melikov <[email protected]>
#
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import argparse
import logging
import os
import re
import subprocess
import sys
import git
LOG = logging.getLogger()
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
zfs_repo_url = 'https://github.com/openzfs/zfs/'
MAN_SECTIONS = {
'1': 'User Commands',
'2': 'System Calls',
'3': 'C Library Functions',
'4': 'Devices and Special Files',
'5': 'File Formats and Conventions',
'6': 'Games',
'7': 'Miscellaneous',
'8': 'System Administration Commands',
}
MAN_SECTION_DIR = 'man'
MAN_SECTION_NAME = 'Man Pages'
BUILD_DIR = '_build'
MAN_BUILD_DIR = os.path.join(BUILD_DIR, 'man')
ZFS_GIT_REPO = 'https://github.com/openzfs/zfs.git'
TAG_REGEX = re.compile(
r'zfs-(?P<version>'
r'(?P<major>[0-9]+)\.(?P<minor>[0-9]+)\.(?P<fix>[0-9]+))')
LINKS_REGEX_TEMPLATE = (r'<a(?P<href_place> )class=\"Xr\"(?P<title>.*?)>%s'
r'\((?P<num>[1-9])\)<\/a>')
LINKS_FINAL_REGEX = (r'<a href="../\g<num>/\g<name>.\g<num>.html" class="Xr"'
r'>\g<name>(\g<num>)</a>')
def add_hyperlinks(out_dir, version, pages):
all_pages = []
for _section, section_pages in pages.items():
all_pages.extend([
os.path.splitext(page)[0] for page in section_pages])
tmp_regex = '(?P<name>' + "|".join(all_pages) + ')'
html_regex = re.compile(
LINKS_REGEX_TEMPLATE % tmp_regex, flags=re.MULTILINE)
for section, pages in pages.items():
for page in pages:
file_path = os.path.join(
out_dir, MAN_BUILD_DIR,
version, 'man' + section, page + '.html')
with open(file_path, "r") as f:
text = f.read()
new_text = re.sub(html_regex, LINKS_FINAL_REGEX, text)
if text != new_text:
with open(file_path, "w") as f:
LOG.debug('Crosslinks detected in %s, generate',
file_path)
text = f.write(new_text)
def run(in_dir, out_dir, version, tag):
pages = {num: [] for num in MAN_SECTIONS}
for subdir, dirs, _ in os.walk(in_dir):
for section in dirs:
section_num = section.replace('man', '')
section_suffix = '.' + section_num
if section_num not in MAN_SECTIONS:
continue
out_section_dir = os.path.join(
out_dir, MAN_BUILD_DIR, version, section)
os.makedirs(out_section_dir, exist_ok=True)
for page in os.listdir(os.path.join(subdir, section)):
if not (page.endswith(section_suffix) or
page.endswith(section_suffix + '.in')):
continue
LOG.debug('Generate %s page', page)
stripped_page = page.rstrip('.in')
page_file = os.path.join(out_section_dir,
stripped_page + '.html')
with open(page_file, "w") as f:
subprocess.run(
['mandoc', '-T', 'html', '-O', 'fragment',
os.path.join(subdir, section, page)], stdout=f,
check=True)
pages[section_num].append(stripped_page)
break
man_path = os.path.join(out_dir, MAN_SECTION_DIR, version)
os.makedirs(man_path, exist_ok=True)
# Index for version
with open(os.path.join(man_path, 'index.rst'), "w") as f:
f.write("""\
.. THIS FILE IS AUTOGENERATED, DO NOT EDIT!
:github_url: {zfs_repo_url}blob/{tag}/man/
{name}
{name_sub}
.. toctree::
:maxdepth: 1
:glob:
*/index
""".format(zfs_repo_url=zfs_repo_url,
name=version,
tag=tag,
name_sub="=" * len(version)))
for section_num, section_pages in pages.items():
if not section_pages:
continue
rst_dir = os.path.join(out_dir, MAN_SECTION_DIR, version, section_num)
os.makedirs(rst_dir, exist_ok=True)
section_name = MAN_SECTIONS[section_num]
section_name_with_num = '{name} ({num})'.format(
name=section_name, num=section_num)
with open(os.path.join(rst_dir, 'index.rst'), "w") as f:
f.write("""\
.. THIS FILE IS AUTOGENERATED, DO NOT EDIT!
:github_url: {zfs_repo_url}blob/{tag}/man/man{section_num}/
{name}
{name_sub}
.. toctree::
:maxdepth: 1
:glob:
*
""".format(zfs_repo_url=zfs_repo_url,
section_num=section_num,
name=section_name_with_num,
tag=tag,
name_sub="=" * len(section_name_with_num),))
for page in section_pages:
with open(os.path.join(rst_dir, page + '.rst'), "w") as f:
f.write("""\
.. THIS FILE IS AUTOGENERATED, DO NOT EDIT!
:github_url: {zfs_repo_url}blob/{tag}/man/man{section_num}/{name}
{name}
{name_sub}
.. raw:: html
<div class="man_container">
.. raw:: html
:file: ../../../{build_dir}/{version}/man{section_num}/{name}.html
.. raw:: html
</div>
""".format(zfs_repo_url=zfs_repo_url,
name=page,
build_dir=MAN_BUILD_DIR,
version=version,
tag=tag,
section_num=section_num,
name_sub="=" * len(page)))
add_hyperlinks(out_dir, version, pages)
def gen_index(out_dir, tags):
# Global index
with open(os.path.join(os.path.join(out_dir, MAN_SECTION_DIR),
'index.rst'), "w") as f:
f.write("""\
.. THIS FILE IS AUTOGENERATED, DO NOT EDIT!
{name}
{name_sub}
.. toctree::
:maxdepth: 1
:glob:
""".format(name=MAN_SECTION_NAME, name_sub="=" * len(MAN_SECTION_NAME))
)
for ver in reversed(tags.keys()):
f.write("""\
{ver}/index
""".format(ver=ver))
def prepare_repo(path):
# TODO(gmelikov): check for actual tags fetch on remote repo updates
repo_dir = os.path.join(path, BUILD_DIR)
os.makedirs(repo_dir, exist_ok=True)
try:
repo = git.Repo(os.path.join(repo_dir, 'zfs'))
LOG.info('zfs repo already cloned')
for remote in repo.remotes:
remote.fetch(tags=None)
except (git.exc.NoSuchPathError, git.exc.InvalidGitRepositoryError):
LOG.info('Clone zfs repo...')
git.Git(repo_dir).clone(ZFS_GIT_REPO)
def iterate_versions(out_dir):
repo_path = os.path.join(BUILD_DIR, 'zfs')
git_cmd = git.Git(repo_path)
repo = git.Repo(os.path.join(BUILD_DIR, 'zfs'))
# sort tags, some versions are not semvers so we'll use latest ones
# (for ex. 0.6.5.11)
repo_tags = sorted(repo.tags, key=lambda t: t.commit.committed_datetime)
tags = {}
for tag in repo_tags:
tag = str(tag)
if 'rc' in tag:
LOG.debug("Skip rc version %s", tag)
continue
version = TAG_REGEX.match(tag)
if not version:
LOG.info("Cannot parse %s version, skipping...", tag)
continue
ver_dict = {k: int(v) if 'version' not in k else v
for k, v in version.groupdict().items()}
# ignore pre-0.6 versions
if ver_dict['major'] > 0 or ver_dict['minor'] > 5:
# get only latest minor versions
tags[f'v{ver_dict["major"]}.{ver_dict["minor"]}'] = tag
tags['master'] = 'master'
LOG.info('Tags to build: %r', tags)
gen_index(out_dir, tags)
for version, tag in tags.items():
git_cmd.checkout(tag)
run(os.path.join(repo_path, 'man'), out_dir, version, tag)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('out_dir',
help='Sphinx docs dir')
args = parser.parse_args()
os.makedirs(os.path.join(args.out_dir, MAN_SECTION_DIR), exist_ok=True)
prepare_repo(args.out_dir)
iterate_versions(args.out_dir)
if __name__ == '__main__':
main()