summaryrefslogtreecommitdiffstats
path: root/release/update_manpages.py
diff options
context:
space:
mode:
authorMike Frysinger <vapier@google.com>2022-12-08 01:39:02 -0500
committerMike Frysinger <vapier@google.com>2022-12-12 23:04:40 +0000
commite0728a5ecdf49f26a0663284acd0fca1099bba15 (patch)
tree2edc4c54bcfd566103b94bf56d2b5410068084fe /release/update_manpages.py
parentd98f3935247818eb5f5ff2f3816d67c680915161 (diff)
downloadgit-repo-e0728a5ecdf49f26a0663284acd0fca1099bba15.tar.gz
update-manpages: clean up symlink in checkout
We don't want symlinks in the git tree as it causes pain for Windows users. We also don't really need it as we can refactor the code we want to import slightly. Change-Id: I4537c07c50ee9449e9f53e0f132a386e8ffe16ec Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/354356 Tested-by: Mike Frysinger <vapier@google.com> Reviewed-by: LaMont Jones <lamontjones@google.com>
Diffstat (limited to 'release/update_manpages.py')
-rw-r--r--release/update_manpages.py119
1 files changed, 119 insertions, 0 deletions
diff --git a/release/update_manpages.py b/release/update_manpages.py
new file mode 100644
index 00000000..9cbb43b6
--- /dev/null
+++ b/release/update_manpages.py
@@ -0,0 +1,119 @@
1# Copyright (C) 2021 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Helper tool for generating manual page for all repo commands.
16
17Most code lives in this module so it can be unittested.
18"""
19
20from pathlib import Path
21from functools import partial
22import argparse
23import multiprocessing
24import os
25import re
26import shutil
27import subprocess
28import sys
29import tempfile
30
31TOPDIR = Path(__file__).resolve().parent.parent
32MANDIR = TOPDIR.joinpath('man')
33
34# Load repo local modules.
35sys.path.insert(0, str(TOPDIR))
36from git_command import RepoSourceVersion
37import subcmds
38
39def worker(cmd, **kwargs):
40 subprocess.run(cmd, **kwargs)
41
42def main(argv):
43 parser = argparse.ArgumentParser(description=__doc__)
44 opts = parser.parse_args(argv)
45
46 if not shutil.which('help2man'):
47 sys.exit('Please install help2man to continue.')
48
49 # Let repo know we're generating man pages so it can avoid some dynamic
50 # behavior (like probing active number of CPUs). We use a weird name &
51 # value to make it less likely for users to set this var themselves.
52 os.environ['_REPO_GENERATE_MANPAGES_'] = ' indeed! '
53
54 # "repo branch" is an alias for "repo branches".
55 del subcmds.all_commands['branch']
56 (MANDIR / 'repo-branch.1').write_text('.so man1/repo-branches.1')
57
58 version = RepoSourceVersion()
59 cmdlist = [['help2man', '-N', '-n', f'repo {cmd} - manual page for repo {cmd}',
60 '-S', f'repo {cmd}', '-m', 'Repo Manual', f'--version-string={version}',
61 '-o', MANDIR.joinpath(f'repo-{cmd}.1.tmp'), './repo',
62 '-h', f'help {cmd}'] for cmd in subcmds.all_commands]
63 cmdlist.append(['help2man', '-N', '-n', 'repository management tool built on top of git',
64 '-S', 'repo', '-m', 'Repo Manual', f'--version-string={version}',
65 '-o', MANDIR.joinpath('repo.1.tmp'), './repo',
66 '-h', '--help-all'])
67
68 with tempfile.TemporaryDirectory() as tempdir:
69 tempdir = Path(tempdir)
70 repo_dir = tempdir / '.repo'
71 repo_dir.mkdir()
72 (repo_dir / 'repo').symlink_to(TOPDIR)
73
74 # Create a repo wrapper using the active Python executable. We can't pass
75 # this directly to help2man as it's too simple, so insert it via shebang.
76 data = (TOPDIR / 'repo').read_text(encoding='utf-8')
77 tempbin = tempdir / 'repo'
78 tempbin.write_text(f'#!{sys.executable}\n' + data, encoding='utf-8')
79 tempbin.chmod(0o755)
80
81 # Run all cmd in parallel, and wait for them to finish.
82 with multiprocessing.Pool() as pool:
83 pool.map(partial(worker, cwd=tempdir, check=True), cmdlist)
84
85 for tmp_path in MANDIR.glob('*.1.tmp'):
86 path = tmp_path.parent / tmp_path.stem
87 old_data = path.read_text() if path.exists() else ''
88
89 data = tmp_path.read_text()
90 tmp_path.unlink()
91
92 data = replace_regex(data)
93
94 # If the only thing that changed was the date, don't refresh. This avoids
95 # a lot of noise when only one file actually updates.
96 old_data = re.sub(r'^(\.TH REPO "1" ")([^"]+)', r'\1', old_data, flags=re.M)
97 new_data = re.sub(r'^(\.TH REPO "1" ")([^"]+)', r'\1', data, flags=re.M)
98 if old_data != new_data:
99 path.write_text(data)
100
101
102def replace_regex(data):
103 """Replace semantically null regexes in the data.
104
105 Args:
106 data: manpage text.
107
108 Returns:
109 Updated manpage text.
110 """
111 regex = (
112 (r'(It was generated by help2man) [0-9.]+', '\g<1>.'),
113 (r'^\033\[[0-9;]*m([^\033]*)\033\[m', '\g<1>'),
114 (r'^\.IP\n(.*:)\n', '.SS \g<1>\n'),
115 (r'^\.PP\nDescription', '.SH DETAILS'),
116 )
117 for pattern, replacement in regex:
118 data = re.sub(pattern, replacement, data, flags=re.M)
119 return data