-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathupdate_version.py
More file actions
67 lines (55 loc) · 2.16 KB
/
update_version.py
File metadata and controls
67 lines (55 loc) · 2.16 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
import os
import sys
import tomllib
extensions_white = [".py", ".ini"]
extensions_black = [".pyc"]
def get_version_number():
"""Get version number from pyproject.toml"""
if os.path.exists("pyproject.toml"):
with open("pyproject.toml", "rb") as f:
data = tomllib.load(f)
if "project" in data and "version" in data["project"]:
return str(data["project"]["version"])
print("Version not specified in pyproject.toml", file=sys.stderr)
sys.exit(1)
print("pyproject.toml file not found", file=sys.stderr)
sys.exit(1)
def get_file_list():
"""Get list of all files with extensions from extensions_white"""
file_list = []
for path, subdirs, files in os.walk(os.getcwd()):
subdirs[:] = [d for d in subdirs if not d.startswith(".")] # skip hidden dirs
for name in files:
file_path = os.path.join(path, name)
if any(name.endswith(x) for x in extensions_white) and not name.startswith("."):
if not any(name.endswith(x) for x in extensions_black):
file_list.append(file_path)
return file_list
def main():
"""Update versions in all files"""
print("Running version update script")
version = get_version_number()
file_list = get_file_list()
any_updated = False
for path in file_list:
if "update_version.py" not in path:
with open(path, "r", encoding="utf-8") as f:
lines = f.readlines()
changed = False
for num, line in enumerate(lines):
if line.startswith("VERSION = ") and line.split("VERSION = ")[-1] != f'"{version}"\n':
lines[num] = f'VERSION = "{version}"\n'
changed = True
break
if changed:
with open(path, "w", encoding="utf-8") as f:
f.writelines(lines)
print(f"Version number updated in: {path}")
any_updated = True
if any_updated:
print(f"New version: {version}")
else:
print("Version in all files is already the latest")
sys.exit(0)
if __name__ == "__main__":
main()