ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/cmscp.py
Revision: 1.41
Committed: Wed Feb 11 16:55:21 2009 UTC (16 years, 2 months ago) by fanzago
Content type: text/x-python
Branch: MAIN
Changes since 1.40: +119 -147 lines
Log Message:
implemented backup copy function

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    
14 ewv 1.7 safe copy of local file in current directory to remote SE via lcg_cp/srmcp,
15 spiga 1.1 including success checking version also for CAF using rfcp command to copy the output to SE
16     input:
17     $1 middleware (CAF, LSF, LCG, OSG)
18 spiga 1.2 $2 local file (the absolute path of output file or just the name if it's in top dir)
19     $3 if needed: file name (the output file name)
20     $5 remote SE (complete endpoint)
21 ewv 1.7 $6 srm version
22 fanzago 1.38 --lfn $LFNBaseName
23 spiga 1.1 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 spiga 1.8
29 spiga 1.1 #set default
30 spiga 1.24 self.params = {"source":'', "destination":'','destinationDir':'', "inputFileList":'', "outputFileList":'', \
31 fanzago 1.38 "protocol":'', "option":'', "middleware":'', "srm_version":'srmv2', "lfn":'' }
32 ewv 1.14 self.debug = 0
33    
34 spiga 1.8 self.params.update( args )
35 ewv 1.7
36 spiga 1.1 return
37    
38 spiga 1.8 def processOptions( self ):
39 spiga 1.1 """
40 spiga 1.8 check command line parameter
41 spiga 1.1 """
42 ewv 1.14 if 'help' in self.params.keys(): HelpOptions()
43     if 'debug' in self.params.keys(): self.debug = 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 fanzago 1.38
64     if not self.params['lfn'] : HelpOptions()
65    
66 spiga 1.1 ## TO DO:
67     #### add check for outFiles
68     #### add map {'inFileNAME':'outFileNAME'} to change out name
69    
70    
71 ewv 1.7 def run( self ):
72 spiga 1.1 """
73 ewv 1.7 Check if running on UI (no $middleware) or
74     on WN (on the Grid), and take different action
75 spiga 1.1 """
76 mcinquil 1.37 self.processOptions()
77 spiga 1.30 if self.debug: print 'calling run() : \n'
78 spiga 1.8 # stage out from WN
79     if self.params['middleware'] :
80 spiga 1.36 results = self.stager(self.params['middleware'],self.params['inputFileList'])
81 spiga 1.35 self.finalReport(results)
82 spiga 1.8 # Local interaction with SE
83 spiga 1.1 else:
84 spiga 1.36 results = self.copy(self.params['inputFileList'], self.params['protocol'], self.params['option'] )
85 spiga 1.35 return results
86 spiga 1.1
87 spiga 1.8 def setProtocol( self, middleware ):
88 spiga 1.1 """
89     define the allowed potocols based on $middlware
90 ewv 1.7 which depend on scheduler
91 spiga 1.1 """
92 spiga 1.8 # default To be used with "middleware"
93 fanzago 1.38 if self.debug:
94     print 'setProtocol() :\n'
95     print '\tmiddleware = %s utils \n'%middleware
96    
97 spiga 1.13 lcgOpt={'srmv1':'-b -D srmv1 -t 2400 --verbose',
98     'srmv2':'-b -D srmv2 -t 2400 --verbose'}
99     srmOpt={'srmv1':' -report ./srmcp.report -retry_timeout 480000 -retry_num 3 -streams_num=1 ',
100 ewv 1.33 'srmv2':' -report=./srmcp.report -retry_timeout=480000 -retry_num=3 '}
101 spiga 1.8 rfioOpt=''
102    
103 ewv 1.14 supported_protocol = None
104 spiga 1.40 if middleware.lower() in ['osg','lcg','condor','sge']:
105 spiga 1.12 supported_protocol = [('srm-lcg',lcgOpt[self.params['srm_version']]),\
106 spiga 1.30 (self.params['srm_version'],srmOpt[self.params['srm_version']])]
107 spiga 1.8 elif middleware.lower() in ['lsf','caf']:
108 ewv 1.14 supported_protocol = [('rfio',rfioOpt)]
109 spiga 1.1 else:
110 ewv 1.7 ## here we can add support for any kind of protocol,
111 spiga 1.1 ## maybe some local schedulers need something dedicated
112     pass
113     return supported_protocol
114 spiga 1.28
115 fanzago 1.41
116     def checkCopy (self, copy_results, len_list_files, prot, lfn='', se=''):
117     """
118     Checks the status of copy and update result dictionary
119 spiga 1.28 """
120     list_retry = []
121 fanzago 1.41 list_not_existing = []
122 spiga 1.28 list_ok = []
123 fanzago 1.41
124     if self.debug:
125     print 'in checkCopy() :\n'
126     for file, dict in copy_results.iteritems():
127     er_code = dict['erCode']
128     if er_code == '0':
129     list_ok.append(file)
130     reason = 'Copy succedeed with %s utils'%prot
131     dict['reason'] = reason
132     elif er_code == '60302':
133     list_not_existing.append( file )
134     else:
135     list_retry.append( file )
136    
137     if self.debug:
138     print "\t file %s \n"%file
139     print "\t dict['erCode'] %s \n"%dict['erCode']
140     print "\t dict['reason'] %s \n"%dict['reason']
141    
142     if (lfn != '') and (se != ''):
143     upDict = self.updateReport(file, er_code, dict['reason'], lfn, se)
144 spiga 1.28 else:
145 fanzago 1.41 upDict = self.updateReport(file, er_code, dict['reason'])
146    
147     copy_results.update(upDict)
148    
149     msg = ''
150     if len(list_ok) != 0:
151     msg += '\tCopy of %s succedeed with %s utils\n'%(str(list_ok),prot)
152     if len(list_ok) != len_list_files :
153     msg += '\tCopy of %s failed using %s for files \n'%(str(list_retry),prot)
154     msg += '\tCopy of %s failed using %s : files not found \n'%(str(list_not_existing),prot)
155     if self.debug : print msg
156    
157     return copy_results, list_ok, list_retry
158    
159     def backupCopy(self, list_retry, results):
160     """
161     Tries the backup copy of output to the CloseSE
162 fanzago 1.38 """
163 fanzago 1.41 if self.debug:
164     print 'in backup() :\n'
165     print '\t list_retry %s utils \n'%list_retry
166     print '\t len(list_retry) %s \n'%len(list_retry)
167    
168     list_files = list_retry
169     self.params['inputFilesList']=list_files
170    
171     ### copy backup
172     from ProdCommon.FwkJobRep.SiteLocalConfig import loadSiteLocalConfig
173     siteCfg = loadSiteLocalConfig()
174     seName = siteCfg.localStageOut.get("se-name", None)
175     catalog = siteCfg.localStageOut.get("catalog", None)
176     implName = siteCfg.localStageOut.get("command", None)
177     if (implName == 'srm'):
178     implName='srmv1'
179     self.params['srm_version']=implName
180     ##### to be improved ###############
181     if (implName == 'rfcp'):
182     self.params['middleware']='lsf'
183     ####################################
184    
185     self.params['protocol']=implName
186     tfc = siteCfg.trivialFileCatalog()
187    
188     if self.debug:
189     print '\t siteCFG %s \n'%siteCfg
190     print '\t seName %s \n'%seName
191     print '\t catalog %s \n'%catalog
192     print "\t self.params['protocol'] %s \n"%self.params['protocol']
193     print '\t tfc %s '%tfc
194     print "\t self.params['inputFilesList'] %s \n"%self.params['inputFilesList']
195    
196     file_backup=[]
197     for input in self.params['inputFilesList']:
198     file = self.params['lfn'] + os.path.basename(input)
199     surl = tfc.matchLFN(tfc.preferredProtocol, file)
200     file_backup.append(surl)
201     if self.debug:
202     print '\t lfn %s \n'%self.params['lfn']
203     print '\t file %s \n'%file
204     print '\t surl %s \n'%surl
205    
206     destination=os.path.dirname(file_backup[0])
207     self.params['destination']=destination
208    
209     if self.debug:
210     print "\t self.params['destination']%s \n"%self.params['destination']
211     print "\t self.params['protocol'] %s \n"%self.params['protocol']
212     print "\t self.params['option']%s \n"%self.params['option']
213    
214     for prot, opt in self.setProtocol( self.params['middleware'] ):
215     if self.debug: print '\tIn backup trying the stage out with %s utils \n'%prot
216     backup_results = self.copy( self.params['inputFileList'], prot, opt )
217     if backup_results.keys() == [''] or backup_results.keys() == '' :
218     results.update(backup_results)
219     else:
220     backup_results, list_ok, list_retry = self.checkCopy(backup_results, len(list_files), prot, self.params['lfn'], seName)
221     results.update(backup_results)
222     if len(list_ok) == len(list_files) :
223     break
224     if len(list_retry):
225     list_files = list_retry
226     else: break
227     if self.debug:
228     print "\t backup_results = %s \n"%backup_results
229 spiga 1.28
230 fanzago 1.41 return results
231    
232 spiga 1.8 def stager( self, middleware, list_files ):
233 spiga 1.1 """
234     Implement the logic for remote stage out
235     """
236 spiga 1.30
237 fanzago 1.38 if self.debug:
238     print 'stager() :\n'
239     print '\tmiddleware %s\n'%middleware
240     print '\list_files %s\n'%list_files
241    
242 spiga 1.6 results={}
243 spiga 1.8 for prot, opt in self.setProtocol( middleware ):
244 spiga 1.30 if self.debug: print '\tTrying the stage out with %s utils \n'%prot
245 spiga 1.8 copy_results = self.copy( list_files, prot, opt )
246 spiga 1.30 if copy_results.keys() == [''] or copy_results.keys() == '' :
247 spiga 1.13 results.update(copy_results)
248     else:
249 fanzago 1.41 copy_results, list_ok, list_retry = self.checkCopy(copy_results, len(list_files), prot)
250 spiga 1.13 results.update(copy_results)
251     if len(list_ok) == len(list_files) :
252     break
253 fanzago 1.41 if len(list_retry):
254     list_files = list_retry
255     else: break
256    
257 fanzago 1.38 if len(list_retry):
258 fanzago 1.41 results = self.backupCopy(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 spiga 1.13 except Exception, ex:
302     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     print dbsmsg
484 spiga 1.13 ErCode = '60307'
485 spiga 1.24 if ErCode == '0' and protocol.find('srmv') == 0:
486 spiga 1.19 remote_file_size = -1
487     local_file_size = os.path.getsize( source_file )
488     try:
489 spiga 1.34 remote_file_size = sbi_dest.getSize( dest_file, opt=option )
490 spiga 1.30 if self.debug : print '\t Check of remote size succeded for file %s\n'%dest_file
491 spiga 1.19 except TransferException, ex:
492 spiga 1.30 msg = "Problem checking the size of %s file" % filetocopy
493     msg += str(ex)
494 spiga 1.19 if self.debug :
495 spiga 1.30 dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n'
496     dbgmsg += '\t'+str(ex.output)+'\n'
497     print dbgmsg
498 spiga 1.19 ErCode = '60307'
499     except WrongOption, ex:
500 spiga 1.30 msg = "Problem checking the size of %s file" % filetocopy
501     msg += str(ex)
502 spiga 1.19 if self.debug :
503 spiga 1.30 dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n'
504     dbgmsg += '\t'+str(ex.output)+'\n'
505     print dbgmsg
506 spiga 1.19 ErCode = '60307'
507     if local_file_size != remote_file_size:
508     msg = "File size dosn't match: local size = %s ; remote size = %s " % (local_file_size, remote_file_size)
509     ErCode = '60307'
510 ewv 1.14
511 spiga 1.27 if ErCode != '0':
512     try :
513 spiga 1.34 self.removeFile( sbi_dest, dest_file, option )
514 spiga 1.27 except Exception, ex:
515     msg += '\n'+str(ex)
516 spiga 1.1 return ErCode, msg
517 ewv 1.7
518 spiga 1.34 def removeFile( self, sbi_dest, filetocopy, option ):
519 spiga 1.30 """
520     """
521     if self.debug : print 'removeFile():\n'
522 spiga 1.21 f_tocopy=filetocopy
523     if self.dest_prot != 'local':f_tocopy = os.path.basename(filetocopy)
524     try:
525 spiga 1.34 sbi_dest.delete( f_tocopy, opt=option )
526 spiga 1.30 if self.debug : '\t deletion of file %s succeeded\n'%str(filetocopy)
527 spiga 1.21 except OperationException, ex:
528 spiga 1.30 msg ='ERROR: problems removing partially staged file %s'%filetocopy
529     msg += str(ex)
530 spiga 1.21 if self.debug :
531 spiga 1.30 dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n'
532     dbgmsg += '\t'+str(ex.output)+'\n'
533     print dbgmsg
534 spiga 1.21 raise Exception(msg)
535    
536     return
537    
538 ewv 1.7 def backup(self):
539 spiga 1.1 """
540     Check infos from TFC using existing api obtaining:
541     1)destination
542     2)protocol
543     """
544     return
545    
546 spiga 1.8 def updateReport(self, file, erCode, reason, lfn='', se='' ):
547     """
548     Update the final stage out infos
549     """
550     jobStageInfo={}
551     jobStageInfo['erCode']=erCode
552     jobStageInfo['reason']=reason
553     jobStageInfo['lfn']=lfn
554     jobStageInfo['se']=se
555 spiga 1.1
556 spiga 1.8 report = { file : jobStageInfo}
557     return report
558 ewv 1.7
559 spiga 1.8 def finalReport( self , results ):
560     """
561     It a list of LFNs for each SE where data are stored.
562     allow "crab -copyLocal" or better "crab -copyOutput". TO_DO.
563 spiga 1.1 """
564 spiga 1.8 outFile = open('cmscpReport.sh',"a")
565     cmscp_exit_status = 0
566     txt = ''
567 ewv 1.14 for file, dict in results.iteritems():
568     if file:
569 spiga 1.11 if dict['lfn']=='':
570     lfn = '$LFNBaseName/'+os.path.basename(file)
571     se = '$SE'
572     else:
573 mcinquil 1.16 lfn = dict['lfn']+os.path.basename(file)
574 spiga 1.11 se = dict['se']
575 fanzago 1.41
576 spiga 1.11 #dict['lfn'] # to be implemented
577 fanzago 1.39 txt += 'echo "Report for File: '+file+'"\n'
578     txt += 'echo "LFN: '+lfn+'"\n'
579     txt += 'echo "StorageElement: '+se+'"\n'
580 spiga 1.11 txt += 'echo "StageOutExitStatusReason ='+dict['reason']+'" | tee -a $RUNTIME_AREA/$repo\n'
581     txt += 'echo "StageOutSE = '+se+'" >> $RUNTIME_AREA/$repo\n'
582 fanzago 1.39 txt += 'export LFNBaseName='+lfn+'\n'
583     txt += 'export SE='+se+'\n'
584 fanzago 1.41
585 spiga 1.11 if dict['erCode'] != '0':
586     cmscp_exit_status = dict['erCode']
587 spiga 1.12 else:
588 spiga 1.30 txt += 'echo "StageOutExitStatusReason ='+dict['reason']+'" | tee -a $RUNTIME_AREA/$repo\n'
589 spiga 1.12 cmscp_exit_status = dict['erCode']
590     cmscp_exit_status = dict['erCode']
591 spiga 1.8 txt += '\n'
592     txt += 'export StageOutExitStatus='+str(cmscp_exit_status)+'\n'
593     txt += 'echo "StageOutExitStatus = '+str(cmscp_exit_status)+'" | tee -a $RUNTIME_AREA/$repo\n'
594     outFile.write(str(txt))
595     outFile.close()
596     return
597    
598    
599     def usage():
600    
601     msg="""
602     required parameters:
603     --source :: REMOTE :
604     --destination :: REMOTE :
605     --debug :
606     --inFile :: absPath : or name NOT RELATIVE PATH
607     --outFIle :: onlyNAME : NOT YET SUPPORTED
608    
609     optional parameters
610     """
611 ewv 1.14 print msg
612 spiga 1.8
613 ewv 1.14 return
614 spiga 1.8
615     def HelpOptions(opts=[]):
616     """
617     Check otps, print help if needed
618 ewv 1.14 prepare dict = { opt : value }
619 spiga 1.8 """
620     dict_args = {}
621     if len(opts):
622     for opt, arg in opts:
623 ewv 1.14 dict_args[opt.split('--')[1]] = arg
624 spiga 1.8 if opt in ('-h','-help','--help') :
625     usage()
626     sys.exit(0)
627     return dict_args
628     else:
629     usage()
630     sys.exit(0)
631 spiga 1.1
632     if __name__ == '__main__' :
633 spiga 1.8
634 ewv 1.14 import getopt
635 spiga 1.8
636     allowedOpt = ["source=", "destination=", "inputFileList=", "outputFileList=", \
637 spiga 1.25 "protocol=","option=", "middleware=", "srm_version=", \
638 fanzago 1.38 "destinationDir=","debug", "help", "lfn="]
639 ewv 1.14 try:
640     opts, args = getopt.getopt( sys.argv[1:], "", allowedOpt )
641 spiga 1.8 except getopt.GetoptError, err:
642     print err
643     HelpOptions()
644     sys.exit(2)
645 ewv 1.14
646 spiga 1.8 dictArgs = HelpOptions(opts)
647 spiga 1.1 try:
648 spiga 1.8 cmscp_ = cmscp(dictArgs)
649 spiga 1.1 cmscp_.run()
650 spiga 1.11 except Exception, ex :
651     print str(ex)
652 spiga 1.1