-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathupdate_version.py
More file actions
60 lines (44 loc) · 1.42 KB
/
Copy pathupdate_version.py
File metadata and controls
60 lines (44 loc) · 1.42 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
import json
import re
VERSION_FILE = "src/rsmetacheck/__init__.py"
PYPROJECT_TOML = "pyproject.toml"
CODEMETA_JSON = "codemeta.json"
def get_version(file_path):
with open(file_path) as f:
for line in f:
if line.startswith("__version__"):
return line.split("=")[1].strip().strip('"')
return None
def update_pyproject_toml(new_version):
with open(PYPROJECT_TOML) as f:
content = f.read()
if "{version-file}" in content:
new_content = content.replace("{version-file}", new_version)
else:
new_content = re.sub(
r'^(version\s*=\s*)"[^"]*"',
rf'\1"{new_version}"',
content,
flags=re.MULTILINE,
)
with open(PYPROJECT_TOML, "w") as f:
f.write(new_content)
def update_codemeta_json(new_version):
with open(CODEMETA_JSON) as f:
data = json.load(f)
data["version"] = new_version
with open(CODEMETA_JSON, "w") as f:
json.dump(data, f, indent=2)
f.write("\n")
def main():
new_version = get_version(VERSION_FILE)
if not new_version:
raise ValueError(
f"Could not read version from '{VERSION_FILE}'. "
'Ensure it contains a line like: __version__ = "x.y.z"'
)
update_pyproject_toml(new_version)
update_codemeta_json(new_version)
print(f"Version synced to {new_version}")
if __name__ == "__main__":
main()