forked from cvxgrp/cvxportfolio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbumpversion.py
90 lines (68 loc) · 2.61 KB
/
bumpversion.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
90
"""Look for __version__ in any __init__.py recursively and upgrade.
Additionally, look for version string in any setup.py and (Sphinx) conf.py
and do the same. Version is assumed to be in the format X.Y.Z, where X, Y,
and Z are integers. Take argument revision (Z -> Z+1),
minor (Y -> Y+1, Z -> 0), or major (X -> X+1, Y -> 0, Z -> 0).
Return the updated version string."""
import subprocess
from pathlib import Path
def findversion(root='.'):
"""Find version number. Skip [env, venv, .*].
We use the first __init__.py with a __version__ that we find.
"""
p = Path(root)
for fname in p.iterdir():
if fname.name == '__init__.py':
with open(fname) as f:
lines = f.readlines()
for line in lines:
if '__version__' in line:
return eval(line.split('=')[1])
if fname.is_dir():
if not (fname.name in ['env', 'venv'] or fname.name[0] == '.'):
result = findversion(fname)
if result:
return result
def replaceversion(new_version, version, root='.'):
"""Replace version number. Skip [env, venv, .*].
We replace in all __init__.py, conf.py, setup.py, and pyproject.toml
"""
p = Path(root)
for fname in p.iterdir():
if fname.name in ['__init__.py', 'conf.py', 'setup.py',
'pyproject.toml']:
lines = []
with open(fname, 'rt') as fin:
for line in fin:
lines.append(line.replace(version, new_version))
with open(fname, "wt") as fout:
for line in lines:
fout.write(line)
subprocess.run(['git', 'add', str(fname)])
if fname.is_dir():
if not (fname.name in ['env', 'venv'] or fname.name[0] == '.'):
replaceversion(new_version, version, root=fname)
if __name__ == "__main__":
while True:
print('[revision/minor/major]')
what = input()
if what in ['revision', 'minor', 'major']:
break
version = findversion()
x, y, z = [int(el) for el in version.split('.')]
if what == 'revision':
z += 1
if what == 'minor':
y += 1
z = 0
if what == 'major':
x += 1
y = 0
z = 0
new_version = f"{x}.{y}.{z}"
print(new_version)
replaceversion(new_version, version)
subprocess.run(['git', 'commit', '--no-verify', '-em',
f"version {new_version}\n"])
subprocess.run(['git', 'tag', new_version])
subprocess.run(['git', 'push', '--no-verify', 'origin', new_version])