ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/PhEDExDatasvcInfo.py
Revision: 1.25
Committed: Tue May 26 10:23:01 2009 UTC (15 years, 11 months ago) by spiga
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_6_1_pre4, CRAB_2_6_1_pre3, CRAB_2_6_1_pre2, CRAB_2_6_1_pre1, CRAB_2_6_1, CRAB_2_6_0, CRAB_2_6_0_pre14, CRAB_2_6_0_pre13, CRAB_2_6_0_pre12, CRAB_2_6_0_pre11, CRAB_2_6_0_pre10, CRAB_2_6_0_pre9, CRAB_2_6_0_pre8, CRAB_2_6_0_pre7, CRAB_2_6_0_pre6, CRAB_2_6_0_pre5, CRAB_2_6_0_pre4, CRAB_2_6_0_pre3
Changes since 1.24: +1 -2 lines
Log Message:
adapting code to logging usage. (crab_logger removed)

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 spiga 1.3 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 afanfani 1.6 self.FacOps_savannah = 'https://savannah.cern.ch/support/?func=additem&group=cmscompinfrasup'
17 spiga 1.23 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     self.srm_version = cfg_params.get("USER.srm_version",'srmv2')
21     self.node = cfg_params.get('USER.storage_element',None)
22    
23 spiga 1.17
24    
25     self.user_lfn = cfg_params.get("USER.lfn",'')
26 spiga 1.3 self.publish_data = cfg_params.get("USER.publish_data",0)
27     self.usenamespace = cfg_params.get("USER.usenamespace",0)
28 fanzago 1.10 self.user_remote_dir = cfg_params.get("USER.user_remote_dir",'')
29 fanzago 1.7 if self.user_remote_dir:
30     if ( self.user_remote_dir[-1] != '/' ) : self.user_remote_dir = self.user_remote_dir + '/'
31 spiga 1.17 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 spiga 1.25 common.logger.info(msg)
36 spiga 1.17
37 spiga 1.3 self.datasetpath = cfg_params.get("CMSSW.datasetpath")
38     self.publish_data_name = cfg_params.get('USER.publish_data_name','')
39    
40 spiga 1.8 self.user_port = cfg_params.get("USER.storage_port",'8443')
41 spiga 1.3 self.user_se_path = cfg_params.get("USER.storage_path",'')
42 fanzago 1.7 if self.user_se_path:
43     if ( self.user_se_path[-1] != '/' ) : self.user_se_path = self.user_se_path + '/'
44    
45 spiga 1.3 #check if using "private" Storage
46     self.usePhedex = True
47 spiga 1.11 if not self.node :
48     msg = 'Please specify the storage_element name in your crab.cfg section [USER].\n'
49 spiga 1.17 msg +='\tFor further information please visit : %s'%stage_out_faq
50 spiga 1.11 raise CrabException(msg)
51 spiga 1.4 if (self.node.find('T1_') + self.node.find('T2_')+self.node.find('T3_')) == -3: self.usePhedex = False
52 spiga 1.15
53     if not self.usePhedex and ( self.user_remote_dir == '' or self.user_se_path == '' ):
54 spiga 1.3 msg = 'You are asking to stage out without using CMS Storage Name convention. In this case you \n'
55 spiga 1.15 msg += '\t must specify both user_remote_dir and storage_path in the crab.cfg section [USER].\n '
56 spiga 1.17 msg += '\t For further information please visit : \n\t%s'%stage_out_faq
57 spiga 1.3 raise CrabException(msg)
58     self.sched = common.scheduler.name().upper()
59     self.protocol = self.srm_version
60 spiga 1.22
61     self.forced_path = '/store/user/'
62 fanzago 1.18 if self.sched in ['CAF','LSF']:
63     self.protocol = 'direct'
64     self.SE = {'CAF':'caf.cern.ch', 'LSF':''}
65 spiga 1.22 if self.sched == 'CAF': self.forced_path = '/store/caf/user/'
66    
67 spiga 1.14 if not self.usePhedex:
68 spiga 1.15 self.forced_path = self.user_remote_dir
69 spiga 1.3 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 fanzago 1.18 SE = self.SE[self.sched]
97 spiga 1.3 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 fanzago 1.19 #### to test #####
107 spiga 1.21 # 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 spiga 1.20 SE = self.node
112     SE_PATH = self.user_se_path + self.user_remote_dir
113 spiga 1.3 try:
114     USER = (self.lfn.split('user')[1]).split('/')[1]
115     except:
116     pass
117    
118     return SE, SE_PATH, USER
119 afanfani 1.2
120 spiga 1.3
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 spiga 1.15 lfn = self.user_remote_dir
133 spiga 1.3 return lfn
134     if self.publish_data_name == '' and int(self.publish_data) == 1:
135 spiga 1.17 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 spiga 1.3 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 or int(self.usenamespace) == 1:
141     if self.sched in ['CAF']: l_User=True
142     primaryDataset = self.computePrimaryDataset()
143 spiga 1.21 lfn = LFNBase(self.forced_path, primaryDataset, self.publish_data_name) + '/${PSETHASH}/'
144 spiga 1.3 else:
145 spiga 1.5 if self.sched in ['CAF','LSF']: l_User=True
146 spiga 1.21 lfn = LFNBase(self.forced_path,self.user_remote_dir)
147 spiga 1.3 return lfn
148    
149     def computePrimaryDataset(self):
150     """
151     compute the last part for the LFN in case of publication
152     """
153     if (self.datasetpath.upper() != 'NONE'):
154     primarydataset = self.datasetpath.split("/")[1]
155     else:
156     primarydataset = self.publish_data_name
157     return primarydataset
158    
159     def lfn2pfn(self):
160     """
161     PhEDEx Data Service lfn2pfn call
162    
163     input: LFN,node name,protocol
164     returns: DOM object with the content of the PhEDEx Data Service call
165     """
166     params = {'node' : self.node , 'lfn': self.lfn , 'protocol': self.protocol}
167     params = urllib.urlencode(params)
168     datasvc_lfn2pfn="%s/lfn2pfn"%self.datasvc_url
169     try:
170 slacapra 1.24 urlresults = urllib.urlopen(datasvc_lfn2pfn, params)
171 spiga 1.3 urlresults = parse(urlresults)
172 slacapra 1.24 except IOError:
173     msg="Unable to access PhEDEx Data Service at %s"%datasvc_lfn2pfn
174     raise CrabException(msg)
175 spiga 1.3 except:
176     urlresults = None
177    
178     return urlresults
179 afanfani 1.1
180 spiga 1.3 def parse_error(self,urlresults):
181     """
182     look for errors in the DOM object returned by PhEDEx Data Service call
183     """
184     errormsg = None
185     errors=urlresults.getElementsByTagName('error')
186     for error in errors:
187     errormsg=error.childNodes[0].data
188     if len(error.childNodes)>1:
189     errormsg+=error.childNodes[1].data
190     return errormsg
191    
192     def parse_lfn2pfn(self,urlresults):
193     """
194     Parse the content of the result of lfn2pfn PhEDEx Data Service call
195    
196     input: DOM object with the content of the lfn2pfn call
197     returns: PFN
198     """
199     result = urlresults.getElementsByTagName('phedex')
200    
201     if not result:
202     return []
203     result = result[0]
204     pfn = None
205     mapping = result.getElementsByTagName('mapping')
206     for m in mapping:
207     pfn=m.getAttribute("pfn")
208     if pfn:
209     return pfn
210    
211     def getStageoutPFN( self ):
212     """
213     input: LFN,node name,protocol
214     returns: PFN
215     """
216     if self.usePhedex:
217     fullurl="%s/lfn2pfn?node=%s&lfn=%s&protocol=%s"%(self.datasvc_url,self.node,self.lfn,self.protocol)
218     domlfn2pfn = self.lfn2pfn()
219     if not domlfn2pfn :
220     msg="Unable to get info from %s"%fullurl
221     raise CrabException(msg)
222    
223     errormsg = self.parse_error(domlfn2pfn)
224     if errormsg:
225     msg="Error extracting info from %s due to: %s"%(fullurl,errormsg)
226     raise CrabException(msg)
227    
228     stageoutpfn = self.parse_lfn2pfn(domlfn2pfn)
229     if not stageoutpfn:
230 afanfani 1.6 msg ='Unable to get stageout path from TFC at Site %s \n'%self.node
231     msg+=' Please alert the CompInfraSup group through their savannah %s \n'%self.FacOps_savannah
232     msg+=' reporting: \n'
233     msg+=' Summary: Unable to get user stageout from TFC at Site %s \n'%self.node
234     msg+=' OriginalSubmission: stageout path is not retrieved from %s \n'%fullurl
235 spiga 1.3 raise CrabException(msg)
236     else:
237 spiga 1.11 if self.sched in ['CAF','LSF'] :
238 spiga 1.12 stageoutpfn = self.user_se_path+self.lfn
239 spiga 1.11 else:
240     stageoutpfn = 'srm://'+self.node+':'+self.user_port+self.user_se_path+self.lfn
241 spiga 1.3
242     return stageoutpfn
243 afanfani 1.6
244    
245    
246     if __name__ == '__main__':
247     """
248     Sort of unit testing to check Phedex API for whatever site and/or lfn.
249     Usage:
250     python PhEDExDatasvcInfo.py --node T2_IT_Bari --lfn /store/maremma
251    
252     """
253     import getopt,sys
254     from crab_util import *
255     import common
256     klass_name = 'SchedulerGlite'
257     klass = importName(klass_name, klass_name)
258     common.scheduler = klass()
259    
260     lfn="/store/user/"
261     node='T2_IT_Bari'
262     valid = ['node=','lfn=']
263     try:
264     opts, args = getopt.getopt(sys.argv[1:], "", valid)
265     except getopt.GetoptError, ex:
266     print str(ex)
267     sys.exit(1)
268     for o, a in opts:
269     if o == "--node":
270     node = a
271     if o == "--lfn":
272     lfn = a
273    
274     mycfg_params = { 'USER.storage_element': node }
275     dsvc = PhEDExDatasvcInfo(mycfg_params)
276     dsvc.lfn = lfn
277     print dsvc.getStageoutPFN()
278