This repository was archived by the owner on Jan 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_version.py
More file actions
67 lines (47 loc) · 2.29 KB
/
update_version.py
File metadata and controls
67 lines (47 loc) · 2.29 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
# standard library
import re
import sys
from pathlib import Path
# RegEx to find versions
VERSION_INIT_REGEX: re.Pattern[str] = re.compile(r"^(__version__\s*=\s*[\'\"])([^\'\"]*)([\'\"])", re.MULTILINE)
VERSION_TOML_REGEX: re.Pattern[str] = re.compile(r"^(version\s*=\s*[\'\"])([^\'\"]*)([\'\"])", re.MULTILINE)
# Paths to files to update
INIT_PATH: Path = Path(__file__).parent / "gypt_matplotlib/__init__.py"
TOML_PATH: Path = Path(__file__).parent / "pyproject.toml"
# Check whether files exist, fail if not
if not INIT_PATH.is_file():
print(f"Invalid path for __init__.py: {INIT_PATH}", file=sys.stderr)
sys.exit(1)
if not TOML_PATH.is_file():
print(f"Invalid path for pyproject.toml: {INIT_PATH}", file=sys.stderr)
sys.exit(1)
# Get file contents
init_text = INIT_PATH.read_text("utf-8", "ignore")
toml_text = TOML_PATH.read_text("utf-8", "ignore")
# Extract the current version
init_version_match = VERSION_INIT_REGEX.search(init_text)
toml_version_match = VERSION_TOML_REGEX.search(toml_text)
init_version = init_version_match.group(2) if init_version_match is not None else None
toml_version = toml_version_match.group(2) if toml_version_match is not None else None
# Check for consistency
if init_version != toml_version:
print(
f"Conflicting versions found in __init__.py and pyproject.toml: {init_version} | {toml_version}",
file=sys.stderr,
)
sys.exit(1)
# Check whether a new version is provided
if len(sys.argv) != 2: # noqa: PLR2004
print("No new version provided as an argument!", file=sys.stderr)
sys.exit(1)
new_version = sys.argv[1].removeprefix("v")
def _replacement_formatter(match: re.Match[str]) -> str:
return f"{match.group(1)}{new_version}{match.group(3)}"
# Create replacement for previous match containing the new version
init_replacement = VERSION_INIT_REGEX.sub(_replacement_formatter, init_version_match.group(0))
toml_replacement = VERSION_TOML_REGEX.sub(_replacement_formatter, toml_version_match.group(0))
# Save updated versions
INIT_PATH.write_text(init_text.replace(init_version_match.group(0), init_replacement), "utf-8", "ignore")
TOML_PATH.write_text(toml_text.replace(toml_version_match.group(0), toml_replacement), "utf-8", "ignore")
print(f"Successfully updated version from {init_version} to {new_version}.")
sys.exit(0)