Mike Frysinger | e0728a5 | 2022-12-08 01:39:02 -0500 | [diff] [blame] | 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 | |
Mike Frysinger | e0728a5 | 2022-12-08 01:39:02 -0500 | [diff] [blame] | 20 | import argparse |
Mike Frysinger | 06ddc8c | 2023-08-21 21:26:51 -0400 | [diff] [blame] | 21 | import functools |
Mike Frysinger | e0728a5 | 2022-12-08 01:39:02 -0500 | [diff] [blame] | 22 | import multiprocessing |
| 23 | import os |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 24 | from pathlib import Path |
Mike Frysinger | e0728a5 | 2022-12-08 01:39:02 -0500 | [diff] [blame] | 25 | import re |
| 26 | import shutil |
| 27 | import subprocess |
| 28 | import sys |
| 29 | import tempfile |
| 30 | |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 31 | |
Mike Frysinger | e0728a5 | 2022-12-08 01:39:02 -0500 | [diff] [blame] | 32 | TOPDIR = Path(__file__).resolve().parent.parent |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 33 | MANDIR = TOPDIR.joinpath("man") |
Mike Frysinger | e0728a5 | 2022-12-08 01:39:02 -0500 | [diff] [blame] | 34 | |
| 35 | # Load repo local modules. |
| 36 | sys.path.insert(0, str(TOPDIR)) |
| 37 | from git_command import RepoSourceVersion |
| 38 | import subcmds |
| 39 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 40 | |
Mike Frysinger | e0728a5 | 2022-12-08 01:39:02 -0500 | [diff] [blame] | 41 | def worker(cmd, **kwargs): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 42 | subprocess.run(cmd, **kwargs) |
| 43 | |
Mike Frysinger | e0728a5 | 2022-12-08 01:39:02 -0500 | [diff] [blame] | 44 | |
| 45 | def main(argv): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 46 | parser = argparse.ArgumentParser(description=__doc__) |
| 47 | parser.parse_args(argv) |
Mike Frysinger | e0728a5 | 2022-12-08 01:39:02 -0500 | [diff] [blame] | 48 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 49 | if not shutil.which("help2man"): |
| 50 | sys.exit("Please install help2man to continue.") |
Mike Frysinger | e0728a5 | 2022-12-08 01:39:02 -0500 | [diff] [blame] | 51 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 52 | # Let repo know we're generating man pages so it can avoid some dynamic |
| 53 | # behavior (like probing active number of CPUs). We use a weird name & |
| 54 | # value to make it less likely for users to set this var themselves. |
| 55 | os.environ["_REPO_GENERATE_MANPAGES_"] = " indeed! " |
Mike Frysinger | e0728a5 | 2022-12-08 01:39:02 -0500 | [diff] [blame] | 56 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 57 | # "repo branch" is an alias for "repo branches". |
| 58 | del subcmds.all_commands["branch"] |
| 59 | (MANDIR / "repo-branch.1").write_text(".so man1/repo-branches.1") |
Mike Frysinger | e0728a5 | 2022-12-08 01:39:02 -0500 | [diff] [blame] | 60 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 61 | version = RepoSourceVersion() |
| 62 | cmdlist = [ |
| 63 | [ |
| 64 | "help2man", |
| 65 | "-N", |
| 66 | "-n", |
| 67 | f"repo {cmd} - manual page for repo {cmd}", |
| 68 | "-S", |
| 69 | f"repo {cmd}", |
| 70 | "-m", |
| 71 | "Repo Manual", |
| 72 | f"--version-string={version}", |
| 73 | "-o", |
| 74 | MANDIR.joinpath(f"repo-{cmd}.1.tmp"), |
| 75 | "./repo", |
| 76 | "-h", |
| 77 | f"help {cmd}", |
| 78 | ] |
| 79 | for cmd in subcmds.all_commands |
| 80 | ] |
| 81 | cmdlist.append( |
| 82 | [ |
| 83 | "help2man", |
| 84 | "-N", |
| 85 | "-n", |
| 86 | "repository management tool built on top of git", |
| 87 | "-S", |
| 88 | "repo", |
| 89 | "-m", |
| 90 | "Repo Manual", |
| 91 | f"--version-string={version}", |
| 92 | "-o", |
| 93 | MANDIR.joinpath("repo.1.tmp"), |
| 94 | "./repo", |
| 95 | "-h", |
| 96 | "--help-all", |
| 97 | ] |
| 98 | ) |
Mike Frysinger | e0728a5 | 2022-12-08 01:39:02 -0500 | [diff] [blame] | 99 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 100 | with tempfile.TemporaryDirectory() as tempdir: |
| 101 | tempdir = Path(tempdir) |
| 102 | repo_dir = tempdir / ".repo" |
| 103 | repo_dir.mkdir() |
| 104 | (repo_dir / "repo").symlink_to(TOPDIR) |
Mike Frysinger | e0728a5 | 2022-12-08 01:39:02 -0500 | [diff] [blame] | 105 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 106 | # Create a repo wrapper using the active Python executable. We can't |
| 107 | # pass this directly to help2man as it's too simple, so insert it via |
| 108 | # shebang. |
| 109 | data = (TOPDIR / "repo").read_text(encoding="utf-8") |
| 110 | tempbin = tempdir / "repo" |
| 111 | tempbin.write_text(f"#!{sys.executable}\n" + data, encoding="utf-8") |
| 112 | tempbin.chmod(0o755) |
Mike Frysinger | e0728a5 | 2022-12-08 01:39:02 -0500 | [diff] [blame] | 113 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 114 | # Run all cmd in parallel, and wait for them to finish. |
| 115 | with multiprocessing.Pool() as pool: |
Mike Frysinger | 06ddc8c | 2023-08-21 21:26:51 -0400 | [diff] [blame] | 116 | pool.map( |
| 117 | functools.partial(worker, cwd=tempdir, check=True), cmdlist |
| 118 | ) |
Mike Frysinger | e0728a5 | 2022-12-08 01:39:02 -0500 | [diff] [blame] | 119 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 120 | for tmp_path in MANDIR.glob("*.1.tmp"): |
| 121 | path = tmp_path.parent / tmp_path.stem |
| 122 | old_data = path.read_text() if path.exists() else "" |
Mike Frysinger | e0728a5 | 2022-12-08 01:39:02 -0500 | [diff] [blame] | 123 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 124 | data = tmp_path.read_text() |
| 125 | tmp_path.unlink() |
Mike Frysinger | e0728a5 | 2022-12-08 01:39:02 -0500 | [diff] [blame] | 126 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 127 | data = replace_regex(data) |
Mike Frysinger | e0728a5 | 2022-12-08 01:39:02 -0500 | [diff] [blame] | 128 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 129 | # If the only thing that changed was the date, don't refresh. This |
| 130 | # avoids a lot of noise when only one file actually updates. |
| 131 | old_data = re.sub( |
| 132 | r'^(\.TH REPO "1" ")([^"]+)', r"\1", old_data, flags=re.M |
| 133 | ) |
| 134 | new_data = re.sub(r'^(\.TH REPO "1" ")([^"]+)', r"\1", data, flags=re.M) |
| 135 | if old_data != new_data: |
| 136 | path.write_text(data) |
Mike Frysinger | e0728a5 | 2022-12-08 01:39:02 -0500 | [diff] [blame] | 137 | |
| 138 | |
| 139 | def replace_regex(data): |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 140 | """Replace semantically null regexes in the data. |
Mike Frysinger | e0728a5 | 2022-12-08 01:39:02 -0500 | [diff] [blame] | 141 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 142 | Args: |
| 143 | data: manpage text. |
Mike Frysinger | e0728a5 | 2022-12-08 01:39:02 -0500 | [diff] [blame] | 144 | |
Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 145 | Returns: |
| 146 | Updated manpage text. |
| 147 | """ |
| 148 | regex = ( |
| 149 | (r"(It was generated by help2man) [0-9.]+", r"\g<1>."), |
| 150 | (r"^\033\[[0-9;]*m([^\033]*)\033\[m", r"\g<1>"), |
| 151 | (r"^\.IP\n(.*:)\n", r".SS \g<1>\n"), |
| 152 | (r"^\.PP\nDescription", r".SH DETAILS"), |
| 153 | ) |
| 154 | for pattern, replacement in regex: |
| 155 | data = re.sub(pattern, replacement, data, flags=re.M) |
| 156 | return data |