-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_version.py
More file actions
101 lines (83 loc) · 2.69 KB
/
Copy pathcheck_version.py
File metadata and controls
101 lines (83 loc) · 2.69 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
import argparse
import re
import sys
import tomllib
from pathlib import Path
from collections.abc import Iterable
VERSION_REGEX = r"(\d+\.\d+(?:\.\d+)*)"
def get_cmake_version(cmake_path: Path) -> str:
text = cmake_path.read_text(encoding="utf-8")
match = re.search(
rf"project\s*\([^\)]*VERSION\s+{VERSION_REGEX}", text, re.IGNORECASE
)
if match:
return match.group(1)
raise ValueError(f"[CMake] Could not find a valid project version in {cmake_path}")
def get_doxy_version(doxyfile_path: Path) -> str:
text = doxyfile_path.read_text(encoding="utf-8")
match = re.search(
rf'^\s*PROJECT_NUMBER\s*=\s*("?){VERSION_REGEX}\1', text, re.MULTILINE
)
if match:
return match.group(2) # group(2) because group(1) is the optional quote
raise ValueError(
f"[Doxygen] Could not find a valid PROJECT_NUMBER in {doxyfile_path}"
)
def get_pyproject_version(pyproject_path: Path) -> str:
with pyproject_path.open("rb") as f:
data = tomllib.load(f)
try:
return data["project"]["version"]
except KeyError:
raise ValueError(
f"[pyproject.toml] Could not find [project.version] in {pyproject_path}"
)
def all_equal(items: Iterable) -> bool:
return len(set(items)) == 1
def main(cmake: Path, doxygen: Path, pyproject: Path):
try:
project_versions = {
"CMake": get_cmake_version(cmake),
"Doxygen": get_doxy_version(doxygen),
"pyproject.toml": get_pyproject_version(pyproject),
}
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
if not all_equal(project_versions.values()):
version_msg_entries = [
f"{source}: {version}" for source, version in project_versions.items()
]
print(
f"Error: Project version mismatch:\n {'\n '.join(version_msg_entries)}",
file=sys.stderr,
)
sys.exit(1)
print(project_versions["CMake"]) # print the version to stdout for shell capture
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-c",
"--cmake",
type=Path,
default=Path("CMakeLists.txt"),
nargs=1,
help="Path to the root CMake file",
)
parser.add_argument(
"-d",
"--doxygen",
type=Path,
default=Path("Doxyfile"),
nargs=1,
help="Path to the Doxygen config file",
)
parser.add_argument(
"-p",
"--pyproject",
type=Path,
default=Path("pyproject.toml"),
nargs=1,
help="Path to the pyproject.toml file",
)
main(**vars(parser.parse_args()))