1 |
#!/usr/bin/env python
|
2 |
|
3 |
import sys, os, getopt
|
4 |
|
5 |
#import dbsCgiApi, dbsApi
|
6 |
import DBSAPI.dbsApi
|
7 |
|
8 |
import dlsApi
|
9 |
import dlsClient
|
10 |
from dlsDataObjects import DlsLocation, DlsFileBlock, DlsEntry
|
11 |
import xml.dom.ext.reader
|
12 |
|
13 |
import exceptions
|
14 |
from DBSAPI.dbsApiException import *
|
15 |
import common
|
16 |
|
17 |
|
18 |
# #######################################
|
19 |
class DBSError_DBS2(exceptions.Exception):
|
20 |
def __init__(self, errorName, errorMessage):
|
21 |
args='\nERROR DBS %s : %s \n'%(errorName,errorMessage)
|
22 |
exceptions.Exception.__init__(self, args)
|
23 |
pass
|
24 |
|
25 |
def getErrorMessage(self):
|
26 |
""" Return error message """
|
27 |
return "%s" % (self.args)
|
28 |
|
29 |
# ####################################
|
30 |
class DataDiscoveryError_DBS2(exceptions.Exception):
|
31 |
def __init__(self, errorMessage):
|
32 |
self.args=errorMessage
|
33 |
exceptions.Exception.__init__(self, self.args)
|
34 |
pass
|
35 |
|
36 |
def getErrorMessage(self):
|
37 |
""" Return exception error """
|
38 |
return "%s" % (self.args)
|
39 |
|
40 |
# ####################################
|
41 |
class NotExistingDatasetError_DBS2(exceptions.Exception):
|
42 |
def __init__(self, errorMessage):
|
43 |
self.args=errorMessage
|
44 |
exceptions.Exception.__init__(self, self.args)
|
45 |
pass
|
46 |
|
47 |
def getErrorMessage(self):
|
48 |
""" Return exception error """
|
49 |
return "%s" % (self.args)
|
50 |
|
51 |
# #######################################
|
52 |
def main(argv) :
|
53 |
"""
|
54 |
|
55 |
DBSDLSQuery
|
56 |
|
57 |
query DBS and DLS to discovery datasets
|
58 |
|
59 |
required parameters
|
60 |
--pattern <pattern> : pattern to restrict discovery
|
61 |
|
62 |
optional parameters :
|
63 |
--help (-h) : help
|
64 |
--debug (-d) : debug statements
|
65 |
|
66 |
"""
|
67 |
|
68 |
# default
|
69 |
pattern = ''
|
70 |
debug = 0
|
71 |
|
72 |
try:
|
73 |
opts, args = getopt.getopt(argv, "", ["help", "debug", "pattern="])
|
74 |
except getopt.GetoptError:
|
75 |
print main.__doc__
|
76 |
sys.exit(2)
|
77 |
|
78 |
# check command line parameter
|
79 |
for opt, arg in opts :
|
80 |
if opt == "--help" :
|
81 |
print main.__doc__
|
82 |
sys.exit()
|
83 |
elif opt == "--debug" :
|
84 |
debug = 1
|
85 |
elif opt == "--pattern" :
|
86 |
pattern = arg
|
87 |
|
88 |
# guarantee that pattern has "*" at beginning and end
|
89 |
if pattern != '' :
|
90 |
if pattern[0] != '*' :
|
91 |
pattern = '*'+pattern
|
92 |
if pattern[-1] != '*' :
|
93 |
pattern = pattern+'*'
|
94 |
else :
|
95 |
pattern = '*'
|
96 |
|
97 |
|
98 |
# DBS initialization
|
99 |
dbs_url="http://cmsdbsprod.cern.ch/cms_dbs_prod_global/servlet/DBSServlet"
|
100 |
dbs_version="v00_00_06"
|
101 |
args = {}
|
102 |
args['url'] = dbs_url
|
103 |
args['version'] = dbs_version
|
104 |
args['level'] = 'CRITICAL'
|
105 |
|
106 |
dbs = DBSAPI.dbsApi.DbsApi(args)
|
107 |
|
108 |
# DLS initialization
|
109 |
type = "DLS_TYPE_DBS"
|
110 |
endpoint= "http://cmsdbsprod.cern.ch/cms_dbs_prod_global/servlet/DBSServlet"
|
111 |
dls = dlsClient.getDlsApi(dls_type=type,dls_endpoint=endpoint)
|
112 |
|
113 |
|
114 |
# DBS query for datasets
|
115 |
dbsDatasets = dbs.listProcessedDatasets(pattern)
|
116 |
|
117 |
for dbsDataset in dbsDatasets:
|
118 |
|
119 |
dataset = dbsDataset.get('PathList')[0]
|
120 |
|
121 |
# DBS query for fileblocks
|
122 |
eventsPerBlock = {}
|
123 |
|
124 |
try:
|
125 |
files = dbs.listFiles(dataset)
|
126 |
except DbsBadRequest, msg:
|
127 |
raise DataDiscoveryError_DBS2(msg)
|
128 |
except DBSError_DBS2, msg:
|
129 |
raise DataDiscoveryError_DBS2(msg)
|
130 |
|
131 |
|
132 |
for file in files :
|
133 |
|
134 |
filename = file['LogicalFileName']
|
135 |
events = file['NumberOfEvents']
|
136 |
fileblock = file['Block']['Name']
|
137 |
|
138 |
# number of events per block
|
139 |
if fileblock in eventsPerBlock.keys() :
|
140 |
eventsPerBlock[fileblock] += events
|
141 |
else :
|
142 |
eventsPerBlock[fileblock] = events
|
143 |
|
144 |
|
145 |
if len(eventsPerBlock) <= 0:
|
146 |
raise NotExistingDatasetError_DBS2 (("\nNo data for %s in DBS\nPlease check" + " dataset path variables in crab.cfg") % self.datasetPath)
|
147 |
|
148 |
siteDictionary = {}
|
149 |
for fileBlock in eventsPerBlock.keys() :
|
150 |
if eventsPerBlock[fileBlock] > 0 :
|
151 |
# query DLS
|
152 |
try:
|
153 |
locationList = dls.getLocations([fileBlock])
|
154 |
except dlsApi.DlsApiError, inst:
|
155 |
# create empty list
|
156 |
locationList = {}
|
157 |
msg = "Error in the DLS query: %s." % str(inst)
|
158 |
if debug :
|
159 |
print msg
|
160 |
pass
|
161 |
|
162 |
for locationItem in locationList:
|
163 |
for location in locationItem.locations:
|
164 |
host = str(location.host)
|
165 |
if host in siteDictionary.keys() :
|
166 |
siteDictionary[host].append(fileBlock)
|
167 |
else :
|
168 |
siteDictionary[host] = [fileBlock]
|
169 |
|
170 |
for site in siteDictionary.keys() :
|
171 |
events = 0
|
172 |
for block in siteDictionary[site] :
|
173 |
events += eventsPerBlock[block]
|
174 |
print dataset,site,events
|
175 |
|
176 |
if __name__ == '__main__' :
|
177 |
main(sys.argv[1:])
|