ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/PhEDExDatasvcInfo.py
Revision: 1.14
Committed: Thu Oct 23 19:53:23 2008 UTC (16 years, 6 months ago) by spiga
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_4_2_pre2, CRAB_2_4_2_pre1, CRAB_2_4_1, CRAB_2_4_1_pre4
Changes since 1.13: +5 -7 lines
Log Message:
minor fix

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