Skip to content

Commit d8841ae

Browse files
committed
update-upstream: add small script to sync the upstream submodule
Signed-off-by: Filipe Laíns <[email protected]>
1 parent 9599d84 commit d8841ae

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

update-upstream.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env python
2+
import os
3+
import pathlib
4+
import shlex
5+
import subprocess
6+
import sys
7+
import tomllib
8+
import warnings
9+
10+
11+
def _use_colors() -> bool:
12+
if 'NO_COLOR' in os.environ:
13+
if 'FORCE_COLOR' in os.environ:
14+
warnings.warn('Both NO_COLOR and FORCE_COLOR environment variables are set, disabling color', stacklevel=2)
15+
return False
16+
elif 'FORCE_COLOR' in os.environ or sys.stdout.isatty():
17+
return os.name != 'nt'
18+
return False
19+
20+
21+
if _use_colors():
22+
dim = '\33[2m'
23+
yellow = '\33[93m'
24+
reset = '\33[0m'
25+
else:
26+
dim = yellow = reset = ''
27+
28+
29+
root = pathlib.Path(__file__).parent
30+
submodule = root / 'subprojects' / 'pkgconf'
31+
32+
version_files = [
33+
root / 'pyproject.toml',
34+
root / 'meson.build',
35+
root / 'src' / 'pkgconf' / '__init__.py',
36+
]
37+
38+
39+
def git(*args: str, cwd: str | os.PathLike = submodule, capture: bool = False) -> subprocess.CompletedProcess:
40+
args = ['git', *args]
41+
print(dim + '$ ' + shlex.join(args) + reset)
42+
try:
43+
process = subprocess.run(args, cwd=cwd, capture_output=capture, text=True, check=True)
44+
except subprocess.CalledProcessError as e:
45+
sys.exit(e.returncode)
46+
if capture:
47+
if process.stdout:
48+
sys.stderr.write(process.stdout)
49+
if process.stderr:
50+
sys.stderr.write(process.stderr)
51+
return process
52+
53+
54+
with root.joinpath('pyproject.toml').open('rb') as f:
55+
pyproject_data = tomllib.load(f)
56+
57+
downstream_version = pyproject_data['project']['version']
58+
downstream_version_tuple = tuple(downstream_version.split('-')[0].split('.'))
59+
print(f'{yellow}Current version: {downstream_version}{reset}')
60+
61+
# Get the latest upstream version
62+
#subprocess.check_output(['git', 'fetch', 'origin', 'master'], cwd=submodule)
63+
git('fetch', 'origin', 'master')
64+
#upstream_latest_tag = subprocess.check_output(['git', 'describe', -'-tags', '--abbrev=0'], cwd=submodule)
65+
upstream_latest_tag = git('describe', '--tags', '--abbrev=0', capture=True).stdout.strip()
66+
upstream_version = upstream_latest_tag.removeprefix('pkgconf-')
67+
upstream_version_tuple = tuple(upstream_version.split('.'))
68+
69+
70+
if upstream_version_tuple > downstream_version_tuple:
71+
print(f'{yellow}Found new upstream version: {upstream_version} (current: {downstream_version}){reset}')
72+
print(f'{yellow}Updating...{reset}')
73+
#subprocess.check_output(['git', 'checkout', upstream_latest_tag], cwd=submodule)
74+
git('checkout', upstream_latest_tag)
75+
new_downstream_version = f'{upstream_version}-0'
76+
print(f'{yellow}New version: {new_downstream_version}{reset}')
77+
for file in version_files:
78+
file.write_text(file.read_text().replace(
79+
f"'{downstream_version}'",
80+
f"'{new_downstream_version}'",
81+
))

0 commit comments

Comments
 (0)