-
Notifications
You must be signed in to change notification settings - Fork 0
/
bump.py
executable file
·144 lines (123 loc) · 3.9 KB
/
bump.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python3
from datetime import datetime
import sys
import subprocess # for check_output
from trf.__version__ import version
def check_output(cmd):
if not cmd:
return
res = ''
try:
res = subprocess.check_output(
cmd,
stderr=subprocess.STDOUT,
shell=True,
universal_newlines=True,
encoding='UTF-8',
)
return True, res
except subprocess.CalledProcessError as e:
print(f"Error running {cmd}\n'{e.output}'")
lines = e.output.strip().split('\n')
msg = lines[-1]
return False, msg
possible_extensions = ['a', 'b', 'rc']
pre = post = version
ext = 'a'
ext_num = 1
for poss in possible_extensions:
if poss in version:
ext = poss
pre, post = version.split(ext)
ext_num = int(post) + 1
break
extension_options = {
'a': {'a': f'a{ext_num}', 'b': 'b0', 'r': 'rc0'},
'b': {'b': f'b{ext_num}', 'r': 'rc0'},
'rc': {'r': f'rc{ext_num}'},
}
# print(version, pre, post, ext)
new_ext = f'{pre}{ext}{ext_num}'
# print(pre)/setup
major, minor, patch = pre.split('.')
# print(major, minor, patch)
b_patch = '.'.join([major, minor, str(int(patch) + 1)])
b_minor = '.'.join([major, str(int(minor) + 1), '0'])
b_major = '.'.join([str(int(major) + 1), '0', '0'])
# print(b_patch, b_minor, b_major)
# parts = pre.split('.')
# parts[-1] = str(int(parts[-1]) + 1)
# new_patch = ".".join(parts)
opts = [f'The current version is {version}']
if ext and ext in extension_options:
i = 0
for k, v in extension_options[ext].items():
opts.append(f' {k}: {pre}{v}')
opts.append(f' p: {b_patch}')
opts.append(f' n: {b_minor}')
opts.append(f' j: {b_major}')
import os
version_file = os.path.join(os.getcwd(), 'trf', '__version__.py')
print('\n'.join(opts))
res = input(f'Which new version? ')
new_version = ''
res = res.lower()
if not res:
print('cancelled')
sys.exit()
bmsg = ''
if res in extension_options[ext]:
new_version = f'{pre}{extension_options[ext][res]}'
bmsg = 'release candidate version update'
elif res == 'p':
new_version = b_patch
bmsg = 'patch version update'
elif res == 'n':
new_version = b_minor
bmsg = 'minor version update'
elif res == 'j':
new_version = b_major
bmsg = 'major version update'
tplus = ''
if bmsg:
tplus = input(f'Optional {bmsg} message:\n')
tmsg = f'Tagged version {new_version}. {tplus}'
print(f'\nThe tag message for the new version will be:\n{tmsg}\n')
ans = input(f'Commit and tag new version: {new_version}? [yN] ')
if ans.lower() != 'y':
print('cancelled')
sys.exit()
if new_version:
with open(version_file, 'w') as fo:
fo.write(f"version = '{new_version}'")
print(f'new version: {new_version}')
tmsg = f'Tagged version {new_version}. {tplus}'
check_output(f"git commit -a -m '{tmsg}'")
ok, version_info = check_output("git log --pretty=format:'%ai' -n 1")
check_output(f"git tag -a -f '{new_version}' -m '{version_info}'")
count = 20
check_output(
f"echo 'Recent tagged changes as of {datetime.now()}:' > CHANGES.txt"
)
check_output(
f"git log --pretty=format:'- %ar%d %an%n %h %ai%n%w(70,4,4)%B' --max-count={count} --no-walk --tags >> CHANGES.txt"
)
check_output(f"git commit -a --amend -m '{tmsg}'")
ans = input('switch to master, merge working and push to origin? [yN] ')
if ans.lower() != 'y':
print('cancelled')
sys.exit()
ok, res = check_output(
f'git checkout master && git merge working && git push && git checkout working && git push'
)
if res:
print(res)
ans = input('upload sdist to PyPi using twine? [yN] ')
if ans.lower() != 'y':
print('cancelled')
sys.exit()
ok, res = check_output('./upload_sdist.sh')
if res:
print(res)
else:
print(f'retained version: {version}')