forked from esrf-bliss/Lima
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddnewtags
executable file
·409 lines (342 loc) · 12.9 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
#!/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)
mod_version_data = {
'core': 'common/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',
'perkinelmer': 'camera/perkinelmer/VERSION',
'xpad': 'camera/xpad/VERSION',
'pco': 'camera/pco/VERSION',
'ueye': 'camera/ueye/VERSION',
'simulator': 'camera/simulator/VERSION',
'taco/camera/frelon': 'applications/taco/VERSION',
'tango/common': 'applications/tango/VERSION',
'tango/camera/maxipix': 'applications/tango/camera/Maxipix_VERSION',
'tango/camera/pilatus': 'applications/tango/camera/Pilatus_VERSION',
'tango/camera/frelon': 'applications/tango/camera/Frelon_VERSION',
'tango/camera/basler': 'applications/tango/camera/Basler_VERSION',
'tango/camera/prosilica': 'applications/tango/camera/Prosilica_VERSION',
'tango/camera/andor': 'applications/tango/camera/Andor_VERSION',
'tango/camera/perkinelmer': 'applications/tango/camera/PerkinElmer_VERSION',
'tango/camera/xpad': 'applications/tango/camera/Xpad_VERSION',
'tango/camera/pco': 'applications/tango/camera/Pco_VERSION',
'tango/camera/ueye': 'applications/tango/camera/Ueye_VERSION',
'tango/camera/simulator': 'applications/tango/camera/Simulator_VERSION',
}
windows_plugins = ['pco', 'perkinelmer']
all_submodules = []
modified_submodules = []
base_mod_depend = {'espia': ['frelon', 'maxipix']}
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 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)
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_name):
if version_file_name is None:
version_file_name = mod_version_data[mod]
version_file = open(version_file_name, 'rt')
version_str = version_file.readlines()[0].strip()
version_file.close()
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 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 get_mod_dep_files(mod, modules):
mod_dep_files = {}
if mod not in base_mod_depend.keys():
return mod_dep_files
depend_list = base_mod_depend[mod]
bmod_ver_file_name = os.path.join('python', '%s_VERSION' % mod.upper())
for omod in depend_list:
if omod not in modules:
continue
omod_ver_file_name = mod_version_data[omod]
omod_dir = os.path.dirname(omod_ver_file_name)
bmod_ver_full_name = os.path.join(omod_dir, bmod_ver_file_name)
if not os.path.exists(bmod_ver_full_name):
raise RuntimeError, ('Cannot find %s version file for %s: %s' %
(mod, omod, bmod_ver_full_name))
mod_dep_files[omod] = bmod_ver_full_name
return mod_dep_files
def check_unmodif_mod_dep_files(modules):
for mod in base_mod_depend.keys():
if mod in modules:
continue
try:
version = get_mod_version(mod, None)
except IOError:
continue
mod_dep_files = get_mod_dep_files(mod, modules)
for omod, dep_file in mod_dep_files.items():
oversion = get_mod_version(mod, dep_file)
if oversion != version:
print 'Error: outdated %s [%s] dependency link on ' \
'submodule %s [%s]' % (mod, version, omod, oversion)
print 'To solve this you should:'
print '+ Compile your module (COMPILE_%s=1):' % omod.upper()
print ' "make config && make && make -C sip && make install"'
print '+ Validate it'
print '+ Update submodule dependency:'
print ' "git add %s"' % dep_file
sys.exit(1)
def git_add_entry_to_commit(entry, mod_tag):
old_dir = os.getcwd()
for mod_dir in all_submodules:
rel_path = get_rel_path(entry, mod_dir)
if not rel_path:
continue
# add in submodule
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]])
return
# file in core tree
rel_path = get_rel_path(entry, base_path)
if os.system('git add %s' % rel_path) != 0:
print 'Error adding %s' % rel_path
sys.exit(1)
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, None)
except IOError:
continue
mod_version_objs[mod] = version
print 'Found %-*s version: %s' % (max_name_len, mod, version)
if change_type == JUST_PRINT:
sys.exit(0)
# Checks before performing git actions, just in case of error
check_unmodif_mod_dep_files(modules)
mod_dep_files = {}
for mod in modules:
mod_dep_files[mod] = get_mod_dep_files(mod, modules)
for mod in modules:
version = mod_version_objs[mod]
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)
mod_ver_file = mod_version_data[mod]
set_mod_version(mod, mod_ver_file, version)
for omod, dep_file in mod_dep_files[mod].items():
set_mod_version(mod, dep_file, version)
git_commit_submodules()
if os.system('git commit') != 0:
print 'Error in core tree commit'
sys.exit(1)
for mod in modules:
version = mod_version_objs[mod]
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)