|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Used to check the input verion against the input |
| 3 | +import re |
| 4 | +import argparse |
| 5 | + |
| 6 | + |
| 7 | +def get_cmake_version(cmake_file): |
| 8 | + with open(cmake_file, 'r') as f: |
| 9 | + contents = f.read() |
| 10 | + match = re.search(r'project\(.*VERSION (\d+)\.(\d+)\.(\d+)', contents) |
| 11 | + if match: |
| 12 | + major, minor, patch = map(int, match.groups()) |
| 13 | + return major, minor, patch |
| 14 | + return None |
| 15 | + |
| 16 | + |
| 17 | +def validate_semver(version): |
| 18 | + pattern = r'^v(\d+)\.(\d+)\.(\d+)$' |
| 19 | + match = re.match(pattern, version) |
| 20 | + if match: |
| 21 | + return tuple(map(int, match.groups())) |
| 22 | + else: |
| 23 | + return None |
| 24 | + |
| 25 | + |
| 26 | +def compare_versions(input_version, cmake_version): |
| 27 | + if input_version > cmake_version: |
| 28 | + print("Input version is greater than CMake version.") |
| 29 | + exit(1) |
| 30 | + elif input_version < cmake_version: |
| 31 | + print("Input version is smaller than CMake version.") |
| 32 | + exit(1) |
| 33 | + else: |
| 34 | + print("Input version is equal to CMake version.") |
| 35 | + |
| 36 | + |
| 37 | +if __name__ == "__main__": |
| 38 | + parser = argparse.ArgumentParser(description="Check input version against CMake project version") |
| 39 | + parser.add_argument("--version", type=str, help="Input version with a 'v' prefix", required=True) |
| 40 | + args = parser.parse_args() |
| 41 | + |
| 42 | + cmake_version = get_cmake_version("CMakeLists.txt") |
| 43 | + if cmake_version is None: |
| 44 | + print("Failed to retrieve version from CMakeLists.txt.") |
| 45 | + exit(1) |
| 46 | + else: |
| 47 | + input_version = validate_semver(args.version) |
| 48 | + if input_version is None: |
| 49 | + print("Invalid input version format. Please provide a version in the format 'vX.Y.Z'") |
| 50 | + exit(1) |
| 51 | + else: |
| 52 | + compare_versions(input_version, cmake_version) |
| 53 | + |
| 54 | + |
| 55 | + |
0 commit comments