ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/cmscp.py
Revision: 1.48
Committed: Mon Apr 20 12:51:04 2009 UTC (16 years ago) by fanzago
Content type: text/x-python
Branch: MAIN
Changes since 1.47: +2 -2 lines
Log Message:
print error

File Contents

# User Rev Content
1 mcinquil 1.32 ##!/usr/bin/env python
2 spiga 1.1
3 mcinquil 1.16 import sys, os
4 ewv 1.7 from ProdCommon.Storage.SEAPI.SElement import SElement, FullPath
5 spiga 1.1 from ProdCommon.Storage.SEAPI.SBinterface import *
6 spiga 1.11 from ProdCommon.Storage.SEAPI.Exceptions import *
7 spiga 1.1
8    
9     class cmscp:
10 spiga 1.8 def __init__(self, args):
11 spiga 1.1 """
12     cmscp
13 spiga 1.46 safe copy of local file to/from remote SE via lcg_cp/srmcp,
14 spiga 1.1 including success checking version also for CAF using rfcp command to copy the output to SE
15     input:
16     $1 middleware (CAF, LSF, LCG, OSG)
17 spiga 1.2 $2 local file (the absolute path of output file or just the name if it's in top dir)
18     $3 if needed: file name (the output file name)
19     $5 remote SE (complete endpoint)
20 ewv 1.7 $6 srm version
21 fanzago 1.38 --lfn $LFNBaseName
22 spiga 1.1 output:
23     return 0 if all ok
24     return 60307 if srmcp failed
25     return 60303 if file already exists in the SE
26     """
27 spiga 1.8
28 spiga 1.1 #set default
29 spiga 1.24 self.params = {"source":'', "destination":'','destinationDir':'', "inputFileList":'', "outputFileList":'', \
30 fanzago 1.38 "protocol":'', "option":'', "middleware":'', "srm_version":'srmv2', "lfn":'' }
31 ewv 1.14 self.debug = 0
32 spiga 1.46 self.local_stage = 0
33 spiga 1.8 self.params.update( args )
34 ewv 1.7
35 spiga 1.1 return
36    
37 spiga 1.8 def processOptions( self ):
38 spiga 1.1 """
39 spiga 1.8 check command line parameter
40 spiga 1.1 """
41 ewv 1.14 if 'help' in self.params.keys(): HelpOptions()
42     if 'debug' in self.params.keys(): self.debug = 1
43 spiga 1.45 if 'local_stage' in self.params.keys(): self.local_stage = 1
44 ewv 1.7
45     # source and dest cannot be undefined at same time
46 spiga 1.8 if not self.params['source'] and not self.params['destination'] :
47 spiga 1.36 HelpOptions()
48 ewv 1.7 # if middleware is not defined --> protocol cannot be empty
49 spiga 1.8 if not self.params['middleware'] and not self.params['protocol'] :
50     HelpOptions()
51    
52 ewv 1.7 # input file must be defined
53 spiga 1.35 if not self.params['inputFileList'] :
54 spiga 1.36 HelpOptions()
55 spiga 1.1 else:
56 spiga 1.8 file_to_copy=[]
57 ewv 1.14 if self.params['inputFileList'].find(','):
58 spiga 1.8 [file_to_copy.append(x.strip()) for x in self.params['inputFileList'].split(',')]
59 ewv 1.7 else:
60 spiga 1.8 file_to_copy.append(self.params['inputFileList'])
61     self.params['inputFileList'] = file_to_copy
62 ewv 1.7
63 spiga 1.46 if not self.params['lfn'] and self.local_stage == 1 : HelpOptions()
64 fanzago 1.38
65 spiga 1.1 ## TO DO:
66     #### add check for outFiles
67     #### add map {'inFileNAME':'outFileNAME'} to change out name
68    
69    
70 ewv 1.7 def run( self ):
71 spiga 1.1 """
72 ewv 1.7 Check if running on UI (no $middleware) or
73     on WN (on the Grid), and take different action
74 spiga 1.1 """
75 mcinquil 1.37 self.processOptions()
76 spiga 1.30 if self.debug: print 'calling run() : \n'
77 spiga 1.8 # stage out from WN
78     if self.params['middleware'] :
79 spiga 1.36 results = self.stager(self.params['middleware'],self.params['inputFileList'])
80 spiga 1.35 self.finalReport(results)
81 spiga 1.8 # Local interaction with SE
82 spiga 1.1 else:
83 spiga 1.36 results = self.copy(self.params['inputFileList'], self.params['protocol'], self.params['option'] )
84 spiga 1.35 return results
85 spiga 1.1
86 spiga 1.8 def setProtocol( self, middleware ):
87 spiga 1.1 """
88     define the allowed potocols based on $middlware
89 ewv 1.7 which depend on scheduler
90 spiga 1.1 """
91 spiga 1.8 # default To be used with "middleware"
92 fanzago 1.38 if self.debug:
93     print 'setProtocol() :\n'
94     print '\tmiddleware = %s utils \n'%middleware
95    
96 spiga 1.13 lcgOpt={'srmv1':'-b -D srmv1 -t 2400 --verbose',
97     'srmv2':'-b -D srmv2 -t 2400 --verbose'}
98     srmOpt={'srmv1':' -report ./srmcp.report -retry_timeout 480000 -retry_num 3 -streams_num=1 ',
99 ewv 1.33 'srmv2':' -report=./srmcp.report -retry_timeout=480000 -retry_num=3 '}
100 spiga 1.8 rfioOpt=''
101    
102 ewv 1.14 supported_protocol = None
103 spiga 1.40 if middleware.lower() in ['osg','lcg','condor','sge']:
104 spiga 1.12 supported_protocol = [('srm-lcg',lcgOpt[self.params['srm_version']]),\
105 spiga 1.30 (self.params['srm_version'],srmOpt[self.params['srm_version']])]
106 spiga 1.8 elif middleware.lower() in ['lsf','caf']:
107 ewv 1.14 supported_protocol = [('rfio',rfioOpt)]
108 spiga 1.1 else:
109 ewv 1.7 ## here we can add support for any kind of protocol,
110 spiga 1.1 ## maybe some local schedulers need something dedicated
111     pass
112     return supported_protocol
113 spiga 1.28
114 fanzago 1.41
115     def checkCopy (self, copy_results, len_list_files, prot, lfn='', se=''):
116     """
117     Checks the status of copy and update result dictionary
118 spiga 1.28 """
119     list_retry = []
120 fanzago 1.41 list_not_existing = []
121 spiga 1.28 list_ok = []
122 fanzago 1.41
123     if self.debug:
124     print 'in checkCopy() :\n'
125     for file, dict in copy_results.iteritems():
126     er_code = dict['erCode']
127     if er_code == '0':
128     list_ok.append(file)
129     reason = 'Copy succedeed with %s utils'%prot
130     dict['reason'] = reason
131     elif er_code == '60302':
132     list_not_existing.append( file )
133     else:
134     list_retry.append( file )
135    
136     if self.debug:
137     print "\t file %s \n"%file
138     print "\t dict['erCode'] %s \n"%dict['erCode']
139     print "\t dict['reason'] %s \n"%dict['reason']
140    
141     if (lfn != '') and (se != ''):
142     upDict = self.updateReport(file, er_code, dict['reason'], lfn, se)
143 spiga 1.28 else:
144 fanzago 1.41 upDict = self.updateReport(file, er_code, dict['reason'])
145    
146     copy_results.update(upDict)
147    
148     msg = ''
149     if len(list_ok) != 0:
150     msg += '\tCopy of %s succedeed with %s utils\n'%(str(list_ok),prot)
151     if len(list_ok) != len_list_files :
152     msg += '\tCopy of %s failed using %s for files \n'%(str(list_retry),prot)
153     msg += '\tCopy of %s failed using %s : files not found \n'%(str(list_not_existing),prot)
154     if self.debug : print msg
155    
156     return copy_results, list_ok, list_retry
157    
158 spiga 1.45 def LocalCopy(self, list_retry, results):
159 fanzago 1.41 """
160 spiga 1.45 Tries the stage out to the CloseSE
161 fanzago 1.38 """
162 fanzago 1.41 if self.debug:
163 spiga 1.45 print 'in LocalCopy() :\n'
164 fanzago 1.41 print '\t list_retry %s utils \n'%list_retry
165     print '\t len(list_retry) %s \n'%len(list_retry)
166    
167     list_files = list_retry
168     self.params['inputFilesList']=list_files
169    
170     ### copy backup
171     from ProdCommon.FwkJobRep.SiteLocalConfig import loadSiteLocalConfig
172     siteCfg = loadSiteLocalConfig()
173     seName = siteCfg.localStageOut.get("se-name", None)
174     catalog = siteCfg.localStageOut.get("catalog", None)
175     implName = siteCfg.localStageOut.get("command", None)
176     if (implName == 'srm'):
177     implName='srmv1'
178     self.params['srm_version']=implName
179     ##### to be improved ###############
180     if (implName == 'rfcp'):
181     self.params['middleware']='lsf'
182     ####################################
183    
184     self.params['protocol']=implName
185     tfc = siteCfg.trivialFileCatalog()
186    
187     if self.debug:
188     print '\t siteCFG %s \n'%siteCfg
189     print '\t seName %s \n'%seName
190     print '\t catalog %s \n'%catalog
191     print "\t self.params['protocol'] %s \n"%self.params['protocol']
192     print '\t tfc %s '%tfc
193     print "\t self.params['inputFilesList'] %s \n"%self.params['inputFilesList']
194    
195     file_backup=[]
196     for input in self.params['inputFilesList']:
197     file = self.params['lfn'] + os.path.basename(input)
198     surl = tfc.matchLFN(tfc.preferredProtocol, file)
199     file_backup.append(surl)
200     if self.debug:
201     print '\t lfn %s \n'%self.params['lfn']
202     print '\t file %s \n'%file
203     print '\t surl %s \n'%surl
204    
205     destination=os.path.dirname(file_backup[0])
206     self.params['destination']=destination
207    
208     if self.debug:
209     print "\t self.params['destination']%s \n"%self.params['destination']
210     print "\t self.params['protocol'] %s \n"%self.params['protocol']
211     print "\t self.params['option']%s \n"%self.params['option']
212    
213     for prot, opt in self.setProtocol( self.params['middleware'] ):
214 spiga 1.45 if self.debug: print '\tIn LocalCopy trying the stage out with %s utils \n'%prot
215     localCopy_results = self.copy( self.params['inputFileList'], prot, opt )
216     if localCopy_results.keys() == [''] or localCopy_results.keys() == '' :
217     results.update(localCopy_results)
218 fanzago 1.41 else:
219 spiga 1.45 localCopy_results, list_ok, list_retry = self.checkCopy(localCopy_results, len(list_files), prot, self.params['lfn'], seName)
220     results.update(localCopy_results)
221 fanzago 1.41 if len(list_ok) == len(list_files) :
222     break
223     if len(list_retry):
224     list_files = list_retry
225     else: break
226     if self.debug:
227 spiga 1.45 print "\t localCopy_results = %s \n"%localCopy_results
228 spiga 1.28
229 fanzago 1.41 return results
230    
231 spiga 1.8 def stager( self, middleware, list_files ):
232 spiga 1.1 """
233     Implement the logic for remote stage out
234     """
235 spiga 1.30
236 fanzago 1.38 if self.debug:
237     print 'stager() :\n'
238     print '\tmiddleware %s\n'%middleware
239 spiga 1.45 print '\tlist_files %s\n'%list_files
240 fanzago 1.38
241 spiga 1.6 results={}
242 spiga 1.8 for prot, opt in self.setProtocol( middleware ):
243 spiga 1.30 if self.debug: print '\tTrying the stage out with %s utils \n'%prot
244 spiga 1.8 copy_results = self.copy( list_files, prot, opt )
245 spiga 1.30 if copy_results.keys() == [''] or copy_results.keys() == '' :
246 spiga 1.13 results.update(copy_results)
247     else:
248 fanzago 1.41 copy_results, list_ok, list_retry = self.checkCopy(copy_results, len(list_files), prot)
249 spiga 1.13 results.update(copy_results)
250     if len(list_ok) == len(list_files) :
251     break
252 fanzago 1.41 if len(list_retry):
253     list_files = list_retry
254     else: break
255    
256 spiga 1.45 if self.local_stage:
257 fanzago 1.43 if len(list_retry):
258 spiga 1.46 results = self.LocalCopy(list_retry, results)
259 fanzago 1.38
260     if self.debug:
261     print "\t results %s \n"%results
262 spiga 1.1 return results
263    
264     def initializeApi(self, protocol ):
265     """
266 ewv 1.7 Instantiate storage interface
267 spiga 1.1 """
268 spiga 1.30 if self.debug : print 'initializeApi() :\n'
269 spiga 1.20 self.source_prot = protocol
270     self.dest_prot = protocol
271     if not self.params['source'] : self.source_prot = 'local'
272     Source_SE = self.storageInterface( self.params['source'], self.source_prot )
273     if not self.params['destination'] : self.dest_prot = 'local'
274     Destination_SE = self.storageInterface( self.params['destination'], self.dest_prot )
275 spiga 1.1
276     if self.debug :
277 spiga 1.30 msg = '\t(source=%s, protocol=%s)'%(self.params['source'], self.source_prot)
278     msg += '\t(destination=%s, protocol=%s)'%(self.params['destination'], self.dest_prot)
279 mcinquil 1.32 print msg
280 spiga 1.1
281     return Source_SE, Destination_SE
282    
283 spiga 1.8 def copy( self, list_file, protocol, options ):
284 spiga 1.1 """
285 ewv 1.7 Make the real file copy using SE API
286 spiga 1.1 """
287 mcinquil 1.37 msg = ""
288 spiga 1.1 if self.debug :
289 spiga 1.30 msg = 'copy() :\n'
290     msg += '\tusing %s protocol\n'%protocol
291     print msg
292 spiga 1.13 try:
293     Source_SE, Destination_SE = self.initializeApi( protocol )
294     except Exception, ex:
295     return self.updateReport('', '-1', str(ex))
296 ewv 1.14
297 ewv 1.7 # create remote dir
298 mcinquil 1.32 if Destination_SE.protocol in ['gridftp','rfio','srmv2']:
299 spiga 1.13 try:
300 mcinquil 1.32 self.createDir( Destination_SE, Destination_SE.protocol )
301 mcinquil 1.47 except OperationException, ex:
302 spiga 1.13 return self.updateReport('', '60316', str(ex))
303 spiga 1.1
304     ## prepare for real copy ##
305 spiga 1.8 try :
306     sbi = SBinterface( Source_SE, Destination_SE )
307     sbi_dest = SBinterface(Destination_SE)
308 spiga 1.20 sbi_source = SBinterface(Source_SE)
309 spiga 1.11 except ProtocolMismatch, ex:
310 spiga 1.30 msg = "ERROR : Unable to create SBinterface with %s protocol"%protocol
311     msg += str(ex)
312     return self.updateReport('', '-1', msg)
313 spiga 1.1
314     results = {}
315 ewv 1.7 ## loop over the complete list of files
316     for filetocopy in list_file:
317 spiga 1.30 if self.debug : print '\tStart real copy for %s'%filetocopy
318 spiga 1.13 try :
319 spiga 1.34 ErCode, msg = self.checkFileExist( sbi_source, sbi_dest, filetocopy, options )
320 spiga 1.13 except Exception, ex:
321 spiga 1.18 ErCode = -1
322     msg = str(ex)
323 ewv 1.7 if ErCode == '0':
324 spiga 1.19 ErCode, msg = self.makeCopy( sbi, filetocopy , options, protocol,sbi_dest )
325 spiga 1.30 if self.debug : print '\tCopy results for %s is %s'%( os.path.basename(filetocopy), ErCode)
326 spiga 1.1 results.update( self.updateReport(filetocopy, ErCode, msg))
327     return results
328 ewv 1.7
329 spiga 1.1
330     def storageInterface( self, endpoint, protocol ):
331     """
332 ewv 1.7 Create the storage interface.
333 spiga 1.1 """
334 spiga 1.30 if self.debug : print 'storageInterface():\n'
335 spiga 1.1 try:
336     interface = SElement( FullPath(endpoint), protocol )
337 spiga 1.11 except ProtocolUnknown, ex:
338 spiga 1.30 msg = "ERROR : Unable to create interface with %s protocol"%protocol
339     msg += str(ex)
340 spiga 1.13 raise Exception(msg)
341 spiga 1.1
342     return interface
343    
344     def createDir(self, Destination_SE, protocol):
345     """
346 spiga 1.8 Create remote dir for gsiftp REALLY TEMPORARY
347 ewv 1.7 this should be transparent at SE API level.
348 spiga 1.1 """
349 spiga 1.30 if self.debug : print 'createDir():\n'
350 ewv 1.14 msg = ''
351 spiga 1.1 try:
352     action = SBinterface( Destination_SE )
353     action.createDir()
354 spiga 1.30 if self.debug: print "\tThe directory has been created using protocol %s"%protocol
355 spiga 1.11 except TransferException, ex:
356 spiga 1.30 msg = "ERROR: problem with the directory creation using %s protocol "%protocol
357     msg += str(ex)
358 spiga 1.11 if self.debug :
359 spiga 1.30 dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n'
360     dbgmsg += '\t'+str(ex.output)+'\n'
361     print dbgmsg
362 spiga 1.29 raise Exception(msg)
363 spiga 1.11 except OperationException, ex:
364 spiga 1.30 msg = "ERROR: problem with the directory creation using %s protocol "%protocol
365     msg += str(ex)
366     if self.debug : print '\t'+msg+'\n\t'+str(ex.detail)+'\n'
367 spiga 1.29 raise Exception(msg)
368 spiga 1.31 except MissingDestination, ex:
369     msg = "ERROR: problem with the directory creation using %s protocol "%protocol
370     msg += str(ex)
371     if self.debug : print '\t'+msg+'\n\t'+str(ex.detail)+'\n'
372     raise Exception(msg)
373     except AlreadyExistsException, ex:
374     if self.debug: print "\tThe directory already exist"
375     pass
376 spiga 1.13 return msg
377 spiga 1.1
378 spiga 1.34 def checkFileExist( self, sbi_source, sbi_dest, filetocopy, option ):
379 spiga 1.1 """
380 spiga 1.20 Check both if source file exist AND
381     if destination file ALREADY exist.
382 ewv 1.7 """
383 spiga 1.30 if self.debug : print 'checkFileExist():\n'
384 spiga 1.8 ErCode = '0'
385     msg = ''
386 spiga 1.20 f_tocopy=filetocopy
387     if self.source_prot != 'local':f_tocopy = os.path.basename(filetocopy)
388 spiga 1.1 try:
389 spiga 1.34 checkSource = sbi_source.checkExists( f_tocopy , opt=option )
390 spiga 1.30 if self.debug : print '\tCheck for local file %s exist succeded \n'%f_tocopy
391 spiga 1.20 except OperationException, ex:
392 spiga 1.30 msg ='ERROR: problems checkig if source file %s exist'%filetocopy
393     msg += str(ex)
394 spiga 1.20 if self.debug :
395 spiga 1.30 dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n'
396     dbgmsg += '\t'+str(ex.output)+'\n'
397     print dbgmsg
398 spiga 1.20 raise Exception(msg)
399     except WrongOption, ex:
400 spiga 1.30 msg ='ERROR problems checkig if source file % exist'%filetocopy
401     msg += str(ex)
402 spiga 1.20 if self.debug :
403 spiga 1.30 dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n'
404     dbgmsg += '\t'+str(ex.output)+'\n'
405     print dbgmsg
406 spiga 1.20 raise Exception(msg)
407 spiga 1.31 except MissingDestination, ex:
408     msg ='ERROR problems checkig if source file % exist'%filetocopy
409     msg += str(ex)
410     if self.debug : print '\t'+msg+'\n\t'+str(ex.detail)+'\n'
411     raise Exception(msg)
412 spiga 1.20 if not checkSource :
413     ErCode = '60302'
414     msg = "ERROR file %s do not exist"%os.path.basename(filetocopy)
415     return ErCode, msg
416     f_tocopy=filetocopy
417     if self.dest_prot != 'local':f_tocopy = os.path.basename(filetocopy)
418     try:
419 spiga 1.34 check = sbi_dest.checkExists( f_tocopy, opt=option )
420 spiga 1.30 if self.debug : print '\tCheck for remote file %s exist succeded \n'%f_tocopy
421 spiga 1.11 except OperationException, ex:
422 spiga 1.30 msg = 'ERROR: problems checkig if file %s already exist'%filetocopy
423     msg += str(ex)
424 spiga 1.11 if self.debug :
425 spiga 1.30 dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n'
426     dbgmsg += '\t'+str(ex.output)+'\n'
427     print dbgmsg
428 spiga 1.13 raise Exception(msg)
429 spiga 1.11 except WrongOption, ex:
430 spiga 1.30 msg = 'ERROR problems checkig if file % already exist'%filetocopy
431     msg += str(ex)
432 spiga 1.11 if self.debug :
433 spiga 1.30 msg += '\t'+msg+'\n\t'+str(ex.detail)+'\n'
434     msg += '\t'+str(ex.output)+'\n'
435 spiga 1.13 raise Exception(msg)
436 spiga 1.31 except MissingDestination, ex:
437     msg ='ERROR problems checkig if source file % exist'%filetocopy
438     msg += str(ex)
439     if self.debug : print '\t'+msg+'\n\t'+str(ex.detail)+'\n'
440     raise Exception(msg)
441 ewv 1.14 if check :
442 ewv 1.7 ErCode = '60303'
443 spiga 1.20 msg = "file %s already exist"%os.path.basename(filetocopy)
444 ewv 1.14
445 spiga 1.13 return ErCode, msg
446 spiga 1.1
447 spiga 1.19 def makeCopy(self, sbi, filetocopy, option, protocol, sbi_dest ):
448 spiga 1.1 """
449 ewv 1.7 call the copy API.
450 spiga 1.1 """
451 spiga 1.30 if self.debug : print 'makeCopy():\n'
452 ewv 1.7 path = os.path.dirname(filetocopy)
453 spiga 1.1 file_name = os.path.basename(filetocopy)
454     source_file = filetocopy
455     dest_file = file_name ## to be improved supporting changing file name TODO
456 spiga 1.8 if self.params['source'] == '' and path == '':
457 spiga 1.1 source_file = os.path.abspath(filetocopy)
458 spiga 1.8 elif self.params['destination'] =='':
459 spiga 1.24 destDir = self.params.get('destinationDir',os.getcwd())
460     dest_file = os.path.join(destDir,file_name)
461 spiga 1.8 elif self.params['source'] != '' and self.params['destination'] != '' :
462 ewv 1.7 source_file = file_name
463 spiga 1.8
464 spiga 1.1 ErCode = '0'
465     msg = ''
466 ewv 1.7
467 spiga 1.1 try:
468 spiga 1.8 sbi.copy( source_file , dest_file , opt = option)
469 spiga 1.11 except TransferException, ex:
470 spiga 1.30 msg = "Problem copying %s file" % filetocopy
471     msg += str(ex)
472 spiga 1.11 if self.debug :
473 spiga 1.30 dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n'
474     dbgmsg += '\t'+str(ex.output)+'\n'
475 mcinquil 1.32 print dbgmsg
476 spiga 1.1 ErCode = '60307'
477 spiga 1.11 except WrongOption, ex:
478 spiga 1.30 msg = "Problem copying %s file" % filetocopy
479     msg += str(ex)
480 spiga 1.11 if self.debug :
481 spiga 1.30 dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n'
482     dbgmsg += '\t'+str(ex.output)+'\n'
483 fanzago 1.48 print dbgmsg
484 spiga 1.44 except SizeZeroException, ex:
485     msg = "Problem copying %s file" % filetocopy
486     msg += str(ex)
487     if self.debug :
488     dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n'
489     dbgmsg += '\t'+str(ex.output)+'\n'
490 fanzago 1.48 print dbgmsg
491 spiga 1.13 ErCode = '60307'
492 spiga 1.24 if ErCode == '0' and protocol.find('srmv') == 0:
493 spiga 1.19 remote_file_size = -1
494     local_file_size = os.path.getsize( source_file )
495     try:
496 spiga 1.34 remote_file_size = sbi_dest.getSize( dest_file, opt=option )
497 spiga 1.30 if self.debug : print '\t Check of remote size succeded for file %s\n'%dest_file
498 spiga 1.19 except TransferException, ex:
499 spiga 1.30 msg = "Problem checking the size of %s file" % filetocopy
500     msg += str(ex)
501 spiga 1.19 if self.debug :
502 spiga 1.30 dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n'
503     dbgmsg += '\t'+str(ex.output)+'\n'
504     print dbgmsg
505 spiga 1.19 ErCode = '60307'
506     except WrongOption, ex:
507 spiga 1.30 msg = "Problem checking the size of %s file" % filetocopy
508     msg += str(ex)
509 spiga 1.19 if self.debug :
510 spiga 1.30 dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n'
511     dbgmsg += '\t'+str(ex.output)+'\n'
512     print dbgmsg
513 spiga 1.19 ErCode = '60307'
514     if local_file_size != remote_file_size:
515     msg = "File size dosn't match: local size = %s ; remote size = %s " % (local_file_size, remote_file_size)
516     ErCode = '60307'
517 ewv 1.14
518 spiga 1.27 if ErCode != '0':
519     try :
520 spiga 1.34 self.removeFile( sbi_dest, dest_file, option )
521 spiga 1.27 except Exception, ex:
522     msg += '\n'+str(ex)
523 spiga 1.1 return ErCode, msg
524 ewv 1.7
525 spiga 1.34 def removeFile( self, sbi_dest, filetocopy, option ):
526 spiga 1.30 """
527     """
528     if self.debug : print 'removeFile():\n'
529 spiga 1.21 f_tocopy=filetocopy
530     if self.dest_prot != 'local':f_tocopy = os.path.basename(filetocopy)
531     try:
532 spiga 1.34 sbi_dest.delete( f_tocopy, opt=option )
533 spiga 1.30 if self.debug : '\t deletion of file %s succeeded\n'%str(filetocopy)
534 spiga 1.21 except OperationException, ex:
535 spiga 1.30 msg ='ERROR: problems removing partially staged file %s'%filetocopy
536     msg += str(ex)
537 spiga 1.21 if self.debug :
538 spiga 1.30 dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n'
539     dbgmsg += '\t'+str(ex.output)+'\n'
540     print dbgmsg
541 spiga 1.21 raise Exception(msg)
542    
543     return
544    
545 spiga 1.8 def updateReport(self, file, erCode, reason, lfn='', se='' ):
546     """
547     Update the final stage out infos
548     """
549     jobStageInfo={}
550     jobStageInfo['erCode']=erCode
551     jobStageInfo['reason']=reason
552     jobStageInfo['lfn']=lfn
553     jobStageInfo['se']=se
554 spiga 1.1
555 spiga 1.8 report = { file : jobStageInfo}
556     return report
557 ewv 1.7
558 spiga 1.8 def finalReport( self , results ):
559     """
560     It a list of LFNs for each SE where data are stored.
561     allow "crab -copyLocal" or better "crab -copyOutput". TO_DO.
562 spiga 1.1 """
563 spiga 1.8 outFile = open('cmscpReport.sh',"a")
564     cmscp_exit_status = 0
565     txt = ''
566 ewv 1.14 for file, dict in results.iteritems():
567     if file:
568 spiga 1.11 if dict['lfn']=='':
569     lfn = '$LFNBaseName/'+os.path.basename(file)
570     se = '$SE'
571     else:
572 mcinquil 1.16 lfn = dict['lfn']+os.path.basename(file)
573 spiga 1.11 se = dict['se']
574 fanzago 1.41
575 spiga 1.11 #dict['lfn'] # to be implemented
576 fanzago 1.39 txt += 'echo "Report for File: '+file+'"\n'
577     txt += 'echo "LFN: '+lfn+'"\n'
578     txt += 'echo "StorageElement: '+se+'"\n'
579 spiga 1.11 txt += 'echo "StageOutExitStatusReason ='+dict['reason']+'" | tee -a $RUNTIME_AREA/$repo\n'
580     txt += 'echo "StageOutSE = '+se+'" >> $RUNTIME_AREA/$repo\n'
581 fanzago 1.42 #txt += 'export LFNBaseName='+lfn+'\n'
582 fanzago 1.39 txt += 'export SE='+se+'\n'
583 fanzago 1.41
584 spiga 1.11 if dict['erCode'] != '0':
585     cmscp_exit_status = dict['erCode']
586 spiga 1.12 else:
587 spiga 1.30 txt += 'echo "StageOutExitStatusReason ='+dict['reason']+'" | tee -a $RUNTIME_AREA/$repo\n'
588 spiga 1.12 cmscp_exit_status = dict['erCode']
589     cmscp_exit_status = dict['erCode']
590 spiga 1.8 txt += '\n'
591     txt += 'export StageOutExitStatus='+str(cmscp_exit_status)+'\n'
592     txt += 'echo "StageOutExitStatus = '+str(cmscp_exit_status)+'" | tee -a $RUNTIME_AREA/$repo\n'
593     outFile.write(str(txt))
594     outFile.close()
595     return
596    
597    
598     def usage():
599    
600     msg="""
601 spiga 1.46 cmscp:
602     safe copy of local file to/from remote SE via lcg_cp/srmcp,
603     including success checking version also for CAF using rfcp command to copy the output to SE
604 spiga 1.8
605 spiga 1.46 accepted parameters:
606     source =
607     destination =
608     inputFileList =
609     outputFileList =
610     protocol =
611     option =
612     middleware =
613     srm_version =
614     destinationDir =
615     lfn= =
616     local_stage = activate stage fall back
617     debug = activate verbose print out
618     help = print on line man and exit
619    
620     mandatory:
621     * "source" and/or "destination" must always be defined
622     * either "middleware" or "protocol" must always be defined
623     * "inputFileList" must always be defined
624     * if "local_stage" = 1 also "lfn" must be defined
625 spiga 1.8 """
626 ewv 1.14 print msg
627 spiga 1.8
628 ewv 1.14 return
629 spiga 1.8
630     def HelpOptions(opts=[]):
631     """
632     Check otps, print help if needed
633 ewv 1.14 prepare dict = { opt : value }
634 spiga 1.8 """
635     dict_args = {}
636     if len(opts):
637     for opt, arg in opts:
638 ewv 1.14 dict_args[opt.split('--')[1]] = arg
639 spiga 1.8 if opt in ('-h','-help','--help') :
640     usage()
641     sys.exit(0)
642     return dict_args
643     else:
644     usage()
645     sys.exit(0)
646 spiga 1.1
647     if __name__ == '__main__' :
648 spiga 1.8
649 ewv 1.14 import getopt
650 spiga 1.8
651     allowedOpt = ["source=", "destination=", "inputFileList=", "outputFileList=", \
652 spiga 1.25 "protocol=","option=", "middleware=", "srm_version=", \
653 spiga 1.46 "destinationDir=", "lfn=", "local_stage", "debug", "help"]
654 ewv 1.14 try:
655     opts, args = getopt.getopt( sys.argv[1:], "", allowedOpt )
656 spiga 1.8 except getopt.GetoptError, err:
657     print err
658     HelpOptions()
659     sys.exit(2)
660 ewv 1.14
661 spiga 1.8 dictArgs = HelpOptions(opts)
662 spiga 1.1 try:
663 spiga 1.8 cmscp_ = cmscp(dictArgs)
664 spiga 1.1 cmscp_.run()
665 spiga 1.11 except Exception, ex :
666     print str(ex)
667 spiga 1.1