-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathupdate_ninja_version.py
More file actions
132 lines (108 loc) · 4.4 KB
/
Copy pathupdate_ninja_version.py
File metadata and controls
132 lines (108 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""
Command line executable allowing to update upstream sources, documentation
and tests given a Ninja version.
"""
from __future__ import annotations
import argparse
import contextlib
import os
import re
import shutil
import subprocess
import textwrap
from pathlib import Path
ROOT_DIR = Path(__file__).parent.parent.resolve(strict=True)
@contextlib.contextmanager
def _log(txt, verbose=True):
if verbose:
print(txt)
yield
if verbose:
print(f"{txt} - done")
@contextlib.contextmanager
def chdir(path: Path):
origin = Path().absolute()
os.chdir(path)
try:
yield
finally:
os.chdir(origin)
def update_submodule(upstream_repository, version):
with chdir(ROOT_DIR):
subprocess.run(["git", "submodule", "deinit", "-f", "ninja-upstream"], check=True)
subprocess.run(["git", "rm", "-f", "ninja-upstream"], check=True)
shutil.rmtree(ROOT_DIR / ".git/modules/ninja-upstream")
subprocess.run(["git", "submodule", "add", f"https://github.com/{upstream_repository}.git", "ninja-upstream"], check=True)
subprocess.run(["git", "submodule", "update", "--init", "--recursive", "ninja-upstream"], check=True)
with chdir(ROOT_DIR / "ninja-upstream"):
subprocess.run(["git", "fetch", "--tags"], check=True)
subprocess.run(["git", "checkout", f"v{version}"], check=True)
def _update_file(filepath: Path, regex, replacement, verbose=True):
msg = f"Updating {os.path.relpath(filepath, ROOT_DIR)}"
with _log(msg, verbose=verbose):
pattern = re.compile(regex)
with filepath.open() as doc_file:
lines = doc_file.readlines()
updated_content = []
for line in lines:
updated_content.append(re.sub(pattern, replacement, line))
with filepath.open("w") as doc_file:
doc_file.writelines(updated_content)
def update_docs(upstream_repository, version):
pattern = re.compile(r"ninja \d+.\d+.\d+(\.[\w\-]+)*")
replacement = f"ninja {version}"
_update_file(ROOT_DIR / "README.rst", pattern, replacement)
pattern = re.compile(r"(?<=v)\d+.\d+.\d+(?:\.[\w\-]+)*(?=(?:\.zip|\.tar\.gz|\/))")
replacement = version
_update_file(ROOT_DIR / "docs/update_ninja_version.rst", pattern, replacement)
pattern = re.compile(r"(?<!v)\d+.\d+.\d+(?:\.[\w\-]+)*")
replacement = version
_update_file(ROOT_DIR / "docs/update_ninja_version.rst", pattern, replacement, verbose=False)
pattern = re.compile(r"github\.com\/[\w\-_]+\/[\w\-_]+(?=\/(?:release|archive))")
replacement = "github.com/" + upstream_repository
_update_file(ROOT_DIR / "docs/update_ninja_version.rst", pattern, replacement, verbose=False)
def update_tests(version):
# Given a version string of the form "x.y.z[.gSHA{5}][.<qualifier>[.<qualifier>]]", replace
# ".gSHA{5}" if "git"
parts = version.split(".")
if len(parts) > 3:
parts[3] = "git"
version = ".".join(parts)
pattern = re.compile(r'expected_version = "\d+.\d+.\d+(\.[\w\-]+)*"')
replacement = f'expected_version = "{version}"'
_update_file(ROOT_DIR / "tests/test_ninja.py", pattern, replacement)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"ninja_version",
metavar="NINJA_VERSION",
type=str,
help="Ninja version, shall match a tag in upstream repository",
)
parser.add_argument(
"--upstream-repository",
metavar="UPSTREAM_REPOSITORY",
choices=["Kitware/ninja", "ninja-build/ninja"],
default="ninja-build/ninja",
help="Ninja upstream repository",
)
parser.add_argument(
"--quiet",
action="store_true",
help="Hide the output",
)
args = parser.parse_args()
update_submodule(args.upstream_repository, args.ninja_version)
update_docs(args.upstream_repository, args.ninja_version)
update_tests(args.ninja_version)
if not args.quiet:
msg = """\
Complete! Now run:
git switch -c update-to-ninja-{release}
git add -u ninja-upstream docs/index.rst README.rst tests/test_ninja.py docs/update_ninja_version.rst
git commit -m "Update to Ninja {release}"
gh pr create --fill --body "Created by update_ninja_version.py"
"""
print(textwrap.dedent(msg.format(release=args.ninja_version)))
if __name__ == "__main__":
main()