ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/PhEDExDatasvcInfo.py
Revision: 1.13
Committed: Thu Oct 23 17:07:22 2008 UTC (16 years, 6 months ago) by fanzago
Content type: text/x-python
Branch: MAIN
CVS Tags: CRAB_2_4_1_pre3
Changes since 1.12: +5 -1 lines
Log Message:
changed for publication of data stored in not official CMS sites

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 fanzago 1.13 #lfn = LFNBase(primaryDataset,self.publish_data_name,LocalUser=l_User) + '/${PSETHASH}/'
129     if self.usePhedex:
130     lfn = LFNBase('/store/user', primaryDataset, self.publish_data_name, LocalUser=l_User) + '/${PSETHASH}/'
131     else:
132     lfn = LFNBase(self.user_lfn, primaryDataset, self.publish_data_name, LocalUser=l_User) + '/${PSETHASH}/'
133 spiga 1.3 else:
134 spiga 1.5 if self.sched in ['CAF','LSF']: l_User=True
135 spiga 1.3 lfn = LFNBase(self.user_remote_dir,LocalUser=l_User)
136     return lfn
137    
138     def computePrimaryDataset(self):
139     """
140     compute the last part for the LFN in case of publication
141     """
142     if (self.datasetpath.upper() != 'NONE'):
143     primarydataset = self.datasetpath.split("/")[1]
144     else:
145     primarydataset = self.publish_data_name
146     return primarydataset
147    
148     def lfn2pfn(self):
149     """
150     PhEDEx Data Service lfn2pfn call
151    
152     input: LFN,node name,protocol
153     returns: DOM object with the content of the PhEDEx Data Service call
154     """
155     params = {'node' : self.node , 'lfn': self.lfn , 'protocol': self.protocol}
156     params = urllib.urlencode(params)
157     datasvc_lfn2pfn="%s/lfn2pfn"%self.datasvc_url
158     urlresults = urllib.urlopen(datasvc_lfn2pfn, params)
159     try:
160     urlresults = parse(urlresults)
161     except:
162     urlresults = None
163    
164     return urlresults
165 afanfani 1.1
166 spiga 1.3 def parse_error(self,urlresults):
167     """
168     look for errors in the DOM object returned by PhEDEx Data Service call
169     """
170     errormsg = None
171     errors=urlresults.getElementsByTagName('error')
172     for error in errors:
173     errormsg=error.childNodes[0].data
174     if len(error.childNodes)>1:
175     errormsg+=error.childNodes[1].data
176     return errormsg
177    
178     def parse_lfn2pfn(self,urlresults):
179     """
180     Parse the content of the result of lfn2pfn PhEDEx Data Service call
181    
182     input: DOM object with the content of the lfn2pfn call
183     returns: PFN
184     """
185     result = urlresults.getElementsByTagName('phedex')
186    
187     if not result:
188     return []
189     result = result[0]
190     pfn = None
191     mapping = result.getElementsByTagName('mapping')
192     for m in mapping:
193     pfn=m.getAttribute("pfn")
194     if pfn:
195     return pfn
196    
197     def getStageoutPFN( self ):
198     """
199     input: LFN,node name,protocol
200     returns: PFN
201     """
202     if self.usePhedex:
203     fullurl="%s/lfn2pfn?node=%s&lfn=%s&protocol=%s"%(self.datasvc_url,self.node,self.lfn,self.protocol)
204     domlfn2pfn = self.lfn2pfn()
205     if not domlfn2pfn :
206     msg="Unable to get info from %s"%fullurl
207     raise CrabException(msg)
208    
209     errormsg = self.parse_error(domlfn2pfn)
210     if errormsg:
211     msg="Error extracting info from %s due to: %s"%(fullurl,errormsg)
212     raise CrabException(msg)
213    
214     stageoutpfn = self.parse_lfn2pfn(domlfn2pfn)
215     if not stageoutpfn:
216 afanfani 1.6 msg ='Unable to get stageout path from TFC at Site %s \n'%self.node
217     msg+=' Please alert the CompInfraSup group through their savannah %s \n'%self.FacOps_savannah
218     msg+=' reporting: \n'
219     msg+=' Summary: Unable to get user stageout from TFC at Site %s \n'%self.node
220     msg+=' OriginalSubmission: stageout path is not retrieved from %s \n'%fullurl
221 spiga 1.3 raise CrabException(msg)
222     else:
223 spiga 1.11 if self.sched in ['CAF','LSF'] :
224 spiga 1.12 stageoutpfn = self.user_se_path+self.lfn
225 spiga 1.11 else:
226     stageoutpfn = 'srm://'+self.node+':'+self.user_port+self.user_se_path+self.lfn
227 spiga 1.3
228     return stageoutpfn
229 afanfani 1.6
230    
231    
232     if __name__ == '__main__':
233     """
234     Sort of unit testing to check Phedex API for whatever site and/or lfn.
235     Usage:
236     python PhEDExDatasvcInfo.py --node T2_IT_Bari --lfn /store/maremma
237    
238     """
239     import getopt,sys
240     from crab_util import *
241     import common
242     klass_name = 'SchedulerGlite'
243     klass = importName(klass_name, klass_name)
244     common.scheduler = klass()
245    
246     lfn="/store/user/"
247     node='T2_IT_Bari'
248     valid = ['node=','lfn=']
249     try:
250     opts, args = getopt.getopt(sys.argv[1:], "", valid)
251     except getopt.GetoptError, ex:
252     print str(ex)
253     sys.exit(1)
254     for o, a in opts:
255     if o == "--node":
256     node = a
257     if o == "--lfn":
258     lfn = a
259    
260     mycfg_params = { 'USER.storage_element': node }
261     dsvc = PhEDExDatasvcInfo(mycfg_params)
262     dsvc.lfn = lfn
263     print dsvc.getStageoutPFN()
264