ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/cmscp.py
Revision: 1.32
Committed: Mon Dec 8 15:40:47 2008 UTC (16 years, 4 months ago) by mcinquil
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_4_3, CRAB_2_4_3_pre8
Changes since 1.31: +5 -4 lines
Log Message:
Now 'copyData' command works fine even for data copied to T2_CH_CAF
Passing different options to different schedulers when copying data
Some typo fixed in cmscp.py and CopyData.py

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