summaryrefslogtreecommitdiffstats
path: root/release/update-manpages
diff options
context:
space:
mode:
authorRoger Shimizu <rosh@debian.org>2021-06-20 21:41:05 +0900
committerMike Frysinger <vapier@google.com>2021-07-16 20:11:41 +0000
commit0a1f533e2835c856473e3e4046341a4d2f66764b (patch)
tree0902c039b76525f1c6785cec561df46265fdb787 /release/update-manpages
parent927d29a8afa26a3f1aec841f2150c65e3931ef9b (diff)
downloadgit-repo-0a1f533e2835c856473e3e4046341a4d2f66764b.tar.gz
Add script 'release/update-manpages' to generate manpages
Debian package started to ship manpages for repo since 2.8 [1] And it's about for one year. So I think it should be upstreamed. The script depends on help2man, which is available in both debian [2] and ubuntu [3]. [1] https://tracker.debian.org/news/1150858/accepted-repo-28-1-source-into-unstable [2] https://tracker.debian.org/pkg/help2man [3] https://launchpad.net/ubuntu/+source/help2man Change-Id: Ide2b356d0944ebde34cc96c6d5a782655bd72288 Signed-off-by: Roger Shimizu <rosh@debian.org> Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/309782 Reviewed-by: Mike Frysinger <vapier@google.com>
Diffstat (limited to 'release/update-manpages')
-rwxr-xr-xrelease/update-manpages85
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
18This is intended to be run before every official Repo release.
19"""
20
21from pathlib import Path
22from functools import partial
23import argparse
24import multiprocessing
25import os
26import re
27import shutil
28import subprocess
29import sys
30import tempfile
31
32TOPDIR = Path(__file__).resolve().parent.parent
33MANDIR = TOPDIR.joinpath('man')
34
35# Load repo local modules.
36sys.path.insert(0, str(TOPDIR))
37from git_command import RepoSourceVersion
38import subcmds
39
40def worker(cmd, **kwargs):
41 subprocess.run(cmd, **kwargs)
42
43def 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
84if __name__ == '__main__':
85 sys.exit(main(sys.argv[1:]))