1 |
|
#!/usr/bin/env python |
2 |
< |
|
3 |
< |
import sys, getopt, string |
4 |
< |
import os, popen2 |
5 |
< |
from ProdCommon.Storage.SEAPI.SElement import SElement, FullPath |
2 |
> |
import sys, os |
3 |
> |
try: |
4 |
> |
import json |
5 |
> |
except: |
6 |
> |
import simplejson as json |
7 |
> |
from ProdCommon.Storage.SEAPI.SElement import SElement, FullPath |
8 |
|
from ProdCommon.Storage.SEAPI.SBinterface import * |
9 |
< |
|
9 |
> |
from ProdCommon.Storage.SEAPI.Exceptions import * |
10 |
|
|
11 |
|
|
12 |
|
class cmscp: |
13 |
< |
def __init__(self, argv): |
13 |
> |
def __init__(self, args): |
14 |
|
""" |
15 |
|
cmscp |
16 |
< |
|
15 |
< |
safe copy of local file in current directory to remote SE via lcg_cp/srmcp, |
16 |
> |
safe copy of local file to/from remote SE via lcg_cp/srmcp, |
17 |
|
including success checking version also for CAF using rfcp command to copy the output to SE |
18 |
|
input: |
19 |
|
$1 middleware (CAF, LSF, LCG, OSG) |
20 |
|
$2 local file (the absolute path of output file or just the name if it's in top dir) |
21 |
|
$3 if needed: file name (the output file name) |
22 |
|
$5 remote SE (complete endpoint) |
23 |
< |
$6 srm version |
23 |
> |
$6 srm version |
24 |
> |
--for_lfn $LFNBaseName |
25 |
|
output: |
26 |
|
return 0 if all ok |
27 |
|
return 60307 if srmcp failed |
28 |
|
return 60303 if file already exists in the SE |
29 |
|
""" |
30 |
< |
#set default |
30 |
> |
self.params = {"source":'', "destination":'','destinationDir':'', "inputFileList":'', "outputFileList":'', \ |
31 |
> |
"protocol":'', "option":'', "middleware":'', "srm_version":'srmv2', "for_lfn":'', "se_name":'' } |
32 |
|
self.debug = 0 |
33 |
< |
self.source = '' |
34 |
< |
self.destination = '' |
35 |
< |
self.file_to_copy = [] |
36 |
< |
self.remote_file_name = [] |
37 |
< |
self.protocol = '' |
38 |
< |
self.middleware = '' |
39 |
< |
self.srmv = '' |
40 |
< |
self.lcgOpt='-b -D srmv2 --vo cms -t 2400 --verbose' |
41 |
< |
self.opt='' |
42 |
< |
try: |
43 |
< |
opts, args = getopt.getopt(argv, "", ["source=", "destination=", "inputFileList=", "outputFileList=", \ |
44 |
< |
"protocol=", "middleware=", "srm_version=", "debug", "help"]) |
45 |
< |
except getopt.GetoptError: |
43 |
< |
print self.usage() |
44 |
< |
sys.exit(2) |
45 |
< |
|
46 |
< |
self.setAndCheck(opts) |
47 |
< |
|
33 |
> |
#### for fallback copy |
34 |
> |
self.local_stage = 0 |
35 |
> |
self.params.update( args ) |
36 |
> |
## timeout needed for subprocess command of SEAPI |
37 |
> |
## they should be a bit higher then the corresponding passed by command line |
38 |
> |
## default values |
39 |
> |
self.subprocesstimeout = { \ |
40 |
> |
'copy': 3600, \ |
41 |
> |
'exists': 1200, \ |
42 |
> |
'delete': 1200, \ |
43 |
> |
'size': 1200 \ |
44 |
> |
} |
45 |
> |
|
46 |
|
return |
47 |
|
|
48 |
< |
def setAndCheck( self, opts ): |
48 |
> |
def processOptions( self ): |
49 |
|
""" |
50 |
< |
Set and check command line parameter |
50 |
> |
check command line parameter |
51 |
|
""" |
52 |
< |
if not opts : |
53 |
< |
print self.usage() |
54 |
< |
sys.exit() |
55 |
< |
for opt, arg in opts : |
56 |
< |
if opt == "--help" : |
57 |
< |
print self.usage() |
58 |
< |
sys.exit() |
59 |
< |
elif opt == "--debug" : |
60 |
< |
self.debug = 1 |
61 |
< |
elif opt == "--source" : |
62 |
< |
self.source = arg |
63 |
< |
elif opt == "--destination": |
64 |
< |
self.destination = arg |
65 |
< |
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() |
52 |
> |
if 'help' in self.params.keys(): HelpOptions() |
53 |
> |
if 'debug' in self.params.keys(): self.debug = 1 |
54 |
> |
if 'local_stage' in self.params.keys(): self.local_stage = 1 |
55 |
> |
|
56 |
> |
# source and dest cannot be undefined at same time |
57 |
> |
if not self.params['source'] and not self.params['destination'] : |
58 |
> |
HelpOptions() |
59 |
> |
# if middleware is not defined --> protocol cannot be empty |
60 |
> |
if not self.params['middleware'] and not self.params['protocol'] : |
61 |
> |
HelpOptions() |
62 |
> |
|
63 |
> |
# input file must be defined |
64 |
> |
if not self.params['inputFileList'] : |
65 |
> |
HelpOptions() |
66 |
|
else: |
67 |
< |
if infile.find(','): |
68 |
< |
[self.file_to_copy.append(x.strip()) for x in infile.split(',')] |
69 |
< |
else: |
70 |
< |
self.file_to_copy.append(infile) |
71 |
< |
|
67 |
> |
file_to_copy=[] |
68 |
> |
if self.params['inputFileList'].find(','): |
69 |
> |
[file_to_copy.append(x.strip()) for x in self.params['inputFileList'].split(',')] |
70 |
> |
else: |
71 |
> |
file_to_copy.append(self.params['inputFileList']) |
72 |
> |
self.params['inputFileList'] = file_to_copy |
73 |
> |
|
74 |
> |
#if not self.params['lfn'] and self.local_stage == 1 : HelpOptions() |
75 |
> |
if not self.params['for_lfn'] and self.local_stage == 1 : HelpOptions() |
76 |
> |
|
77 |
|
## TO DO: |
78 |
|
#### add check for outFiles |
79 |
|
#### add map {'inFileNAME':'outFileNAME'} to change out name |
80 |
|
|
100 |
– |
return |
81 |
|
|
82 |
< |
def run( self ): |
82 |
> |
def run( self ): |
83 |
|
""" |
84 |
< |
Check if running on UI (no $middleware) or |
85 |
< |
on WN (on the Grid), and take different action |
84 |
> |
Check if running on UI (no $middleware) or |
85 |
> |
on WN (on the Grid), and take different action |
86 |
|
""" |
87 |
< |
if self.middleware : |
88 |
< |
results = self.stager() |
87 |
> |
self.processOptions() |
88 |
> |
if self.debug: print 'calling run() : \n' |
89 |
> |
# stage out from WN |
90 |
> |
if self.params['middleware'] : |
91 |
> |
results = self.stager(self.params['middleware'],self.params['inputFileList']) |
92 |
> |
self.writeJsonFile(results) |
93 |
> |
self.finalReport(results) |
94 |
> |
# Local interaction with SE |
95 |
|
else: |
96 |
< |
results = self.copy( self.file_to_copy, self.protocol , self.opt) |
96 |
> |
results = self.copy(self.params['inputFileList'], self.params['protocol'], self.params['option'] ) |
97 |
> |
self.writeJsonFile(results) |
98 |
> |
return results |
99 |
> |
|
100 |
> |
def writeJsonFile( self, results ): |
101 |
> |
""" |
102 |
> |
write a json file containing copy results for each file |
103 |
> |
""" |
104 |
> |
if self.debug: |
105 |
> |
print 'in writeJsonFile() : \n' |
106 |
> |
print "---->>>> in writeJsonFile results = ", results |
107 |
> |
fp = open('resultCopyFile', 'w') |
108 |
> |
json.dump(results, fp) |
109 |
> |
fp.close() |
110 |
> |
if self.debug: |
111 |
> |
print ' reading resultCopyFile : \n' |
112 |
> |
lp = open('resultCopyFile', "r") |
113 |
> |
inputDict = json.load(lp) |
114 |
> |
lp.close() |
115 |
> |
print " inputDict = ", inputDict |
116 |
> |
return |
117 |
|
|
118 |
< |
self.finalReport(results,self.middleware) |
118 |
> |
def checkLcgUtils( self ): |
119 |
> |
""" |
120 |
> |
_checkLcgUtils_ |
121 |
> |
check the lcg-utils version and report |
122 |
> |
""" |
123 |
> |
import commands |
124 |
> |
cmd = "lcg-cp --version | grep lcg_util" |
125 |
> |
status, output = commands.getstatusoutput( cmd ) |
126 |
> |
num_ver = -1 |
127 |
> |
if output.find("not found") == -1 or status == 0: |
128 |
> |
temp = output.split("-") |
129 |
> |
version = "" |
130 |
> |
if len(temp) >= 2: |
131 |
> |
version = output.split("-")[1] |
132 |
> |
temp = version.split(".") |
133 |
> |
if len(temp) >= 1: |
134 |
> |
num_ver = int(temp[0])*10 |
135 |
> |
num_ver += int(temp[1]) |
136 |
> |
return num_ver |
137 |
|
|
138 |
< |
return |
115 |
< |
|
116 |
< |
def setProtocol( self ): |
138 |
> |
def setProtocol( self, middleware ): |
139 |
|
""" |
140 |
|
define the allowed potocols based on $middlware |
141 |
< |
which depend on scheduler |
141 |
> |
which depend on scheduler |
142 |
|
""" |
143 |
< |
if self.middleware.lower() in ['osg','lcg']: |
144 |
< |
supported_protocol = {'srm-lcg': self.lcgOpt }#, |
145 |
< |
# 'srmv2' : '' } |
146 |
< |
elif self.middleware.lower() in ['lsf','caf']: |
147 |
< |
supported_protocol = {'rfio': '' } |
143 |
> |
# default To be used with "middleware" |
144 |
> |
if self.debug: |
145 |
> |
print 'setProtocol() :\n' |
146 |
> |
print '\tmiddleware = %s utils \n'%middleware |
147 |
> |
|
148 |
> |
lcgOpt={'srmv1':'-b -D srmv1 -t 2400 --verbose', |
149 |
> |
'srmv2':'-b -D srmv2 -t 2400 --verbose'} |
150 |
> |
if self.checkLcgUtils() >= 17: |
151 |
> |
lcgOpt={'srmv1':'-b -D srmv1 --srm-timeout 2400 --sendreceive-timeout 2400 --connect-timeout 300 --verbose', |
152 |
> |
'srmv2':'-b -D srmv2 --srm-timeout 2400 --sendreceive-timeout 2400 --connect-timeout 300 --verbose'} |
153 |
> |
|
154 |
> |
srmOpt={'srmv1':' -report ./srmcp.report -retry_timeout 480000 -retry_num 3 -streams_num=1 ', |
155 |
> |
'srmv2':' -report=./srmcp.report -retry_timeout=480000 -retry_num=3 -storagetype=permanent '} |
156 |
> |
rfioOpt='' |
157 |
> |
|
158 |
> |
supported_protocol = None |
159 |
> |
if middleware.lower() in ['osg','lcg','condor','sge']: |
160 |
> |
supported_protocol = [('srm-lcg',lcgOpt[self.params['srm_version']]),\ |
161 |
> |
(self.params['srm_version'],srmOpt[self.params['srm_version']])] |
162 |
> |
elif middleware.lower() in ['lsf','caf']: |
163 |
> |
supported_protocol = [('rfio',rfioOpt)] |
164 |
> |
elif middleware.lower() in ['pbs']: |
165 |
> |
supported_protocol = [('rfio',rfioOpt),('local','')] |
166 |
> |
elif middleware.lower() in ['arc']: |
167 |
> |
supported_protocol = [('srmv2','-debug'),('srmv1','-debug')] |
168 |
|
else: |
169 |
< |
## here we can add support for any kind of protocol, |
169 |
> |
## here we can add support for any kind of protocol, |
170 |
|
## maybe some local schedulers need something dedicated |
171 |
|
pass |
172 |
|
return supported_protocol |
173 |
|
|
174 |
< |
def stager( self ): |
174 |
> |
|
175 |
> |
def checkCopy (self, copy_results, len_list_files, prot): |
176 |
|
""" |
177 |
< |
Implement the logic for remote stage out |
177 |
> |
Checks the status of copy and update result dictionary |
178 |
|
""" |
179 |
< |
protocols = self.setProtocol() |
180 |
< |
count=0 |
181 |
< |
list_files = self.file_to_copy |
182 |
< |
results={} |
183 |
< |
for prot in protocols.keys(): |
184 |
< |
if self.debug: print 'Trying stage out with %s utils \n'%prot |
185 |
< |
copy_results = self.copy( list_files, prot, protocols[prot] ) |
186 |
< |
list_retry = [] |
187 |
< |
list_existing = [] |
188 |
< |
list_ok = [] |
189 |
< |
for file, dict in copy_results.iteritems(): |
190 |
< |
er_code = dict['erCode'] |
191 |
< |
if er_code == '60307': list_retry.append( file ) |
192 |
< |
elif er_code == '60303': list_existing.append( file ) |
193 |
< |
else: |
194 |
< |
list_ok.append(file) |
195 |
< |
reason = 'Copy succedeed with %s utils'%prot |
196 |
< |
upDict = self.updateReport(file, er_code, reason) |
197 |
< |
copy_results.update(upDict) |
198 |
< |
results.update(copy_results) |
199 |
< |
if len(list_ok) != 0: |
200 |
< |
msg = 'Copy of %s succedeed with %s utils\n'%(str(list_ok),prot) |
201 |
< |
# print msg |
202 |
< |
if len(list_ok) == len(list_files) : |
203 |
< |
break |
179 |
> |
|
180 |
> |
list_retry = [] |
181 |
> |
list_not_existing = [] |
182 |
> |
list_already_existing = [] |
183 |
> |
list_fallback = [] |
184 |
> |
list_ok = [] |
185 |
> |
|
186 |
> |
if self.debug: |
187 |
> |
print 'in checkCopy() :\n' |
188 |
> |
for file, dict in copy_results.iteritems(): |
189 |
> |
er_code = dict['erCode'] |
190 |
> |
if er_code == '0': |
191 |
> |
list_ok.append(file) |
192 |
> |
reason = 'Copy succedeed with %s utils'%prot |
193 |
> |
dict['reason'] = reason |
194 |
> |
elif er_code == '60308': |
195 |
> |
list_fallback.append( file ) |
196 |
> |
reason = 'Copy succedeed with %s utils'%prot |
197 |
> |
dict['reason'] = reason |
198 |
> |
elif er_code == '60302': |
199 |
> |
list_not_existing.append( file ) |
200 |
> |
elif er_code == '60303': |
201 |
> |
list_already_existing.append( file ) |
202 |
> |
else : |
203 |
> |
list_retry.append( file ) |
204 |
> |
|
205 |
> |
if self.debug: |
206 |
> |
print "\t file %s \n"%file |
207 |
> |
print "\t dict['erCode'] %s \n"%dict['erCode'] |
208 |
> |
print "\t dict['reason'] %s \n"%dict['reason'] |
209 |
> |
|
210 |
> |
upDict = self.updateReport(file, er_code, dict['reason']) |
211 |
> |
|
212 |
> |
copy_results.update(upDict) |
213 |
> |
|
214 |
> |
msg = '' |
215 |
> |
if len(list_ok) != 0: |
216 |
> |
msg += '\tCopy of %s succedeed with %s utils\n'%(str(list_ok),prot) |
217 |
> |
if len(list_ok) != len_list_files : |
218 |
> |
if len(list_fallback)!=0: |
219 |
> |
msg += '\tCopy of %s succedeed with %s utils in the fallback SE\n'%(str(list_fallback),prot) |
220 |
> |
if len(list_retry)!=0: |
221 |
> |
msg += '\tCopy of %s failed using %s for files \n'%(str(list_retry),prot) |
222 |
> |
if len(list_not_existing)!=0: |
223 |
> |
msg += '\tCopy of %s failed using %s : files not found \n'%(str(list_not_existing),prot) |
224 |
> |
if len(list_already_existing)!=0: |
225 |
> |
msg += '\tCopy of %s failed using %s : files already existing\n'%(str(list_already_existing),prot) |
226 |
> |
if self.debug : print msg |
227 |
> |
|
228 |
> |
return copy_results, list_ok, list_retry, list_fallback |
229 |
> |
|
230 |
> |
def check_for_retry_localSE (self, copy_results): |
231 |
> |
""" |
232 |
> |
Checks the status of copy and create the list of file to copy to CloseSE |
233 |
> |
""" |
234 |
> |
list_retry_localSE = [] |
235 |
> |
|
236 |
> |
if self.debug: |
237 |
> |
print 'in check_for_retry_localSE() :\n' |
238 |
> |
print "\t results in check local = ", copy_results |
239 |
> |
for file, dict in copy_results.iteritems(): |
240 |
> |
er_code = dict['erCode'] |
241 |
> |
if er_code != '0' and er_code != '60302' and er_code != '60308': |
242 |
> |
list_retry_localSE.append( file ) |
243 |
> |
|
244 |
> |
if self.debug: |
245 |
> |
print "\t file %s \n"%file |
246 |
> |
print "\t dict['erCode'] %s \n"%dict['erCode'] |
247 |
> |
print "\t dict['reason'] %s \n"%dict['reason'] |
248 |
> |
|
249 |
> |
return list_retry_localSE |
250 |
> |
|
251 |
> |
|
252 |
> |
def LocalCopy(self, list_retry, results): |
253 |
> |
""" |
254 |
> |
Tries the stage out to the CloseSE |
255 |
> |
""" |
256 |
> |
if self.debug: |
257 |
> |
print 'in LocalCopy() :\n' |
258 |
> |
print '\t list_retry %s utils \n'%list_retry |
259 |
> |
print '\t len(list_retry) %s \n'%len(list_retry) |
260 |
> |
|
261 |
> |
list_files = list_retry |
262 |
> |
self.params['inputFileList']=list_files |
263 |
> |
|
264 |
> |
### copy backup |
265 |
> |
from ProdCommon.FwkJobRep.SiteLocalConfig import loadSiteLocalConfig |
266 |
> |
siteCfg = loadSiteLocalConfig() |
267 |
> |
seName = siteCfg.localStageOut.get("se-name", None) |
268 |
> |
catalog = siteCfg.localStageOut.get("catalog", None) |
269 |
> |
implName = siteCfg.localStageOut.get("command", None) |
270 |
> |
if (implName == 'srm'): |
271 |
> |
implName='srmv1' |
272 |
> |
self.params['srm_version']=implName |
273 |
> |
##### to be improved ############### |
274 |
> |
if (implName == 'rfcp'): |
275 |
> |
self.params['middleware']='lsf' |
276 |
> |
#################################### |
277 |
> |
|
278 |
> |
self.params['protocol']=implName |
279 |
> |
tfc = siteCfg.trivialFileCatalog() |
280 |
> |
|
281 |
> |
if self.debug: |
282 |
> |
print '\t siteCFG %s \n'%siteCfg |
283 |
> |
print '\t seName %s \n'%seName |
284 |
> |
print '\t catalog %s \n'%catalog |
285 |
> |
print "\t self.params['protocol'] %s \n"%self.params['protocol'] |
286 |
> |
print '\t tfc %s '%tfc |
287 |
> |
print "\t self.params['inputFileList'] %s \n"%self.params['inputFileList'] |
288 |
> |
|
289 |
> |
if (str(self.params['for_lfn']).find("/store/") == 0): |
290 |
> |
temp = str(self.params['for_lfn']).replace("/store/","/store/temp/",1) |
291 |
> |
self.params['for_lfn']= temp |
292 |
> |
|
293 |
> |
if ( self.params['for_lfn'][-1] != '/' ) : self.params['for_lfn'] = self.params['for_lfn'] + '/' |
294 |
> |
|
295 |
> |
file_backup=[] |
296 |
> |
for input in self.params['inputFileList']: |
297 |
> |
file = self.params['for_lfn'] + os.path.basename(input) |
298 |
> |
surl = tfc.matchLFN(tfc.preferredProtocol, file) |
299 |
> |
file_backup.append(surl) |
300 |
> |
if self.debug: |
301 |
> |
print '\t for_lfn %s \n'%self.params['for_lfn'] |
302 |
> |
print '\t file %s \n'%file |
303 |
> |
print '\t surl %s \n'%surl |
304 |
> |
|
305 |
> |
destination=os.path.dirname(file_backup[0]) |
306 |
> |
if ( destination[-1] != '/' ) : destination = destination + '/' |
307 |
> |
|
308 |
> |
self.params['destination']=destination |
309 |
> |
|
310 |
> |
self.params['se_name']=seName |
311 |
> |
|
312 |
> |
if self.debug: |
313 |
> |
print "\t self.params['destination']%s \n"%self.params['destination'] |
314 |
> |
print "\t self.params['protocol'] %s \n"%self.params['protocol'] |
315 |
> |
print "\t self.params['option']%s \n"%self.params['option'] |
316 |
> |
|
317 |
> |
for prot, opt in self.setProtocol( self.params['middleware'] ): |
318 |
> |
if self.debug: print '\tIn LocalCopy trying the stage out with %s utils \n'%prot |
319 |
> |
localCopy_results = self.copy( self.params['inputFileList'], prot, opt, backup='yes' ) |
320 |
> |
if localCopy_results.keys() == [''] or localCopy_results.keys() == '' : |
321 |
> |
results.update(localCopy_results) |
322 |
|
else: |
323 |
< |
# print 'Copy of files %s failed using %s...\n'%(str(list_retry)+str(list_existing),prot) |
324 |
< |
if len(list_retry): list_files = list_retry |
325 |
< |
else: break |
326 |
< |
count =+1 |
323 |
> |
localCopy_results, list_ok, list_retry, list_fallback = self.checkCopy(localCopy_results, len(list_files), prot) |
324 |
> |
results.update(localCopy_results) |
325 |
> |
if len(list_fallback) == len(list_files) : |
326 |
> |
break |
327 |
> |
if len(list_retry): |
328 |
> |
list_files = list_retry |
329 |
> |
else: break |
330 |
> |
if self.debug: |
331 |
> |
print "\t localCopy_results = %s \n"%localCopy_results |
332 |
> |
|
333 |
> |
return results |
334 |
> |
|
335 |
> |
def stager( self, middleware, list_files ): |
336 |
> |
""" |
337 |
> |
Implement the logic for remote stage out |
338 |
> |
""" |
339 |
|
|
340 |
< |
#### TODO Daniele |
341 |
< |
#check is something fails and created related dict |
342 |
< |
# backup = self.analyzeResults(results) |
343 |
< |
|
344 |
< |
# if backup : |
345 |
< |
# msg = 'WARNING: backup logic is under implementation\n' |
346 |
< |
# #backupDict = self.backup() |
347 |
< |
# ### NOTE: IT MUST RETURN a DICT contains also LFN and SE Name |
348 |
< |
# results.update(backupDict) |
349 |
< |
# print msg |
340 |
> |
if self.debug: |
341 |
> |
print 'stager() :\n' |
342 |
> |
print '\tmiddleware %s\n'%middleware |
343 |
> |
print '\tlist_files %s\n'%list_files |
344 |
> |
|
345 |
> |
results={} |
346 |
> |
for prot, opt in self.setProtocol( middleware ): |
347 |
> |
if self.debug: |
348 |
> |
print '\tTrying the stage out with %s utils \n'%prot |
349 |
> |
print '\tand options %s\n'%opt |
350 |
> |
|
351 |
> |
copy_results = self.copy( list_files, prot, opt ) |
352 |
> |
if copy_results.keys() == [''] or copy_results.keys() == '' : |
353 |
> |
results.update(copy_results) |
354 |
> |
else: |
355 |
> |
copy_results, list_ok, list_retry, list_fallback = self.checkCopy(copy_results, len(list_files), prot) |
356 |
> |
results.update(copy_results) |
357 |
> |
if len(list_ok) == len(list_files) : |
358 |
> |
break |
359 |
> |
if len(list_retry): |
360 |
> |
list_files = list_retry |
361 |
> |
else: break |
362 |
> |
|
363 |
> |
if self.local_stage: |
364 |
> |
list_retry_localSE = self.check_for_retry_localSE(results) |
365 |
> |
if len(list_retry_localSE): |
366 |
> |
if self.debug: |
367 |
> |
print "\t list_retry_localSE %s \n"%list_retry_localSE |
368 |
> |
results = self.LocalCopy(list_retry_localSE, results) |
369 |
> |
|
370 |
> |
if self.debug: |
371 |
> |
print "\t results %s \n"%results |
372 |
|
return results |
373 |
|
|
374 |
|
def initializeApi(self, protocol ): |
375 |
|
""" |
376 |
< |
Instantiate storage interface |
376 |
> |
Instantiate storage interface |
377 |
|
""" |
378 |
< |
source_prot = protocol |
379 |
< |
dest_prot = protocol |
380 |
< |
if self.source == '' : source_prot = 'local' |
381 |
< |
Source_SE = self.storageInterface( self.source, source_prot ) |
382 |
< |
if self.destination == '' : dest_prot = 'local' |
383 |
< |
Destination_SE = self.storageInterface( self.destination, dest_prot ) |
378 |
> |
if self.debug : print 'initializeApi() :\n' |
379 |
> |
self.source_prot = protocol |
380 |
> |
self.dest_prot = protocol |
381 |
> |
if not self.params['source'] : self.source_prot = 'local' |
382 |
> |
Source_SE = self.storageInterface( self.params['source'], self.source_prot ) |
383 |
> |
if not self.params['destination'] : |
384 |
> |
self.dest_prot = 'local' |
385 |
> |
Destination_SE = self.storageInterface( self.params['destinationDir'], self.dest_prot ) |
386 |
> |
else: |
387 |
> |
Destination_SE = self.storageInterface( self.params['destination'], self.dest_prot ) |
388 |
|
|
389 |
|
if self.debug : |
390 |
< |
print '(source=%s, protocol=%s)'%(self.source, source_prot) |
391 |
< |
print '(destination=%s, protocol=%s)'%(self.destination, dest_prot) |
390 |
> |
msg = '\t(source=%s, protocol=%s)'%(self.params['source'], self.source_prot) |
391 |
> |
msg += '\t(destination=%s, protocol=%s)'%(self.params['destination'], self.dest_prot) |
392 |
> |
msg += '\t(destinationDir=%s, protocol=%s)'%(self.params['destinationDir'], self.dest_prot) |
393 |
> |
print msg |
394 |
|
|
395 |
|
return Source_SE, Destination_SE |
396 |
|
|
397 |
< |
def copy( self, list_file, protocol, opt): |
397 |
> |
def copy( self, list_file, protocol, options, backup='no' ): |
398 |
|
""" |
399 |
< |
Make the real file copy using SE API |
399 |
> |
Make the real file copy using SE API |
400 |
|
""" |
401 |
+ |
msg = "" |
402 |
+ |
results = {} |
403 |
|
if self.debug : |
404 |
< |
print 'copy(): using %s protocol'%protocol |
405 |
< |
Source_SE, Destination_SE = self.initializeApi( protocol ) |
406 |
< |
|
407 |
< |
# create remote dir |
408 |
< |
if protocol in ['gridftp','rfio']: |
409 |
< |
self.createDir( Destination_SE, protocol ) |
404 |
> |
msg = 'copy() :\n' |
405 |
> |
msg += '\tusing %s protocol\n'%protocol |
406 |
> |
print msg |
407 |
> |
try: |
408 |
> |
Source_SE, Destination_SE = self.initializeApi( protocol ) |
409 |
> |
except Exception, ex: |
410 |
> |
for filetocopy in list_file: |
411 |
> |
results.update( self.updateReport(filetocopy, '-1', str(ex))) |
412 |
> |
return results |
413 |
> |
|
414 |
> |
prot = Destination_SE.protocol |
415 |
> |
self.hostname=Destination_SE.hostname |
416 |
> |
|
417 |
> |
# create remote dir |
418 |
> |
if Destination_SE.protocol in ['gridftp','rfio','srmv2']: |
419 |
> |
try: |
420 |
> |
self.createDir( Destination_SE, Destination_SE.protocol ) |
421 |
> |
except OperationException, ex: |
422 |
> |
for filetocopy in list_file: |
423 |
> |
results.update( self.updateReport(filetocopy, '60316', str(ex))) |
424 |
> |
return results |
425 |
> |
## when the client commands are not found (wrong env or really missing) |
426 |
> |
except MissingCommand, ex: |
427 |
> |
msg = "ERROR %s %s" %(str(ex), str(ex.detail)) |
428 |
> |
for filetocopy in list_file: |
429 |
> |
results.update( self.updateReport(filetocopy, '10041', msg)) |
430 |
> |
return results |
431 |
> |
except Exception, ex: |
432 |
> |
msg = "ERROR %s" %(str(ex)) |
433 |
> |
for filetocopy in list_file: |
434 |
> |
results.update( self.updateReport(filetocopy, '-1', msg)) |
435 |
> |
return results |
436 |
|
|
437 |
|
## prepare for real copy ## |
438 |
< |
sbi = SBinterface( Source_SE, Destination_SE ) |
439 |
< |
sbi_dest = SBinterface(Destination_SE) |
440 |
< |
|
441 |
< |
results = {} |
442 |
< |
## loop over the complete list of files |
443 |
< |
for filetocopy in list_file: |
444 |
< |
if self.debug : print 'start real copy for %s'%filetocopy |
445 |
< |
ErCode, msg = self.checkFileExist( sbi_dest, os.path.basename(filetocopy) ) |
446 |
< |
if ErCode == '0': |
447 |
< |
ErCode, msg = self.makeCopy( sbi, filetocopy , opt) |
448 |
< |
if self.debug : print 'Copy results for %s is %s'%( os.path.basename(filetocopy) ,ErCode) |
438 |
> |
try : |
439 |
> |
sbi = SBinterface( Source_SE, Destination_SE ) |
440 |
> |
sbi_dest = SBinterface(Destination_SE) |
441 |
> |
sbi_source = SBinterface(Source_SE) |
442 |
> |
except ProtocolMismatch, ex: |
443 |
> |
msg = "ERROR : Unable to create SBinterface with %s protocol"%protocol |
444 |
> |
msg += str(ex) |
445 |
> |
for filetocopy in list_file: |
446 |
> |
results.update( self.updateReport(filetocopy, '-1', msg)) |
447 |
> |
return results |
448 |
> |
|
449 |
> |
self.hostname = Destination_SE.hostname |
450 |
> |
|
451 |
> |
## loop over the complete list of files |
452 |
> |
for filetocopy in list_file: |
453 |
> |
if self.debug : print '\tStart real copy for %s'%filetocopy |
454 |
> |
try : |
455 |
> |
ErCode, msg = self.checkFileExist( sbi_source, sbi_dest, filetocopy, options ) |
456 |
> |
except Exception, ex: |
457 |
> |
ErCode = '60307' |
458 |
> |
msg = str(ex) |
459 |
> |
if ErCode == '0': |
460 |
> |
ErCode, msg = self.makeCopy( sbi, filetocopy , options, protocol,sbi_dest ) |
461 |
> |
if (ErCode == '0') and (backup == 'yes'): |
462 |
> |
ErCode = '60308' |
463 |
> |
if self.debug : print '\tCopy results for %s is %s'%( os.path.basename(filetocopy), ErCode) |
464 |
|
results.update( self.updateReport(filetocopy, ErCode, msg)) |
465 |
|
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 |
232 |
– |
|
233 |
– |
report = { file : jobStageInfo} |
234 |
– |
return report |
466 |
|
|
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 |
467 |
|
|
468 |
|
def storageInterface( self, endpoint, protocol ): |
469 |
|
""" |
470 |
< |
Create the storage interface. |
470 |
> |
Create the storage interface. |
471 |
|
""" |
472 |
+ |
if self.debug : print 'storageInterface():\n' |
473 |
|
try: |
474 |
|
interface = SElement( FullPath(endpoint), protocol ) |
475 |
< |
except Exception, ex: |
476 |
< |
msg = '' |
477 |
< |
if self.debug : msg = str(ex)+'\n' |
478 |
< |
msg += "ERROR : Unable to create interface with %s protocol\n"%protocol |
280 |
< |
print msg |
475 |
> |
except ProtocolUnknown, ex: |
476 |
> |
msg = "ERROR : Unable to create interface with %s protocol"%protocol |
477 |
> |
msg += str(ex) |
478 |
> |
raise Exception(msg) |
479 |
|
|
480 |
|
return interface |
481 |
|
|
284 |
– |
def checkDir(self, Destination_SE, protocol): |
285 |
– |
''' |
286 |
– |
ToBeImplemented NEEDED for castor |
287 |
– |
''' |
288 |
– |
return |
289 |
– |
|
482 |
|
def createDir(self, Destination_SE, protocol): |
483 |
|
""" |
484 |
< |
Create remote dir for gsiftp/rfio REALLY TEMPORARY |
485 |
< |
this should be transparent at SE API level. |
484 |
> |
Create remote dir for gsiftp REALLY TEMPORARY |
485 |
> |
this should be transparent at SE API level. |
486 |
|
""" |
487 |
< |
ErCode = '0' |
488 |
< |
msg_1 = '' |
487 |
> |
if self.debug : print 'createDir():\n' |
488 |
> |
msg = '' |
489 |
|
try: |
490 |
|
action = SBinterface( Destination_SE ) |
491 |
|
action.createDir() |
492 |
< |
if self.debug: print "The directory has been created using protocol %s\n"%protocol |
493 |
< |
except Exception, ex: |
494 |
< |
msg = '' |
495 |
< |
if self.debug : msg = str(ex)+'\n' |
496 |
< |
msg_1 = "ERROR: problem with the directory creation using %s protocol \n"%protocol |
497 |
< |
msg += msg_1 |
498 |
< |
ErCode = '60316' |
499 |
< |
#print msg |
500 |
< |
|
501 |
< |
return ErCode, msg_1 |
492 |
> |
if self.debug: print "\tThe directory has been created using protocol %s"%protocol |
493 |
> |
except TransferException, ex: |
494 |
> |
msg = "ERROR: problem with the directory creation using %s protocol "%protocol |
495 |
> |
msg += str(ex) |
496 |
> |
if self.debug : |
497 |
> |
dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n' |
498 |
> |
dbgmsg += '\t'+str(ex.output)+'\n' |
499 |
> |
print dbgmsg |
500 |
> |
raise Exception(msg) |
501 |
> |
except OperationException, ex: |
502 |
> |
msg = "ERROR: problem with the directory creation using %s protocol "%protocol |
503 |
> |
msg += str(ex) |
504 |
> |
if self.debug : print '\t'+msg+'\n\t'+str(ex.detail)+'\n' |
505 |
> |
raise Exception(msg) |
506 |
> |
except MissingDestination, ex: |
507 |
> |
msg = "ERROR: problem with the directory creation using %s protocol "%protocol |
508 |
> |
msg += str(ex) |
509 |
> |
if self.debug : print '\t'+msg+'\n\t'+str(ex.detail)+'\n' |
510 |
> |
raise Exception(msg) |
511 |
> |
except AlreadyExistsException, ex: |
512 |
> |
if self.debug: print "\tThe directory already exist" |
513 |
> |
pass |
514 |
> |
except Exception, ex: |
515 |
> |
msg = "ERROR %s %s" %(str(ex), str(ex.detail)) |
516 |
> |
if self.debug : print '\t'+msg+'\n\t'+str(ex.detail)+'\n' |
517 |
> |
raise Exception(msg) |
518 |
> |
return msg |
519 |
|
|
520 |
< |
def checkFileExist(self, sbi, filetocopy): |
520 |
> |
def checkFileExist( self, sbi_source, sbi_dest, filetocopy, option ): |
521 |
|
""" |
522 |
< |
Check if file to copy already exist |
523 |
< |
""" |
524 |
< |
try: |
525 |
< |
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 |
522 |
> |
Check both if source file exist AND |
523 |
> |
if destination file ALREADY exist. |
524 |
> |
""" |
525 |
> |
if self.debug : print 'checkFileExist():\n' |
526 |
|
ErCode = '0' |
527 |
|
msg = '' |
528 |
< |
if check : |
529 |
< |
ErCode = '60303' |
530 |
< |
msg = "file %s already exist"%filetocopy |
531 |
< |
print msg |
528 |
> |
f_tocopy=filetocopy |
529 |
> |
if self.source_prot != 'local':f_tocopy = os.path.basename(filetocopy) |
530 |
> |
try: |
531 |
> |
checkSource = sbi_source.checkExists( f_tocopy , opt=option, tout = self.subprocesstimeout['exists'] ) |
532 |
> |
if self.debug : print '\tCheck for local file %s exist succeded \n'%f_tocopy |
533 |
> |
except OperationException, ex: |
534 |
> |
msg ='ERROR: problems checkig if source file %s exist'%filetocopy |
535 |
> |
msg += str(ex) |
536 |
> |
if self.debug : |
537 |
> |
dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n' |
538 |
> |
dbgmsg += '\t'+str(ex.output)+'\n' |
539 |
> |
print dbgmsg |
540 |
> |
raise Exception(msg) |
541 |
> |
except WrongOption, ex: |
542 |
> |
msg ='ERROR problems checkig if source file % exist'%filetocopy |
543 |
> |
msg += str(ex) |
544 |
> |
if self.debug : |
545 |
> |
dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n' |
546 |
> |
dbgmsg += '\t'+str(ex.output)+'\n' |
547 |
> |
print dbgmsg |
548 |
> |
raise Exception(msg) |
549 |
> |
except MissingDestination, ex: |
550 |
> |
msg ='ERROR problems checkig if source file % exist'%filetocopy |
551 |
> |
msg += str(ex) |
552 |
> |
if self.debug : print '\t'+msg+'\n\t'+str(ex.detail)+'\n' |
553 |
> |
raise Exception(msg) |
554 |
> |
## when the client commands are not found (wrong env or really missing) |
555 |
> |
except MissingCommand, ex: |
556 |
> |
ErCode = '10041' |
557 |
> |
msg = "ERROR %s %s" %(str(ex), str(ex.detail)) |
558 |
> |
return ErCode, msg |
559 |
> |
if not checkSource : |
560 |
> |
ErCode = '60302' |
561 |
> |
msg = "ERROR file %s do not exist"%os.path.basename(filetocopy) |
562 |
> |
return ErCode, msg |
563 |
> |
f_tocopy=filetocopy |
564 |
> |
if self.dest_prot != 'local':f_tocopy = os.path.basename(filetocopy) |
565 |
> |
try: |
566 |
> |
check = sbi_dest.checkExists( f_tocopy, opt=option, tout = self.subprocesstimeout['exists'] ) |
567 |
> |
if self.debug : print '\tCheck for remote file %s exist succeded \n'%f_tocopy |
568 |
> |
except OperationException, ex: |
569 |
> |
msg = 'ERROR: problems checkig if file %s already exist'%filetocopy |
570 |
> |
msg += str(ex) |
571 |
> |
if self.debug : |
572 |
> |
dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n' |
573 |
> |
dbgmsg += '\t'+str(ex.output)+'\n' |
574 |
> |
print dbgmsg |
575 |
> |
raise Exception(msg) |
576 |
> |
except WrongOption, ex: |
577 |
> |
msg = 'ERROR problems checkig if file % already exist'%filetocopy |
578 |
> |
msg += str(ex) |
579 |
> |
if self.debug : |
580 |
> |
msg += '\t'+msg+'\n\t'+str(ex.detail)+'\n' |
581 |
> |
msg += '\t'+str(ex.output)+'\n' |
582 |
> |
raise Exception(msg) |
583 |
> |
except MissingDestination, ex: |
584 |
> |
msg ='ERROR problems checkig if source file % exist'%filetocopy |
585 |
> |
msg += str(ex) |
586 |
> |
if self.debug : print '\t'+msg+'\n\t'+str(ex.detail)+'\n' |
587 |
> |
raise Exception(msg) |
588 |
> |
## when the client commands are not found (wrong env or really missing) |
589 |
> |
except MissingCommand, ex: |
590 |
> |
ErCode = '10041' |
591 |
> |
msg = "ERROR %s %s" %(str(ex), str(ex.detail)) |
592 |
> |
return ErCode, msg |
593 |
> |
if check : |
594 |
> |
ErCode = '60303' |
595 |
> |
msg = "file %s already exist"%os.path.basename(filetocopy) |
596 |
|
|
597 |
< |
return ErCode,msg |
597 |
> |
return ErCode, msg |
598 |
|
|
599 |
< |
def makeCopy(self, sbi, filetocopy, opt ): |
599 |
> |
def makeCopy(self, sbi, filetocopy, option, protocol, sbi_dest ): |
600 |
|
""" |
601 |
< |
call the copy API. |
601 |
> |
call the copy API. |
602 |
|
""" |
603 |
< |
path = os.path.dirname(filetocopy) |
603 |
> |
if self.debug : print 'makeCopy():\n' |
604 |
> |
path = os.path.dirname(filetocopy) |
605 |
|
file_name = os.path.basename(filetocopy) |
606 |
|
source_file = filetocopy |
607 |
|
dest_file = file_name ## to be improved supporting changing file name TODO |
608 |
< |
if self.source == '' and path == '': |
608 |
> |
if self.params['source'] == '' and path == '': |
609 |
|
source_file = os.path.abspath(filetocopy) |
610 |
< |
elif self.destination =='': |
611 |
< |
dest_file = os.path.join(os.getcwd(),file_name) |
612 |
< |
elif self.source != '' and self.destination != '' : |
613 |
< |
source_file = file_name |
610 |
> |
elif self.params['destination'] =='': |
611 |
> |
destDir = self.params.get('destinationDir',os.getcwd()) |
612 |
> |
dest_file = os.path.join(destDir,file_name) |
613 |
> |
elif self.params['source'] != '' and self.params['destination'] != '' : |
614 |
> |
source_file = file_name |
615 |
> |
|
616 |
|
ErCode = '0' |
617 |
|
msg = '' |
618 |
< |
|
618 |
> |
|
619 |
> |
if self.params['option'].find('space_token')>=0: |
620 |
> |
space_token=self.params['option'].split('=')[1] |
621 |
> |
if protocol == 'srmv2': option = '%s -space_token=%s'%(option,space_token) |
622 |
> |
if protocol == 'srm-lcg': option = '%s -S %s'%(option,space_token) |
623 |
|
try: |
624 |
< |
pippo = sbi.copy( source_file , dest_file , opt) |
625 |
< |
if self.protocol == 'srm' : self.checkSize( sbi, filetocopy ) |
626 |
< |
except Exception, ex: |
627 |
< |
msg = '' |
628 |
< |
if self.debug : msg = str(ex)+'\n' |
629 |
< |
msg = "Problem copying %s file with %s command"%( filetocopy, protocol ) |
624 |
> |
sbi.copy( source_file , dest_file , opt = option, tout = self.subprocesstimeout['copy']) |
625 |
> |
except TransferException, ex: |
626 |
> |
msg = "Problem copying %s file" % filetocopy |
627 |
> |
msg += str(ex) |
628 |
> |
if self.debug : |
629 |
> |
dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n' |
630 |
> |
dbgmsg += '\t'+str(ex.output)+'\n' |
631 |
> |
print dbgmsg |
632 |
|
ErCode = '60307' |
633 |
< |
#print msg |
634 |
< |
|
633 |
> |
except WrongOption, ex: |
634 |
> |
msg = "Problem copying %s file" % filetocopy |
635 |
> |
msg += str(ex) |
636 |
> |
if self.debug : |
637 |
> |
dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n' |
638 |
> |
dbgmsg += '\t'+str(ex.output)+'\n' |
639 |
> |
print dbgmsg |
640 |
> |
except SizeZeroException, ex: |
641 |
> |
msg = "Problem copying %s file" % filetocopy |
642 |
> |
msg += str(ex) |
643 |
> |
if self.debug : |
644 |
> |
dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n' |
645 |
> |
dbgmsg += '\t'+str(ex.output)+'\n' |
646 |
> |
print dbgmsg |
647 |
> |
ErCode = '60307' |
648 |
> |
## when the client commands are not found (wrong env or really missing) |
649 |
> |
except MissingCommand, ex: |
650 |
> |
ErCode = '10041' |
651 |
> |
msg = "Problem copying %s file" % filetocopy |
652 |
> |
msg += str(ex) |
653 |
> |
if self.debug : |
654 |
> |
dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n' |
655 |
> |
dbgmsg += '\t'+str(ex.output)+'\n' |
656 |
> |
print dbgmsg |
657 |
> |
except AuthorizationException, ex: |
658 |
> |
ErCode = '60307' |
659 |
> |
msg = "Problem copying %s file" % filetocopy |
660 |
> |
msg += str(ex) |
661 |
> |
if self.debug : |
662 |
> |
dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n' |
663 |
> |
dbgmsg += '\t'+str(ex.output)+'\n' |
664 |
> |
print dbgmsg |
665 |
> |
except SEAPITimeout, ex: |
666 |
> |
ErCode = '60317' |
667 |
> |
msg = "Problem copying %s file" % filetocopy |
668 |
> |
msg += str(ex) |
669 |
> |
if self.debug : |
670 |
> |
dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n' |
671 |
> |
dbgmsg += '\t'+str(ex.output)+'\n' |
672 |
> |
print dbgmsg |
673 |
> |
|
674 |
> |
if ErCode == '0' and protocol.find('srmv') == 0: |
675 |
> |
remote_file_size = -1 |
676 |
> |
local_file_size = os.path.getsize( source_file ) |
677 |
> |
try: |
678 |
> |
remote_file_size = sbi_dest.getSize( dest_file, opt=option, tout = self.subprocesstimeout['size'] ) |
679 |
> |
if self.debug : print '\t Check of remote size succeded for file %s\n'%dest_file |
680 |
> |
except TransferException, ex: |
681 |
> |
msg = "Problem checking the size of %s file" % filetocopy |
682 |
> |
msg += str(ex) |
683 |
> |
if self.debug : |
684 |
> |
dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n' |
685 |
> |
dbgmsg += '\t'+str(ex.output)+'\n' |
686 |
> |
print dbgmsg |
687 |
> |
ErCode = '60307' |
688 |
> |
except WrongOption, ex: |
689 |
> |
msg = "Problem checking the size of %s file" % filetocopy |
690 |
> |
msg += str(ex) |
691 |
> |
if self.debug : |
692 |
> |
dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n' |
693 |
> |
dbgmsg += '\t'+str(ex.output)+'\n' |
694 |
> |
print dbgmsg |
695 |
> |
ErCode = '60307' |
696 |
> |
if local_file_size != remote_file_size: |
697 |
> |
msg = "File size dosn't match: local size = %s ; remote size = %s " % (local_file_size, remote_file_size) |
698 |
> |
ErCode = '60307' |
699 |
> |
|
700 |
> |
if ErCode != '0': |
701 |
> |
try : |
702 |
> |
self.removeFile( sbi_dest, dest_file, option ) |
703 |
> |
except Exception, ex: |
704 |
> |
msg += '\n'+str(ex) |
705 |
|
return ErCode, msg |
706 |
< |
|
707 |
< |
''' |
708 |
< |
def checkSize() |
706 |
> |
|
707 |
> |
def removeFile( self, sbi_dest, filetocopy, option ): |
708 |
> |
""" |
709 |
> |
""" |
710 |
> |
if self.debug : print 'removeFile():\n' |
711 |
> |
f_tocopy=filetocopy |
712 |
> |
if self.dest_prot != 'local':f_tocopy = os.path.basename(filetocopy) |
713 |
> |
try: |
714 |
> |
sbi_dest.delete( f_tocopy, opt=option, tout = self.subprocesstimeout['delete'] ) |
715 |
> |
if self.debug : '\t deletion of file %s succeeded\n'%str(filetocopy) |
716 |
> |
except OperationException, ex: |
717 |
> |
msg ='ERROR: problems removing partially staged file %s'%filetocopy |
718 |
> |
msg += str(ex) |
719 |
> |
if self.debug : |
720 |
> |
dbgmsg = '\t'+msg+'\n\t'+str(ex.detail)+'\n' |
721 |
> |
dbgmsg += '\t'+str(ex.output)+'\n' |
722 |
> |
print dbgmsg |
723 |
> |
raise Exception(msg) |
724 |
> |
|
725 |
> |
return |
726 |
> |
|
727 |
> |
#def updateReport(self, file, erCode, reason, lfn='', se='' ): |
728 |
> |
def updateReport(self, file, erCode, reason): |
729 |
|
""" |
730 |
< |
Using srm needed a check of the ouptut file size. |
730 |
> |
Update the final stage out infos |
731 |
|
""" |
732 |
< |
|
733 |
< |
echo "--> remoteSize = $remoteSize" |
734 |
< |
## for local file |
735 |
< |
localSize=$(stat -c%s "$path_out_file") |
736 |
< |
echo "--> localSize = $localSize" |
737 |
< |
if [ $localSize != $remoteSize ]; then |
738 |
< |
echo "Local fileSize $localSize does not match remote fileSize $remoteSize" |
739 |
< |
echo "Copy failed: removing remote file $destination" |
740 |
< |
srmrm $destination |
741 |
< |
cmscp_exit_status=60307 |
742 |
< |
|
743 |
< |
|
744 |
< |
echo "Problem copying $path_out_file to $destination with srmcp command" |
745 |
< |
StageOutExitStatusReason='remote and local file dimension not match' |
746 |
< |
echo "StageOutReport = `cat ./srmcp.report`" |
747 |
< |
''' |
748 |
< |
def backup(self): |
749 |
< |
""" |
750 |
< |
Check infos from TFC using existing api obtaining: |
751 |
< |
1)destination |
385 |
< |
2)protocol |
732 |
> |
jobStageInfo={} |
733 |
> |
jobStageInfo['erCode']=erCode |
734 |
> |
jobStageInfo['reason']=reason |
735 |
> |
if not self.params['for_lfn']: self.params['for_lfn']='' |
736 |
> |
if not self.params['se_name']: self.params['se_name']='' |
737 |
> |
if not self.hostname: self.hostname='' |
738 |
> |
if (erCode != '0') and (erCode != '60308'): |
739 |
> |
jobStageInfo['for_lfn']='/copy_problem/' |
740 |
> |
else: |
741 |
> |
jobStageInfo['for_lfn']=self.params['for_lfn'] |
742 |
> |
jobStageInfo['se_name']=self.params['se_name'] |
743 |
> |
jobStageInfo['endpoint']=self.hostname |
744 |
> |
|
745 |
> |
report = { file : jobStageInfo} |
746 |
> |
return report |
747 |
> |
|
748 |
> |
def finalReport( self , results ): |
749 |
> |
""" |
750 |
> |
It a list of LFNs for each SE where data are stored. |
751 |
> |
allow "crab -copyLocal" or better "crab -copyOutput". TO_DO. |
752 |
|
""" |
753 |
+ |
outFile = open('cmscpReport.sh',"a") |
754 |
+ |
cmscp_exit_status = 0 |
755 |
+ |
txt = '' |
756 |
+ |
for file, dict in results.iteritems(): |
757 |
+ |
reason = str(dict['reason']) |
758 |
+ |
if str(reason).find("'") > -1: |
759 |
+ |
reason = " ".join(reason.split("'")) |
760 |
+ |
reason="'%s'"%reason |
761 |
+ |
if file: |
762 |
+ |
if dict['for_lfn']=='': |
763 |
+ |
lfn = '${LFNBaseName}'+os.path.basename(file) |
764 |
+ |
se = '$SE' |
765 |
+ |
LFNBaseName = '$LFNBaseName' |
766 |
+ |
else: |
767 |
+ |
lfn = dict['for_lfn']+os.path.basename(file) |
768 |
+ |
se = dict['se_name'] |
769 |
+ |
LFNBaseName = os.path.dirname(lfn) |
770 |
+ |
if (LFNBaseName[-1] != '/'): |
771 |
+ |
LFNBaseName = LFNBaseName + '/' |
772 |
+ |
|
773 |
+ |
|
774 |
+ |
txt += 'echo "Report for File: '+file+'"\n' |
775 |
+ |
txt += 'echo "LFN: '+lfn+'"\n' |
776 |
+ |
txt += 'echo "StorageElement: '+se+'"\n' |
777 |
+ |
txt += 'echo "StageOutExitStatusReason = %s" | tee -a $RUNTIME_AREA/$repo\n'%reason |
778 |
+ |
txt += 'echo "StageOutSE = '+se+'" >> $RUNTIME_AREA/$repo\n' |
779 |
+ |
|
780 |
+ |
|
781 |
+ |
if dict['erCode'] != '0': |
782 |
+ |
cmscp_exit_status = dict['erCode'] |
783 |
+ |
else: |
784 |
+ |
txt += 'echo "StageOutExitStatusReason = %s" | tee -a $RUNTIME_AREA/$repo\n'%reason |
785 |
+ |
cmscp_exit_status = dict['erCode'] |
786 |
+ |
txt += '\n' |
787 |
+ |
txt += 'export StageOutExitStatus='+str(cmscp_exit_status)+'\n' |
788 |
+ |
txt += 'echo "StageOutExitStatus = '+str(cmscp_exit_status)+'" | tee -a $RUNTIME_AREA/$repo\n' |
789 |
+ |
outFile.write(str(txt)) |
790 |
+ |
outFile.close() |
791 |
|
return |
792 |
|
|
389 |
– |
def usage(self): |
793 |
|
|
794 |
< |
msg=""" |
795 |
< |
required parameters: |
796 |
< |
--source :: REMOTE : |
797 |
< |
--destination :: REMOTE : |
798 |
< |
--debug : |
799 |
< |
--inFile :: absPath : or name NOT RELATIVE PATH |
800 |
< |
--outFIle :: onlyNAME : NOT YET SUPPORTED |
801 |
< |
|
802 |
< |
optional parameters |
803 |
< |
""" |
804 |
< |
return msg |
794 |
> |
def usage(): |
795 |
> |
|
796 |
> |
msg=""" |
797 |
> |
cmscp: |
798 |
> |
safe copy of local file to/from remote SE via lcg_cp/srmcp, |
799 |
> |
including success checking version also for CAF using rfcp command to copy the output to SE |
800 |
> |
|
801 |
> |
accepted parameters: |
802 |
> |
source = |
803 |
> |
destination = |
804 |
> |
inputFileList = |
805 |
> |
outputFileList = |
806 |
> |
protocol = |
807 |
> |
option = |
808 |
> |
middleware = |
809 |
> |
srm_version = |
810 |
> |
destinationDir = |
811 |
> |
lfn= = |
812 |
> |
local_stage = activate stage fall back |
813 |
> |
debug = activate verbose print out |
814 |
> |
help = print on line man and exit |
815 |
> |
|
816 |
> |
mandatory: |
817 |
> |
* "source" and/or "destination" must always be defined |
818 |
> |
* either "middleware" or "protocol" must always be defined |
819 |
> |
* "inputFileList" must always be defined |
820 |
> |
* if "local_stage" = 1 also "lfn" must be defined |
821 |
> |
""" |
822 |
> |
print msg |
823 |
> |
|
824 |
> |
return |
825 |
> |
|
826 |
> |
def HelpOptions(opts=[]): |
827 |
> |
""" |
828 |
> |
Check otps, print help if needed |
829 |
> |
prepare dict = { opt : value } |
830 |
> |
""" |
831 |
> |
dict_args = {} |
832 |
> |
if len(opts): |
833 |
> |
for opt, arg in opts: |
834 |
> |
dict_args[opt.split('--')[1]] = arg |
835 |
> |
if opt in ('-h','-help','--help') : |
836 |
> |
usage() |
837 |
> |
sys.exit(0) |
838 |
> |
return dict_args |
839 |
> |
else: |
840 |
> |
usage() |
841 |
> |
sys.exit(0) |
842 |
|
|
843 |
|
if __name__ == '__main__' : |
844 |
+ |
|
845 |
+ |
import getopt |
846 |
+ |
|
847 |
+ |
allowedOpt = ["source=", "destination=", "inputFileList=", "outputFileList=", \ |
848 |
+ |
"protocol=","option=", "middleware=", "srm_version=", \ |
849 |
+ |
"destinationDir=", "for_lfn=", "local_stage", "debug", "help", "se_name="] |
850 |
+ |
try: |
851 |
+ |
opts, args = getopt.getopt( sys.argv[1:], "", allowedOpt ) |
852 |
+ |
except getopt.GetoptError, err: |
853 |
+ |
print err |
854 |
+ |
HelpOptions() |
855 |
+ |
sys.exit(2) |
856 |
+ |
|
857 |
+ |
dictArgs = HelpOptions(opts) |
858 |
|
try: |
859 |
< |
cmscp_ = cmscp(sys.argv[1:]) |
859 |
> |
cmscp_ = cmscp(dictArgs) |
860 |
|
cmscp_.run() |
861 |
< |
except: |
862 |
< |
pass |
861 |
> |
except Exception, ex : |
862 |
> |
print str(ex) |
863 |
|
|