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