forked from voting-tools/pref_voting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_version.py
89 lines (75 loc) · 2.86 KB
/
update_version.py
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
from tempfile import mkstemp
from shutil import move, copymode
from os import fdopen, remove
import argparse
init_file = './pref_voting/__init__.py'
setup_file = './setup.py'
pyproject_file = './pyproject.toml'
conf_file = './docs/source/conf.py'
def replace(file_path, pattern, subst):
#Create temp file
fh, abs_path = mkstemp()
with fdopen(fh,'w') as new_file:
with open(file_path) as old_file:
for line in old_file:
new_file.write(line.replace(pattern, subst))
#Copy the file permissions from the old file to the new file
copymode(file_path, abs_path)
#Remove original file
remove(file_path)
#Move new file
move(abs_path, file_path)
# Instantiate the parser
parser = argparse.ArgumentParser(description='Update version of pref_voting')
parser.add_argument('--version', type=str)
parser.add_argument('--check', action='store_true')
args = parser.parse_args()
check = args.check
if check:
versions = list()
print("Checking version\n")
print("__init__.py: ")
with open(init_file) as old_file:
for line in old_file:
print(line)
versions.append(line.split("=")[1].strip().replace("'", ""))
print("conf.py: ")
with open(conf_file) as old_file:
for line in old_file:
if "release" in line:
print(line)
versions.append(line.split("=")[1].strip().replace("'", ""))
print("setup.py: ")
with open(setup_file) as old_file:
for line in old_file:
if "version" in line:
print(line)
versions.append(line.split("=")[1].strip()[0:-1].replace('"',''))
print("pyproject.toml: ")
with open(pyproject_file) as old_file:
for line in old_file:
if "version" in line:
print(line)
versions.append(line.split("=")[1].strip().replace('"',''))
if len(list(set(versions))) > 1:
print("ERROR: Multiple versions found!!")
print("Versions: ", list(set(versions)))
else:
new_version = args.version
print(new_version)
with open(init_file) as old_file:
for line in old_file:
old_version = line.split("=")[1].strip().replace("'", "")
print("The current version is: ", old_version)
print("The new version is: ", new_version)
if old_version == new_version:
print("Versions are the same, exiting")
exit()
replace(pyproject_file, f'version = "{old_version}"', f'version = "{new_version}"')
print("Updated pyproject.toml")
replace(setup_file, f'version="{old_version}"', f'version="{new_version}"')
print("Updated setup.py")
replace(conf_file, f"release = '{old_version}'", f"release = '{new_version}'")
print("Updated conf.py")
replace(init_file, f"__version__ = '{old_version}'", f"__version__ = '{new_version}'")
print("Updated __init__.py")