1 |
#!/usr/bin/env python
|
2 |
import sys, os, commands,string, re
|
3 |
import exceptions
|
4 |
from crab_exceptions import *
|
5 |
from crab_util import *
|
6 |
import common
|
7 |
|
8 |
try:
|
9 |
import dlsApi
|
10 |
import dlsClient
|
11 |
from dlsDataObjects import DlsLocation, DlsFileBlock, DlsEntry
|
12 |
except:
|
13 |
try:
|
14 |
Crabpydir=commands.getoutput('which crab')
|
15 |
Topdir=string.replace(Crabpydir,'/python/crab','')
|
16 |
sys.path.append(Topdir+'/DLSAPI')
|
17 |
import dlsApi
|
18 |
import dlsClient
|
19 |
from dlsDataObjects import DlsLocation, DlsFileBlock, DlsEntry
|
20 |
except:
|
21 |
msg="ERROR no DLS API available"
|
22 |
raise CrabException(msg)
|
23 |
|
24 |
## for python 2.2 add the pyexpat.so to PYTHONPATH
|
25 |
pythonV=sys.version.split(' ')[0]
|
26 |
if pythonV.find('2.2') >= 0 :
|
27 |
Crabpydir=commands.getoutput('which crab')
|
28 |
Topdir=string.replace(Crabpydir,'/python/crab','')
|
29 |
extradir=Topdir+'/DLSAPI/extra'
|
30 |
if sys.path.count(extradir) <= 0:
|
31 |
if os.path.exists(extradir):
|
32 |
sys.path.insert(0, extradir)
|
33 |
|
34 |
|
35 |
class DLSError:
|
36 |
def __init__(self, fileblocks):
|
37 |
print '\nERROR accessing DLS for fileblock '+fileblocks+'\n'
|
38 |
pass
|
39 |
|
40 |
|
41 |
class DLSNoReplicas(exceptions.Exception):
|
42 |
def __init__(self, FileBlock):
|
43 |
args ="No replicas exists for fileblock: "+FileBlock+"\n"
|
44 |
exceptions.Exception.__init__(self, args)
|
45 |
pass
|
46 |
|
47 |
def getClassName(self):
|
48 |
""" Return class name. """
|
49 |
return "%s" % (self.__class__.__name__)
|
50 |
|
51 |
def getErrorMessage(self):
|
52 |
""" Return exception error. """
|
53 |
return "%s" % (self.args)
|
54 |
|
55 |
|
56 |
##############################################################################
|
57 |
# Class to extract info from DLS
|
58 |
##############################################################################
|
59 |
|
60 |
class DLSInfo:
|
61 |
def __init__(self, type):
|
62 |
if type=="DLS_TYPE_DLI":
|
63 |
endpoint="lfc-cms-test.cern.ch/grid/cms/DLS/LFCProto"
|
64 |
try:
|
65 |
import xml.dom.ext.reader
|
66 |
except:
|
67 |
crabdir=os.getenv('CRABDIR')
|
68 |
## Let the user set up PyXML by hand
|
69 |
msg="There is no setup of PyXML python module required by DLS (DLI). Do the following:\n"
|
70 |
msg+=" - check that in %s/configure the function configureDLSAPI is not commented \n"%crabdir
|
71 |
msg+=" - uncomment it and re-run the configuration :"
|
72 |
msg+="\n cd %s\n"%crabdir
|
73 |
msg+=" ./configure\n"
|
74 |
msg+=" source crab.(c)sh\n"
|
75 |
raise CrabException(msg)
|
76 |
|
77 |
elif type=="DLS_TYPE_MYSQL":
|
78 |
endpoint="lxgate10.cern.ch:18081"
|
79 |
else:
|
80 |
msg = "DLS type %s not among the supported DLS ( DLS_TYPE_DLI and DLS_TYPE_MYSQL ) "%type
|
81 |
raise CrabException(msg)
|
82 |
|
83 |
try:
|
84 |
self.api = dlsClient.getDlsApi(dls_type=type,dls_endpoint=endpoint)
|
85 |
except dlsApi.DlsApiError, inst:
|
86 |
msg = "Error when binding the DLS interface: %s Server %s"%(str(inst),self.DLSServer_)
|
87 |
#print msg
|
88 |
raise CrabException(msg)
|
89 |
|
90 |
# ####################################
|
91 |
def getReplicas(self,fileblocks):
|
92 |
"""
|
93 |
query DLS to get replicas
|
94 |
"""
|
95 |
##
|
96 |
try:
|
97 |
entryList=self.api.getLocations([fileblocks])
|
98 |
except dlsApi.DlsApiError, inst:
|
99 |
msg = "Error in the DLS query: %s." % str(inst)
|
100 |
#print msg
|
101 |
raise DLSNoReplicas(fileblocks)
|
102 |
|
103 |
ListSites=[]
|
104 |
for entry in entryList:
|
105 |
for loc in entry.locations:
|
106 |
ListSites.append(str(loc.host))
|
107 |
if len(ListSites)<=0:
|
108 |
raise DLSNoReplicas(fileblocks)
|
109 |
|
110 |
return ListSites
|