1 |
dhidas |
1.1 |
#!/usr/bin/env python
|
2 |
|
|
'''
|
3 |
|
|
Created on 7 Jun 2010
|
4 |
|
|
|
5 |
|
|
@author: kreczko
|
6 |
|
|
|
7 |
|
|
Email: kreczko@cern.ch
|
8 |
|
|
'''
|
9 |
|
|
import sys, os
|
10 |
|
|
try:
|
11 |
|
|
import subprocess
|
12 |
|
|
except:
|
13 |
|
|
print "You have to use Python 2.4 or higher"
|
14 |
|
|
sys.exit(0)
|
15 |
|
|
from optparse import OptionParser
|
16 |
|
|
|
17 |
|
|
srmBase = 'srm://srm-cms.cern.ch:8443/srm/managerv2?SFN=/castor/cern.ch/cms'
|
18 |
|
|
srmBaseCopy = 'srm://srm-cms.cern.ch:8443/srm/managerv2?SFN='
|
19 |
|
|
|
20 |
|
|
def getFiles(gridFolder, input):
|
21 |
|
|
if gridFolder.endswith('/'):
|
22 |
|
|
gridFolder = gridFolder.rstrip('/')
|
23 |
|
|
input = input.rstrip(' ')
|
24 |
|
|
fileLinesOnly = input[input.find(gridFolder) + len(gridFolder):]
|
25 |
|
|
fileLines = fileLinesOnly.split('\n')[1:]
|
26 |
|
|
fileLines = [fileLine.lstrip(' ') for fileLine in fileLines]
|
27 |
|
|
files = [line.split(' ')[1] for line in fileLines if len(line.split(' ')) == 2]
|
28 |
|
|
files.sort()
|
29 |
|
|
return files
|
30 |
|
|
|
31 |
|
|
def listFiles(gridFolder):
|
32 |
|
|
output = subprocess.Popen(['srmls', srmBase + gridFolder], stdout=subprocess.PIPE).communicate()[0]
|
33 |
|
|
return output
|
34 |
|
|
|
35 |
|
|
def copySrm(file):
|
36 |
|
|
ommitExisting = '-overwrite_mode=WHEN_FILES_ARE_DIFFERENT'
|
37 |
|
|
protocolVersion = '-srm_protocol_version=2'
|
38 |
|
|
output = subprocess.Popen(['srmcp', ommitExisting, protocolVersion, srmBase + file], stdout=subprocess.PIPE).communicate()[0]
|
39 |
|
|
return ''
|
40 |
|
|
|
41 |
|
|
def copylcg(file, to):
|
42 |
|
|
print '>> lcg-cp ' + srmBaseCopy + file + ' ' + to
|
43 |
|
|
output = subprocess.Popen(['lcg-cp', srmBaseCopy + file, to], stdout=subprocess.PIPE).communicate()[0]
|
44 |
|
|
return output
|
45 |
|
|
|
46 |
|
|
if __name__ == '__main__':
|
47 |
|
|
|
48 |
|
|
parser = OptionParser()
|
49 |
|
|
(options, args) = parser.parse_args()
|
50 |
|
|
if len(args) > 0:
|
51 |
|
|
path = args[0]
|
52 |
|
|
filelist = listFiles(path)
|
53 |
|
|
for file in getFiles(path, filelist):
|
54 |
|
|
filename = file.split('/')[-1]
|
55 |
|
|
if not os.path.exists(filename):
|
56 |
|
|
to = os.getcwd() + filename
|
57 |
|
|
print 'Copying file', srmBase + file
|
58 |
|
|
copylcg(file, to)
|
59 |
|
|
print
|
60 |
|
|
else:
|
61 |
|
|
print filename, 'already exists'
|
62 |
|
|
else:
|
63 |
|
|
print 'Copy path was not specified. Use script "./copyGridFolder path"'
|