ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/PhEDExDatasvcInfo.py
Revision: 1.17
Committed: Sat Nov 22 12:01:33 2008 UTC (16 years, 5 months ago) by spiga
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_4_3_pre7, CRAB_2_4_3_pre6, CRAB_2_4_3_pre5, CRAB_2_4_3_pre3
Changes since 1.16: +15 -13 lines
Log Message:
typos. Messages and protection improvements

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