This repository was archived by the owner on Dec 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate_version.py
More file actions
251 lines (203 loc) · 7.71 KB
/
update_version.py
File metadata and controls
251 lines (203 loc) · 7.71 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env python3
"""
Version Update Script for libsparseir
Updates version numbers across C++ and Python components:
- include/sparseir/version.h (C++ library)
- python/pyproject.toml (Python package)
Usage:
python update_version.py 0.4.3
python update_version.py 1.0.0
"""
import sys
import re
import os
from pathlib import Path
def update_cpp_version(version_parts, repo_root):
"""Update C++ version in backend/cxx/include/sparseir/version.h"""
version_h_path = repo_root / "backend" / "cxx" / "include" / "sparseir" / "version.h"
if not version_h_path.exists():
print(f"Error: {version_h_path} not found")
return False
# Read current content
with open(version_h_path, 'r') as f:
content = f.read()
# Update version macros
content = re.sub(
r'#define SPARSEIR_VERSION_MAJOR \d+',
f'#define SPARSEIR_VERSION_MAJOR {version_parts[0]}',
content
)
content = re.sub(
r'#define SPARSEIR_VERSION_MINOR \d+',
f'#define SPARSEIR_VERSION_MINOR {version_parts[1]}',
content
)
content = re.sub(
r'#define SPARSEIR_VERSION_PATCH \d+',
f'#define SPARSEIR_VERSION_PATCH {version_parts[2]}',
content
)
# Write updated content
with open(version_h_path, 'w') as f:
f.write(content)
print(f"✓ Updated C++ version in {version_h_path}")
return True
def update_python_version(version_string, repo_root):
"""Update Python version in python/pyproject.toml"""
pyproject_path = repo_root / "python" / "pyproject.toml"
if not pyproject_path.exists():
print(f"Error: {pyproject_path} not found")
return False
# Read current content
with open(pyproject_path, 'r') as f:
content = f.read()
# Update version in [project] section only (not cmake.version)
content = re.sub(
r'(\[project\][^\[]*?)version = "[^"]*"',
f'\\1version = "{version_string}"',
content,
flags=re.DOTALL
)
# If no version field exists, add it after name
if 'version = ' not in content:
content = re.sub(
r'(name = "[^"]*")',
f'\\1\nversion = "{version_string}"',
content
)
# Write updated content
with open(pyproject_path, 'w') as f:
f.write(content)
print(f"✓ Updated Python version in {pyproject_path}")
return True
def validate_version(version_string):
"""Validate version string format (x.y.z)"""
pattern = r'^\d+\.\d+\.\d+$'
if not re.match(pattern, version_string):
print(f"Error: Invalid version format '{version_string}'. Expected format: x.y.z (e.g., 0.4.3)")
return False
return True
def parse_version(version_string):
"""Parse version string into components"""
parts = version_string.split('.')
return [int(part) for part in parts]
def get_cpp_version(repo_root):
"""Get current C++ version from version.h"""
version_h_path = repo_root / "backend" / "cxx" / "include" / "sparseir" / "version.h"
if not version_h_path.exists():
return None
with open(version_h_path, 'r') as f:
content = f.read()
major_match = re.search(r'#define SPARSEIR_VERSION_MAJOR (\d+)', content)
minor_match = re.search(r'#define SPARSEIR_VERSION_MINOR (\d+)', content)
patch_match = re.search(r'#define SPARSEIR_VERSION_PATCH (\d+)', content)
if major_match and minor_match and patch_match:
return f"{major_match.group(1)}.{minor_match.group(1)}.{patch_match.group(1)}"
return None
def get_python_version(repo_root):
"""Get current Python version from pyproject.toml"""
pyproject_path = repo_root / "python" / "pyproject.toml"
if not pyproject_path.exists():
return None
with open(pyproject_path, 'r') as f:
content = f.read()
version_match = re.search(r'version = "([^"]*)"', content)
if version_match:
return version_match.group(1)
return None
def show_current_versions(repo_root):
"""Show current versions in both files"""
print("Current versions:")
cpp_version = get_cpp_version(repo_root)
python_version = get_python_version(repo_root)
if cpp_version:
print(f" C++ (version.h): {cpp_version}")
else:
print(f" C++ (version.h): Unable to parse or file not found")
if python_version:
print(f" Python (pyproject.toml): {python_version}")
else:
print(f" Python (pyproject.toml): No version field found or file not found")
return cpp_version, python_version
def check_version_consistency(repo_root):
"""Check if versions are consistent across files"""
cpp_version, python_version = show_current_versions(repo_root)
print()
if not cpp_version:
print("❌ Could not read C++ version from backend/cxx/include/sparseir/version.h")
return False
if not python_version:
print("❌ Could not read Python version from python/pyproject.toml")
return False
if cpp_version == python_version:
print(f"✅ Versions are consistent: {cpp_version}")
return True
else:
print(f"❌ Version mismatch:")
print(f" C++: {cpp_version}")
print(f" Python: {python_version}")
print()
print(f"To fix this, run: python update_version.py {cpp_version}")
return False
def main():
repo_root = Path(__file__).parent
if len(sys.argv) == 1:
# No arguments - check version consistency
print("Version Consistency Check")
print("=" * 30)
print()
if check_version_consistency(repo_root):
print()
print("\U0001F44D All versions are in sync!")
else:
print()
print("\U0001F6A8 Version inconsistency detected.")
sys.exit(1)
return
if len(sys.argv) != 2:
print("Usage:")
print(" python update_version.py # Check version consistency")
print(" python update_version.py <version> # Update all versions")
print()
print("Examples:")
print(" python update_version.py # Check current versions")
print(" python update_version.py 0.4.3 # Update to version 0.4.3")
print()
# Show current versions
show_current_versions(repo_root)
sys.exit(1)
version_string = sys.argv[1]
repo_root = Path(__file__).parent
# Validate version format
if not validate_version(version_string):
sys.exit(1)
# Parse version components
version_parts = parse_version(version_string)
print(f"Updating version to {version_string}")
print()
# Show current versions
show_current_versions(repo_root)
print()
# Update versions
success = True
success &= update_cpp_version(version_parts, repo_root)
success &= update_python_version(version_string, repo_root)
if success:
print()
print(f"✅ Successfully updated all versions to {version_string}")
print()
print("Updated files:")
print(" - backend/cxx/include/sparseir/version.h (C++ library)")
print(" - python/pyproject.toml (Python package)")
print(" - Conda recipe will automatically use version.h")
print()
print("Next steps:")
print("1. Review the changes: git diff")
print("2. Test the build: cd python && pip wheel .")
print("3. Commit the changes: git add -A && git commit -m 'Bump version to {}'".format(version_string))
else:
print()
print("❌ Some updates failed. Please check the errors above.")
sys.exit(1)
if __name__ == "__main__":
main()