ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/PhEDExDatasvcInfo.py
Revision: 1.7
Committed: Thu Oct 9 10:51:37 2008 UTC (16 years, 6 months ago) by fanzago
Content type: text/x-python
Branch: MAIN
Changes since 1.6: +11 -0 lines
Log Message:
added check on path and added SE=CAF-LSF in the case of direct protocol

File Contents

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