ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/PhEDExDatasvcInfo.py
Revision: 1.16
Committed: Fri Nov 21 14:08:17 2008 UTC (16 years, 5 months ago) by spiga
Content type: text/x-python
Branch: MAIN
Changes since 1.15: +0 -14 lines
Log Message:
lfn parameter not more supported

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