-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbump_version.py
More file actions
executable file
·114 lines (81 loc) · 3.1 KB
/
bump_version.py
File metadata and controls
executable file
·114 lines (81 loc) · 3.1 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
#!/usr/bin/env python3
import os
import argparse
import subprocess
def main():
msg = 'Update bump version and commit with the tag'
parser = argparse.ArgumentParser(description=msg)
msg = "Either 'major', 'minor', 'patch', or 'post"
parser.add_argument('which', type=str, help=msg)
args = parser.parse_args()
file1 = VersionedFile('setup.py')
file2 = VersionedFile('./autoed/__init__.py')
version = file1.get_version()
new_version = update_version(version, which=args.which)
file1.update(new_version)
file2.update(new_version)
message = f'"Bump version: {version} -> {new_version}"'
print(message)
cmd1 = f'git add -u'
cmd2 = f'git commit -m {message}'
cmd3 = f'git tag v{new_version}'
result = subprocess.run(cmd1, shell=True, capture_output=True, text=True)
result = subprocess.run(cmd2, shell=True, capture_output=True, text=True)
result = subprocess.run(cmd3, shell=True, capture_output=True, text=True)
class VersionedFile:
def __init__(self, filename):
self.file = os.path.abspath(filename)
with open(self.file, 'r') as file:
self.lines = file.readlines()
version_line_index = None
version_line = None
for ln_index, line in enumerate(self.lines):
if 'version' in line and '=' in line:
version_line_index = ln_index
version_line = line
break
self.version_line_index = version_line_index
self.version_line = version_line
def get_version(self):
version = self.version_line.split('=')[1].strip().replace(',', '')
version = version.replace("'", "")
return version
def update(self, new_version):
new_version_line = self.version_line.split('=')[0]
if ' = ' in self.version_line:
new_version_line += '= ' + f"'{new_version}'"
else:
new_version_line += '=' + f"'{new_version}'"
if ',' in self.version_line:
new_version_line += ",\n"
else:
new_version_line += "\n"
self.lines[self.version_line_index] = new_version_line
with open(self.file, 'w') as file:
for line in self.lines:
file.write(line)
def update_version(version, which='patch'):
version = version.replace("'", "")
components = version.split('.')
n = len(components)
if not (n == 3 or n == 4):
raise ValueError(f'Version format: {version} not recognized')
post_index = 0
if n == 4:
post_index = int(components[3].replace('post', ''))
major = int(components[0])
minor = int(components[1])
patch = int(components[2])
if which == 'patch':
out_version = f'{major}.{minor}.{patch + 1}'
elif which == 'minor':
out_version = f'{major}.{minor + 1}.0'
elif which == 'major':
out_version = f'{major+1}.0.0'
elif which == 'post':
out_version = f'{major}.{minor}.{patch}.post{post_index+1}'
else:
raise ValueError(f'which = "{which}" not recognized')
return out_version
if __name__ == '__main__':
main()