diff options
-rwxr-xr-x | release/update-manpages | 103 | ||||
-rw-r--r-- | release/update_manpages.py | 119 | ||||
-rw-r--r-- | tests/test_update_manpages.py | 5 | ||||
l--------- | tests/update_manpages.py | 1 |
4 files changed, 124 insertions, 104 deletions
diff --git a/release/update-manpages b/release/update-manpages index 739cedb1..0402ad66 100755 --- a/release/update-manpages +++ b/release/update-manpages | |||
@@ -18,107 +18,8 @@ | |||
18 | This is intended to be run before every official Repo release. | 18 | This is intended to be run before every official Repo release. |
19 | """ | 19 | """ |
20 | 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 | 21 | import sys |
30 | import tempfile | ||
31 | 22 | ||
32 | TOPDIR = Path(__file__).resolve().parent.parent | 23 | import update_manpages |
33 | MANDIR = TOPDIR.joinpath('man') | ||
34 | 24 | ||
35 | # Load repo local modules. | 25 | sys.exit(update_manpages.main(sys.argv[1:])) |
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 | |||
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 | |||
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}', | ||
62 | '-o', MANDIR.joinpath(f'repo-{cmd}.1.tmp'), './repo', | ||
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}', | ||
66 | '-o', MANDIR.joinpath('repo.1.tmp'), './repo', | ||
67 | '-h', '--help-all']) | ||
68 | |||
69 | with tempfile.TemporaryDirectory() as tempdir: | ||
70 | tempdir = Path(tempdir) | ||
71 | repo_dir = tempdir / '.repo' | ||
72 | repo_dir.mkdir() | ||
73 | (repo_dir / 'repo').symlink_to(TOPDIR) | ||
74 | |||
75 | # Create a repo wrapper using the active Python executable. We can't pass | ||
76 | # this directly to help2man as it's too simple, so insert it via shebang. | ||
77 | data = (TOPDIR / 'repo').read_text(encoding='utf-8') | ||
78 | tempbin = tempdir / 'repo' | ||
79 | tempbin.write_text(f'#!{sys.executable}\n' + data, encoding='utf-8') | ||
80 | tempbin.chmod(0o755) | ||
81 | |||
82 | # Run all cmd in parallel, and wait for them to finish. | ||
83 | with multiprocessing.Pool() as pool: | ||
84 | pool.map(partial(worker, cwd=tempdir, check=True), cmdlist) | ||
85 | |||
86 | for tmp_path in MANDIR.glob('*.1.tmp'): | ||
87 | path = tmp_path.parent / tmp_path.stem | ||
88 | old_data = path.read_text() if path.exists() else '' | ||
89 | |||
90 | data = tmp_path.read_text() | ||
91 | tmp_path.unlink() | ||
92 | |||
93 | data = replace_regex(data) | ||
94 | |||
95 | # If the only thing that changed was the date, don't refresh. This avoids | ||
96 | # a lot of noise when only one file actually updates. | ||
97 | old_data = re.sub(r'^(\.TH REPO "1" ")([^"]+)', r'\1', old_data, flags=re.M) | ||
98 | new_data = re.sub(r'^(\.TH REPO "1" ")([^"]+)', r'\1', data, flags=re.M) | ||
99 | if old_data != new_data: | ||
100 | path.write_text(data) | ||
101 | |||
102 | |||
103 | def replace_regex(data): | ||
104 | """Replace semantically null regexes in the data. | ||
105 | |||
106 | Args: | ||
107 | data: manpage text. | ||
108 | |||
109 | Returns: | ||
110 | Updated manpage text. | ||
111 | """ | ||
112 | regex = ( | ||
113 | (r'(It was generated by help2man) [0-9.]+', '\g<1>.'), | ||
114 | (r'^\033\[[0-9;]*m([^\033]*)\033\[m', '\g<1>'), | ||
115 | (r'^\.IP\n(.*:)\n', '.SS \g<1>\n'), | ||
116 | (r'^\.PP\nDescription', '.SH DETAILS'), | ||
117 | ) | ||
118 | for pattern, replacement in regex: | ||
119 | data = re.sub(pattern, replacement, data, flags=re.M) | ||
120 | return data | ||
121 | |||
122 | |||
123 | if __name__ == '__main__': | ||
124 | sys.exit(main(sys.argv[1:])) | ||
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 | |||
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 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.]+', '\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 | ||
diff --git a/tests/test_update_manpages.py b/tests/test_update_manpages.py index f0ef72af..0de85be9 100644 --- a/tests/test_update_manpages.py +++ b/tests/test_update_manpages.py | |||
@@ -15,7 +15,8 @@ | |||
15 | """Unittests for the update_manpages module.""" | 15 | """Unittests for the update_manpages module.""" |
16 | 16 | ||
17 | import unittest | 17 | import unittest |
18 | import tests.update_manpages as um | 18 | |
19 | from release import update_manpages | ||
19 | 20 | ||
20 | 21 | ||
21 | class UpdateManpagesTest(unittest.TestCase): | 22 | class UpdateManpagesTest(unittest.TestCase): |
@@ -24,4 +25,4 @@ class UpdateManpagesTest(unittest.TestCase): | |||
24 | def test_replace_regex(self): | 25 | def test_replace_regex(self): |
25 | """Check that replace_regex works.""" | 26 | """Check that replace_regex works.""" |
26 | data = '\n\033[1mSummary\033[m\n' | 27 | data = '\n\033[1mSummary\033[m\n' |
27 | self.assertEqual(um.replace_regex(data),'\nSummary\n') | 28 | self.assertEqual(update_manpages.replace_regex(data),'\nSummary\n') |
diff --git a/tests/update_manpages.py b/tests/update_manpages.py deleted file mode 120000 index e89c5d8f..00000000 --- a/tests/update_manpages.py +++ /dev/null | |||
@@ -1 +0,0 @@ | |||
1 | ../release/update-manpages \ No newline at end of file | ||