|
1 | 1 | import sys |
2 | 2 | import re |
| 3 | +import argparse |
3 | 4 |
|
4 | | -def update_version_info(version_str, file_path='file_version_info.txt'): |
| 5 | +def update_version_info(version_str, build_number=None, file_path='file_version_info.txt'): |
5 | 6 | """ |
6 | 7 | Updates the file_version_info.txt with the given version. |
7 | | - Handles versions with suffixes like '2025.12.0-beta' or '2025.12.0-dev'. |
| 8 | + Handles versions with suffixes like '2025.12.0-beta' or '2026.1.5.dev0+hash'. |
8 | 9 | """ |
9 | | - # Strip suffix for numeric tuple (e.g., "2025.12.0-beta" -> "2025.12.0") |
10 | | - base_version = re.sub(r'-.*$', '', version_str) |
| 10 | + print(f"Processing version: {version_str} (Build Number: {build_number})") |
11 | 11 |
|
| 12 | + # 1. Extract the BASE numeric part (Major.Minor.Patch) |
| 13 | + # We want only the MAJOR.MINOR.PATCH part for the fixed file info tuples. |
| 14 | + match = re.search(r'([0-9]+\.[0-9]+\.[0-9]+)', version_str) |
| 15 | + if not match: |
| 16 | + print(f"Error: Could not find a valid numeric version in '{version_str}'") |
| 17 | + sys.exit(1) |
| 18 | + |
| 19 | + base_version = match.group(1) |
12 | 20 | parts = base_version.split('.') |
13 | | - while len(parts) < 4: |
| 21 | + |
| 22 | + # Ensure we have at least 3 parts |
| 23 | + while len(parts) < 3: |
14 | 24 | parts.append('0') |
15 | 25 |
|
16 | | - # Ensure all parts are numeric |
17 | | - numeric_parts = [] |
18 | | - for p in parts[:4]: |
19 | | - try: |
20 | | - numeric_parts.append(str(int(p))) |
21 | | - except ValueError: |
22 | | - numeric_parts.append('0') |
| 26 | + # 2. Add/Override the 4th component (Build/Revision) |
| 27 | + if build_number is not None: |
| 28 | + parts.append(str(build_number)) |
| 29 | + else: |
| 30 | + # Default to 0 if not provided |
| 31 | + parts.append('0') |
| 32 | + |
| 33 | + # Limit to 4 components for Windows VersionInfo |
| 34 | + numeric_parts = parts[:4] |
| 35 | + |
| 36 | + # 3. Generate the formats needed for file_version_info.txt |
23 | 37 |
|
24 | 38 | # tuple format: (2025, 12, 0, 0) |
25 | 39 | tuple_str = f"({', '.join(numeric_parts)})" |
26 | | - # string format: '2025.12.0.0' for file version (numeric only) |
| 40 | + |
| 41 | + # string format: '2025.12.0.0' for FileVersion (must be numeric only) |
27 | 42 | full_version_str = '.'.join(numeric_parts) |
28 | | - # Display version includes suffix for ProductVersion string |
29 | | - display_version_str = version_str if '-' in version_str else full_version_str |
30 | 43 |
|
31 | | - with open(file_path, 'r', encoding='utf-8') as f: |
32 | | - content = f.read() |
| 44 | + # display_version_str is for ProductVersion (can include suffixes) |
| 45 | + display_version_str = version_str |
| 46 | + |
| 47 | + # 4. Read and Update the File |
| 48 | + try: |
| 49 | + with open(file_path, 'r', encoding='utf-8') as f: |
| 50 | + content = f.read() |
| 51 | + except FileNotFoundError: |
| 52 | + print(f"Error: {file_path} not found.") |
| 53 | + sys.exit(1) |
33 | 54 |
|
34 | 55 | # Update filevers and prodvers tuples (must be numeric) |
35 | | - content = re.sub(r'filevers=\(\d+, \d+, \d+, \d+\)', f'filevers={tuple_str}', content) |
36 | | - content = re.sub(r'prodvers=\(\d+, \d+, \d+, \d+\)', f'prodvers={tuple_str}', content) |
| 56 | + # Use smart regex to match variation in whitespace |
| 57 | + content = re.sub(r'filevers\s*=\s*\([^)]+\)', f'filevers={tuple_str}', content) |
| 58 | + content = re.sub(r'prodvers\s*=\s*\([^)]+\)', f'prodvers={tuple_str}', content) |
37 | 59 |
|
38 | | - # Update StringStructs |
39 | | - # Update StringStructs |
40 | | - # FileVersion should be numeric only |
| 60 | + # Update StringStructs (FileVersion and ProductVersion) |
| 61 | + # FileVersion should be numeric only for Windows stability |
41 | 62 | content = re.sub(r"StringStruct\(u?'FileVersion', u?'[^']+'\)", f"StringStruct(u'FileVersion', u'{full_version_str}')", content) |
42 | 63 | # ProductVersion can include suffix for display |
43 | 64 | content = re.sub(r"StringStruct\(u?'ProductVersion', u?'[^']+'\)", f"StringStruct(u'ProductVersion', u'{display_version_str}')", content) |
44 | 65 |
|
45 | 66 | with open(file_path, 'w', encoding='utf-8') as f: |
46 | 67 | f.write(content) |
47 | 68 |
|
48 | | - print(f"Updated {file_path} to version {display_version_str} (numeric: {full_version_str})") |
| 69 | + print(f"Successfully updated {file_path}:") |
| 70 | + print(f" - FileVersion (Numeric): {full_version_str}") |
| 71 | + print(f" - ProductVersion (Display): {display_version_str}") |
| 72 | + print(f" - Tuple (FFI): {tuple_str}") |
49 | 73 |
|
50 | 74 | if __name__ == "__main__": |
51 | | - if len(sys.argv) < 2: |
52 | | - print("Usage: python update_version_info.py <version>") |
53 | | - sys.exit(1) |
| 75 | + parser = argparse.ArgumentParser(description="Update version information in file_version_info.txt") |
| 76 | + parser.add_argument("version", help="Full version string (e.g., 2026.1.5.dev0+hash)") |
| 77 | + parser.add_argument("--build", type=int, help="Optional build number (4th component override)", default=None) |
| 78 | + parser.add_argument("--file", help="Path to version info file", default="file_version_info.txt") |
| 79 | + |
| 80 | + args = parser.parse_args() |
54 | 81 |
|
55 | | - update_version_info(sys.argv[1]) |
| 82 | + update_version_info(args.version, build_number=args.build, file_path=args.file) |
0 commit comments