ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/PhEDExDatasvcInfo.py
Revision: 1.26
Committed: Fri Jul 31 16:21:12 2009 UTC (15 years, 9 months ago) by fanzago
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_7_0_pre2, CRAB_2_6_3_pre1, test_1, CRAB_2_7_0_pre1, CRAB_2_6_2, CRAB_2_6_2_pre2, CRAB_2_6_2_pre1
Branch point for: CRAB_2_6_X_br
Changes since 1.25: +7 -1 lines
Log Message:
changes related publication name too long, savannah task 10664

File Contents

# Content
1 from Actor import *
2 import urllib
3 from xml.dom.minidom import parse
4 from crab_exceptions import *
5 from WorkSpace import *
6 from urlparse import urlparse
7 from LFNBaseName import *
8
9 class PhEDExDatasvcInfo:
10 def __init__( self , cfg_params ):
11
12 ## PhEDEx Data Service URL
13 url="https://cmsweb.cern.ch/phedex/datasvc/xml/prod"
14 self.datasvc_url = cfg_params.get("USER.datasvc_url",url)
15
16 self.FacOps_savannah = 'https://savannah.cern.ch/support/?func=additem&group=cmscompinfrasup'
17 stage_out_faq='https://twiki.cern.ch/twiki/bin/view/CMS/SWGuideCrabHowTo#Stageout_and_publication'
18 self.dataPub_faq = 'https://twiki.cern.ch/twiki/bin/view/CMS/SWGuideCrabForPublication'
19
20 self.srm_version = cfg_params.get("USER.srm_version",'srmv2')
21 self.node = cfg_params.get('USER.storage_element',None)
22
23
24
25 self.user_lfn = cfg_params.get("USER.lfn",'')
26 self.publish_data = cfg_params.get("USER.publish_data",0)
27 self.usenamespace = cfg_params.get("USER.usenamespace",0)
28 self.user_remote_dir = cfg_params.get("USER.user_remote_dir",'')
29 if self.user_remote_dir:
30 if ( self.user_remote_dir[-1] != '/' ) : self.user_remote_dir = self.user_remote_dir + '/'
31 if self.user_lfn:
32 msg = 'Warning: lfn has been deprecated, CRAB will ignore it.\n'
33 msg += '\t Please use only user_remote_dir removing lfn from your crab.cfg\n'
34 msg += '\t For further information please visit : \n\t%s'%stage_out_faq
35 common.logger.info(msg)
36
37 self.datasetpath = cfg_params.get("CMSSW.datasetpath")
38 self.publish_data_name = cfg_params.get('USER.publish_data_name','')
39
40 self.user_port = cfg_params.get("USER.storage_port",'8443')
41 self.user_se_path = cfg_params.get("USER.storage_path",'')
42 if self.user_se_path:
43 if ( self.user_se_path[-1] != '/' ) : self.user_se_path = self.user_se_path + '/'
44
45 #check if using "private" Storage
46 self.usePhedex = True
47 if not self.node :
48 msg = 'Please specify the storage_element name in your crab.cfg section [USER].\n'
49 msg +='\tFor further information please visit : %s'%stage_out_faq
50 raise CrabException(msg)
51 if (self.node.find('T1_') + self.node.find('T2_')+self.node.find('T3_')) == -3: self.usePhedex = False
52
53 if not self.usePhedex and ( self.user_remote_dir == '' or self.user_se_path == '' ):
54 msg = 'You are asking to stage out without using CMS Storage Name convention. In this case you \n'
55 msg += '\t must specify both user_remote_dir and storage_path in the crab.cfg section [USER].\n '
56 msg += '\t For further information please visit : \n\t%s'%stage_out_faq
57 raise CrabException(msg)
58 self.sched = common.scheduler.name().upper()
59 self.protocol = self.srm_version
60
61 self.forced_path = '/store/user/'
62 if self.sched in ['CAF','LSF']:
63 self.protocol = 'direct'
64 self.SE = {'CAF':'caf.cern.ch', 'LSF':''}
65 if self.sched == 'CAF': self.forced_path = '/store/caf/user/'
66
67 if not self.usePhedex:
68 self.forced_path = self.user_remote_dir
69 return
70
71 def getEndpoint(self):
72 '''
73 Return full SE endpoint and related infos
74 '''
75 self.lfn = self.getLFN()
76
77 #extract the PFN for the given node,LFN,protocol
78 endpoint = self.getStageoutPFN()
79
80 #extract SE name an SE_PATH (needed for publication)
81 SE, SE_PATH, User = self.splitEndpoint(endpoint)
82
83 return endpoint, self.lfn , SE, SE_PATH, User
84
85 def splitEndpoint(self, endpoint):
86 '''
87 Return relevant infos from endpoint
88 '''
89 SE = ''
90 SE_PATH = ''
91 USER = ''
92 if self.usePhedex:
93 if self.protocol == 'direct':
94 query=endpoint
95 SE_PATH = endpoint
96 SE = self.SE[self.sched]
97 else:
98 url = 'http://'+endpoint.split('://')[1]
99 # python > 2.4
100 # SE = urlparse(url).hostname
101 scheme, host, path, params, query, fragment = urlparse(url)
102 SE = host.split(':')[0]
103 SE_PATH = endpoint.split(host)[1]
104 USER = (query.split('user')[1]).split('/')[1]
105 else:
106 #### to test #####
107 # url = 'http://'+endpoint.split('://')[1]
108 # scheme, host, path, params, query, fragment = urlparse(url)
109 # SE = host.split(':')[0]
110 # SE_PATH = endpoint.split(host)[1]
111 SE = self.node
112 SE_PATH = self.user_se_path + self.user_remote_dir
113 try:
114 USER = (self.lfn.split('user')[1]).split('/')[1]
115 except:
116 pass
117
118 return SE, SE_PATH, USER
119
120
121 def getLFN(self):
122 """
123 define the LFN composing the needed pieces
124 """
125 lfn = ''
126 l_User = False
127 if not self.usePhedex and (int(self.publish_data) == 0 and int(self.usenamespace) == 0) :
128 ### add here check if user is trying to force a wrong LFN using a T2 TODO
129 ## check if storage_name is a T2 (siteDB query)
130 ## if yes :match self.user_lfn with LFNBaseName...
131 ## if NOT : raise (you are using a T2. It's not allowed stage out into self.user_path+self.user_lfn)
132 lfn = self.user_remote_dir
133 return lfn
134 if self.publish_data_name == '' and int(self.publish_data) == 1:
135 msg = "Error. The [USER] section does not have 'publish_data_name'\n"
136 msg += '\tFor further information please visit : \n\t%s'%self.dataPub_faq
137 raise CrabException(msg)
138 if self.publish_data_name == '' and int(self.usenamespace) == 1:
139 self.publish_data_name = "DefaultDataset"
140 if int(self.publish_data) == 1:
141 if self.sched in ['CAF']: l_User=True
142 primaryDataset = self.computePrimaryDataset()
143 ### added the case lfn = LFNBase(self.forced_path, primaryDataset, self.publish_data_name, publish=True)
144 ### for the publication in order to be able to check the lfn length
145 lfn = LFNBase(self.forced_path, primaryDataset, self.publish_data_name, publish=True) + '/${PSETHASH}/'
146 elif int(self.usenamespace) == 1:
147 if self.sched in ['CAF']: l_User=True
148 primaryDataset = self.computePrimaryDataset()
149 lfn = LFNBase(self.forced_path, primaryDataset, self.publish_data_name) + '/${PSETHASH}/'
150 else:
151 if self.sched in ['CAF','LSF']: l_User=True
152 lfn = LFNBase(self.forced_path,self.user_remote_dir)
153 return lfn
154
155 def computePrimaryDataset(self):
156 """
157 compute the last part for the LFN in case of publication
158 """
159 if (self.datasetpath.upper() != 'NONE'):
160 primarydataset = self.datasetpath.split("/")[1]
161 else:
162 primarydataset = self.publish_data_name
163 return primarydataset
164
165 def lfn2pfn(self):
166 """
167 PhEDEx Data Service lfn2pfn call
168
169 input: LFN,node name,protocol
170 returns: DOM object with the content of the PhEDEx Data Service call
171 """
172 params = {'node' : self.node , 'lfn': self.lfn , 'protocol': self.protocol}
173 params = urllib.urlencode(params)
174 datasvc_lfn2pfn="%s/lfn2pfn"%self.datasvc_url
175 try:
176 urlresults = urllib.urlopen(datasvc_lfn2pfn, params)
177 urlresults = parse(urlresults)
178 except IOError:
179 msg="Unable to access PhEDEx Data Service at %s"%datasvc_lfn2pfn
180 raise CrabException(msg)
181 except:
182 urlresults = None
183
184 return urlresults
185
186 def parse_error(self,urlresults):
187 """
188 look for errors in the DOM object returned by PhEDEx Data Service call
189 """
190 errormsg = None
191 errors=urlresults.getElementsByTagName('error')
192 for error in errors:
193 errormsg=error.childNodes[0].data
194 if len(error.childNodes)>1:
195 errormsg+=error.childNodes[1].data
196 return errormsg
197
198 def parse_lfn2pfn(self,urlresults):
199 """
200 Parse the content of the result of lfn2pfn PhEDEx Data Service call
201
202 input: DOM object with the content of the lfn2pfn call
203 returns: PFN
204 """
205 result = urlresults.getElementsByTagName('phedex')
206
207 if not result:
208 return []
209 result = result[0]
210 pfn = None
211 mapping = result.getElementsByTagName('mapping')
212 for m in mapping:
213 pfn=m.getAttribute("pfn")
214 if pfn:
215 return pfn
216
217 def getStageoutPFN( self ):
218 """
219 input: LFN,node name,protocol
220 returns: PFN
221 """
222 if self.usePhedex:
223 fullurl="%s/lfn2pfn?node=%s&lfn=%s&protocol=%s"%(self.datasvc_url,self.node,self.lfn,self.protocol)
224 domlfn2pfn = self.lfn2pfn()
225 if not domlfn2pfn :
226 msg="Unable to get info from %s"%fullurl
227 raise CrabException(msg)
228
229 errormsg = self.parse_error(domlfn2pfn)
230 if errormsg:
231 msg="Error extracting info from %s due to: %s"%(fullurl,errormsg)
232 raise CrabException(msg)
233
234 stageoutpfn = self.parse_lfn2pfn(domlfn2pfn)
235 if not stageoutpfn:
236 msg ='Unable to get stageout path from TFC at Site %s \n'%self.node
237 msg+=' Please alert the CompInfraSup group through their savannah %s \n'%self.FacOps_savannah
238 msg+=' reporting: \n'
239 msg+=' Summary: Unable to get user stageout from TFC at Site %s \n'%self.node
240 msg+=' OriginalSubmission: stageout path is not retrieved from %s \n'%fullurl
241 raise CrabException(msg)
242 else:
243 if self.sched in ['CAF','LSF'] :
244 stageoutpfn = self.user_se_path+self.lfn
245 else:
246 stageoutpfn = 'srm://'+self.node+':'+self.user_port+self.user_se_path+self.lfn
247
248 return stageoutpfn
249
250
251
252 if __name__ == '__main__':
253 """
254 Sort of unit testing to check Phedex API for whatever site and/or lfn.
255 Usage:
256 python PhEDExDatasvcInfo.py --node T2_IT_Bari --lfn /store/maremma
257
258 """
259 import getopt,sys
260 from crab_util import *
261 import common
262 klass_name = 'SchedulerGlite'
263 klass = importName(klass_name, klass_name)
264 common.scheduler = klass()
265
266 lfn="/store/user/"
267 node='T2_IT_Bari'
268 valid = ['node=','lfn=']
269 try:
270 opts, args = getopt.getopt(sys.argv[1:], "", valid)
271 except getopt.GetoptError, ex:
272 print str(ex)
273 sys.exit(1)
274 for o, a in opts:
275 if o == "--node":
276 node = a
277 if o == "--lfn":
278 lfn = a
279
280 mycfg_params = { 'USER.storage_element': node }
281 dsvc = PhEDExDatasvcInfo(mycfg_params)
282 dsvc.lfn = lfn
283 print dsvc.getStageoutPFN()
284