diff options
Diffstat (limited to 'release/update-manpages')
-rwxr-xr-x | release/update-manpages | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/release/update-manpages b/release/update-manpages new file mode 100755 index 00000000..3aeee206 --- /dev/null +++ b/release/update-manpages | |||
@@ -0,0 +1,85 @@ | |||
1 | #!/usr/bin/env python3 | ||
2 | # Copyright (C) 2021 The Android Open Source Project | ||
3 | # | ||
4 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | # you may not use this file except in compliance with the License. | ||
6 | # You may obtain a copy of the License at | ||
7 | # | ||
8 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | # | ||
10 | # Unless required by applicable law or agreed to in writing, software | ||
11 | # distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | # See the License for the specific language governing permissions and | ||
14 | # limitations under the License. | ||
15 | |||
16 | """Helper tool for generating manual page for all repo commands. | ||
17 | |||
18 | This is intended to be run before every official Repo release. | ||
19 | """ | ||
20 | |||
21 | from pathlib import Path | ||
22 | from functools import partial | ||
23 | import argparse | ||
24 | import multiprocessing | ||
25 | import os | ||
26 | import re | ||
27 | import shutil | ||
28 | import subprocess | ||
29 | import sys | ||
30 | import tempfile | ||
31 | |||
32 | TOPDIR = Path(__file__).resolve().parent.parent | ||
33 | MANDIR = TOPDIR.joinpath('man') | ||
34 | |||
35 | # Load repo local modules. | ||
36 | sys.path.insert(0, str(TOPDIR)) | ||
37 | from git_command import RepoSourceVersion | ||
38 | import subcmds | ||
39 | |||
40 | def worker(cmd, **kwargs): | ||
41 | subprocess.run(cmd, **kwargs) | ||
42 | |||
43 | def main(argv): | ||
44 | parser = argparse.ArgumentParser(description=__doc__) | ||
45 | opts = parser.parse_args(argv) | ||
46 | |||
47 | if not shutil.which('help2man'): | ||
48 | sys.exit('Please install help2man to continue.') | ||
49 | |||
50 | # "repo branch" is an alias for "repo branches". | ||
51 | del subcmds.all_commands['branch'] | ||
52 | (MANDIR / 'repo-branch.1').write_text('.so man1/repo-branches.1') | ||
53 | |||
54 | version = RepoSourceVersion() | ||
55 | cmdlist = [['help2man', '-N', '-n', f'repo {cmd} - manual page for repo {cmd}', | ||
56 | '-S', f'repo {cmd}', '-m', 'Repo Manual', f'--version-string={version}', | ||
57 | '-o', MANDIR.joinpath(f'repo-{cmd}.1'), TOPDIR.joinpath('repo'), | ||
58 | '-h', f'help {cmd}'] for cmd in subcmds.all_commands] | ||
59 | cmdlist.append(['help2man', '-N', '-n', 'repository management tool built on top of git', | ||
60 | '-S', 'repo', '-m', 'Repo Manual', f'--version-string={version}', | ||
61 | '-o', MANDIR.joinpath('repo.1'), TOPDIR.joinpath('repo'), | ||
62 | '-h', 'help --all']) | ||
63 | |||
64 | with tempfile.TemporaryDirectory() as tempdir: | ||
65 | repo_dir = Path(tempdir) / '.repo' | ||
66 | repo_dir.mkdir() | ||
67 | (repo_dir / 'repo').symlink_to(TOPDIR) | ||
68 | |||
69 | # Run all cmd in parallel, and wait for them to finish. | ||
70 | with multiprocessing.Pool() as pool: | ||
71 | pool.map(partial(worker, cwd=tempdir, check=True), cmdlist) | ||
72 | |||
73 | regex = ( | ||
74 | (r'(It was generated by help2man) [0-9.]+', '\g<1>.'), | ||
75 | (r'^\.IP\n(.*:)\n', '.SS \g<1>\n'), | ||
76 | (r'^\.PP\nDescription', '.SH DETAILS'), | ||
77 | ) | ||
78 | for path in MANDIR.glob('*.1'): | ||
79 | data = path.read_text() | ||
80 | for pattern, replacement in regex: | ||
81 | data = re.sub(pattern, replacement, data, flags=re.M) | ||
82 | path.write_text(data) | ||
83 | |||
84 | if __name__ == '__main__': | ||
85 | sys.exit(main(sys.argv[1:])) | ||