1 |
afanfani |
1.1 |
#!/usr/bin/env python2
|
2 |
|
|
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 |
|
|
## find the replicas for each block
|
69 |
|
|
for fileblocks in self.Listfileblocks:
|
70 |
slacapra |
1.5 |
#print fileblocks
|
71 |
|
|
for afileblock in fileblocks:
|
72 |
|
|
countblock=countblock+1
|
73 |
|
|
dbspath=string.split(afileblock,'#')[0]
|
74 |
|
|
(null,ds,tier,ow)=string.split(dbspath,'/')
|
75 |
|
|
datablock=ow+"/"+ds
|
76 |
|
|
#
|
77 |
|
|
dls=DLSInfo(datablock)
|
78 |
|
|
try:
|
79 |
|
|
replicas=dls.getReplicas()
|
80 |
|
|
except DLSNoReplicas, ex:
|
81 |
|
|
raise DataLocationError(ex.getErrorMessage())
|
82 |
|
|
except:
|
83 |
|
|
raise DataLocationError('')
|
84 |
afanfani |
1.1 |
|
85 |
slacapra |
1.5 |
for replica in replicas:
|
86 |
|
|
Sites.append(replica)
|
87 |
afanfani |
1.1 |
|
88 |
|
|
## select only sites that contains _all_ the fileblocks
|
89 |
|
|
allblockSites=self.SelectSites(countblock,Sites)
|
90 |
|
|
#for as in allblockSites:
|
91 |
|
|
# print " site is "+as
|
92 |
afanfani |
1.4 |
## select sites that are not in a BlackList , if any
|
93 |
|
|
self.SelectedNotBlackSites=self.checkBlackList(allblockSites)
|
94 |
|
|
## select sites there are in the white list , if any
|
95 |
|
|
self.SelectedSites=self.checkWhiteList(self.SelectedNotBlackSites)
|
96 |
afanfani |
1.1 |
|
97 |
|
|
# #######################################################################
|
98 |
|
|
def getSites(self):
|
99 |
|
|
"""
|
100 |
slacapra |
1.5 |
get the sites hosting all the needed data
|
101 |
afanfani |
1.1 |
"""
|
102 |
|
|
return self.SelectedSites
|
103 |
|
|
|
104 |
|
|
# #######################################################################
|
105 |
|
|
def SelectSites(self, countblock, Sites ):
|
106 |
|
|
"""
|
107 |
slacapra |
1.5 |
select only sites that contains _all_ the fileblocks
|
108 |
afanfani |
1.1 |
"""
|
109 |
|
|
goodSites=[]
|
110 |
|
|
for aSite in Sites :
|
111 |
slacapra |
1.5 |
if ( Sites.count(aSite)==countblock ) :
|
112 |
|
|
goodSites.append(aSite)
|
113 |
afanfani |
1.1 |
|
114 |
|
|
return self.uniquelist(goodSites)
|
115 |
|
|
|
116 |
|
|
# #######################################################################
|
117 |
|
|
def checkBlackList(self, Sites):
|
118 |
|
|
"""
|
119 |
afanfani |
1.4 |
select sites that are not excluded by the user (via CE black list)
|
120 |
afanfani |
1.1 |
"""
|
121 |
|
|
goodSites = []
|
122 |
|
|
for aSite in Sites:
|
123 |
|
|
good=1
|
124 |
|
|
for re in self.reCEBlackList:
|
125 |
|
|
if re.search(aSite):
|
126 |
|
|
common.logger.message('CE in black list, skipping site '+aSite)
|
127 |
|
|
good=0
|
128 |
|
|
pass
|
129 |
|
|
if good: goodSites.append(aSite)
|
130 |
|
|
if len(goodSites) == 0:
|
131 |
|
|
common.logger.debug(3,"No selected Sites")
|
132 |
|
|
return goodSites
|
133 |
|
|
|
134 |
afanfani |
1.4 |
# #######################################################################
|
135 |
|
|
def checkWhiteList(self, Sites):
|
136 |
|
|
"""
|
137 |
|
|
select sites that are defined by the user (via CE white list)
|
138 |
|
|
"""
|
139 |
|
|
if len(self.reCEWhiteList)==0: return Sites
|
140 |
|
|
goodSites = []
|
141 |
|
|
for aSite in Sites:
|
142 |
|
|
good=0
|
143 |
|
|
for re in self.reCEWhiteList:
|
144 |
|
|
if re.search(aSite):
|
145 |
|
|
common.logger.message('CE in white list, adding site '+aSite)
|
146 |
|
|
good=1
|
147 |
|
|
pass
|
148 |
|
|
if good: goodSites.append(aSite)
|
149 |
|
|
if len(goodSites) == 0:
|
150 |
|
|
common.logger.debug(3,"No selected Sites")
|
151 |
|
|
return goodSites
|
152 |
|
|
|
153 |
afanfani |
1.1 |
#######################################################################
|
154 |
|
|
def uniquelist(self, old):
|
155 |
|
|
"""
|
156 |
slacapra |
1.5 |
remove duplicates from a list
|
157 |
afanfani |
1.1 |
"""
|
158 |
|
|
nd={}
|
159 |
|
|
for e in old:
|
160 |
|
|
nd[e]=0
|
161 |
|
|
return nd.keys()
|
162 |
slacapra |
1.5 |
|