ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/cmscp.py
Revision: 1.39
Committed: Fri Feb 6 16:06:20 2009 UTC (16 years, 2 months ago) by fanzago
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_5_0_pre3, CRAB_2_5_0_pre2
Changes since 1.38: +5 -4 lines
Log Message:
update of SE and LFNBaseName value in case of backup copy

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     if middleware.lower() in ['osg','lcg','condor']:
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     # def checkCopy(self, copy_results, list_files):
116     """
117     #results={}
118     list_retry = []
119     list_existing = []
120     list_ok = []
121     if copy_results.keys() == '':
122     self.results.update(copy_results)
123     else:
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     msg = 'Copy of all files succedeed\n'
139     #break
140     else:
141     if self.debug : print 'Copy of files %s failed using %s...\n'%(str(list_retry)+str(list_existing),prot)
142     #if len(list_retry): list_files = list_retry
143     return list_retry, results
144 fanzago 1.38 """
145 spiga 1.28
146 spiga 1.8 def stager( self, middleware, list_files ):
147 spiga 1.1 """
148     Implement the logic for remote stage out
149     """
150 spiga 1.30
151 fanzago 1.38 if self.debug:
152     print 'stager() :\n'
153     print '\tmiddleware %s\n'%middleware
154     print '\list_files %s\n'%list_files
155    
156 spiga 1.6 results={}
157 spiga 1.8 for prot, opt in self.setProtocol( middleware ):
158 spiga 1.30 if self.debug: print '\tTrying the stage out with %s utils \n'%prot
159 spiga 1.8 copy_results = self.copy( list_files, prot, opt )
160 fanzago 1.26 ######## to define a new function checkCopy ################
161     #list_retry, self.results = self.checkCopy(copy_results, list_files)
162     #def checkCopy (self, copy_results):
163     # """
164     # """
165     # results={}
166 ewv 1.7 list_retry = []
167     list_existing = []
168     list_ok = []
169 spiga 1.30 if copy_results.keys() == [''] or copy_results.keys() == '' :
170 spiga 1.13 results.update(copy_results)
171     else:
172     for file, dict in copy_results.iteritems():
173     er_code = dict['erCode']
174     if er_code == '0':
175     list_ok.append(file)
176     reason = 'Copy succedeed with %s utils'%prot
177     upDict = self.updateReport(file, er_code, reason)
178     copy_results.update(upDict)
179 fanzago 1.38 # Fede: we can do the backup copy also for already existing files..... ##
180     #elif er_code in ['60303','60302']: list_existing.append( file )
181     elif er_code == '60302': list_existing.append( file )
182     ####
183 spiga 1.13 else: list_retry.append( file )
184     results.update(copy_results)
185 spiga 1.30 msg = ''
186 spiga 1.13 if len(list_ok) != 0:
187 spiga 1.30 msg += '\tCopy of %s succedeed with %s utils\n'%(str(list_ok),prot)
188 spiga 1.13 if len(list_ok) == len(list_files) :
189     break
190 spiga 1.1 else:
191 spiga 1.30 if self.debug: msg += '\tCopy of %s failed using %s...\n'%(str(list_retry)+str(list_existing),prot)
192 fanzago 1.38 if len(list_retry):
193     list_files = list_retry
194 spiga 1.13 else: break
195 spiga 1.30 if self.debug : print msg
196 fanzago 1.38 ### this part is related to backup copy
197     ### it shoud became a separated function
198     ### work in progress
199    
200     if len(list_retry):
201     list_files = list_retry
202     if self.debug:
203     print '\t list_retry %s utils \n'%list_retry
204     print '\t len(list_retry) %s \n'%len(list_retry)
205    
206 spiga 1.27 #def backupCopy(list_retry)
207 fanzago 1.38 if self.debug: print 'in backup() :\n'
208    
209     self.params['inputFilesList']=list_files
210    
211     ### copy backup
212     from ProdCommon.FwkJobRep.SiteLocalConfig import loadSiteLocalConfig
213     siteCfg = loadSiteLocalConfig()
214     seName = siteCfg.localStageOut.get("se-name", None)
215     catalog = siteCfg.localStageOut.get("catalog", None)
216     implName = siteCfg.localStageOut.get("command", None)
217     if (implName == 'srm'):
218     implName='srmv1'
219     self.params['srm_version']=implName
220    
221    
222     ##### to change ########
223     if (implName == 'rfcp'):
224     self.params['middleware']='lsf'
225     #####
226    
227     self.params['protocol']=implName
228     tfc = siteCfg.trivialFileCatalog()
229    
230     if self.debug:
231     print '\t siteCFG %s \n'%siteCfg
232     print '\t seName %s \n'%seName
233     print '\t catalog %s \n'%catalog
234     print "\t self.params['protocol'] %s \n"%self.params['protocol']
235     print '\t tfc %s '%tfc
236     print "\t self.params['inputFilesList'] %s \n"%self.params['inputFilesList']
237    
238     file_backup=[]
239     for input in self.params['inputFilesList']:
240     file = self.params['lfn'] + os.path.basename(input)
241     surl = tfc.matchLFN(tfc.preferredProtocol, file)
242     file_backup.append(surl)
243     if self.debug:
244     print '\t lfn %s \n'%self.params['lfn']
245     print '\t file %s \n'%file
246     print '\t surl %s \n'%surl
247    
248     destination=os.path.dirname(file_backup[0])
249     self.params['destination']=destination
250    
251     if self.debug:
252     print "\t self.params['destination']%s \n"%self.params['destination']
253     print "\t self.params['protocol'] %s \n"%self.params['protocol']
254     print "\t self.params['option']%s \n"%self.params['option']
255    
256     for prot, opt in self.setProtocol( self.params['middleware'] ):
257     if self.debug: print '\tIn backup trying the stage out with %s utils \n'%prot
258     backup_results = self.copy( self.params['inputFileList'], prot, opt )
259     ######## to define a new function checkCopy ################
260     #list_retry, self.results = self.checkCopy(copy_results, list_files)
261     #def checkCopy (self, copy_results):
262     # results={}
263     list_retry = []
264     list_existing = []
265     list_ok = []
266     if backup_results.keys() == [''] or copy_results.keys() == '' :
267     results.update(backup_results)
268     else:
269     for file, dict in backup_results.iteritems():
270     er_code = dict['erCode']
271     if er_code == '0':
272     list_ok.append(file)
273     reason = 'Copy succedeed with %s utils'%prot
274     upDict = self.updateReport(file, er_code, reason, self.params['lfn'], seName)
275     backup_results.update(upDict)
276     if self.debug: print '\tIn backup backup_results %s \n'%backup_results
277     elif er_code in ['60303','60302']: list_existing.append( file )
278     else: list_retry.append( file )
279     results.update(backup_results)
280     msg = ''
281     if len(list_ok) != 0:
282     msg += '\tCopy of %s succedeed with %s utils\n'%(str(list_ok),prot)
283    
284     if len(list_ok) == len(list_files) :
285     msg += '\tCopy ok for all files\n'
286     break
287     else:
288     if self.debug: msg += '\tCopy of %s failed using %s...\n'%(str(list_retry)+str(list_existing),prot)
289     if self.debug : print msg
290     if self.debug:
291     print "\t results %s \n"%results
292 spiga 1.1 return results
293    
294     def initializeApi(self, protocol ):
295     """
296 ewv 1.7 Instantiate storage interface
297 spiga 1.1 """
298 spiga 1.30 if self.debug : print 'initializeApi() :\n'
299 spiga 1.20 self.source_prot = protocol
300     self.dest_prot = protocol
301     if not self.params['source'] : self.source_prot = 'local'
302     Source_SE = self.storageInterface( self.params['source'], self.source_prot )
303     if not self.params['destination'] : self.dest_prot = 'local'
304     Destination_SE = self.storageInterface( self.params['destination'], self.dest_prot )
305 spiga 1.1
306     if self.debug :
307 spiga 1.30 msg = '\t(source=%s, protocol=%s)'%(self.params['source'], self.source_prot)
308     msg += '\t(destination=%s, protocol=%s)'%(self.params['destination'], self.dest_prot)
309 mcinquil 1.32 print msg
310 spiga 1.1
311     return Source_SE, Destination_SE
312    
313 spiga 1.8 def copy( self, list_file, protocol, options ):
314 spiga 1.1 """
315 ewv 1.7 Make the real file copy using SE API
316 spiga 1.1 """
317 mcinquil 1.37 msg = ""
318 spiga 1.1 if self.debug :
319 spiga 1.30 msg = 'copy() :\n'
320     msg += '\tusing %s protocol\n'%protocol
321     print msg
322 spiga 1.13 try:
323     Source_SE, Destination_SE = self.initializeApi( protocol )
324     except Exception, ex:
325     return self.updateReport('', '-1', str(ex))
326 ewv 1.14
327 ewv 1.7 # create remote dir
328 mcinquil 1.32 if Destination_SE.protocol in ['gridftp','rfio','srmv2']:
329 spiga 1.13 try:
330 mcinquil 1.32 self.createDir( Destination_SE, Destination_SE.protocol )
331 spiga 1.13 except Exception, ex:
332     return self.updateReport('', '60316', str(ex))
333 spiga 1.1
334     ## prepare for real copy ##
335 spiga 1.8 try :
336     sbi = SBinterface( Source_SE, Destination_SE )
337     sbi_dest = SBinterface(Destination_SE)
338 spiga 1.20 sbi_source = SBinterface(Source_SE)
339 spiga 1.11 except ProtocolMismatch, ex:
340 spiga 1.30 msg = "ERROR : Unable to create SBinterface with %s protocol"%protocol
341     msg += str(ex)
342     return self.updateReport('', '-1', msg)
343 spiga 1.1
344     results = {}
345 ewv 1.7 ## loop over the complete list of files
346     for filetocopy in list_file:
347 spiga 1.30 if self.debug : print '\tStart real copy for %s'%filetocopy
348 spiga 1.13 try :
349 spiga 1.34 ErCode, msg = self.checkFileExist( sbi_source, sbi_dest, filetocopy, options )
350 spiga 1.13 except Exception, ex:
351 spiga 1.18 ErCode = -1
352     msg = str(ex)
353 ewv 1.7 if ErCode == '0':
354 spiga 1.19 ErCode, msg = self.makeCopy( sbi, filetocopy , options, protocol,sbi_dest )
355 spiga 1.30 if self.debug : print '\tCopy results for %s is %s'%( os.path.basename(filetocopy), ErCode)
356 spiga 1.1 results.update( self.updateReport(filetocopy, ErCode, msg))
357     return results
358 ewv 1.7
359 spiga 1.1
360     def storageInterface( self, endpoint, protocol ):
361     """
362 ewv 1.7 Create the storage interface.
363 spiga 1.1 """
364 spiga 1.30 if self.debug : print 'storageInterface():\n'
365 spiga 1.1 try:
366     interface = SElement( FullPath(endpoint), protocol )
367 spiga 1.11 except ProtocolUnknown, ex:
368 spiga 1.30 msg = "ERROR : Unable to create interface with %s protocol"%protocol
369     msg += str(ex)
370 spiga 1.13 raise Exception(msg)
371 spiga 1.1
372     return interface
373    
374     def createDir(self, Destination_SE, protocol):
375     """
376 spiga 1.8 Create remote dir for gsiftp REALLY TEMPORARY
377 ewv 1.7 this should be transparent at SE API level.
378 spiga 1.1 """
379 spiga 1.30 if self.debug : print 'createDir():\n'
380 ewv 1.14 msg = ''
381 spiga 1.1 try:
382     action = SBinterface( Destination_SE )
383     action.createDir()
384 spiga 1.30 if self.debug: print "\tThe directory has been created using protocol %s"%protocol
385 spiga 1.11 except TransferException, ex:
386 spiga 1.30 msg = "ERROR: problem with the directory creation using %s protocol "%protocol
387     msg += str(ex)
388 spiga 1.11 if self.debug :
389 spiga 1.30 dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n'
390     dbgmsg += '\t'+str(ex.output)+'\n'
391     print dbgmsg
392 spiga 1.29 raise Exception(msg)
393 spiga 1.11 except OperationException, ex:
394 spiga 1.30 msg = "ERROR: problem with the directory creation using %s protocol "%protocol
395     msg += str(ex)
396     if self.debug : print '\t'+msg+'\n\t'+str(ex.detail)+'\n'
397 spiga 1.29 raise Exception(msg)
398 spiga 1.31 except MissingDestination, ex:
399     msg = "ERROR: problem with the directory creation using %s protocol "%protocol
400     msg += str(ex)
401     if self.debug : print '\t'+msg+'\n\t'+str(ex.detail)+'\n'
402     raise Exception(msg)
403     except AlreadyExistsException, ex:
404     if self.debug: print "\tThe directory already exist"
405     pass
406 spiga 1.13 return msg
407 spiga 1.1
408 spiga 1.34 def checkFileExist( self, sbi_source, sbi_dest, filetocopy, option ):
409 spiga 1.1 """
410 spiga 1.20 Check both if source file exist AND
411     if destination file ALREADY exist.
412 ewv 1.7 """
413 spiga 1.30 if self.debug : print 'checkFileExist():\n'
414 spiga 1.8 ErCode = '0'
415     msg = ''
416 spiga 1.20 f_tocopy=filetocopy
417     if self.source_prot != 'local':f_tocopy = os.path.basename(filetocopy)
418 spiga 1.1 try:
419 spiga 1.34 checkSource = sbi_source.checkExists( f_tocopy , opt=option )
420 spiga 1.30 if self.debug : print '\tCheck for local file %s exist succeded \n'%f_tocopy
421 spiga 1.20 except OperationException, ex:
422 spiga 1.30 msg ='ERROR: problems checkig if source file %s exist'%filetocopy
423     msg += str(ex)
424 spiga 1.20 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.20 raise Exception(msg)
429     except WrongOption, ex:
430 spiga 1.30 msg ='ERROR problems checkig if source file % exist'%filetocopy
431     msg += str(ex)
432 spiga 1.20 if self.debug :
433 spiga 1.30 dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n'
434     dbgmsg += '\t'+str(ex.output)+'\n'
435     print dbgmsg
436 spiga 1.20 raise Exception(msg)
437 spiga 1.31 except MissingDestination, ex:
438     msg ='ERROR problems checkig if source file % exist'%filetocopy
439     msg += str(ex)
440     if self.debug : print '\t'+msg+'\n\t'+str(ex.detail)+'\n'
441     raise Exception(msg)
442 spiga 1.20 if not checkSource :
443     ErCode = '60302'
444     msg = "ERROR file %s do not exist"%os.path.basename(filetocopy)
445     return ErCode, msg
446     f_tocopy=filetocopy
447     if self.dest_prot != 'local':f_tocopy = os.path.basename(filetocopy)
448     try:
449 spiga 1.34 check = sbi_dest.checkExists( f_tocopy, opt=option )
450 spiga 1.30 if self.debug : print '\tCheck for remote file %s exist succeded \n'%f_tocopy
451 spiga 1.11 except OperationException, ex:
452 spiga 1.30 msg = 'ERROR: problems checkig if file %s already exist'%filetocopy
453     msg += str(ex)
454 spiga 1.11 if self.debug :
455 spiga 1.30 dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n'
456     dbgmsg += '\t'+str(ex.output)+'\n'
457     print dbgmsg
458 spiga 1.13 raise Exception(msg)
459 spiga 1.11 except WrongOption, ex:
460 spiga 1.30 msg = 'ERROR problems checkig if file % already exist'%filetocopy
461     msg += str(ex)
462 spiga 1.11 if self.debug :
463 spiga 1.30 msg += '\t'+msg+'\n\t'+str(ex.detail)+'\n'
464     msg += '\t'+str(ex.output)+'\n'
465 spiga 1.13 raise Exception(msg)
466 spiga 1.31 except MissingDestination, ex:
467     msg ='ERROR problems checkig if source file % exist'%filetocopy
468     msg += str(ex)
469     if self.debug : print '\t'+msg+'\n\t'+str(ex.detail)+'\n'
470     raise Exception(msg)
471 ewv 1.14 if check :
472 ewv 1.7 ErCode = '60303'
473 spiga 1.20 msg = "file %s already exist"%os.path.basename(filetocopy)
474 ewv 1.14
475 spiga 1.13 return ErCode, msg
476 spiga 1.1
477 spiga 1.19 def makeCopy(self, sbi, filetocopy, option, protocol, sbi_dest ):
478 spiga 1.1 """
479 ewv 1.7 call the copy API.
480 spiga 1.1 """
481 spiga 1.30 if self.debug : print 'makeCopy():\n'
482 ewv 1.7 path = os.path.dirname(filetocopy)
483 spiga 1.1 file_name = os.path.basename(filetocopy)
484     source_file = filetocopy
485     dest_file = file_name ## to be improved supporting changing file name TODO
486 spiga 1.8 if self.params['source'] == '' and path == '':
487 spiga 1.1 source_file = os.path.abspath(filetocopy)
488 spiga 1.8 elif self.params['destination'] =='':
489 spiga 1.24 destDir = self.params.get('destinationDir',os.getcwd())
490     dest_file = os.path.join(destDir,file_name)
491 spiga 1.8 elif self.params['source'] != '' and self.params['destination'] != '' :
492 ewv 1.7 source_file = file_name
493 spiga 1.8
494 spiga 1.1 ErCode = '0'
495     msg = ''
496 ewv 1.7
497 spiga 1.1 try:
498 spiga 1.8 sbi.copy( source_file , dest_file , opt = option)
499 spiga 1.11 except TransferException, ex:
500 spiga 1.30 msg = "Problem copying %s file" % filetocopy
501     msg += str(ex)
502 spiga 1.11 if self.debug :
503 spiga 1.30 dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n'
504     dbgmsg += '\t'+str(ex.output)+'\n'
505 mcinquil 1.32 print dbgmsg
506 spiga 1.1 ErCode = '60307'
507 spiga 1.11 except WrongOption, ex:
508 spiga 1.30 msg = "Problem copying %s file" % filetocopy
509     msg += str(ex)
510 spiga 1.11 if self.debug :
511 spiga 1.30 dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n'
512     dbgmsg += '\t'+str(ex.output)+'\n'
513     print dbsmsg
514 spiga 1.13 ErCode = '60307'
515 spiga 1.24 if ErCode == '0' and protocol.find('srmv') == 0:
516 spiga 1.19 remote_file_size = -1
517     local_file_size = os.path.getsize( source_file )
518     try:
519 spiga 1.34 remote_file_size = sbi_dest.getSize( dest_file, opt=option )
520 spiga 1.30 if self.debug : print '\t Check of remote size succeded for file %s\n'%dest_file
521 spiga 1.19 except TransferException, ex:
522 spiga 1.30 msg = "Problem checking the size of %s file" % filetocopy
523     msg += str(ex)
524 spiga 1.19 if self.debug :
525 spiga 1.30 dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n'
526     dbgmsg += '\t'+str(ex.output)+'\n'
527     print dbgmsg
528 spiga 1.19 ErCode = '60307'
529     except WrongOption, ex:
530 spiga 1.30 msg = "Problem checking the size of %s file" % filetocopy
531     msg += str(ex)
532 spiga 1.19 if self.debug :
533 spiga 1.30 dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n'
534     dbgmsg += '\t'+str(ex.output)+'\n'
535     print dbgmsg
536 spiga 1.19 ErCode = '60307'
537     if local_file_size != remote_file_size:
538     msg = "File size dosn't match: local size = %s ; remote size = %s " % (local_file_size, remote_file_size)
539     ErCode = '60307'
540 ewv 1.14
541 spiga 1.27 if ErCode != '0':
542     try :
543 spiga 1.34 self.removeFile( sbi_dest, dest_file, option )
544 spiga 1.27 except Exception, ex:
545     msg += '\n'+str(ex)
546 spiga 1.1 return ErCode, msg
547 ewv 1.7
548 spiga 1.34 def removeFile( self, sbi_dest, filetocopy, option ):
549 spiga 1.30 """
550     """
551     if self.debug : print 'removeFile():\n'
552 spiga 1.21 f_tocopy=filetocopy
553     if self.dest_prot != 'local':f_tocopy = os.path.basename(filetocopy)
554     try:
555 spiga 1.34 sbi_dest.delete( f_tocopy, opt=option )
556 spiga 1.30 if self.debug : '\t deletion of file %s succeeded\n'%str(filetocopy)
557 spiga 1.21 except OperationException, ex:
558 spiga 1.30 msg ='ERROR: problems removing partially staged file %s'%filetocopy
559     msg += str(ex)
560 spiga 1.21 if self.debug :
561 spiga 1.30 dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n'
562     dbgmsg += '\t'+str(ex.output)+'\n'
563     print dbgmsg
564 spiga 1.21 raise Exception(msg)
565    
566     return
567    
568 ewv 1.7 def backup(self):
569 spiga 1.1 """
570     Check infos from TFC using existing api obtaining:
571     1)destination
572     2)protocol
573     """
574     return
575    
576 spiga 1.8 def updateReport(self, file, erCode, reason, lfn='', se='' ):
577     """
578     Update the final stage out infos
579     """
580     jobStageInfo={}
581     jobStageInfo['erCode']=erCode
582     jobStageInfo['reason']=reason
583     jobStageInfo['lfn']=lfn
584     jobStageInfo['se']=se
585 spiga 1.1
586 spiga 1.8 report = { file : jobStageInfo}
587     return report
588 ewv 1.7
589 spiga 1.8 def finalReport( self , results ):
590     """
591     It a list of LFNs for each SE where data are stored.
592     allow "crab -copyLocal" or better "crab -copyOutput". TO_DO.
593 spiga 1.1 """
594 spiga 1.8 outFile = open('cmscpReport.sh',"a")
595     cmscp_exit_status = 0
596     txt = ''
597 ewv 1.14 for file, dict in results.iteritems():
598     if file:
599 spiga 1.11 if dict['lfn']=='':
600     lfn = '$LFNBaseName/'+os.path.basename(file)
601     se = '$SE'
602     else:
603 mcinquil 1.16 lfn = dict['lfn']+os.path.basename(file)
604 spiga 1.11 se = dict['se']
605     #dict['lfn'] # to be implemented
606 fanzago 1.39 txt += 'echo "Report for File: '+file+'"\n'
607     txt += 'echo "LFN: '+lfn+'"\n'
608     txt += 'echo "StorageElement: '+se+'"\n'
609 spiga 1.11 txt += 'echo "StageOutExitStatusReason ='+dict['reason']+'" | tee -a $RUNTIME_AREA/$repo\n'
610     txt += 'echo "StageOutSE = '+se+'" >> $RUNTIME_AREA/$repo\n'
611 fanzago 1.39 txt += 'export LFNBaseName='+lfn+'\n'
612     txt += 'export SE='+se+'\n'
613 spiga 1.11 if dict['erCode'] != '0':
614     cmscp_exit_status = dict['erCode']
615 spiga 1.12 else:
616 spiga 1.30 txt += 'echo "StageOutExitStatusReason ='+dict['reason']+'" | tee -a $RUNTIME_AREA/$repo\n'
617 spiga 1.12 cmscp_exit_status = dict['erCode']
618     cmscp_exit_status = dict['erCode']
619 spiga 1.8 txt += '\n'
620     txt += 'export StageOutExitStatus='+str(cmscp_exit_status)+'\n'
621     txt += 'echo "StageOutExitStatus = '+str(cmscp_exit_status)+'" | tee -a $RUNTIME_AREA/$repo\n'
622     outFile.write(str(txt))
623     outFile.close()
624     return
625    
626    
627     def usage():
628    
629     msg="""
630     required parameters:
631     --source :: REMOTE :
632     --destination :: REMOTE :
633     --debug :
634     --inFile :: absPath : or name NOT RELATIVE PATH
635     --outFIle :: onlyNAME : NOT YET SUPPORTED
636    
637     optional parameters
638     """
639 ewv 1.14 print msg
640 spiga 1.8
641 ewv 1.14 return
642 spiga 1.8
643     def HelpOptions(opts=[]):
644     """
645     Check otps, print help if needed
646 ewv 1.14 prepare dict = { opt : value }
647 spiga 1.8 """
648     dict_args = {}
649     if len(opts):
650     for opt, arg in opts:
651 ewv 1.14 dict_args[opt.split('--')[1]] = arg
652 spiga 1.8 if opt in ('-h','-help','--help') :
653     usage()
654     sys.exit(0)
655     return dict_args
656     else:
657     usage()
658     sys.exit(0)
659 spiga 1.1
660     if __name__ == '__main__' :
661 spiga 1.8
662 ewv 1.14 import getopt
663 spiga 1.8
664     allowedOpt = ["source=", "destination=", "inputFileList=", "outputFileList=", \
665 spiga 1.25 "protocol=","option=", "middleware=", "srm_version=", \
666 fanzago 1.38 "destinationDir=","debug", "help", "lfn="]
667 ewv 1.14 try:
668     opts, args = getopt.getopt( sys.argv[1:], "", allowedOpt )
669 spiga 1.8 except getopt.GetoptError, err:
670     print err
671     HelpOptions()
672     sys.exit(2)
673 ewv 1.14
674 spiga 1.8 dictArgs = HelpOptions(opts)
675 spiga 1.1 try:
676 spiga 1.8 cmscp_ = cmscp(dictArgs)
677 spiga 1.1 cmscp_.run()
678 spiga 1.11 except Exception, ex :
679     print str(ex)
680 spiga 1.1