diff options
Diffstat (limited to 'release/update-manpages')
-rwxr-xr-x | release/update-manpages | 103 |
1 files changed, 2 insertions, 101 deletions
diff --git a/release/update-manpages b/release/update-manpages index 739cedb1..0402ad66 100755 --- a/release/update-manpages +++ b/release/update-manpages | |||
@@ -18,107 +18,8 @@ | |||
18 | This is intended to be run before every official Repo release. | 18 | This is intended to be run before every official Repo release. |
19 | """ | 19 | """ |
20 | 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 | 21 | import sys |
30 | import tempfile | ||
31 | 22 | ||
32 | TOPDIR = Path(__file__).resolve().parent.parent | 23 | import update_manpages |
33 | MANDIR = TOPDIR.joinpath('man') | ||
34 | 24 | ||
35 | # Load repo local modules. | 25 | sys.exit(update_manpages.main(sys.argv[1:])) |
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 | # Let repo know we're generating man pages so it can avoid some dynamic | ||
51 | # behavior (like probing active number of CPUs). We use a weird name & | ||
52 | # value to make it less likely for users to set this var themselves. | ||
53 | os.environ['_REPO_GENERATE_MANPAGES_'] = ' indeed! ' | ||
54 | |||
55 | # "repo branch" is an alias for "repo branches". | ||
56 | del subcmds.all_commands['branch'] | ||
57 | (MANDIR / 'repo-branch.1').write_text('.so man1/repo-branches.1') | ||
58 | |||
59 | version = RepoSourceVersion() | ||
60 | cmdlist = [['help2man', '-N', '-n', f'repo {cmd} - manual page for repo {cmd}', | ||
61 | '-S', f'repo {cmd}', '-m', 'Repo Manual', f'--version-string={version}', | ||
62 | '-o', MANDIR.joinpath(f'repo-{cmd}.1.tmp'), './repo', | ||
63 | '-h', f'help {cmd}'] for cmd in subcmds.all_commands] | ||
64 | cmdlist.append(['help2man', '-N', '-n', 'repository management tool built on top of git', | ||
65 | '-S', 'repo', '-m', 'Repo Manual', f'--version-string={version}', | ||
66 | '-o', MANDIR.joinpath('repo.1.tmp'), './repo', | ||
67 | '-h', '--help-all']) | ||
68 | |||
69 | with tempfile.TemporaryDirectory() as tempdir: | ||
70 | tempdir = Path(tempdir) | ||
71 | repo_dir = tempdir / '.repo' | ||
72 | repo_dir.mkdir() | ||
73 | (repo_dir / 'repo').symlink_to(TOPDIR) | ||
74 | |||
75 | # Create a repo wrapper using the active Python executable. We can't pass | ||
76 | # this directly to help2man as it's too simple, so insert it via shebang. | ||
77 | data = (TOPDIR / 'repo').read_text(encoding='utf-8') | ||
78 | tempbin = tempdir / 'repo' | ||
79 | tempbin.write_text(f'#!{sys.executable}\n' + data, encoding='utf-8') | ||
80 | tempbin.chmod(0o755) | ||
81 | |||
82 | # Run all cmd in parallel, and wait for them to finish. | ||
83 | with multiprocessing.Pool() as pool: | ||
84 | pool.map(partial(worker, cwd=tempdir, check=True), cmdlist) | ||
85 | |||
86 | for tmp_path in MANDIR.glob('*.1.tmp'): | ||
87 | path = tmp_path.parent / tmp_path.stem | ||
88 | old_data = path.read_text() if path.exists() else '' | ||
89 | |||
90 | data = tmp_path.read_text() | ||
91 | tmp_path.unlink() | ||
92 | |||
93 | data = replace_regex(data) | ||
94 | |||
95 | # If the only thing that changed was the date, don't refresh. This avoids | ||
96 | # a lot of noise when only one file actually updates. | ||
97 | old_data = re.sub(r'^(\.TH REPO "1" ")([^"]+)', r'\1', old_data, flags=re.M) | ||
98 | new_data = re.sub(r'^(\.TH REPO "1" ")([^"]+)', r'\1', data, flags=re.M) | ||
99 | if old_data != new_data: | ||
100 | path.write_text(data) | ||
101 | |||
102 | |||
103 | def replace_regex(data): | ||
104 | """Replace semantically null regexes in the data. | ||
105 | |||
106 | Args: | ||
107 | data: manpage text. | ||
108 | |||
109 | Returns: | ||
110 | Updated manpage text. | ||
111 | """ | ||
112 | regex = ( | ||
113 | (r'(It was generated by help2man) [0-9.]+', '\g<1>.'), | ||
114 | (r'^\033\[[0-9;]*m([^\033]*)\033\[m', '\g<1>'), | ||
115 | (r'^\.IP\n(.*:)\n', '.SS \g<1>\n'), | ||
116 | (r'^\.PP\nDescription', '.SH DETAILS'), | ||
117 | ) | ||
118 | for pattern, replacement in regex: | ||
119 | data = re.sub(pattern, replacement, data, flags=re.M) | ||
120 | return data | ||
121 | |||
122 | |||
123 | if __name__ == '__main__': | ||
124 | sys.exit(main(sys.argv[1:])) | ||