1 |
antoniov |
1.1 |
#! /usr/bin/env python
|
2 |
|
|
|
3 |
|
|
import sys
|
4 |
|
|
import optparse
|
5 |
|
|
from subprocess import Popen,PIPE
|
6 |
|
|
|
7 |
sheila |
1.2 |
def listFilesInCastor(castor_dir,type,veto,prefix):
|
8 |
antoniov |
1.1 |
p1 = Popen(['nsls',castor_dir],stdout=PIPE)
|
9 |
|
|
p2 = Popen(['grep',type],stdin=p1.stdout,stdout=PIPE)
|
10 |
sheila |
1.2 |
files = None
|
11 |
|
|
if veto:
|
12 |
|
|
p3 = Popen(['grep','-v',veto],stdin=p2.stdout,stdout=PIPE)
|
13 |
|
|
files = [prefix + castor_dir + "/" + item[:-1] for item in p3.stdout]
|
14 |
|
|
p3.stdout.close()
|
15 |
|
|
else:
|
16 |
|
|
files = [prefix + castor_dir + "/" + item[:-1] for item in p2.stdout]
|
17 |
|
|
p2.stdout.close()
|
18 |
antoniov |
1.1 |
return files
|
19 |
|
|
|
20 |
sheila |
1.2 |
def copyFilesFromCastor(castor_dir,output_dir,type,veto,prefix,suffix):
|
21 |
antoniov |
1.1 |
from subprocess import call
|
22 |
sheila |
1.2 |
files = listFilesInCastor(castor_dir,type,veto,prefix)
|
23 |
antoniov |
1.1 |
if suffix: files = [item + suffix for item in files]
|
24 |
|
|
|
25 |
|
|
print "Copying from %s to %s" % (castor_dir,output_dir)
|
26 |
|
|
for item in files:
|
27 |
|
|
cmd = ['rfcp',item,output_dir]
|
28 |
|
|
print "..." + item
|
29 |
|
|
retcode = call(cmd)
|
30 |
|
|
if retcode != 0: raise RuntimeError,'Error in copying file %s to directory %s' % (item,output_dir)
|
31 |
|
|
|
32 |
|
|
if __name__ == '__main__':
|
33 |
|
|
parser = optparse.OptionParser(usage="usage: %prog [options]")
|
34 |
|
|
parser.add_option("-o","--out", dest="out", metavar="OUT", help="output directory")
|
35 |
|
|
parser.add_option("-d","--dir", dest="dir", metavar="DIR", help="copy files from DIR")
|
36 |
|
|
parser.add_option("-t","--type", dest="type", default="root", metavar="TYPE", help="select only files with substring TYPE (Default: 'root')")
|
37 |
sheila |
1.2 |
parser.add_option("-v","--veto", dest="veto", default="", metavar="VETO", help="select only files not matching substring VETO")
|
38 |
antoniov |
1.1 |
parser.add_option("-p","--prefix", dest="prefix", default="", metavar="PREFIX", help="prepend PREFIX to file path")
|
39 |
|
|
parser.add_option("-s","--suffix", dest="suffix", default="", metavar="SUFFIX", help="append SUFFIX to file path")
|
40 |
|
|
|
41 |
|
|
(input, args) = parser.parse_args()
|
42 |
|
|
|
43 |
|
|
if not input.dir: parser.error('must set input directory')
|
44 |
|
|
if not input.out: parser.error('must set output directory')
|
45 |
|
|
|
46 |
|
|
copyFilesFromCastor(castor_dir = input.dir,
|
47 |
|
|
output_dir = input.out,
|
48 |
|
|
type = input.type,
|
49 |
sheila |
1.2 |
veto = input.veto,
|
50 |
antoniov |
1.1 |
prefix = input.prefix,
|
51 |
|
|
suffix = input.suffix)
|
52 |
|
|
|
53 |
|
|
sys.exit(0)
|