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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines