ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/PhEDExDatasvcInfo.py
(Generate patch)

Comparing COMP/CRAB/python/PhEDExDatasvcInfo.py (file contents):
Revision 1.2 by afanfani, Fri Jul 18 10:00:54 2008 UTC vs.
Revision 1.12 by spiga, Wed Oct 15 13:54:13 2008 UTC

# Line 1 | Line 1
1 < #!/usr/bin/env python
2 <
1 > from Actor import *
2   import urllib
3   from xml.dom.minidom import parse
4   from crab_exceptions import *
5 + from crab_logger import Logger
6 + from WorkSpace import *
7 + from urlparse import urlparse
8 + from LFNBaseName import *
9  
10   class PhEDExDatasvcInfo:
11 <  """
12 <  provides information from PhEDEx Data Service
13 <  """
14 <  ## PhEDEx Data Service URL
15 <  #datasvc_url="https://cmsweb.cern.ch/phedex/test/datasvc/xml/prod"
16 <  datasvc_url="https://cmsweb.cern.ch/phedex/datasvc/xml/prod"
11 >    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 >        self.FacOps_savannah = 'https://savannah.cern.ch/support/?func=additem&group=cmscompinfrasup'
18 >
19 >
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 >        self.user_remote_dir = cfg_params.get("USER.user_remote_dir",'')
26 >        if self.user_remote_dir:
27 >            if ( self.user_remote_dir[-1] != '/' ) : self.user_remote_dir = self.user_remote_dir + '/'
28 >            
29 >        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 >        if self.user_lfn:
34 >            if ( self.user_lfn[-1] != '/' ) : self.user_lfn = self.user_lfn + '/'
35 >            
36 >        self.user_port = cfg_params.get("USER.storage_port",'8443')
37 >        self.user_se_path = cfg_params.get("USER.storage_path",'')
38 >        if self.user_se_path:
39 >            if ( self.user_se_path[-1] != '/' ) : self.user_se_path = self.user_se_path + '/'
40 >                                                    
41 >      
42 >        #check if using "private" Storage
43 >        self.usePhedex = True
44 >        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 >        if (self.node.find('T1_') + self.node.find('T2_')+self.node.find('T3_')) == -3: self.usePhedex = False
50 >        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 >            msg += '      For further information please visit : %s'%stage_out_faq
54 >            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 <  def lfn2pfn(self,node,lfn,protocol):
71 <      """
72 <      PhEDEx Data Service lfn2pfn call
73 <
74 <      input:   LFN,node name,protocol
75 <      returns: DOM object with the content of the PhEDEx Data Service call
76 <      """  
77 <      params = {'node' : node , 'lfn': lfn , 'protocol': protocol}
78 <      params = urllib.urlencode(params)
79 <      #print params
80 <      datasvc_lfn2pfn="%s/lfn2pfn"%self.datasvc_url
81 <      urlresults = urllib.urlopen(datasvc_lfn2pfn, params)
82 <      try:
83 <          urlresults = parse(urlresults)
84 <      except:
85 <          urlresults = None
86 <      return urlresults
87 <
88 <  def parse_error(self,urlresults):
89 <      """
90 <      look for errors in the DOM object returned by PhEDEx Data Service call
91 <      """
92 <      errormsg = None
93 <      errors=urlresults.getElementsByTagName('error')
94 <      for error in errors:
95 <          errormsg=error.childNodes[0].data
96 <          if len(error.childNodes)>1:
97 <             errormsg+=error.childNodes[1].data
98 <      return errormsg
99 <
100 <  def parse_lfn2pfn(self,urlresults):
101 <      """
102 <      Parse the content of the result of lfn2pfn PhEDEx Data Service  call
103 <
104 <      input:    DOM object with the content of the lfn2pfn call
105 <      returns:  PFN  
106 <      """
107 <      result = urlresults.getElementsByTagName('phedex')
108 <      if not result:
109 <            return []
110 <      result = result[0]
111 <      pfn = None
112 <      mapping = result.getElementsByTagName('mapping')
113 <      for m in mapping:
114 <          pfn=m.getAttribute("pfn")
115 <          if pfn:
116 <            return pfn
117 <
118 <  def getStageoutPFN(self,node,lfn,protocol):
119 <      """
120 <      input:   LFN,node name,protocol
121 <      returns: PFN
122 <      """
123 <      fullurl="%s/lfn2pfn?node=%s&lfn=%s&protocol=%s"%(self.datasvc_url,node,lfn,protocol)
124 <      domlfn2pfn = self.lfn2pfn(node,lfn,protocol)
125 <      if not domlfn2pfn :
126 <          msg="Unable to get info from %s"%fullurl
127 <          raise CrabException(msg)
128 <
129 <      errormsg = self.parse_error(domlfn2pfn)
130 <      if errormsg:
131 <          msg="Error extracting info from %s due to: %s"%(fullurl,errormsg)
132 <          raise CrabException(msg)
133 <
134 <      stageoutpfn = self.parse_lfn2pfn(domlfn2pfn)
135 <      if not stageoutpfn:
136 <          msg="Unable to get stageout path (PFN) from %s"%fullurl
137 <          raise CrabException(msg)
138 <      return stageoutpfn
139 <
140 <
141 < if __name__ == '__main__' :
142 <    """
143 <    """
144 <    from crab_logger import Logger
145 <    from WorkSpace import *
146 <    continue_dir="/home/fanfani/CRAB"
92 <    cfg_params={'USER.logdir' : continue_dir }
93 <    common.work_space = WorkSpace(continue_dir, cfg_params)
94 <    log = Logger()
95 <    common.logger = log
96 <
97 <    from LFNBaseName import *
98 <    # test values
99 <    lfn = LFNBase("datasetstring")
100 <    node='T2_IT_Bari'
101 <    protocol="srmv2"
102 <
103 <    #create an instance of the PhEDExDatasvcInfo object
104 <    dsvc = PhEDExDatasvcInfo()
105 <    #extract the PFN for the given node,LFN,protocol
106 <    print "Stageout to %s"%dsvc.getStageoutPFN(node,lfn,protocol)
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 >                ### FEDE added SE ###
87 >                SE = self.sched
88 >            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 >  
106 >
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 >            if self.sched in ['CAF','LSF']: l_User=True
131 >            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 +
162 +    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 +                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 +                raise CrabException(msg)
218 +        else:
219 +            if self.sched in ['CAF','LSF'] :
220 +                stageoutpfn = self.user_se_path+self.lfn
221 +            else:
222 +                stageoutpfn = 'srm://'+self.node+':'+self.user_port+self.user_se_path+self.lfn
223 +
224 +        return stageoutpfn
225 +
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 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines