Roger Shimizu | 0a1f533 | 2021-06-20 21:41:05 +0900 | [diff] [blame] | 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 | |
Mike Frysinger | df8b1cb | 2021-07-26 15:59:20 -0400 | [diff] [blame] | 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 | |
Roger Shimizu | 0a1f533 | 2021-06-20 21:41:05 +0900 | [diff] [blame] | 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}', |
Mike Frysinger | 0468fea | 2021-09-22 14:15:35 -0400 | [diff] [blame] | 62 | '-o', MANDIR.joinpath(f'repo-{cmd}.1.tmp'), TOPDIR.joinpath('repo'), |
Roger Shimizu | 0a1f533 | 2021-06-20 21:41:05 +0900 | [diff] [blame] | 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}', |
Mike Frysinger | 0468fea | 2021-09-22 14:15:35 -0400 | [diff] [blame] | 66 | '-o', MANDIR.joinpath('repo.1.tmp'), TOPDIR.joinpath('repo'), |
Mike Frysinger | 56345c3 | 2021-07-26 23:46:32 -0400 | [diff] [blame] | 67 | '-h', '--help-all']) |
Roger Shimizu | 0a1f533 | 2021-06-20 21:41:05 +0900 | [diff] [blame] | 68 | |
| 69 | with tempfile.TemporaryDirectory() as tempdir: |
| 70 | repo_dir = Path(tempdir) / '.repo' |
| 71 | repo_dir.mkdir() |
| 72 | (repo_dir / 'repo').symlink_to(TOPDIR) |
| 73 | |
| 74 | # Run all cmd in parallel, and wait for them to finish. |
| 75 | with multiprocessing.Pool() as pool: |
| 76 | pool.map(partial(worker, cwd=tempdir, check=True), cmdlist) |
| 77 | |
| 78 | regex = ( |
| 79 | (r'(It was generated by help2man) [0-9.]+', '\g<1>.'), |
| 80 | (r'^\.IP\n(.*:)\n', '.SS \g<1>\n'), |
| 81 | (r'^\.PP\nDescription', '.SH DETAILS'), |
| 82 | ) |
Mike Frysinger | 0468fea | 2021-09-22 14:15:35 -0400 | [diff] [blame] | 83 | for tmp_path in MANDIR.glob('*.1.tmp'): |
| 84 | path = tmp_path.parent / tmp_path.stem |
| 85 | old_data = path.read_text() if path.exists() else '' |
| 86 | |
| 87 | data = tmp_path.read_text() |
| 88 | tmp_path.unlink() |
| 89 | |
Roger Shimizu | 0a1f533 | 2021-06-20 21:41:05 +0900 | [diff] [blame] | 90 | for pattern, replacement in regex: |
| 91 | data = re.sub(pattern, replacement, data, flags=re.M) |
Mike Frysinger | 0468fea | 2021-09-22 14:15:35 -0400 | [diff] [blame] | 92 | |
| 93 | # If the only thing that changed was the date, don't refresh. This avoids |
| 94 | # a lot of noise when only one file actually updates. |
| 95 | old_data = re.sub(r'^(\.TH REPO "1" ")([^"]+)', r'\1', old_data, flags=re.M) |
| 96 | new_data = re.sub(r'^(\.TH REPO "1" ")([^"]+)', r'\1', data, flags=re.M) |
| 97 | if old_data != new_data: |
| 98 | path.write_text(data) |
| 99 | |
Roger Shimizu | 0a1f533 | 2021-06-20 21:41:05 +0900 | [diff] [blame] | 100 | |
| 101 | if __name__ == '__main__': |
| 102 | sys.exit(main(sys.argv[1:])) |