ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/PhEDExDatasvcInfo.py
Revision: 1.38
Committed: Mon Jan 31 14:16:04 2011 UTC (14 years, 2 months ago) by belforte
Content type: text/x-python
Branch: MAIN
Changes since 1.37: +1 -1 lines
Log Message:
remove code obsolete since https://savannah.cern.ch/bugs/?55624

File Contents

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