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