ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/PhEDExDatasvcInfo.py
Revision: 1.12
Committed: Wed Oct 15 13:54:13 2008 UTC (16 years, 6 months ago) by spiga
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_4_1_pre2, CRAB_2_4_1_pre1, CRAB_2_4_0_Tutorial, CRAB_2_4_0_Tutorial_pre1, CRAB_2_4_0
Changes since 1.11: +1 -1 lines
Log Message:
on castor NOT needed srm portvim PhEDExDatasvcInfo.py

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