-
Notifications
You must be signed in to change notification settings - Fork 9
/
.bumpver.py
executable file
·161 lines (134 loc) · 4.76 KB
/
.bumpver.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
from datetime import datetime, date
from os import path, makedirs, unlink
from zipfile import ZipFile
from shutil import copy2
parser = argparse.ArgumentParser()
parser.add_argument('bump',
nargs = 1,
type = str,
metavar = 'BUMP',
help = "What to bump (major, minor, patch)")
parser.add_argument('--dry',
dest = 'dry',
action = 'store_true',
help = "Dry run (do not run)",
required = False)
args = vars(parser.parse_args())
# ---------------------------------------------------------------------
# Config
config_token = "CrossPlatformCompatibilityCookie"
config_version = "0.7.1"
config_date = date(2024, 9, 24)
config_files = [
('.bumpver.py', 'config_version = "{major}.{minor}.{patch}"'),
('.bumpver.py', f'config_date = date({{date:%Y, {config_token}%m, {config_token}%d}})'),
('README.md', 'version {major}.{minor}.{patch} {date:%d%b%Y}'),
('staggered.pkg', 'v {major}.{minor}.{patch}'),
('staggered.pkg', 'd Distribution-Date: {date:%Y%m%d}'),
('stata.toc', 'v {major}.{minor}.{patch}'),
('doc/staggered.sthlp', 'version {major}.{minor}.{patch} {date:%d%b%Y}'),
('src/ado/staggered.ado', 'version {major}.{minor}.{patch} {date:%d%b%Y}')
]
config_standalone = {
'staggered': [
'src/build/lstaggered.mlib',
'src/mata//staggered.mata',
'src/ado/staggered.ado',
'doc/staggered.sthlp'
]
}
# ---------------------------------------------------------------------
# Bump
def main(bump, dry = False):
args = ['major', 'minor', 'patch', 'standalone', 'standalone-zip']
if bump not in args:
msg = f"'{bump}' uknown; can only bump: {', '.join(args[:3])}"
raise Warning(msg)
if bump in ['standalone', 'standalone-zip']:
make_standalone(config_standalone, dry, '-zip' in bump)
else:
current_kwargs, update_kwargs = bump_kwargs(bump, config_version, config_date)
for file, string in config_files:
bump_file(file, string, current_kwargs, update_kwargs, dry)
def make_standalone(standalone, dry=False, inzip=False):
for label, files in standalone.items():
outzip = f'standalone/{label}-{config_version}.zip'
if dry:
if inzip:
for f in files:
print(f'{f} -> {outzip}')
else:
for f in files:
print(f'{f} -> standalone/')
else:
if not path.isdir('standalone'):
makedirs('standalone')
if inzip and path.isfile(outzip):
unlink(outzip)
if inzip:
with ZipFile(outzip, 'w') as zf:
for f in files:
print(f'{f} -> {outzip}')
zf.write(f, path.basename(f))
else:
for f in files:
print(f'{f} -> standalone/')
copy2(f, 'standalone')
def bump_file(file, string, current, update, dry = False):
find = (
string
.format(**current)
.replace(config_token + "0", "")
.replace(config_token, "")
)
with open(file, 'r') as fh:
lines = fh.readlines()
if find not in ''.join(lines):
print(f'WARNING: nothing to bump in {file}')
replace = (
string
.format(**update)
.replace(config_token + "0", "")
.replace(config_token, "")
)
ulines = []
for line in lines:
if find in line:
print(f'{file}: {find} -> {replace}')
ulines += [line.replace(find, replace)]
else:
ulines += [line]
if not dry:
with open(file, 'w') as fh:
fh.write(''.join(ulines))
def bump_kwargs(bump, config_version, config_date):
today = datetime.now()
major, minor, patch = config_version.split('.')
umajor, uminor, upatch = bump_sever(bump, major, minor, patch)
current_kwargs = {
'major': major,
'minor': minor,
'patch': patch,
'date': config_date
}
update_kwargs = {
'major': umajor,
'minor': uminor,
'patch': upatch,
'date': today
}
return current_kwargs, update_kwargs
def bump_sever(bump, major, minor, patch):
if bump == 'major':
return str(int(major) + 1), '0', '0'
elif bump == 'minor':
return major, str(int(minor) + 1), '0'
elif bump == 'patch':
return major, minor, str(int(patch) + 1)
else:
return major, minor, patch
if __name__ == "__main__":
main(args['bump'][0], args['dry'])