1 |
#! /usr/bin/env python
|
2 |
|
3 |
import sys
|
4 |
import optparse
|
5 |
from subprocess import Popen,PIPE
|
6 |
|
7 |
def listFilesInCastor(castor_dir,type,prefix):
|
8 |
p1 = Popen(['nsls',castor_dir],stdout=PIPE)
|
9 |
p2 = Popen(['grep',type],stdin=p1.stdout,stdout=PIPE)
|
10 |
files = [prefix + castor_dir + "/" + item[:-1] for item in p2.stdout]
|
11 |
p2.stdout.close()
|
12 |
return files
|
13 |
|
14 |
def haddFilesInCastor(castor_dir,result_file,type,prefix,suffix,option):
|
15 |
from subprocess import call
|
16 |
files = listFilesInCastor(castor_dir,type,prefix)
|
17 |
if suffix: files = [item + suffix for item in files]
|
18 |
|
19 |
cmd = ['hadd']
|
20 |
if option: cmd.append(option)
|
21 |
cmd.append(result_file)
|
22 |
cmd.extend(files)
|
23 |
#print cmd
|
24 |
#return 0
|
25 |
retcode = call(cmd)
|
26 |
return retcode
|
27 |
|
28 |
if __name__ == '__main__':
|
29 |
parser = optparse.OptionParser(usage="usage: %prog [options]")
|
30 |
parser.add_option("-f","--file", dest="file", metavar="FILE", help="output file")
|
31 |
parser.add_option("-d","--dir", dest="dir", metavar="DIR", help="add files in DIR")
|
32 |
parser.add_option("-t","--type", dest="type", default="root", metavar="TYPE", help="select only files with substring TYPE (Default: 'root')")
|
33 |
parser.add_option("-p","--prefix", dest="prefix", default="rfio:", metavar="PREFIX", help="prepend PREFIX to file path (Default 'rfio:')")
|
34 |
parser.add_option("-s","--suffix", dest="suffix", default="", metavar="SUFFIX", help="append SUFFIX to file path")
|
35 |
parser.add_option("-o","--option", dest="option", default="", metavar="OPTION", help="pass OPTION to hadd command")
|
36 |
|
37 |
(input, args) = parser.parse_args()
|
38 |
|
39 |
if not input.dir: parser.error('must set directory option')
|
40 |
|
41 |
retcode = haddFilesInCastor(castor_dir = input.dir,
|
42 |
result_file = input.file,
|
43 |
type = input.type,
|
44 |
prefix = input.prefix,
|
45 |
suffix = input.suffix,
|
46 |
option = input.option)
|
47 |
|
48 |
sys.exit(retcode)
|