1 |
antoniov |
1.1 |
#! /usr/bin/env python
|
2 |
|
|
|
3 |
|
|
import sys
|
4 |
|
|
import optparse
|
5 |
|
|
from subprocess import Popen,PIPE
|
6 |
antoniov |
1.4 |
from listFilesInCastor import listFilesInCastor
|
7 |
antoniov |
1.5 |
from ForwardAnalysis.Scripts.CopyWatch import CopyWatch
|
8 |
antoniov |
1.1 |
|
9 |
antoniov |
1.4 |
"""
|
10 |
antoniov |
1.3 |
def listFilesInCastor(castor_dir,type,prefix):
|
11 |
antoniov |
1.1 |
p1 = Popen(['nsls',castor_dir],stdout=PIPE)
|
12 |
|
|
p2 = Popen(['grep',type],stdin=p1.stdout,stdout=PIPE)
|
13 |
antoniov |
1.3 |
files = [prefix + castor_dir + "/" + item[:-1] for item in p2.stdout]
|
14 |
|
|
p2.stdout.close()
|
15 |
antoniov |
1.1 |
return files
|
16 |
antoniov |
1.4 |
"""
|
17 |
antoniov |
1.1 |
|
18 |
antoniov |
1.4 |
def copyFilesFromCastor(castor_dir,output_dir,type,veto,prefix,suffix,enable):
|
19 |
antoniov |
1.1 |
from subprocess import call
|
20 |
antoniov |
1.4 |
files = listFilesInCastor(castor_dir,type,veto,prefix)
|
21 |
antoniov |
1.1 |
if suffix: files = [item + suffix for item in files]
|
22 |
|
|
|
23 |
|
|
print "Copying from %s to %s" % (castor_dir,output_dir)
|
24 |
antoniov |
1.4 |
copyList = []
|
25 |
antoniov |
1.1 |
for item in files:
|
26 |
antoniov |
1.4 |
#cmd = ['rfcp',item,output_dir]
|
27 |
antoniov |
1.5 |
cmd = 'nice rfcp %s %s' % (item,output_dir)
|
28 |
antoniov |
1.4 |
#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 |
antoniov |
1.1 |
|
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 |
antoniov |
1.4 |
parser.add_option("-v","--veto", dest="veto", default="", metavar="VETO", help="select only files without substring VETO")
|
44 |
antoniov |
1.1 |
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 |
antoniov |
1.4 |
parser.add_option("--no_exec", dest="enable", action="store_false", default=True, help="files will not be copied")
|
47 |
antoniov |
1.1 |
|
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 |
antoniov |
1.4 |
veto = input.veto,
|
57 |
antoniov |
1.1 |
prefix = input.prefix,
|
58 |
antoniov |
1.4 |
suffix = input.suffix,
|
59 |
|
|
enable = input.enable)
|
60 |
|
|
|
61 |
|
|
print "========================="
|
62 |
|
|
print "----> Transfer completed."
|
63 |
|
|
print "========================="
|
64 |
antoniov |
1.1 |
|
65 |
|
|
sys.exit(0)
|