|
| 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 | +
|
| 17 | +Most code lives in this module so it can be unittested. |
| 18 | +""" |
| 19 | + |
| 20 | +from pathlib import Path |
| 21 | +from functools import partial |
| 22 | +import argparse |
| 23 | +import multiprocessing |
| 24 | +import os |
| 25 | +import re |
| 26 | +import shutil |
| 27 | +import subprocess |
| 28 | +import sys |
| 29 | +import tempfile |
| 30 | + |
| 31 | +TOPDIR = Path(__file__).resolve().parent.parent |
| 32 | +MANDIR = TOPDIR.joinpath('man') |
| 33 | + |
| 34 | +# Load repo local modules. |
| 35 | +sys.path.insert(0, str(TOPDIR)) |
| 36 | +from repo.git_command import RepoSourceVersion |
| 37 | +import subcmds |
| 38 | + |
| 39 | +def worker(cmd, **kwargs): |
| 40 | + subprocess.run(cmd, **kwargs) |
| 41 | + |
| 42 | +def 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 | + |
| 102 | +def 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.]+', r'\g<1>.'), |
| 113 | + (r'^\033\[[0-9;]*m([^\033]*)\033\[m', r'\g<1>'), |
| 114 | + (r'^\.IP\n(.*:)\n', r'.SS \g<1>\n'), |
| 115 | + (r'^\.PP\nDescription', r'.SH DETAILS'), |
| 116 | + ) |
| 117 | + for pattern, replacement in regex: |
| 118 | + data = re.sub(pattern, replacement, data, flags=re.M) |
| 119 | + return data |
0 commit comments