ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/CRAB/python/DataLocation.py
Revision: 1.14
Committed: Wed Aug 16 15:06:49 2006 UTC (18 years, 8 months ago) by mkirn
Content type: text/x-python
Branch: MAIN
CVS Tags: HEAD_20092006
Changes since 1.13: +1 -1 lines
Log Message:
Fixed dls_endpoint

File Contents

# User Rev Content
1 gutsche 1.7 #!/usr/bin/env python
2 afanfani 1.1 import sys, os, string, re
3     import urllib, urllister
4     import urllib2
5     import common
6     from DLSInfo import *
7    
8     # ####################################
9 afanfani 1.3 class DataLocationError(exceptions.Exception):
10 slacapra 1.5 def __init__(self, errorMessage):
11     args=errorMessage
12     exceptions.Exception.__init__(self, args)
13     pass
14 afanfani 1.3
15 slacapra 1.5 def getErrorMessage(self):
16     """ Return exception error """
17     return "%s" % (self.args)
18 afanfani 1.1
19     # ####################################
20     # class to extact data location
21     class DataLocation:
22     def __init__(self, Listfileblocks, cfg_params):
23    
24     # Attributes
25     self.Listfileblocks = Listfileblocks # DLS input: list of fileblocks lists
26 slacapra 1.5
27 afanfani 1.1 self.cfg_params = cfg_params
28    
29     self.SelectedSites = [] # DLS output: list of sites hosting all the needed fileblocks
30     # retireved using method getSites
31    
32     CEBlackList = []
33     try:
34     tmpBad = string.split(self.cfg_params['EDG.ce_black_list'],',')
35     for tmp in tmpBad:
36     tmp=string.strip(tmp)
37     CEBlackList.append(tmp)
38     except KeyError:
39     pass
40     common.logger.debug(5,'CEBlackList: '+str(CEBlackList))
41     self.reCEBlackList=[]
42     for bad in CEBlackList:
43     self.reCEBlackList.append(re.compile( bad ))
44    
45 afanfani 1.4 CEWhiteList = []
46     try:
47     tmpGood = string.split(self.cfg_params['EDG.ce_white_list'],',')
48     for tmp in tmpGood:
49     tmp=string.strip(tmp)
50     CEWhiteList.append(tmp)
51     except KeyError:
52     pass
53     common.logger.debug(5,'CEWhiteList: '+str(CEWhiteList))
54     self.reCEWhiteList=[]
55     for good in CEWhiteList:
56     self.reCEWhiteList.append(re.compile( good ))
57    
58    
59 afanfani 1.1 # #######################################################################
60     def fetchDLSInfo(self):
61     """
62     Contact DLS
63     """
64     countblock=0
65     Sites = []
66     allblockSites = []
67    
68 afanfani 1.9
69 afanfani 1.8 try:
70     dlstype=self.cfg_params['ORCA.dls_type']
71     except KeyError:
72 afanfani 1.10 ## default to DLS-DLI:
73     dlstype='dli'
74    
75 afanfani 1.8 DLS_type="DLS_TYPE_%s"%dlstype.upper()
76 afanfani 1.6
77 mkirn 1.11 ## find the replicas for each block
78 afanfani 1.1 for fileblocks in self.Listfileblocks:
79 slacapra 1.5 for afileblock in fileblocks:
80     countblock=countblock+1
81     dbspath=string.split(afileblock,'#')[0]
82     (null,ds,tier,ow)=string.split(dbspath,'/')
83     datablock=ow+"/"+ds
84     #
85 mkirn 1.14 dls=DLSInfo(DLS_type,self.cfg_params)
86 slacapra 1.5 try:
87 afanfani 1.6 replicas=dls.getReplicas(datablock)
88 slacapra 1.12 for replica in replicas:
89     Sites.append(replica)
90 slacapra 1.5 except DLSNoReplicas, ex:
91 gutsche 1.13 raise DataLocationError(ex.getErrorMessage())
92 slacapra 1.5 except:
93     raise DataLocationError('')
94 afanfani 1.1
95     ## select only sites that contains _all_ the fileblocks
96     allblockSites=self.SelectSites(countblock,Sites)
97     #for as in allblockSites:
98     # print " site is "+as
99 afanfani 1.4 ## select sites that are not in a BlackList , if any
100     self.SelectedNotBlackSites=self.checkBlackList(allblockSites)
101     ## select sites there are in the white list , if any
102     self.SelectedSites=self.checkWhiteList(self.SelectedNotBlackSites)
103 afanfani 1.1
104     # #######################################################################
105     def getSites(self):
106     """
107 slacapra 1.5 get the sites hosting all the needed data
108 afanfani 1.1 """
109     return self.SelectedSites
110    
111     # #######################################################################
112     def SelectSites(self, countblock, Sites ):
113     """
114 slacapra 1.5 select only sites that contains _all_ the fileblocks
115 afanfani 1.1 """
116     goodSites=[]
117     for aSite in Sites :
118 slacapra 1.5 if ( Sites.count(aSite)==countblock ) :
119     goodSites.append(aSite)
120 afanfani 1.1
121     return self.uniquelist(goodSites)
122    
123     # #######################################################################
124     def checkBlackList(self, Sites):
125     """
126 afanfani 1.4 select sites that are not excluded by the user (via CE black list)
127 afanfani 1.1 """
128     goodSites = []
129     for aSite in Sites:
130     good=1
131     for re in self.reCEBlackList:
132     if re.search(aSite):
133     common.logger.message('CE in black list, skipping site '+aSite)
134     good=0
135     pass
136     if good: goodSites.append(aSite)
137     if len(goodSites) == 0:
138     common.logger.debug(3,"No selected Sites")
139     return goodSites
140    
141 afanfani 1.4 # #######################################################################
142     def checkWhiteList(self, Sites):
143     """
144     select sites that are defined by the user (via CE white list)
145     """
146     if len(self.reCEWhiteList)==0: return Sites
147     goodSites = []
148     for aSite in Sites:
149     good=0
150     for re in self.reCEWhiteList:
151     if re.search(aSite):
152     common.logger.message('CE in white list, adding site '+aSite)
153     good=1
154     pass
155     if good: goodSites.append(aSite)
156     if len(goodSites) == 0:
157     common.logger.debug(3,"No selected Sites")
158     return goodSites
159    
160 afanfani 1.1 #######################################################################
161     def uniquelist(self, old):
162     """
163 slacapra 1.5 remove duplicates from a list
164 afanfani 1.1 """
165     nd={}
166     for e in old:
167     nd[e]=0
168     return nd.keys()
169 slacapra 1.5