1 |
#!/usr/bin/env python
|
2 |
|
3 |
import sys, os, getopt
|
4 |
|
5 |
import dbsCgiApi
|
6 |
import dbsApi
|
7 |
|
8 |
import dlsApi
|
9 |
import dlsClient
|
10 |
from dlsDataObjects import DlsLocation, DlsFileBlock, DlsEntry
|
11 |
import xml.dom.ext.reader
|
12 |
|
13 |
|
14 |
def main(argv) :
|
15 |
"""
|
16 |
|
17 |
DBSDLSQuery
|
18 |
|
19 |
query DBS and DLS to discovery datasets
|
20 |
|
21 |
required parameters
|
22 |
--pattern <pattern> : pattern to restrict discovery
|
23 |
|
24 |
optional parameters :
|
25 |
--help (-h) : help
|
26 |
--debug (-d) : debug statements
|
27 |
|
28 |
"""
|
29 |
|
30 |
# default
|
31 |
pattern = ''
|
32 |
debug = 0
|
33 |
|
34 |
try:
|
35 |
opts, args = getopt.getopt(argv, "", ["help", "debug", "pattern="])
|
36 |
except getopt.GetoptError:
|
37 |
print main.__doc__
|
38 |
sys.exit(2)
|
39 |
|
40 |
# check command line parameter
|
41 |
for opt, arg in opts :
|
42 |
if opt == "--help" :
|
43 |
print main.__doc__
|
44 |
sys.exit()
|
45 |
elif opt == "--debug" :
|
46 |
debug = 1
|
47 |
elif opt == "--pattern" :
|
48 |
pattern = arg
|
49 |
|
50 |
# guarantee that pattern has "*" at beginning and end
|
51 |
if pattern != '' :
|
52 |
if pattern[0] != '*' :
|
53 |
pattern = '*'+pattern
|
54 |
if pattern[-1] != '*' :
|
55 |
pattern = pattern+'*'
|
56 |
else :
|
57 |
pattern = '*'
|
58 |
|
59 |
# DBS initialization
|
60 |
dbs_url="http://cmsdoc.cern.ch/cms/test/aprom/DBS/CGIServer/prodquery"
|
61 |
dbs_instance = {'instance' : 'MCGlobal/Writer'}
|
62 |
dbs = dbsCgiApi.DbsCgiApi(dbs_url, dbs_instance)
|
63 |
|
64 |
# DLS initialization
|
65 |
type = "DLS_TYPE_DLI"
|
66 |
endpoint="prod-lfc-cms-central.cern.ch/grid/cms/DLS/LFC"
|
67 |
dls = dlsClient.getDlsApi(dls_type=type,dls_endpoint=endpoint)
|
68 |
|
69 |
# DBS query for datasets
|
70 |
try:
|
71 |
dbsDatasets = dbs.listProcessedDatasets(pattern)
|
72 |
except dbsApi.InvalidDataTier, ex:
|
73 |
raise DBSInvalidDataTierError(ex.getClassName(),ex.getErrorMessage())
|
74 |
except dbsApi.DbsApiException, ex:
|
75 |
raise DBSError(ex.getClassName(),ex.getErrorMessage())
|
76 |
except dbsCgiApi.DbsCgiToolError , ex:
|
77 |
raise DBSError(ex.getClassName(),ex.getErrorMessage())
|
78 |
except dbsCgiApi.DbsCgiBadResponse , ex:
|
79 |
raise DBSError(ex.getClassName(),ex.getErrorMessage())
|
80 |
|
81 |
for dbsDataset in dbsDatasets:
|
82 |
dataset = dbsDataset.get('datasetPathName')
|
83 |
|
84 |
# DBS query for fileblocks
|
85 |
eventsPerBlock = {}
|
86 |
try:
|
87 |
fileBlocks = dbs.getDatasetContents(dataset)
|
88 |
except dbsApi.DbsApiException, ex:
|
89 |
raise DBSError(ex.getClassName(),ex.getErrorMessage())
|
90 |
except dbsCgiApi.DbsCgiBadResponse, ex:
|
91 |
raise DBSError(ex.getClassName(),ex.getErrorMessage())
|
92 |
for fileBlock in fileBlocks:
|
93 |
## get the event collections for each block
|
94 |
nevts = 0
|
95 |
eventCollectionList = fileBlock.get('eventCollectionList')
|
96 |
for eventCollection in eventCollectionList:
|
97 |
nevts = nevts + eventCollection.get('numberOfEvents')
|
98 |
|
99 |
eventsPerBlock[fileBlock.get('blockName')]=nevts
|
100 |
if debug :
|
101 |
print 'DBS query result in block:',fileBlock.get('blockName'),'with',nevts,'events'
|
102 |
|
103 |
siteDictionary = {}
|
104 |
for fileBlock in eventsPerBlock.keys() :
|
105 |
if eventsPerBlock[fileBlock] > 0 :
|
106 |
# query DLS
|
107 |
try:
|
108 |
locationList = dls.getLocations([fileBlock])
|
109 |
except dlsApi.DlsApiError, inst:
|
110 |
# create empty list
|
111 |
locationList = {}
|
112 |
msg = "Error in the DLS query: %s." % str(inst)
|
113 |
if debug :
|
114 |
print msg
|
115 |
pass
|
116 |
|
117 |
for locationItem in locationList:
|
118 |
for location in locationItem.locations:
|
119 |
host = str(location.host)
|
120 |
if host in siteDictionary.keys() :
|
121 |
siteDictionary[host].append(fileBlock)
|
122 |
else :
|
123 |
siteDictionary[host] = [fileBlock]
|
124 |
|
125 |
for site in siteDictionary.keys() :
|
126 |
events = 0
|
127 |
for block in siteDictionary[site] :
|
128 |
events += eventsPerBlock[block]
|
129 |
print dataset,site,events
|
130 |
|
131 |
if __name__ == '__main__' :
|
132 |
main(sys.argv[1:])
|