-
Notifications
You must be signed in to change notification settings - Fork 84
/
version-tool.py
executable file
·289 lines (232 loc) · 8.11 KB
/
version-tool.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/python
import argparse
import json
import os
import re
import subprocess
import sys
verbose = False
def warn(line):
print('[WARN] ' + line, file=sys.stderr)
def err(line):
raise Exception(line)
def dbg(line):
if verbose:
print('[DBG] ' + line, file=sys.stderr)
def underline(string):
return f'\033[4m{string}\033[0m'
def do_meson_ver(new_ver):
path = 'meson.build'
with open(path, 'r') as f:
lines = f.readlines()
ver_lineno = -1
for lineno, line in enumerate(lines):
ver_re = r"(^.*version:\s*')([0-9.]*)('.*$)"
m = re.match(ver_re, line.rstrip())
if m:
ver_lineno = lineno
pre = m.group(1)
ver = m.group(2)
post = m.group(3)
dbg(f'[{path}:{lineno+1}] {pre}{underline(ver)}{post}')
break
if ver_lineno < 0:
err(f'[{path}] Failed to find verion')
if new_ver is None or ver == new_ver:
return ver
print(f'[{path}:{ver_lineno+1}] Updating from {ver} to {new_ver}')
lines[ver_lineno] = f'{pre}{new_ver}{post}\n'
with open(path, 'w') as f:
f.writelines(lines)
return ver
def get_rust_paths():
result = subprocess.run(['git', 'ls-files'], stdout=subprocess.PIPE)
lines = result.stdout.decode('utf-8').splitlines()
paths = []
for line in lines:
# ignore root Cargo.toml
if line.endswith('Cargo.toml') and '/' in line:
paths.append(line)
return paths
def cargo_path_to_crate(path):
return path.split('/')[-2]
def do_rust_ver(path, new_ver):
with open(path, 'r') as f:
lines = f.readlines()
name_lineno = -1
ver_lineno = -1
name = None
ver = None
for lineno, line in enumerate(lines):
workspace_re = r'(^\s*)(\[\s*workspace\s*\])(.*$)'
name_re = r'(^\s*name\s*=\s*")(.*)(".*$)'
ver_re = r'(^\s*version\s*=\s*")(.*)(".*$)'
line = line.rstrip()
m = re.match(workspace_re, line)
if m:
dbg(f'[{path}:{lineno}] SKIP: {m.group(1)}{underline(m.group(2))}{m.group(3)}')
return None
m = re.match(name_re, line)
if m:
name_lineno = lineno
name = m.group(2)
dbg(f'[{path}:{lineno+1}] {m.group(1)}{underline(name)}{m.group(3)}')
else:
m = re.match(ver_re, line)
if m:
ver_lineno = lineno
pre = m.group(1)
ver = m.group(2)
post = m.group(3)
dbg(f'[{path}:{lineno+1}] {pre}{underline(ver)}{post}')
if name_lineno >= 0 and ver_lineno >= 0:
break
if name_lineno < 0 or ver_lineno < 0:
err(f'[{path}] Failed to find name or version')
if name != cargo_path_to_crate(path):
warn(f'[{path}:{name_lineno}] name \"{name}\" does not match the path')
if new_ver is None or ver == new_ver:
return ver
print(f'[{path}:{ver_lineno+1}] Updating from {ver} to {new_ver}')
lines[ver_lineno] = f'{pre}{new_ver}{post}\n'
with open(path, 'w') as f:
f.writelines(lines)
return ver
def do_rust_deps(path, deps, new_deps):
with open(path, 'r') as f:
lines = f.readlines()
in_dep = None
block_depth = 0
crate = None
need_write = False
for lineno, line in enumerate(lines):
line = line.rstrip()
# determine whether in a dependencies section
sect_re = r'^\s*\[([^\[\]]*)]\s*$'
m = re.match(sect_re, line)
if m:
if block_depth != 0:
err(f'[{path}:{lineno+1}] Unbalanced block_depth {block_depth}');
sect = m.group(1).strip()
if sect.endswith('dependencies'):
dbg(f'[{path}{lineno+1}] [{sect}]')
in_dep = sect
else:
in_dep = None
continue
if not in_dep:
continue
# strip and store comment
body = line
comment_re = r'(^.*)(#.*$)'
comment = ""
m = re.match(comment_re, body)
if m:
body = m.group(1)
comment = m.group(2)
if len(body.strip()) == 0:
continue
# determine the current crate
if block_depth == 0:
crate_re = r'^\s*([^=\s]*)\s*=.*$'
m = re.match(crate_re, body)
if m:
crate = m.group(1)
crate_on_line = True
else:
warn(f'[{path}:{lineno+1}] Failed to find crate name')
crate = None
else:
crate_on_line = False
# do dumb nesting depth tracking
block_depth += body.count('{') - body.count('}')
block_depth += body.count('[') - body.count(']')
if crate is None:
continue
# determine the crate version
ver = None
if crate_on_line:
ver_re = r'(^[^=].*=\s*")([^"]*)("\s*$)'
m = re.match(ver_re, body)
if m:
pre = m.group(1)
ver = m.group(2)
post = m.group(3)
if ver is None:
ver_re = r'(^.*version\s*=\s*")([^"]*)(".*$)'
m = re.match(ver_re, body)
if m:
pre = m.group(1)
ver = m.group(2)
post = m.group(3)
if ver is None:
if block_depth == 0:
warn(f'{path}:{lineno+1} no version')
continue
dbg(f'[{path}:{lineno+1}] {crate}: {pre}{underline(ver)}{post}')
# check whether the version matches
if crate in deps:
if deps[crate] != ver:
warn(f'[{path}:{lineno+1}] crate "{crate}" {ver} mismatches existing {deps[crate]}')
else:
deps[crate] = ver
if crate in new_deps:
new_ver = new_deps[crate]
if ver != new_ver:
print(f'[{path}:{lineno+1}] Updating dep {crate} = "{ver}" -> "{new_ver}"')
lines[lineno] = f'{pre}{new_ver}{post}{comment}\n'
need_write = True
crate = None
if block_depth != 0:
err(f'[{path}:{lineno+1}] Unbalanced block_depth {block_depth}');
if need_write:
with open(path, 'w') as f:
f.writelines(lines)
def main():
parser = argparse.ArgumentParser(prog='version-tool.py',
description='Check and update versions. "version-tool.py > vers.json" to generate the template. Apply the edited version with "version-too.py -u vers.json"')
parser.add_argument('-u', '--update', metavar='JSON', type=str,
help='Update versions from the specified json file')
parser.add_argument('-v', '--verbose', action='store_true')
args = parser.parse_args()
global verbose
verbose = args.verbose
vers_key = '00-versions'
rust_vers_key = '01-rust-versions'
rust_deps_key = '02-rust-deps'
vers = {}
rust_vers = {}
rust_deps = {}
new_vers = {}
new_rust_vers = {}
new_rust_deps = {}
if args.update:
with open(args.update, 'r') as f:
parsed = json.loads(f.read())
new_vers = parsed[vers_key]
new_rust_vers = parsed[rust_vers_key]
new_rust_deps = parsed[rust_deps_key]
# package version
vers['meson'] = do_meson_ver(new_vers.get('meson'))
# rust crates implemented in the tree
rust_paths = get_rust_paths()
for path in rust_paths:
name = cargo_path_to_crate(path)
ver = do_rust_ver(path, new_rust_vers.get(name))
if ver:
rust_vers[name] = ver
# crates implemented in the tree are included as deps by default
rust_deps.update(rust_vers)
new_rust_deps.update(new_rust_vers)
# rust dependencies
for path in rust_paths:
do_rust_deps(path, rust_deps, new_rust_deps)
# if not updating, print out what's read
if args.update is None:
for crate in rust_vers:
rust_deps.pop(crate)
manifest = { vers_key: vers,
rust_vers_key: rust_vers,
rust_deps_key: rust_deps }
print(json.dumps(manifest, sort_keys=True, indent=4))
main()