forked from cms-sw/cmsdist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmspm.file
executable file
·606 lines (492 loc) · 18.9 KB
/
cmspm.file
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#!/usr/bin/env python
"""CMS Package Management script. Feedback and Help: mojedasa@cern.ch
This script must be able to run in Python 2.4.
Note: All commands that checkout/export tags try to make a 'src' folder
if it does not exist. Then they checkout inside that folder.
Syntax:
cmspm <command> [parameters]
Commands:
co Checkouts/exports tags read from stdin.
corel Checkouts/exports a full release using the tags
from Tag Collector (i.e. works with non-tagged releases).
frombase 1. Runs 'scram p CMSSW <project>'.
2. Checkouts the diff tags between <release> and <baserelease>.
3. Runs 'checkdeps'.
4. Checkouts the packages from the 'checkdeps' output.
5. Optionally, removes the CVS folders.
The packages are checked-out one by one.
The diff tags are fetched from Tag Collector.
query Query the tags of a release from Tag Collector.
tag Tags a release in CVS (using rtag).
The packages are tagged one by one.
The tags are fetched from Tag Collector.
Checkout (co) Command:
Syntax:
cmspm co [options]
Options:
-e --export Use CVS export instead of CVS checkout
(export does not download CVS folders
and is much faster).
-n --dry-run Do not do anything, just print.
-j --n-threads Number of threads used (default: 10, max: 99).
Checkout Release (corel) Command:
Syntax:
cmspm corel <release> [options]
Options:
-e --export Use CVS export instead of CVS checkout
(export does not download CVS folders
and is much faster).
-o --dumptags <file> Dump the release's tags to a file.
-n --dry-run Do not do anything, just print.
-j --n-threads Number of threads used (default: 10, max: 99).
Frombase (frombase) Command:
Syntax:
cmspm frombase <release> <baserelease> <project> [options]
Options:
-e --export Remove the CVS folders after checkdeps has run
(i.e. the final results will look like using
the -e option in the 'corel' command).
-o --dumptags <file> Dump the release's tags to a file.
-j --n-threads Number of threads used (default: 10, max: 99).
Query (query) Command:
Syntax:
cmspm query <release>
Tag (tag) Command:
Syntax:
cmspm tag <release> [options]
Options:
-n --dry-run Do not do anything, just print.
-j --n-threads Number of threads used (default: 10, max: 99).
Examples:
cmspm co < tags.txt
cmspm corel CMSSW_4_2_9_HLT1 -e -o tags.txt
cmspm frombase CMSSW_4_2_9_HLT1_hltpatch1 CMSSW_4_2_9_HLT1 CMSSW_4_2_9_HLT1
cmspm query CMSSW_4_2_9_HLT1
cmspm tag CMSSW_4_2_9_HLT1
"""
__author__ = "Miguel Ojeda"
__copyright__ = "Copyright 2011, CERN CMS"
__credits__ = ["Miguel Ojeda"]
__license__ = "Unknown"
__maintainer__ = "Miguel Ojeda"
__email__ = "mojedasa@cern.ch"
__status__ = "Staging"
import time
import os
import sys
import shutil
import fnmatch
import optparse
import subprocess
import threading
import Queue
from commands import getstatusoutput
from os import getenv
try:
from subprocess import CalledProcessError
except ImportError:
# Python 2.4 doesn't implement CalledProcessError
class CalledProcessError(Exception):
"""This exception is raised when a process run by check_call() returns
a non-zero exit status. The exit status will be stored in the
returncode attribute."""
def __init__(self, returncode, cmd):
self.returncode = returncode
self.cmd = cmd
def __str__(self):
return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode)
_maxtaglen = len('V01-01-01-01')
# Simple class similar to Pool in Python 2.6, but for threads and supporting
# only the useful map() method.
# Note: it is not a true pool as the threads are not kept between map() calls,
# i.e. they are not created in __init__().
class ThreadPool(object):
def __init__(self, n_threads):
self.n_threads = n_threads
def map(self, function, arguments, common_arguments = ()):
threads = []
queue = Queue.Queue()
for a in arguments:
queue.put(a)
for i in range(self.n_threads):
t = threading.Thread(target = self._target, args = (i, function, queue, common_arguments, ))
threads.append(t)
t.start()
for t in threads:
t.join()
def _target(self, tid, function, queue, common_arguments):
while True:
try:
if function(tid, queue.get_nowait(), *common_arguments):
return
except Queue.Empty:
return
# A recursive globbing helper (from cmsBuild).
def recursive_glob(treeroot, pattern):
results = []
for base, dirs, files in os.walk(treeroot):
goodfiles = fnmatch.filter(files, pattern) + fnmatch.filter(dirs, pattern)
results.extend(os.path.join(base, f) for f in goodfiles)
return results
def die(msg):
print msg
sys.exit(1)
def deleteCVSFolders(treeroot):
map(shutil.rmtree, recursive_glob(treeroot, 'CVS'))
def parseJSONTag(tag):
if tag == 'null':
return None
return tag.strip('"')
def parseJSONTags(tags):
tags = tags[1:-1]
real_tags = []
while True:
start = tags.find('[')
if start == -1:
break
end = tags.find(']')
if end == -1:
raise Exception('Error while parsing tags: Found the start of a tag ("[") but not the end ("]")')
row = tags[start+1:end].split(', ')
real_tag = [row[0].strip('"')]
for tag in row[1:]:
real_tag.append(parseJSONTag(tag))
real_tags.append(real_tag)
tags = tags[end+1:]
return real_tags
def getTcData(url):
tcurl = 'https://cmstags.cern.ch/tc/'+url
err, output = getstatusoutput('which wget')
cmd = "wget --no-check-certificate -nv -o /dev/null -O- '"+tcurl+"'"
if err: cmd = "curl -L -k --stderr /dev/null '"+tcurl.replace('[','\[').replace(']','\]')+"'"
err, output = getstatusoutput(cmd)
if err: raise Exception("Error: running command"+cmd+"\n"+output)
return output
def getReleasesTags(releases, diff):
if diff:
diff = 'true'
else:
diff = 'false'
packs = getTcData ('py_getReleasesTags?releases=["' + '","'.join(releases) + '"]&diff=' + diff)
return parseJSONTags(packs)
# Get return a dictionary with package => tag for the
# specified @a tagset
def getTagSetTags(tagsets):
extratags = {}
for tagset in tagsets:
packs = getTcData('py_getTagsetTags?tagset=' + tagset)
extratags.update(dict(parseJSONTags(packs)))
return extratags
def formatTag(tag, maxlen = _maxtaglen):
return tag + ' ' * (maxlen - len(tag))
class TagsDumper:
def __init__(self, filename):
try:
self._dumptags_fd = None
if filename:
self._dumptags_fd = open(filename, 'w')
except OSError:
die("Cannot open file %s for writing" % opts.dumptags)
def dump(self, tags):
if self._dumptags_fd:
for tag in tags:
self._dumptags_fd.write(tag[0] + " " + tag[1] + "\n")
self._dumptags_fd.close()
def popen(args, merge_stderr = True, shell = False):
stderr = None
if merge_stderr:
stderr = subprocess.STDOUT
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=stderr, close_fds=True, shell=shell)
r = p.communicate()[0]
if p.returncode != 0:
raise CalledProcessError(p.returncode, ' '.join(args))
return r
def queryCVS(args, cvsroot, tries=5):
if tries<1: tries=1
while tries>0:
tries -= 1
try:
return popen(['cvs', '-Q', '-d', cvsroot] + args)
except Exception, ex:
if tries==0: raise ex
time.sleep(5)
def exportCVS(tag, args, cvsroot):
return queryCVS(['export', '-r', tag] + args, cvsroot)
def checkoutCVS(tag, args, cvsroot):
return queryCVS(['co', '-r', tag] + args, cvsroot)
def rtagCVS(tag, args, cvsroot):
return queryCVS(['rtag', '-r', tag] + args, cvsroot)
def mergeTags(tags):
new_tags = {}
for (package, tag) in tags:
if tag not in new_tags:
new_tags[tag] = []
new_tags[tag].append(package)
return new_tags
def checkoutTag(tid, tag, lock, export, dryrun, cvsroot, errors):
(tag, packages) = tag
action = 'Checking out'
if export:
action = 'Exporting'
try:
lock.acquire()
if len(errors) != 0:
print "[Thread %2i] Stopping because something else failed." % tid
return True
print "[Thread %2i]" % tid, action + ":", formatTag(tag), ' '.join(packages)
finally:
lock.release()
if dryrun:
return
try:
if export:
exportCVS(tag, packages, cvsroot)
else:
checkoutCVS(tag, packages, cvsroot)
except:
try:
lock.acquire()
print "[Thread %2i]" % tid, action, "failed:", formatTag(tag), ' '.join(packages)
errors.append(True)
return True
finally:
lock.release()
def checkoutSubsystem(tid, tags, lock, export, dryrun, cvsroot, errors):
tags = mergeTags(tags)
failed = []
for tag in tags:
if checkoutTag(tid, (tag, tags[tag]), lock, export, dryrun, cvsroot, errors):
return True
def checkNThreads(nthreads):
if nthreads < 1:
raise Exception("Error: The number of threads should be at least 1.")
if nthreads > 99:
raise Exception("Error: The number of threads should not be more than 99, in order to prevent loading the CVS server too much.")
# This function checkouts a list of tags using the best 'algorithm' to checkout them:
# When checkout out, merge tags per subsystem and checkout the subsystems in parallel.
# When exporting, merge all tags and export them in parallel.
def checkoutTags(tags, nthreads, export, dryrun, cvsroot):
checkNThreads(nthreads)
lock = threading.Lock()
errors = []
pool = ThreadPool(nthreads)
if export:
tags = mergeTags(tags)
pool.map(checkoutTag, tags.items(), (lock, export, dryrun, cvsroot, errors))
else:
# When checking out, we can't run cvs in parallel for the same subsystem.
# Otherwise, cvs would randomly access the CVS/* files.
subsystems = {}
for tag in tags:
subsystem = tag[0].split('/')[0]
if subsystem not in subsystems:
subsystems[subsystem] = []
subsystems[subsystem].append(tag)
pool.map(checkoutSubsystem, subsystems.values(), (lock, export, dryrun, cvsroot, errors))
if len(errors) != 0:
die('Error: A checkout/export failed.')
def rtag(tid, tag, lock, release, dryrun, errors, cvsroot):
(tag, packages) = tag
try:
lock.acquire()
if len(errors) != 0:
print "[Thread %2i] Stopping because something else failed." % tid
return True
print "[Thread %2i]" % tid, "Tagging:", formatTag(tag), release, ' '.join(packages)
finally:
lock.release()
if dryrun:
return
try:
rtagCVS(tag, [release] + packages, cvsroot)
except:
try:
lock.acquire()
print "[Thread %2i]" % tid, "Tagging failed:", formatTag(tag), release, ' '.join(packages)
errors.append(True)
return True
finally:
lock.release()
def cmd_co(args):
parser = optparse.OptionParser(usage="cmspm co [options]")
parser.add_option("-e", "--export", action="store_true", dest="export", default=False)
parser.add_option("-n", "--dry-run", action="store_true", dest="dryrun", default=False)
parser.add_option("-j", "--n-threads", action="store", type="int", dest="nthreads", default=10)
parser.add_option("-d", "--cvsroot", dest="cvsroot", default=getenv("CVSROOT"))
(opts, args) = parser.parse_args(args)
if len(args) != 0:
raise Exception("Error: No arguments expected.")
folder = 'src'
if not opts.dryrun:
try:
os.mkdir(folder)
except OSError, e:
pass
os.chdir(folder)
tags = {}
for line in sys.stdin.readlines():
line = line.split()
if len(line) == 0:
continue
if len(line) != 2:
raise Exception("Error: The input must be one (package, tag) pair per line.")
(package, tag) = line
if package in tags and tag != tags[package]:
raise Exception("Error: The input contains a duplicate package with a different tag.")
tags[package] = tag
removedTags = [k.strip("/").strip(".").strip("/") for (k,v) in tags.items() if v == "-" and k]
for tag in removedTags:
error, out = getstatusoutput("rm -rf %s" % tag)
if error:
print out
exit(1)
addedTag = [(k,v) for (k,v) in tags.items() if v != "-"]
checkoutTags(addedTag, opts.nthreads, opts.export, opts.dryrun, opts.cvsroot)
def cmd_corel(args):
parser = optparse.OptionParser(usage="cmspm corel <release> [options]")
parser.add_option("-e", "--export", action="store_true", dest="export", default=False)
parser.add_option("-o", "--dump-tags", action="store", type="string", dest="dumptags", default=None)
parser.add_option("-n", "--dry-run", action="store_true", dest="dryrun", default=False)
parser.add_option("-j", "--n-threads", action="store", type="int", dest="nthreads", default=10)
parser.add_option("-t", "--additional-tagsets", metavar="TAGSET1[+TAGSET2[+...]]", action="store", type="string", dest="additionalTagSetsSpec", default="")
parser.add_option("-d", "--cvsroot", dest="cvsroot", default=getenv("CVSROOT"))
(opts, args) = parser.parse_args(args)
if len(args) != 1:
raise Exception("Error: Expected <release>.")
release = args[0]
# Create destination directory, if needed.
folder = 'src'
if not opts.dryrun:
try:
os.mkdir(folder)
except OSError, e:
pass
# Open the file before doing anything else to catch errors earlier
dumper = TagsDumper(opts.dumptags)
tags = getReleasesTags([release], False)
# If additional tagsets are specified, we replace them int the final list of tags
# and in the tags to be checked out.
additionalTagSets = [x for x in opts.additionalTagSetsSpec.split("+") if x]
extraTagsetTags = getTagSetTags(additionalTagSets)
tags = dict(tags)
tags.update(extraTagsetTags)
tags = tags.items()
tags.sort()
# Dump the tags first so that we can tell the difference.
dumper.dump(tags)
if opts.dryrun:
sys.exit(0)
os.chdir(folder)
checkoutTags(tags, opts.nthreads, opts.export, opts.dryrun, opts.cvsroot)
def cmd_frombase(args):
parser = optparse.OptionParser(usage="cmspm frombase <base-release> <release> <project>")
parser.add_option("-e", "--export", action="store_true", dest="export", default=False)
parser.add_option("-o", "--dump-tags", action="store", type="string", dest="dumptags", default=None)
parser.add_option("-j", "--n-threads", action="store", type="int", dest="nthreads", default=10)
parser.add_option("-n", "--dry-run", action="store_true", dest="dryrun", default=False)
parser.add_option("-t", "--additional-tagsets", metavar="TAGSET1[+TAGSET2[+...]]", action="store", type="string", dest="additionalTagSetsSpec", default="")
parser.add_option("-d", "--cvsroot", dest="cvsroot", default=getenv("CVSROOT"))
(opts, args) = parser.parse_args(args)
if len(args) != 3:
raise Exception("Error: Expected <release> <baserelease> <project>.")
release = args[0]
baserelease = args[1]
project = args[2]
print "Getting tags from TagCollector..."
tags = getReleasesTags([release], False)
tagsdict = dict(tags)
difftags = getReleasesTags([release, baserelease], True)
# Check that no package was removed and warn if a package was added
for tag in difftags:
if tag[1] is None:
die('Error: The base release contains the ' + tag[0] + ' package but the release does not (i.e. the package was removed).')
if tag[2] is None:
print 'Warning: The release contains the ' + tag[0] + ' package but the base release does not (i.e. the package was added).'
# We only need the release's tags, not baserelease's tags
difftags = dict((x[0], x[1]) for x in difftags)
print "Getting tags from TagCollector... DONE"
# If additional tagsets are specified, we replace them int the final list of tags
# and in the tags to be checked out.
additionalTagSets = [x for x in opts.additionalTagSetsSpec.split("+") if x]
extraTagsetTags = getTagSetTags(additionalTagSets)
tags = dict(tags)
tags.update(extraTagsetTags)
tags = tags.items()
tags.sort()
difftags.update(extraTagsetTags)
if opts.dryrun:
# Open the file before doing anything else to catch errors earlier
dumper = TagsDumper(opts.dumptags)
dumper.dump(tags)
sys.exit(0)
print "Running scram project..."
popen(['scram', 'p', 'CMSSW', project])
os.chdir(project)
os.chdir('src')
print "Running scram project... DONE"
print "Checking out diff tags..."
checkoutTags(difftags.items(), opts.nthreads, False, False, opts.cvsroot)
print "Checking out diff tags... DONE"
print "Running checkdeps..."
deps = popen('export CVSROOT=%s && eval `scram run -sh` && checkdeps' % opts.cvsroot, False, True)
deps = deps.splitlines()
deps_tags = []
for dep in deps:
# XXX FIXME Use a proper way to get the correct lines. Maybe make checkdeps print to stderr the info messages.
if '(' not in dep:
continue
package = dep.split('(')[0].strip()
tag = tagsdict[package]
deps_tags.append([package, tag])
print "Running checkdeps... DONE"
print "Checking out dependencies..."
checkoutTags(deps_tags, opts.nthreads, False, False, opts.cvsroot)
print "Checking out dependencies... DONE"
if opts.export:
deleteCVSFolders('.')
os.chdir('..')
os.chdir('..')
shutil.move(os.path.join(project, 'src'), 'src')
shutil.rmtree(project)
dumper = TagsDumper(opts.dumptags)
dumper.dump(tags)
def cmd_query(args):
parser = optparse.OptionParser(usage="cmspm query <release>")
(opts, args) = parser.parse_args(args)
if len(args) != 1:
raise Exception("Error: Expected <release>.")
release = args[0]
tags = getReleasesTags([release], False)
for tag in tags:
print tag[0], tag[1]
def cmd_tag(args):
parser = optparse.OptionParser(usage="cmspm tag <release> [options]")
parser.add_option("-n", "--dry-run", action="store_true", dest="dryrun", default=False)
parser.add_option("-j", "--n-threads", action="store", type="int", dest="nthreads", default=10)
parser.add_option("-d", "--cvsroot", dest="cvsroot", default=getenv("CVSROOT"))
(opts, args) = parser.parse_args(args)
if len(args) != 1:
raise Exception("Error: Expected <release>.")
release = args[0]
checkNThreads(opts.nthreads)
print "Getting tags from TagCollector...",
tags = getReleasesTags([release], False)
print "DONE"
lock = threading.Lock()
errors = []
pool = ThreadPool(opts.nthreads)
tags = mergeTags(tags)
pool.map(rtag, tags.items(), (lock, release, opts.dryrun, errors, opts.cvsroot))
if len(errors) != 0:
raise Exception('Error: A tagging failed.')
def main():
try:
args = sys.argv[1:]
cmd = globals()['cmd_' + args[0]]
except:
print __doc__
print "Exception: Expected <command>."
sys.exit(-1)
cmd(args[1:])
if __name__ == '__main__':
main()