|
| 1 | +import argparse |
| 2 | +import re |
| 3 | +import sys |
| 4 | +from pathlib import Path |
| 5 | +from collections.abc import Iterable |
| 6 | + |
| 7 | + |
| 8 | +VERSION_REGEX = r"(\d+\.\d+(?:\.\d+)*)" |
| 9 | + |
| 10 | + |
| 11 | +def get_cmake_version(cmake_path: Path) -> str: |
| 12 | + text = cmake_path.read_text() |
| 13 | + match = re.search(rf'project\s*\([^\)]*VERSION\s+{VERSION_REGEX}', text, re.IGNORECASE) |
| 14 | + if match: |
| 15 | + return match.group(1) |
| 16 | + raise ValueError(f"[CMake] Could not find a valid project version in {cmake_path}") |
| 17 | + |
| 18 | + |
| 19 | +def get_doxy_version(doxyfile_path: Path) -> str: |
| 20 | + text = doxyfile_path.read_text() |
| 21 | + match = re.search(rf'^\s*PROJECT_NUMBER\s*=\s*("?){VERSION_REGEX}\1', text, re.MULTILINE) |
| 22 | + if match: |
| 23 | + return match.group(2) # group(2) because group(1) is the optional quote |
| 24 | + raise ValueError(f"[Doxygen] Could not find a valid PROJECT_NUMBER in {doxyfile_path}") |
| 25 | + |
| 26 | + |
| 27 | +def all_equal(items: Iterable) -> bool: |
| 28 | + return len(set(items)) == 1 |
| 29 | + |
| 30 | + |
| 31 | +def main(cmake: Path, doxygen: Path): |
| 32 | + try: |
| 33 | + project_versions = { |
| 34 | + "CMake": get_cmake_version(cmake), |
| 35 | + "Doxygen": get_doxy_version(doxygen), |
| 36 | + } |
| 37 | + except Exception as e: |
| 38 | + print(f"Error: {e}", file=sys.stderr) |
| 39 | + sys.exit(1) |
| 40 | + |
| 41 | + |
| 42 | + if not all_equal(project_versions.values()): |
| 43 | + version_msg_entries = [f"{source}: {version}" for source, version in project_versions.items()] |
| 44 | + print(f"Error: Project version mismatch:\n {'\n '.join(version_msg_entries)}", file=sys.stderr) |
| 45 | + sys.exit(1) |
| 46 | + |
| 47 | + print(project_versions['CMake']) # print the version to stdout for shell capture |
| 48 | + |
| 49 | + |
| 50 | +if __name__ == "__main__": |
| 51 | + parser = argparse.ArgumentParser() |
| 52 | + parser.add_argument( |
| 53 | + "-c", "--cmake", |
| 54 | + type=Path, |
| 55 | + default="CMakeLists.txt", |
| 56 | + nargs=1, |
| 57 | + help="Path to the root CMake file" |
| 58 | + ) |
| 59 | + parser.add_argument( |
| 60 | + "-d", "--doxygen", |
| 61 | + type=Path, |
| 62 | + default="Doxyfile", |
| 63 | + nargs=1, |
| 64 | + help="Path to the Doxygen config file" |
| 65 | + ) |
| 66 | + |
| 67 | + main(**vars(parser.parse_args())) |
0 commit comments