|
| 1 | +"""Test script to check if SPARC package version is older than |
| 2 | +the latest Changelog entry |
| 3 | +
|
| 4 | +We generally do not check the other way around since sometimes a trivial bump of SPARC version may be necessary |
| 5 | +""" |
| 6 | +import re |
| 7 | +import json |
| 8 | +from datetime import datetime |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | + |
| 12 | +def load_parameters_json(json_path): |
| 13 | + """ |
| 14 | + Load the parameters from the parameters.json file. |
| 15 | + """ |
| 16 | + with open(json_path, 'r') as f: |
| 17 | + parameters = json.load(f) |
| 18 | + if "sparc_version" not in parameters: |
| 19 | + raise KeyError("The 'sparc_version' field is missing in parameters.json") |
| 20 | + return parameters["sparc_version"] |
| 21 | + |
| 22 | + |
| 23 | +def extract_latest_date_from_changelog(changelog_path): |
| 24 | + """ |
| 25 | + Extracts the latest date from the changelog file. |
| 26 | + """ |
| 27 | + date_patterns = [ |
| 28 | + r"(?P<date>\b(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{1,2}, \d{4})", |
| 29 | + r"(?P<date>\b(?:January|February|March|April|May|June|July|August|September|October|November|December) \d{1,2}, \d{4})", |
| 30 | + ] |
| 31 | + |
| 32 | + latest_date = None |
| 33 | + changelog_path = Path(changelog_path) |
| 34 | + |
| 35 | + with changelog_path.open("r") as f: |
| 36 | + content = f.read() |
| 37 | + |
| 38 | + for pattern in date_patterns: |
| 39 | + matches = re.findall(pattern, content) |
| 40 | + for match in matches: |
| 41 | + try: |
| 42 | + # Convert matched date to datetime object |
| 43 | + parsed_date = datetime.strptime(match, "%b %d, %Y") if "," in match else datetime.strptime(match, "%B %d, %Y") |
| 44 | + if latest_date is None or parsed_date > latest_date: |
| 45 | + latest_date = parsed_date |
| 46 | + except ValueError: |
| 47 | + continue # Skip invalid date formats |
| 48 | + |
| 49 | + if latest_date is None: |
| 50 | + raise ValueError("No valid date found in the changelog.") |
| 51 | + return latest_date |
| 52 | + |
| 53 | + |
| 54 | +def check_version_against_changelog(parameters_json_path, changelog_path): |
| 55 | + """ |
| 56 | + Check if the package version in parameters.json is older than the latest changelog date. |
| 57 | + """ |
| 58 | + # Load sparc_version from parameters.json |
| 59 | + sparc_version = load_parameters_json(parameters_json_path) |
| 60 | + version_date = datetime.strptime(sparc_version, "%Y.%m.%d") |
| 61 | + |
| 62 | + # Extract the latest date from the changelog |
| 63 | + latest_changelog_date = extract_latest_date_from_changelog(changelog_path) |
| 64 | + |
| 65 | + if version_date < latest_changelog_date: |
| 66 | + print("Version Check Report:") |
| 67 | + print("-" * 60) |
| 68 | + print(f"ERROR: SPARC version ({version_date.strftime('%Y.%m.%d')}) " |
| 69 | + f"is older than the latest changelog date ({latest_changelog_date.strftime('%Y.%m.%d')}).") |
| 70 | + print("Please update initialization.c!") |
| 71 | + print("-" * 60) |
| 72 | + return False |
| 73 | + else: |
| 74 | + print("Version Check Report:") |
| 75 | + print("-" * 60) |
| 76 | + print("SUCCESS:") |
| 77 | + print("-" * 60) |
| 78 | + return True |
| 79 | + |
| 80 | + |
| 81 | +def main(): |
| 82 | + import argparse |
| 83 | + parser = argparse.ArgumentParser( |
| 84 | + description="Check if package version is up-to-date with the changelog." |
| 85 | + ) |
| 86 | + parser.add_argument( |
| 87 | + "parameters_json", |
| 88 | + type=str, |
| 89 | + help="Path to the parameters.json file." |
| 90 | + ) |
| 91 | + parser.add_argument( |
| 92 | + "changelog", |
| 93 | + type=str, |
| 94 | + help="Path to the changelog file." |
| 95 | + ) |
| 96 | + |
| 97 | + args = parser.parse_args() |
| 98 | + |
| 99 | + # Run the version check |
| 100 | + success = check_version_against_changelog(args.parameters_json, args.changelog) |
| 101 | + if not success: |
| 102 | + exit(1) |
| 103 | + else: |
| 104 | + exit(0) |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + main() |
0 commit comments