forked from esrf-bliss/Lima
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addnewtags
executable file
·481 lines (414 loc) · 16.1 KB
/
addnewtags
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#!/usr/bin/env python
##############################################################################
#
# This file is part of LImA, a Library for Image Acquisition
#
# Copyright (C) : 2009-2011
# European Synchrotron Radiation Facility
# BP 220, Grenoble 38043
# FRANCE
#
# This is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
############################################################################
import os, sys, getopt
from subprocess import Popen, PIPE
class os_proxy:
def __init__(self, debug=False, os=os):
self.debug = debug
self.os = os
def system(self, cmd, *args, **kw):
if self.debug:
cwd = get_rel_path(self.os.getcwd(), base_path)
print 'os.system: cmd="%s". cwd=%s' % (cmd, cwd)
return self.os.system(cmd, *args, **kw)
def __getattr__(self, name):
return getattr(self.os, name)
base_path = None
def get_rel_path(entry, base_dir):
if os.path.samefile(entry, base_dir):
return '.'
elif entry in ['/', '.']:
return None
entry_dir, entry_name = os.path.split(entry)
if not entry_dir:
entry_dir = '.'
rel_path = get_rel_path(entry_dir, base_dir)
if rel_path is None:
return None
elif not entry_name:
return rel_path
elif rel_path == '.':
return entry_name
else:
return os.path.join(rel_path, entry_name)
CHG_MAJOR, CHG_MINOR, CHG_RELEASE = 'major', 'minor', 'release'
JUST_PRINT = 'print'
ALL = 'all'
class Version:
RePat = '[0-9]+(\\.[0-9]+(\\.[0-9]+)?)?'
def __init__(self, major=1, minor=None, rel=None, fromstr=None):
if fromstr:
x = map(int, fromstr.split('.'))
major = x[0]
minor = rel = None
try:
minor = x[1]
rel = x[2]
except:
pass
self.major = major
self.minor = minor
self.rel = rel
def __str__(self):
s = '%d' % self.major
if self.minor is not None:
s += '.%d' % (self.minor)
if self.rel is not None:
s += '.%d' % (self.rel)
return s
def __repr__(self):
return str(self)
def __cmp__(self, other):
if not isinstance(other, Version):
other = Version(fromstr=repr(other))
return cmp(hash(self), hash(other))
def __hash__(self):
return (self.major << 16) + ((self.minor or 0) << 8) + (self.rel or 0)
def effective(self, ver_ctrl):
if ver_ctrl == 'MAJOR':
return Version(self.major)
elif ver_ctrl == 'MINOR':
return Version(self.major, self.minor)
elif ver_ctrl == 'FULL':
return Version(self.major, self.minor, self.rel)
raise RuntimeError, 'Invalid ver_ctrl: %s' % ver_ctrl
mod_version_data = {
'core': 'common/VERSION',
'gldisplay': 'third-party/gldisplay/VERSION',
'espia': 'camera/common/espia/VERSION',
'frelon': 'camera/frelon/VERSION',
'maxipix': 'camera/maxipix/VERSION',
'pilatus': 'camera/pilatus/VERSION',
'basler': 'camera/basler/VERSION',
'prosilica': 'camera/prosilica/VERSION',
'andor': 'camera/andor/VERSION',
'andor3': 'camera/andor3/VERSION',
'perkinelmer': 'camera/perkinelmer/VERSION',
'xpad': 'camera/xpad/VERSION',
'pco': 'camera/pco/VERSION',
'ueye': 'camera/ueye/VERSION',
'simulator': 'camera/simulator/VERSION',
'roperscientific': 'camera/roperscientific/VERSION',
'rayonixhs': 'camera/rayonixhs/VERSION',
'v4l2': 'camera/v4l2/VERSION',
'xh': 'camera/xh/VERSION',
'pointgrey': 'camera/pointgrey/VERSION',
'pixirad': 'camera/pixirad/VERSION',
'dexela': 'camera/dexela/VERSION',
'meta': 'camera/common/meta/VERSION',
'spec': 'applications/spec/VERSION',
'taco/camera/frelon': 'applications/taco/VERSION',
'tango/common': 'applications/tango/python/VERSION',
'tango/camera/maxipix': 'applications/tango/python/camera/Maxipix_VERSION',
'tango/camera/pilatus': 'applications/tango/python/camera/Pilatus_VERSION',
'tango/camera/frelon': 'applications/tango/python/camera/Frelon_VERSION',
'tango/camera/basler': 'applications/tango/python/camera/Basler_VERSION',
'tango/camera/prosilica': 'applications/tango/python/camera/Prosilica_VERSION',
'tango/camera/andor': 'applications/tango/python/camera/Andor_VERSION',
'tango/camera/andor3': 'applications/tango/python/camera/Andor3_VERSION',
'tango/camera/perkinelmer': 'applications/python/tango/camera/PerkinElmer_VERSION',
'tango/camera/xpad': 'applications/tango/python/camera/Xpad_VERSION',
'tango/camera/pco': 'applications/tango/python/camera/Pco_VERSION',
'tango/camera/ueye': 'applications/tango/python/camera/Ueye_VERSION',
'tango/camera/simulator': 'applications/tango/python/camera/Simulator_VERSION',
'tango/camera/roperscientific': 'applications/tango/python/camera/RoperScientific_VERSION',
'tango/camera/v4l2': 'applications/tango/python/camera/V4l2_VERSION',
'tango/camera/xh': 'applications/tango/python/camera/Xh_VERSION',
'tango/camera/rayonixhs': 'applications/tango/python/camera/RayonixHs_VERSION',
'tango/camera/pointgrey': 'applications/tango/python/camera/PointGrey_VERSION',
'tango/camera/pixirad': 'applications/tango/python/camera/Pixirad_VERSION',
'tango/camera/dexela': 'applications/tango/python/camera/Dexela_VERSION',
}
windows_plugins = ['perkinelmer']
all_submodules = []
modified_submodules = []
base_mod_depend = {'espia': ['frelon', 'maxipix']}
dep_ver_ctrl = 'MINOR'
def mod_is_core_dependent(x):
excluded_mod = ['core'] + windows_plugins
# espia depends on core -> espia-dependent modules do not need
# explicit core dependency
excluded_mod += base_mod_depend['espia']
if x in excluded_mod:
return False
# only plugin submodules in dependency check
core_depend_mod_exclude_prefix = ['tango', 'taco']
for m in core_depend_mod_exclude_prefix:
if x.startswith(m):
return False
return True
def get_all_submodules(base_dir=None):
if base_dir is None:
base_dir = os.getcwd()
git_cmd = 'git submodule foreach bash -c pwd | grep -v "Entering"'
obj = Popen(git_cmd, shell=True, stdout=PIPE, stderr=PIPE)
for line in obj.stdout.readlines():
submod_dir = line.strip()
rel_path = get_rel_path(submod_dir, base_dir)
if not rel_path:
raise RuntimeError, 'Cannot find submod rel_path: %s' % submod_dir
all_submodules.append(rel_path)
def read_config_inc():
global dep_ver_ctrl
config_inc = 'config.inc'
if not os.path.exists(config_inc):
return
f = open(config_inc)
for l in f.readlines():
sep = '='
if l.count(sep) == 1:
key, val = l.strip().split(sep)
if key == 'LINK_STRICT_VERSION':
dep_ver_ctrl = (int(val) and 'FULL') or 'MINOR'
def init_global_data(argv):
global base_path, os
os_debug = False
opts, args = getopt.getopt(argv[1:], 'v')
for opt, val in opts:
if opt == '-v':
os_debug = True
if os_debug:
os = os_proxy(os_debug)
base_path = os.path.dirname(argv[0])
if base_path == '.':
base_path = os.getcwd()
os.chdir(base_path)
read_config_inc()
get_all_submodules()
base_mod_depend.update()
core_depend_mod = filter(mod_is_core_dependent, mod_version_data.keys())
base_mod_depend.update({'core': core_depend_mod})
return args
def get_mod_version(mod, version_file=None):
if version_file is None:
version_file = mod_version_data[mod]
if type(version_file) == str:
version_file = open(version_file, 'rt')
version_str = version_file.readline().strip()
return Version(fromstr=version_str)
def set_mod_version(mod, version_file_name, version):
version_file = open(version_file_name, 'wt')
version_file.write('%s\n' % version)
version_file.close()
mod_tag = get_git_tag(mod, version)
git_add_entry_to_commit(version_file_name, mod_tag)
def get_mod_version_at_commit(mod, commit):
ver_file_name = mod_version_data[mod]
mod_dir, rel_path = get_entry_submodule_path(ver_file_name)
old_dir = None
if mod_dir:
git_cmd = 'git ls-tree %s %s' % (commit, mod_dir)
obj = Popen(git_cmd, shell=True, stdout=PIPE)
out = obj.stdout.readline().strip()
commit = out.split()[2]
if obj.wait() != 0:
raise RuntimeError, 'Error reading git tree: %s' % git_cmd
old_dir = os.getcwd()
os.chdir(mod_dir)
try:
if not os.path.exists(rel_path):
raise IOError, 'Version file %s not available' % ver_file_name
git_cmd = 'git archive %s %s | tar -xOf -' % (commit, rel_path)
obj = Popen(git_cmd, shell=True, stdout=PIPE)
version = get_mod_version(mod, obj.stdout)
if obj.wait() != 0:
msg = ('commit=%s, mod_dir=%s, rel_path=%s' %
(commit, mod_dir, rel_path))
raise RuntimeError, 'Error extracting version file: ' + msg
finally:
if old_dir:
os.chdir(old_dir)
return version
def get_entry_submodule_path(entry):
for mod_dir in all_submodules:
rel_path = get_rel_path(entry, mod_dir)
if rel_path:
return mod_dir, rel_path
return None, get_rel_path(entry, base_path)
def depend_cmp(a, b):
table = {'core': -2, 'espia': -1}
a = table.get(a, 0)
b = table.get(b, 0)
return cmp(a, b)
def get_depend_sorted_modules(modules):
modules = list(modules)
modules.sort(depend_cmp)
return modules
def check_outdated_mod_dep_versions(mod_version_objs):
unmodif_deps = {}
for mod in mod_version_objs:
for bmod, dmods in base_mod_depend.items():
if bmod in mod_version_objs or mod not in dmods:
continue
for bbmod, bdmods in base_mod_depend.items():
if bmod in bdmods:
if bmod not in unmodif_deps:
unmodif_deps[bmod] = bbmod, []
unmodif_deps[bmod][1].append(mod)
break
for mod, mod_data in unmodif_deps.items():
bmod, dep_mods = mod_data
try:
try:
bversion = mod_version_objs[bmod]
except:
bversion = get_mod_version(bmod)
mversion = get_mod_version(mod)
tag = '%s-%s' % (mod, mversion)
oversion = get_mod_version_at_commit(bmod, tag)
except IOError:
print 'Error: cannot find version for modules %s, %s' % (bmod, mod)
print '+ Please do git submodule init/update first'
sys.exit(1)
eoversion = oversion.effective(dep_ver_ctrl)
ebversion = bversion.effective(dep_ver_ctrl)
if eoversion == ebversion:
continue
new_tag = '%s-%s' % (bmod, bversion)
print 'Error: outdated %s dependency link on submodule %s ' % \
(bmod, mod)
print ' [needed by %s]' % ', '.join(dep_mods)
print '+ %s %s version is %s, current is %s' % \
(tag, bmod, oversion, new_tag)
print 'To solve this you should:'
print '+ Validate the %s module with %s' % (mod, new_tag)
print '+ Add %s to the list of modules to tag' % mod
sys.exit(1)
def git_add_entry_to_commit(entry, mod_tag):
mod_dir, rel_path = get_entry_submodule_path(entry)
if not mod_dir:
# file in core tree
if os.system('git add %s' % rel_path) != 0:
print 'Error adding %s' % rel_path
sys.exit(1)
else:
# add in submodule
old_dir = os.getcwd()
os.chdir(mod_dir)
if os.system('git add %s' % rel_path) != 0:
print 'Error adding %s [%s,%s]' % (entry, mod_dir, rel_path)
sys.exit(1)
os.chdir(old_dir)
# keep track to later commit
in_modified_list = False
for data in modified_submodules:
in_modified_list = (data[0] == mod_dir)
if in_modified_list:
data[1].append(mod_tag)
break
if not in_modified_list:
modified_submodules.append([mod_dir, [mod_tag]])
def git_commit_submodules():
old_dir = os.getcwd()
for mod_dir, tag_list in modified_submodules:
commit_msg = 'New tag%s : ' % (((len(tag_list) > 1) and 's') or '')
for i in range(len(tag_list)):
sep = (i and ', ') or ''
commit_msg += '%s%s' % (sep, tag_list[i])
os.chdir(mod_dir)
if os.system('git commit -m "%s"' % commit_msg) != 0:
print 'Error in commit %s: %s' % (mod_dir, commit_msg)
sys.exit(1)
os.chdir(old_dir)
if os.system('git add %s' % mod_dir) != 0:
print 'Error adding submodule %s commit' % mod_dir
sys.exit(1)
def get_git_tag(mod, version):
tag_base = os.path.join(mod, '%s' % version)
return tag_base.replace(os.sep,'-')
def usage(argv):
prgname = os.path.basename(argv[0])
print 'Usage: %s [opts] <change_type | print> <all | module ...>' % prgname
print ''
print ' change_type: major | minor | release'
print ''
print 'Opts:'
print ' -v Verbose output'
print ''
sys.exit(1)
def main(argv):
args = init_global_data(argv)
try:
change_type = args[0]
if change_type not in [CHG_MAJOR, CHG_MINOR, CHG_RELEASE, JUST_PRINT]:
raise InvalidValue
modules = args[1:]
if not modules:
raise InvalidValue
except:
usage(argv)
if modules == [ALL]:
modules = mod_version_data.keys()
modules = get_depend_sorted_modules(modules)
max_name_len = 0
for mod in modules:
max_name_len = max(max_name_len, len(mod))
mod_version_objs = {}
for mod in modules:
try:
version = get_mod_version(mod)
except IOError:
if change_type == JUST_PRINT:
continue
print 'Error: trying to tag a module not checked-out: %s' % mod
print '+ Please do git submodule init/update first'
sys.exit(1)
mod_version_objs[mod] = version
print 'Found %-*s version: %s' % (max_name_len, mod, version)
if change_type == JUST_PRINT:
sys.exit(0)
for mod, version in mod_version_objs.items():
if change_type == CHG_MAJOR:
version.major += 1
version.minor = 0
version.rel = 0
elif change_type == CHG_MINOR:
version.minor += 1
version.rel = 0
elif change_type == CHG_RELEASE:
version.rel += 1
print '+ New %-*s version: %s' % (max_name_len, mod, version)
# Check deps before performing any action, just in case of error
check_outdated_mod_dep_versions(mod_version_objs)
# Now can start changes
for mod, version in mod_version_objs.items():
mod_ver_file = mod_version_data[mod]
set_mod_version(mod, mod_ver_file, version)
git_commit_submodules()
if os.system('git commit') != 0:
print 'Error in core tree commit'
sys.exit(1)
for mod, version in mod_version_objs.items():
git_tag = get_git_tag(mod, version)
print 'Creating git tag: %s' % git_tag
cmd = 'git tag %s' % (git_tag)
if os.system(cmd) != 0:
print 'Error in git-tag'
sys.exit(1)
if modified_submodules:
print 'Don\'t forget to push these submodules:'
for data in modified_submodules:
print '\t->', data[0]
if __name__ == '__main__':
main(sys.argv)