ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/JOBROBOT/DBSDLSQuery.py
Revision: 1.2
Committed: Mon Oct 16 18:54:41 2006 UTC (18 years, 6 months ago) by gutsche
Content type: text/x-python
Branch: MAIN
Changes since 1.1: +6 -0 lines
Log Message:
secured script when DLS query fails

File Contents

# User Rev Content
1 gutsche 1.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     if pattern == '' :
51     print main.__doc__
52     sys.exit(2)
53    
54     # guarantee that pattern has "*" at beginning and end
55     if pattern[0] != '*' :
56     pattern = '*'+pattern
57     if pattern[-1] != '*' :
58     pattern = pattern+'*'
59    
60     # DBS initialization
61     dbs_url="http://cmsdoc.cern.ch/cms/test/aprom/DBS/CGIServer/prodquery"
62     dbs_instance = {'instance' : 'MCGlobal/Writer'}
63     dbs = dbsCgiApi.DbsCgiApi(dbs_url, dbs_instance)
64    
65     # DLS initialization
66     type = "DLS_TYPE_DLI"
67     endpoint="prod-lfc-cms-central.cern.ch/grid/cms/DLS/LFC"
68     dls = dlsClient.getDlsApi(dls_type=type,dls_endpoint=endpoint)
69    
70     # DBS query for datasets
71     try:
72     dbsDatasets = dbs.listProcessedDatasets(pattern)
73     except dbsApi.InvalidDataTier, ex:
74     raise DBSInvalidDataTierError(ex.getClassName(),ex.getErrorMessage())
75     except dbsApi.DbsApiException, ex:
76     raise DBSError(ex.getClassName(),ex.getErrorMessage())
77     except dbsCgiApi.DbsCgiToolError , ex:
78     raise DBSError(ex.getClassName(),ex.getErrorMessage())
79     except dbsCgiApi.DbsCgiBadResponse , ex:
80     raise DBSError(ex.getClassName(),ex.getErrorMessage())
81    
82     for dbsDataset in dbsDatasets:
83     dataset = dbsDataset.get('datasetPathName')
84    
85     # DBS query for fileblocks
86     eventsPerBlock = {}
87     try:
88     fileBlocks = dbs.getDatasetContents(dataset)
89     except dbsApi.DbsApiException, ex:
90     raise DBSError(ex.getClassName(),ex.getErrorMessage())
91     except dbsCgiApi.DbsCgiBadResponse, ex:
92     raise DBSError(ex.getClassName(),ex.getErrorMessage())
93     for fileBlock in fileBlocks:
94     ## get the event collections for each block
95     nevts = 0
96     eventCollectionList = fileBlock.get('eventCollectionList')
97     for eventCollection in eventCollectionList:
98     nevts = nevts + eventCollection.get('numberOfEvents')
99    
100     eventsPerBlock[fileBlock.get('blockName')]=nevts
101 gutsche 1.2 if debug :
102     print 'DBS query result in block:',fileBlock.get('blockName'),'with',nevts,'events'
103 gutsche 1.1
104     siteDictionary = {}
105     for fileBlock in eventsPerBlock.keys() :
106     if eventsPerBlock[fileBlock] > 0 :
107     # query DLS
108     try:
109     locationList = dls.getLocations([fileBlock])
110     except dlsApi.DlsApiError, inst:
111 gutsche 1.2 # create empty list
112     locationList = {}
113 gutsche 1.1 msg = "Error in the DLS query: %s." % str(inst)
114 gutsche 1.2 if debug :
115     print msg
116 gutsche 1.1 pass
117    
118     for locationItem in locationList:
119     for location in locationItem.locations:
120     host = str(location.host)
121     if host in siteDictionary.keys() :
122     siteDictionary[host].append(fileBlock)
123     else :
124     siteDictionary[host] = [fileBlock]
125    
126     for site in siteDictionary.keys() :
127     events = 0
128     for block in siteDictionary[site] :
129     events += eventsPerBlock[block]
130     print dataset,site,events
131    
132     if __name__ == '__main__' :
133     main(sys.argv[1:])