summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Frysinger <vapier@google.com>2022-12-08 01:39:02 -0500
committerMike Frysinger <vapier@google.com>2022-12-12 23:04:40 +0000
commite0728a5ecdf49f26a0663284acd0fca1099bba15 (patch)
tree2edc4c54bcfd566103b94bf56d2b5410068084fe
parentd98f3935247818eb5f5ff2f3816d67c680915161 (diff)
downloadgit-repo-e0728a5ecdf49f26a0663284acd0fca1099bba15.tar.gz
update-manpages: clean up symlink in checkout
We don't want symlinks in the git tree as it causes pain for Windows users. We also don't really need it as we can refactor the code we want to import slightly. Change-Id: I4537c07c50ee9449e9f53e0f132a386e8ffe16ec Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/354356 Tested-by: Mike Frysinger <vapier@google.com> Reviewed-by: LaMont Jones <lamontjones@google.com>
-rwxr-xr-xrelease/update-manpages103
-rw-r--r--release/update_manpages.py119
-rw-r--r--tests/test_update_manpages.py5
l---------tests/update_manpages.py1
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 @@
18This is intended to be run before every official Repo release. 18This is intended to be run before every official Repo release.
19""" 19"""
20 20
21from pathlib import Path
22from functools import partial
23import argparse
24import multiprocessing
25import os
26import re
27import shutil
28import subprocess
29import sys 21import sys
30import tempfile
31 22
32TOPDIR = Path(__file__).resolve().parent.parent 23import update_manpages
33MANDIR = TOPDIR.joinpath('man')
34 24
35# Load repo local modules. 25sys.exit(update_manpages.main(sys.argv[1:]))
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 # 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
103def 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
123if __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
17Most code lives in this module so it can be unittested.
18"""
19
20from pathlib import Path
21from functools import partial
22import argparse
23import multiprocessing
24import os
25import re
26import shutil
27import subprocess
28import sys
29import tempfile
30
31TOPDIR = Path(__file__).resolve().parent.parent
32MANDIR = TOPDIR.joinpath('man')
33
34# Load repo local modules.
35sys.path.insert(0, str(TOPDIR))
36from git_command import RepoSourceVersion
37import subcmds
38
39def worker(cmd, **kwargs):
40 subprocess.run(cmd, **kwargs)
41
42def 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
102def 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
17import unittest 17import unittest
18import tests.update_manpages as um 18
19from release import update_manpages
19 20
20 21
21class UpdateManpagesTest(unittest.TestCase): 22class 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