1 |
|
#!/usr/bin/env python |
2 |
|
|
3 |
< |
import sys, getopt, string |
3 |
> |
import sys, string |
4 |
|
import os, popen2 |
5 |
< |
from ProdCommon.Storage.SEAPI.SElement import SElement, FullPath |
5 |
> |
from ProdCommon.Storage.SEAPI.SElement import SElement, FullPath |
6 |
|
from ProdCommon.Storage.SEAPI.SBinterface import * |
7 |
< |
|
7 |
> |
from ProdCommon.Storage.SEAPI.Exceptions import * |
8 |
|
|
9 |
|
|
10 |
|
class cmscp: |
11 |
< |
def __init__(self, argv): |
11 |
> |
def __init__(self, args): |
12 |
|
""" |
13 |
|
cmscp |
14 |
|
|
15 |
< |
safe copy of local file in current directory to remote SE via lcg_cp/srmcp, |
15 |
> |
safe copy of local file in current directory to remote SE via lcg_cp/srmcp, |
16 |
|
including success checking version also for CAF using rfcp command to copy the output to SE |
17 |
|
input: |
18 |
|
$1 middleware (CAF, LSF, LCG, OSG) |
19 |
|
$2 local file (the absolute path of output file or just the name if it's in top dir) |
20 |
|
$3 if needed: file name (the output file name) |
21 |
|
$5 remote SE (complete endpoint) |
22 |
< |
$6 srm version |
22 |
> |
$6 srm version |
23 |
|
output: |
24 |
|
return 0 if all ok |
25 |
|
return 60307 if srmcp failed |
26 |
|
return 60303 if file already exists in the SE |
27 |
|
""" |
28 |
+ |
|
29 |
|
#set default |
30 |
+ |
self.params = {"source":'', "destination":'', "inputFileList":'', "outputFileList":'', \ |
31 |
+ |
"protocol":'', "option":'', "middleware":'', "srm_version":'srmv2'} |
32 |
|
self.debug = 0 |
33 |
< |
self.source = '' |
34 |
< |
self.destination = '' |
35 |
< |
self.file_to_copy = [] |
33 |
< |
self.remote_file_name = [] |
34 |
< |
self.protocol = '' |
35 |
< |
self.middleware = '' |
36 |
< |
self.srmv = '' |
37 |
< |
|
38 |
< |
try: |
39 |
< |
opts, args = getopt.getopt(argv, "", ["source=", "destination=", "inputFileList=", "outputFileList=", \ |
40 |
< |
"protocol=", "middleware=", "srm_version=", "debug", "help"]) |
41 |
< |
except getopt.GetoptError: |
42 |
< |
print self.usage() |
43 |
< |
sys.exit(2) |
44 |
< |
|
45 |
< |
self.setAndCheck(opts) |
46 |
< |
|
33 |
> |
|
34 |
> |
self.params.update( args ) |
35 |
> |
|
36 |
|
return |
37 |
|
|
38 |
< |
def setAndCheck( self, opts ): |
38 |
> |
def processOptions( self ): |
39 |
|
""" |
40 |
< |
Set and check command line parameter |
40 |
> |
check command line parameter |
41 |
|
""" |
42 |
< |
if not opts : |
43 |
< |
print self.usage() |
44 |
< |
sys.exit() |
45 |
< |
for opt, arg in opts : |
46 |
< |
if opt == "--help" : |
47 |
< |
print self.usage() |
48 |
< |
sys.exit() |
49 |
< |
elif opt == "--debug" : |
50 |
< |
self.debug = 1 |
51 |
< |
elif opt == "--source" : |
52 |
< |
self.source = arg |
53 |
< |
elif opt == "--destination": |
54 |
< |
self.destination = arg |
55 |
< |
elif opt == "--inputFileList": |
67 |
< |
infile = arg |
68 |
< |
elif opt == "--outputFileList": |
69 |
< |
out_file |
70 |
< |
elif opt == "--protocol": |
71 |
< |
self.protocol = arg |
72 |
< |
elif opt == "--middleware": |
73 |
< |
self.middleware = arg |
74 |
< |
elif opt == "--srm_version": |
75 |
< |
self.srmv = arg |
76 |
< |
|
77 |
< |
# source and dest cannot be undefined at same time |
78 |
< |
if self.source == '' and self.destination == '': |
79 |
< |
print self.usage() |
80 |
< |
sys.exit() |
81 |
< |
# if middleware is not defined --> protocol cannot be empty |
82 |
< |
if self.middleware == '' and self.protocol == '': |
83 |
< |
print self.usage() |
84 |
< |
sys.exit() |
85 |
< |
# input file must be defined |
86 |
< |
if infile == '': |
87 |
< |
print self.usage() |
88 |
< |
sys.exit() |
42 |
> |
|
43 |
> |
if 'help' in self.params.keys(): HelpOptions() |
44 |
> |
if 'debug' in self.params.keys(): self.debug = 1 |
45 |
> |
|
46 |
> |
# source and dest cannot be undefined at same time |
47 |
> |
if not self.params['source'] and not self.params['destination'] : |
48 |
> |
HelpOptions() |
49 |
> |
|
50 |
> |
# if middleware is not defined --> protocol cannot be empty |
51 |
> |
if not self.params['middleware'] and not self.params['protocol'] : |
52 |
> |
HelpOptions() |
53 |
> |
|
54 |
> |
# input file must be defined |
55 |
> |
if not self.params['inputFileList'] : HelpOptions() |
56 |
|
else: |
57 |
< |
if infile.find(','): |
58 |
< |
[self.file_to_copy.append(x.strip()) for x in infile.split(',')] |
59 |
< |
else: |
60 |
< |
self.file_to_copy.append(infile) |
61 |
< |
|
57 |
> |
file_to_copy=[] |
58 |
> |
if self.params['inputFileList'].find(','): |
59 |
> |
[file_to_copy.append(x.strip()) for x in self.params['inputFileList'].split(',')] |
60 |
> |
else: |
61 |
> |
file_to_copy.append(self.params['inputFileList']) |
62 |
> |
self.params['inputFileList'] = file_to_copy |
63 |
> |
|
64 |
|
## TO DO: |
65 |
|
#### add check for outFiles |
66 |
|
#### add map {'inFileNAME':'outFileNAME'} to change out name |
67 |
|
|
68 |
|
return |
69 |
|
|
70 |
< |
def run( self ): |
70 |
> |
def run( self ): |
71 |
|
""" |
72 |
< |
Check if running on UI (no $middleware) or |
73 |
< |
on WN (on the Grid), and take different action |
72 |
> |
Check if running on UI (no $middleware) or |
73 |
> |
on WN (on the Grid), and take different action |
74 |
|
""" |
106 |
– |
if self.middleware : |
107 |
– |
results = self.stager() |
108 |
– |
else: |
109 |
– |
results = self.copy( self.file_to_copy, self.protocol ) |
75 |
|
|
76 |
< |
self.finalReport(results,self.middleware) |
76 |
> |
self.processOptions() |
77 |
> |
# stage out from WN |
78 |
> |
if self.params['middleware'] : |
79 |
> |
results = self.stager(self.params['middleware'],self.params['inputFileList']) |
80 |
> |
self.finalReport(results) |
81 |
> |
# Local interaction with SE |
82 |
> |
else: |
83 |
> |
results = self.copy(self.params['inputFilesList'], self.params['protocol'], self.protocols['option'] ) |
84 |
> |
return results |
85 |
|
|
86 |
< |
return |
114 |
< |
|
115 |
< |
def setProtocol( self ): |
86 |
> |
def setProtocol( self, middleware ): |
87 |
|
""" |
88 |
|
define the allowed potocols based on $middlware |
89 |
< |
which depend on scheduler |
89 |
> |
which depend on scheduler |
90 |
|
""" |
91 |
< |
if self.middleware.lower() in ['osg','lcg']: |
92 |
< |
supported_protocol = ['srm-lcg','srmv2'] |
93 |
< |
elif self.middleware.lower() in ['lsf','caf']: |
94 |
< |
supported_protocol = ['rfio'] |
91 |
> |
# default To be used with "middleware" |
92 |
> |
lcgOpt={'srmv1':'-b -D srmv1 -t 2400 --verbose', |
93 |
> |
'srmv2':'-b -D srmv2 -t 2400 --verbose'} |
94 |
> |
srmOpt={'srmv1':' -report ./srmcp.report -retry_timeout 480000 -retry_num 3 -streams_num=1 ', |
95 |
> |
'srmv2':' -report ./srmcp.report -retry_timeout 480000 -retry_num 3 '} |
96 |
> |
rfioOpt='' |
97 |
> |
|
98 |
> |
supported_protocol = None |
99 |
> |
if middleware.lower() in ['osg','lcg','condor']: |
100 |
> |
supported_protocol = [('srm-lcg',lcgOpt[self.params['srm_version']]),\ |
101 |
> |
(self.params['srm_version'],srmOpt[self.params['srm_version']])] |
102 |
> |
elif middleware.lower() in ['lsf','caf']: |
103 |
> |
supported_protocol = [('rfio',rfioOpt)] |
104 |
|
else: |
105 |
< |
## here we can add support for any kind of protocol, |
105 |
> |
## here we can add support for any kind of protocol, |
106 |
|
## maybe some local schedulers need something dedicated |
107 |
|
pass |
108 |
|
return supported_protocol |
109 |
|
|
110 |
< |
def stager( self ): |
110 |
> |
def stager( self, middleware, list_files ): |
111 |
|
""" |
112 |
|
Implement the logic for remote stage out |
113 |
|
""" |
114 |
< |
protocols = self.setProtocol() |
115 |
< |
count=0 |
116 |
< |
list_files = self.file_to_copy |
117 |
< |
results={} |
118 |
< |
for prot in protocols: |
119 |
< |
if self.debug: print 'Trying stage out with %s utils \n'%prot |
120 |
< |
copy_results = self.copy( list_files, prot ) |
121 |
< |
list_retry = [] |
122 |
< |
list_existing = [] |
143 |
< |
list_ok = [] |
144 |
< |
for file, dict in copy_results.iteritems(): |
145 |
< |
er_code = dict['erCode'] |
146 |
< |
if er_code == '60307': list_retry.append( file ) |
147 |
< |
elif er_code == '60303': list_existing.append( file ) |
148 |
< |
else: |
149 |
< |
list_ok.append(file) |
150 |
< |
reason = 'Copy succedeed with %s utils'%prot |
151 |
< |
upDict = self.updateReport(file, er_code, reason) |
152 |
< |
copy_results.update(upDict) |
153 |
< |
results.update(copy_results) |
154 |
< |
if len(list_ok) != 0: |
155 |
< |
msg = 'Copy of %s succedeed with %s utils\n'%(str(list_ok),prot) |
156 |
< |
# print msg |
157 |
< |
if len(list_ok) == len(list_files) : |
158 |
< |
break |
114 |
> |
results={} |
115 |
> |
for prot, opt in self.setProtocol( middleware ): |
116 |
> |
if self.debug: print 'Trying stage out with %s utils \n'%prot |
117 |
> |
copy_results = self.copy( list_files, prot, opt ) |
118 |
> |
list_retry = [] |
119 |
> |
list_existing = [] |
120 |
> |
list_ok = [] |
121 |
> |
if copy_results.keys() == '': |
122 |
> |
results.update(copy_results) |
123 |
|
else: |
124 |
< |
# print 'Copy of files %s failed using %s...\n'%(str(list_retry)+str(list_existing),prot) |
125 |
< |
if len(list_retry): list_files = list_retry |
126 |
< |
else: break |
127 |
< |
count =+1 |
128 |
< |
|
129 |
< |
#### TODO Daniele |
124 |
> |
for file, dict in copy_results.iteritems(): |
125 |
> |
er_code = dict['erCode'] |
126 |
> |
if er_code == '0': |
127 |
> |
list_ok.append(file) |
128 |
> |
reason = 'Copy succedeed with %s utils'%prot |
129 |
> |
upDict = self.updateReport(file, er_code, reason) |
130 |
> |
copy_results.update(upDict) |
131 |
> |
elif er_code == '60303': list_existing.append( file ) |
132 |
> |
else: list_retry.append( file ) |
133 |
> |
results.update(copy_results) |
134 |
> |
if len(list_ok) != 0: |
135 |
> |
msg = 'Copy of %s succedeed with %s utils\n'%(str(list_ok),prot) |
136 |
> |
if self.debug : print msg |
137 |
> |
if len(list_ok) == len(list_files) : |
138 |
> |
break |
139 |
> |
else: |
140 |
> |
if self.debug : print 'Copy of files %s failed using %s...\n'%(str(list_retry)+str(list_existing),prot) |
141 |
> |
if len(list_retry): list_files = list_retry |
142 |
> |
else: break |
143 |
> |
|
144 |
> |
#### TODO Daniele |
145 |
|
#check is something fails and created related dict |
146 |
< |
# backup = self.analyzeResults(results) |
147 |
< |
|
148 |
< |
# if backup : |
146 |
> |
# backup = self.analyzeResults(results) |
147 |
> |
|
148 |
> |
# if backup : |
149 |
|
# msg = 'WARNING: backup logic is under implementation\n' |
150 |
|
# #backupDict = self.backup() |
151 |
< |
# ### NOTE: IT MUST RETURN a DICT contains also LFN and SE Name |
151 |
> |
# ### NOTE: IT MUST RETURN a DICT contains also LFN and SE Name |
152 |
|
# results.update(backupDict) |
153 |
|
# print msg |
154 |
|
return results |
155 |
|
|
156 |
|
def initializeApi(self, protocol ): |
157 |
|
""" |
158 |
< |
Instantiate storage interface |
158 |
> |
Instantiate storage interface |
159 |
|
""" |
160 |
< |
source_prot = protocol |
161 |
< |
dest_prot = protocol |
162 |
< |
if self.source == '' : source_prot = 'local' |
163 |
< |
Source_SE = self.storageInterface( self.source, source_prot ) |
164 |
< |
if self.destination == '' : dest_prot = 'local' |
165 |
< |
Destination_SE = self.storageInterface( self.destination, dest_prot ) |
160 |
> |
source_prot = protocol |
161 |
> |
dest_prot = protocol |
162 |
> |
if not self.params['source'] : source_prot = 'local' |
163 |
> |
Source_SE = self.storageInterface( self.params['source'], source_prot ) |
164 |
> |
if not self.params['destination'] : dest_prot = 'local' |
165 |
> |
Destination_SE = self.storageInterface( self.params['destination'], dest_prot ) |
166 |
|
|
167 |
|
if self.debug : |
168 |
< |
print '(source=%s, protocol=%s)'%(self.source, source_prot) |
169 |
< |
print '(destination=%s, protocol=%s)'%(self.destination, dest_prot) |
168 |
> |
print '(source=%s, protocol=%s)'%(self.params['source'], source_prot) |
169 |
> |
print '(destination=%s, protocol=%s)'%(self.params['destination'], dest_prot) |
170 |
|
|
171 |
|
return Source_SE, Destination_SE |
172 |
|
|
173 |
< |
def copy( self, list_file, protocol ): |
173 |
> |
def copy( self, list_file, protocol, options ): |
174 |
|
""" |
175 |
< |
Make the real file copy using SE API |
175 |
> |
Make the real file copy using SE API |
176 |
|
""" |
177 |
|
if self.debug : |
178 |
< |
print 'copy(): using %s protocol'%protocol |
179 |
< |
Source_SE, Destination_SE = self.initializeApi( protocol ) |
178 |
> |
print 'copy(): using %s protocol'%protocol |
179 |
> |
try: |
180 |
> |
Source_SE, Destination_SE = self.initializeApi( protocol ) |
181 |
> |
except Exception, ex: |
182 |
> |
return self.updateReport('', '-1', str(ex)) |
183 |
|
|
184 |
< |
# create remote dir |
184 |
> |
# create remote dir |
185 |
|
if protocol in ['gridftp','rfio']: |
186 |
< |
self.createDir( Destination_SE, protocol ) |
186 |
> |
try: |
187 |
> |
self.createDir( Destination_SE, protocol ) |
188 |
> |
except Exception, ex: |
189 |
> |
return self.updateReport('', '60316', str(ex)) |
190 |
|
|
191 |
|
## prepare for real copy ## |
192 |
< |
sbi = SBinterface( Source_SE, Destination_SE ) |
193 |
< |
sbi_dest = SBinterface(Destination_SE) |
192 |
> |
try : |
193 |
> |
sbi = SBinterface( Source_SE, Destination_SE ) |
194 |
> |
sbi_dest = SBinterface(Destination_SE) |
195 |
> |
except ProtocolMismatch, ex: |
196 |
> |
msg = str(ex)+'\n' |
197 |
> |
msg += "ERROR : Unable to create SBinterface with %s protocol\n"%protocol |
198 |
> |
return self.updateReport('', '-1', str(ex)) |
199 |
|
|
200 |
|
results = {} |
201 |
< |
## loop over the complete list of files |
202 |
< |
for filetocopy in list_file: |
201 |
> |
## loop over the complete list of files |
202 |
> |
for filetocopy in list_file: |
203 |
|
if self.debug : print 'start real copy for %s'%filetocopy |
204 |
< |
ErCode, msg = self.checkFileExist( sbi_dest, os.path.basename(filetocopy) ) |
205 |
< |
if ErCode == '0': |
206 |
< |
ErCode, msg = self.makeCopy( sbi, filetocopy ) |
207 |
< |
if self.debug : print 'Copy results for %s is %s'%( os.path.basename(filetocopy) ,ErCode) |
204 |
> |
try : |
205 |
> |
ErCode, msg = self.checkFileExist( sbi_dest, os.path.basename(filetocopy) ) |
206 |
> |
except Exception, ex: |
207 |
> |
return self.updateReport(filetocopy, '-1', str(ex)) |
208 |
> |
if ErCode == '0': |
209 |
> |
ErCode, msg = self.makeCopy( sbi, filetocopy , options ) |
210 |
> |
if self.debug : print 'Copy results for %s is %s'%( os.path.basename(filetocopy), ErCode) |
211 |
|
results.update( self.updateReport(filetocopy, ErCode, msg)) |
212 |
|
return results |
220 |
– |
|
221 |
– |
def updateReport(self, file, erCode, reason, lfn='', se='' ): |
222 |
– |
""" |
223 |
– |
Update the final stage out infos |
224 |
– |
""" |
225 |
– |
jobStageInfo={} |
226 |
– |
jobStageInfo['erCode']=erCode |
227 |
– |
jobStageInfo['reason']=reason |
228 |
– |
jobStageInfo['lfn']=lfn |
229 |
– |
jobStageInfo['se']=se |
213 |
|
|
231 |
– |
report = { file : jobStageInfo} |
232 |
– |
return report |
233 |
– |
|
234 |
– |
def finalReport( self , results, middleware ): |
235 |
– |
""" |
236 |
– |
It should return a clear list of LFNs for each SE where data are stored. |
237 |
– |
allow "crab -copyLocal" or better "crab -copyOutput". TO_DO. |
238 |
– |
""" |
239 |
– |
if middleware: |
240 |
– |
outFile = open('cmscpReport.sh',"a") |
241 |
– |
cmscp_exit_status = 0 |
242 |
– |
txt = '' |
243 |
– |
for file, dict in results.iteritems(): |
244 |
– |
if dict['lfn']=='': |
245 |
– |
lfn = '$LFNBaseName/'+os.path.basename(file) |
246 |
– |
se = '$SE' |
247 |
– |
else: |
248 |
– |
lfn = dict['lfn']+os.pat.basename(file) |
249 |
– |
se = dict['se'] |
250 |
– |
#dict['lfn'] # to be implemented |
251 |
– |
txt += 'echo "Report for File: '+file+'"\n' |
252 |
– |
txt += 'echo "LFN: '+lfn+'"\n' |
253 |
– |
txt += 'echo "StorageElement: '+se+'"\n' |
254 |
– |
txt += 'echo "StageOutExitStatusReason ='+dict['reason']+'" | tee -a $RUNTIME_AREA/$repo\n' |
255 |
– |
txt += 'echo "StageOutSE = '+se+'" >> $RUNTIME_AREA/$repo\n' |
256 |
– |
if dict['erCode'] != '0': |
257 |
– |
cmscp_exit_status = dict['erCode'] |
258 |
– |
txt += '\n' |
259 |
– |
txt += 'export StageOutExitStatus='+str(cmscp_exit_status)+'\n' |
260 |
– |
txt += 'echo "StageOutExitStatus = '+str(cmscp_exit_status)+'" | tee -a $RUNTIME_AREA/$repo\n' |
261 |
– |
outFile.write(str(txt)) |
262 |
– |
outFile.close() |
263 |
– |
else: |
264 |
– |
for file, code in results.iteritems(): |
265 |
– |
print 'error code = %s for file %s'%(code,file) |
266 |
– |
return |
214 |
|
|
215 |
|
def storageInterface( self, endpoint, protocol ): |
216 |
|
""" |
217 |
< |
Create the storage interface. |
217 |
> |
Create the storage interface. |
218 |
|
""" |
219 |
|
try: |
220 |
|
interface = SElement( FullPath(endpoint), protocol ) |
221 |
< |
except Exception, ex: |
222 |
< |
msg = '' |
221 |
> |
except ProtocolUnknown, ex: |
222 |
> |
msg = '' |
223 |
|
if self.debug : msg = str(ex)+'\n' |
224 |
< |
msg += "ERROR : Unable to create interface with %s protocol\n"%protocol |
225 |
< |
print msg |
224 |
> |
msg += "ERROR : Unable to create interface with %s protocol\n"%protocol |
225 |
> |
raise Exception(msg) |
226 |
|
|
227 |
|
return interface |
228 |
|
|
282 |
– |
def checkDir(self, Destination_SE, protocol): |
283 |
– |
''' |
284 |
– |
ToBeImplemented NEEDED for castor |
285 |
– |
''' |
286 |
– |
return |
287 |
– |
|
229 |
|
def createDir(self, Destination_SE, protocol): |
230 |
|
""" |
231 |
< |
Create remote dir for gsiftp/rfio REALLY TEMPORARY |
232 |
< |
this should be transparent at SE API level. |
231 |
> |
Create remote dir for gsiftp REALLY TEMPORARY |
232 |
> |
this should be transparent at SE API level. |
233 |
|
""" |
234 |
< |
ErCode = '0' |
294 |
< |
msg_1 = '' |
234 |
> |
msg = '' |
235 |
|
try: |
236 |
|
action = SBinterface( Destination_SE ) |
237 |
|
action.createDir() |
238 |
|
if self.debug: print "The directory has been created using protocol %s\n"%protocol |
239 |
< |
except Exception, ex: |
240 |
< |
msg = '' |
241 |
< |
if self.debug : msg = str(ex)+'\n' |
242 |
< |
msg_1 = "ERROR: problem with the directory creation using %s protocol \n"%protocol |
243 |
< |
msg += msg_1 |
244 |
< |
ErCode = '60316' |
245 |
< |
#print msg |
239 |
> |
except TransferException, ex: |
240 |
> |
msg = str(ex) |
241 |
> |
if self.debug : |
242 |
> |
msg += str(ex.detail)+'\n' |
243 |
> |
msg += str(ex.output)+'\n' |
244 |
> |
msg += "ERROR: problem with the directory creation using %s protocol \n"%protocol |
245 |
> |
except OperationException, ex: |
246 |
> |
msg = str(ex) |
247 |
> |
if self.debug : msg += str(ex.detail)+'\n' |
248 |
> |
msg = "ERROR: problem with the directory creation using %s protocol \n"%protocol |
249 |
|
|
250 |
< |
return ErCode, msg_1 |
250 |
> |
return msg |
251 |
|
|
252 |
< |
def checkFileExist(self, sbi, filetocopy): |
252 |
> |
def checkFileExist( self, sbi, filetocopy ): |
253 |
> |
""" |
254 |
> |
Check if file to copy already exist |
255 |
|
""" |
311 |
– |
Check if file to copy already exist |
312 |
– |
""" |
313 |
– |
try: |
314 |
– |
check = sbi.checkExists(filetocopy) |
315 |
– |
except Exception, ex: |
316 |
– |
msg = '' |
317 |
– |
if self.debug : msg = str(ex)+'\n' |
318 |
– |
msg += "ERROR: problem with check File Exist using %s protocol \n"%protocol |
319 |
– |
# print msg |
256 |
|
ErCode = '0' |
257 |
|
msg = '' |
258 |
< |
if check : |
259 |
< |
ErCode = '60303' |
258 |
> |
try: |
259 |
> |
check = sbi.checkExists(filetocopy) |
260 |
> |
except OperationException, ex: |
261 |
> |
msg = str(ex) |
262 |
> |
if self.debug : |
263 |
> |
msg += str(ex.detail)+'\n' |
264 |
> |
msg += str(ex.output)+'\n' |
265 |
> |
msg +='ERROR: problems checkig if file %s already exist'%filetocopy |
266 |
> |
raise Exception(msg) |
267 |
> |
except WrongOption, ex: |
268 |
> |
msg = str(ex) |
269 |
> |
if self.debug : |
270 |
> |
msg += str(ex.detail)+'\n' |
271 |
> |
msg += str(ex.output)+'\n' |
272 |
> |
msg +='ERROR problems checkig if file % already exist'%filetocopy |
273 |
> |
raise Exception(msg) |
274 |
> |
if check : |
275 |
> |
ErCode = '60303' |
276 |
|
msg = "file %s already exist"%filetocopy |
325 |
– |
print msg |
277 |
|
|
278 |
< |
return ErCode,msg |
278 |
> |
return ErCode, msg |
279 |
|
|
280 |
< |
def makeCopy(self, sbi, filetocopy ): |
280 |
> |
def makeCopy(self, sbi, filetocopy, option ): |
281 |
|
""" |
282 |
< |
call the copy API. |
282 |
> |
call the copy API. |
283 |
|
""" |
284 |
< |
path = os.path.dirname(filetocopy) |
284 |
> |
path = os.path.dirname(filetocopy) |
285 |
|
file_name = os.path.basename(filetocopy) |
286 |
|
source_file = filetocopy |
287 |
|
dest_file = file_name ## to be improved supporting changing file name TODO |
288 |
< |
if self.source == '' and path == '': |
288 |
> |
if self.params['source'] == '' and path == '': |
289 |
|
source_file = os.path.abspath(filetocopy) |
290 |
< |
elif self.destination =='': |
290 |
> |
elif self.params['destination'] =='': |
291 |
|
dest_file = os.path.join(os.getcwd(),file_name) |
292 |
< |
elif self.source != '' and self.destination != '' : |
293 |
< |
source_file = file_name |
292 |
> |
elif self.params['source'] != '' and self.params['destination'] != '' : |
293 |
> |
source_file = file_name |
294 |
> |
|
295 |
|
ErCode = '0' |
296 |
|
msg = '' |
297 |
+ |
|
298 |
|
try: |
299 |
< |
pippo = sbi.copy( source_file , dest_file ) |
300 |
< |
if self.protocol == 'srm' : self.checkSize( sbi, filetocopy ) |
301 |
< |
except Exception, ex: |
302 |
< |
msg = '' |
303 |
< |
if self.debug : msg = str(ex)+'\n' |
304 |
< |
msg = "Problem copying %s file with %s command"%( filetocopy, protocol ) |
299 |
> |
sbi.copy( source_file , dest_file , opt = option) |
300 |
> |
except TransferException, ex: |
301 |
> |
msg = str(ex) |
302 |
> |
if self.debug : |
303 |
> |
msg += str(ex.detail)+'\n' |
304 |
> |
msg += str(ex.output)+'\n' |
305 |
> |
msg += "Problem copying %s file" % filetocopy |
306 |
> |
ErCode = '60307' |
307 |
> |
except WrongOption, ex: |
308 |
> |
msg = str(ex) |
309 |
> |
if self.debug : |
310 |
> |
msg += str(ex.detail)+'\n' |
311 |
> |
msg += str(ex.output)+'\n' |
312 |
> |
msg += "Problem copying %s file" % filetocopy |
313 |
|
ErCode = '60307' |
314 |
< |
#print msg |
314 |
> |
|
315 |
> |
## TO BE IMPLEMENTED if NEEDED |
316 |
> |
## NOTE: SE API Already available |
317 |
> |
# if self.protocol.find('srm') : self.checkSize( sbi, filetocopy ) |
318 |
|
|
319 |
|
return ErCode, msg |
320 |
< |
|
321 |
< |
''' |
358 |
< |
def checkSize() |
359 |
< |
""" |
360 |
< |
Using srm needed a check of the ouptut file size. |
361 |
< |
""" |
362 |
< |
|
363 |
< |
echo "--> remoteSize = $remoteSize" |
364 |
< |
## for local file |
365 |
< |
localSize=$(stat -c%s "$path_out_file") |
366 |
< |
echo "--> localSize = $localSize" |
367 |
< |
if [ $localSize != $remoteSize ]; then |
368 |
< |
echo "Local fileSize $localSize does not match remote fileSize $remoteSize" |
369 |
< |
echo "Copy failed: removing remote file $destination" |
370 |
< |
srmrm $destination |
371 |
< |
cmscp_exit_status=60307 |
372 |
< |
|
373 |
< |
|
374 |
< |
echo "Problem copying $path_out_file to $destination with srmcp command" |
375 |
< |
StageOutExitStatusReason='remote and local file dimension not match' |
376 |
< |
echo "StageOutReport = `cat ./srmcp.report`" |
377 |
< |
''' |
378 |
< |
def backup(self): |
320 |
> |
|
321 |
> |
def backup(self): |
322 |
|
""" |
323 |
|
Check infos from TFC using existing api obtaining: |
324 |
|
1)destination |
326 |
|
""" |
327 |
|
return |
328 |
|
|
329 |
< |
def usage(self): |
329 |
> |
def updateReport(self, file, erCode, reason, lfn='', se='' ): |
330 |
> |
""" |
331 |
> |
Update the final stage out infos |
332 |
> |
""" |
333 |
> |
jobStageInfo={} |
334 |
> |
jobStageInfo['erCode']=erCode |
335 |
> |
jobStageInfo['reason']=reason |
336 |
> |
jobStageInfo['lfn']=lfn |
337 |
> |
jobStageInfo['se']=se |
338 |
> |
|
339 |
> |
report = { file : jobStageInfo} |
340 |
> |
return report |
341 |
|
|
342 |
< |
msg=""" |
343 |
< |
required parameters: |
344 |
< |
--source :: REMOTE : |
345 |
< |
--dest :: REMOTE : |
392 |
< |
--debug : |
393 |
< |
--inFile :: absPath : or name NOT RELATIVE PATH |
394 |
< |
--outFIle :: onlyNAME : NOT YET SUPPORTED |
395 |
< |
|
396 |
< |
optional parameters |
342 |
> |
def finalReport( self , results ): |
343 |
> |
""" |
344 |
> |
It a list of LFNs for each SE where data are stored. |
345 |
> |
allow "crab -copyLocal" or better "crab -copyOutput". TO_DO. |
346 |
|
""" |
347 |
< |
return msg |
347 |
> |
outFile = open('cmscpReport.sh',"a") |
348 |
> |
cmscp_exit_status = 0 |
349 |
> |
txt = '' |
350 |
> |
for file, dict in results.iteritems(): |
351 |
> |
if file: |
352 |
> |
if dict['lfn']=='': |
353 |
> |
lfn = '$LFNBaseName/'+os.path.basename(file) |
354 |
> |
se = '$SE' |
355 |
> |
else: |
356 |
> |
lfn = dict['lfn']+os.pat.basename(file) |
357 |
> |
se = dict['se'] |
358 |
> |
#dict['lfn'] # to be implemented |
359 |
> |
txt += 'echo "Report for File: '+file+'"\n' |
360 |
> |
txt += 'echo "LFN: '+lfn+'"\n' |
361 |
> |
txt += 'echo "StorageElement: '+se+'"\n' |
362 |
> |
txt += 'echo "StageOutExitStatusReason ='+dict['reason']+'" | tee -a $RUNTIME_AREA/$repo\n' |
363 |
> |
txt += 'echo "StageOutSE = '+se+'" >> $RUNTIME_AREA/$repo\n' |
364 |
> |
if dict['erCode'] != '0': |
365 |
> |
cmscp_exit_status = dict['erCode'] |
366 |
> |
cmscp_exit_status = dict['erCode'] |
367 |
> |
else: |
368 |
> |
cmscp_exit_status = dict['erCode'] |
369 |
> |
cmscp_exit_status = dict['erCode'] |
370 |
> |
txt += '\n' |
371 |
> |
txt += 'export StageOutExitStatus='+str(cmscp_exit_status)+'\n' |
372 |
> |
txt += 'echo "StageOutExitStatus = '+str(cmscp_exit_status)+'" | tee -a $RUNTIME_AREA/$repo\n' |
373 |
> |
outFile.write(str(txt)) |
374 |
> |
outFile.close() |
375 |
> |
return |
376 |
> |
|
377 |
> |
|
378 |
> |
def usage(): |
379 |
> |
|
380 |
> |
msg=""" |
381 |
> |
required parameters: |
382 |
> |
--source :: REMOTE : |
383 |
> |
--destination :: REMOTE : |
384 |
> |
--debug : |
385 |
> |
--inFile :: absPath : or name NOT RELATIVE PATH |
386 |
> |
--outFIle :: onlyNAME : NOT YET SUPPORTED |
387 |
> |
|
388 |
> |
optional parameters |
389 |
> |
""" |
390 |
> |
print msg |
391 |
> |
|
392 |
> |
return |
393 |
> |
|
394 |
> |
def HelpOptions(opts=[]): |
395 |
> |
""" |
396 |
> |
Check otps, print help if needed |
397 |
> |
prepare dict = { opt : value } |
398 |
> |
""" |
399 |
> |
dict_args = {} |
400 |
> |
if len(opts): |
401 |
> |
for opt, arg in opts: |
402 |
> |
dict_args[opt.split('--')[1]] = arg |
403 |
> |
if opt in ('-h','-help','--help') : |
404 |
> |
usage() |
405 |
> |
sys.exit(0) |
406 |
> |
return dict_args |
407 |
> |
else: |
408 |
> |
usage() |
409 |
> |
sys.exit(0) |
410 |
|
|
411 |
|
if __name__ == '__main__' : |
412 |
+ |
|
413 |
+ |
import getopt |
414 |
+ |
|
415 |
+ |
allowedOpt = ["source=", "destination=", "inputFileList=", "outputFileList=", \ |
416 |
+ |
"protocol=","option=", "middleware=", "srm_version=", "debug", "help"] |
417 |
+ |
try: |
418 |
+ |
opts, args = getopt.getopt( sys.argv[1:], "", allowedOpt ) |
419 |
+ |
except getopt.GetoptError, err: |
420 |
+ |
print err |
421 |
+ |
HelpOptions() |
422 |
+ |
sys.exit(2) |
423 |
+ |
|
424 |
+ |
dictArgs = HelpOptions(opts) |
425 |
|
try: |
426 |
< |
cmscp_ = cmscp(sys.argv[1:]) |
426 |
> |
cmscp_ = cmscp(dictArgs) |
427 |
|
cmscp_.run() |
428 |
< |
except: |
429 |
< |
pass |
428 |
> |
except Exception, ex : |
429 |
> |
print str(ex) |
430 |
|
|