ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/PhEDExDatasvcInfo.py
Revision: 1.35
Committed: Fri Dec 4 12:08:21 2009 UTC (15 years, 4 months ago) by fanzago
Content type: text/x-python
Branch: MAIN
CVS Tags: fede_170310, CRAB_2_7_1_pre12, CRAB_2_7_1_pre11, CRAB_2_7_1_pre10, CRAB_2_7_1_pre9, CRAB_LumiMask, CRAB_2_7_lumi, from_LimiMask, CRAB_2_7_1_pre8, CRAB_2_7_1_pre6, CRAB_2_7_1_pre5, CRAB_2_7_1_wmbs_pre4, CRAB_2_7_1_pre4, CRAB_2_7_1_pre3, CRAB_2_7_1_pre2, CRAB_2_7_1_pre1
Branch point for: CRAB_multiout, CRAB_2_7_1_branch
Changes since 1.34: +2 -2 lines
Log Message:
changed for backup copy to close SE. adding temp in the path dir and wrapper exit code 60308

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