forked from CMSCompOps/WmAgentScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbs3Client.py
executable file
·368 lines (328 loc) · 11.4 KB
/
dbs3Client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env python
"""
DBS 3 Client
Ports all the functionality from previously used
dbsTest.py to use DBS3 directly.
"""
import urllib2,urllib, httplib, sys, re, os, json, datetime
from xml.dom.minidom import getDOMImplementation
from dbs.apis.dbsClient import DbsApi
#das_host='https://das.cern.ch'
das_host='https://cmsweb.cern.ch'
#das_host='https://cmsweb-testbed.cern.ch'
#das_host='https://das-dbs3.cern.ch'
#das_host='https://dastest.cern.ch'
dbs3_url = r'https://cmsweb.cern.ch/dbs/prod/global/DBSReader'
def duplicateRunLumi(dataset, verbose=False, skipInvalid=False):
"""
checks if output dataset has duplicate lumis
for every run.
returns true if at least one duplicate lumi was found
That is if there is the same lumi in the same run and
two different files
This can be used on datasets that have separate
runs.
Verbose: if true prints details
skipInvalid: if true skips invalid files, by default is False because is faster
"""
dbsapi = DbsApi(url=dbs3_url)
duplicated = False
#check each run
runs = getRunsDataset(dataset)
#if only one run in the list
if len(runs) == 1:
if verbose:
print "only one run:",runs
return duplicateLumi(dataset, verbose, skipInvalid)
#else manually
for run in runs:
#create a set
lumisChecked={}
# retrieve files for that run
reply = dbsapi.listFiles(dataset=dataset, detail=skipInvalid)
for f in reply:
#skip invalid files
if skipInvalid and f['is_file_valid'] != 1 :
continue
logical_file_name = f['logical_file_name']
reply2 = dbsapi.listFileLumis(logical_file_name=logical_file_name, run_num=run)
#retrieve lumis for each file
if reply2:
lumis = reply2[0]['lumi_section_num']
else:
continue
#check that each lumi is only in one file
for lumi in lumis:
if lumi in lumisChecked:
#if verbose print results, if not end quickly
if verbose:
print 'Lumi',lumi,'in run',run,'is in these files'
print logical_file_name
print lumisChecked[lumi]
duplicated = True
else:
return True
else:
lumisChecked[lumi] = logical_file_name
return duplicated
def duplicateLumi(dataset, verbose=False, skipInvalid=False):
"""
checks if output dataset has duplicate lumis
returns true if at least one duplicate lumi was found
Verbose: if true prints details
skipInvalid: if true skips invalid files, by default is False because is faster
"""
# initialize API to DBS3
dbsapi = DbsApi(url=dbs3_url)
duplicated = False
lumisChecked={}
# retrieve files
reply = dbsapi.listFiles(dataset=dataset, detail=skipInvalid)
for f in reply:
logical_file_name = f['logical_file_name']
#skip invalid files
if skipInvalid and f['is_file_valid'] != 1 :
continue
reply2 = dbsapi.listFileLumis(logical_file_name=logical_file_name)
#retrieve lumis for each file
lumis = reply2[0]['lumi_section_num']
#check that each lumi is only in one file
for lumi in lumis:
if lumi in lumisChecked:
#if verbose print results, if not end quickly
if verbose:
print 'Lumi',lumi,'is in these files'
print logical_file_name
print lumisChecked[lumi]
duplicated = True
else:
return True
else:
lumisChecked[lumi] = logical_file_name
return duplicated
def getDatasetInfo(dataset):
"""
Gets a summary of a dataset, returns a tuple with:
( Open for writing: 1 if at least one block is open for writing,
creation date: the creation date of the first block,
last modified: the latest modification date)
"""
dbsapi = DbsApi(url=dbs3_url)
reply = dbsapi.listBlocks(dataset=dataset, detail=True)
if not reply:
return (0,0,0)
#first block
max_last_modified = reply[0]['last_modification_date']
min_creation_date = reply[0]['creation_date']
open_for_writing = reply[0]['open_for_writing']
#for all the blocks, get the details
for block in reply:
max_last_modified = max(max_last_modified, block['last_modification_date'])
min_creation_date = min(min_creation_date, block['creation_date'])
open_for_writing |= block['open_for_writing']
return (open_for_writing, min_creation_date, max_last_modified)
def getMaxLumi(dataset):
"""
Gets the number of the last lumi in a given dataset
This is useful for appending new events to dataset
without collision
"""
dbsapi = DbsApi(url=dbs3_url)
reply = dbsapi.listBlocks(dataset=dataset)
maxl = 0
for b in reply:
reply2 = dbsapi.listFileLumis(block_name=b['block_name'])
#retrieve lumis for each file
for f in reply2:
lumis = f['lumi_section_num']
#check max of lumi
lumi = max(lumis)
if lumi > maxl:
maxl = lumi
return maxl
def getRunsDataset(dataset):
"""
returns a list with number of each run.
"""
dbsapi = DbsApi(url=dbs3_url)
# retrieve runs
reply = dbsapi.listRuns(dataset=dataset)
#a list with only the run numbersT3_US_Omaha
runs = []
#filter and clean
for run in reply:
if type(run['run_num']) is list:
runs.extend(run['run_num'])
else:
runs.append(run['run_num'])
return runs
def getNumberofFilesPerRun(das_url, dataset, run):
"""
Count number of files
"""
# initialize API to DBS3
dbsapi = DbsApi(url=dbs3_url)
# retrieve file list
reply = dbsapi.listFiles(dataset=dataset)
return len(reply)
def getFileCountDataset(dataset):
"""
Returns the number of files registered in DBS3
"""
# initialize API to DBS3
dbsapi = DbsApi(url=dbs3_url)
# retrieve file list
reply = dbsapi.listFiles(dataset=dataset)
return len(reply)
def getEventCountDataSet(dataset):
"""
Returns the number of events in a dataset using DBS3
"""
# initialize API to DBS3
dbsapi = DbsApi(url=dbs3_url)
# retrieve dataset summary
reply = dbsapi.listBlockSummaries(dataset=dataset)
if not reply:
return 0
return reply[0]['num_event']
def getLumiCountDataSet(dataset):
"""
Get the number of unique lumis in a dataset
"""
# initialize API to DBS3
dbsapi = DbsApi(url=dbs3_url)
# retrieve dataset summary
reply = dbsapi.listFileSummaries(dataset=dataset)
if not reply:
return 0
return reply[0]['num_lumi']
def getLumiCountDataSetBlockList(dataset, blockList):
"""
Counts and adds all the lumis for a given lists
blocks inside a dataset
"""
# initialize API to DBS3
dbsapi = DbsApi(url=dbs3_url)
#transform from strin to list
if type(blockList) in (str, unicode):
blockList = eval(blockList)
total = 0
#get one by one block and add it so uri wont be too large
for block in blockList:
reply = dbsapi.listFileSummaries(block_name=block)
total += reply[0]['num_lumi']
return total
def hasAllBlocksClosed(dataset):
"""
checks if a given dataset has all blocks closed
"""
# initialize API to DBS3
dbsapi = DbsApi(url=dbs3_url)
# retrieve dataset summary
reply = dbsapi.listBlocks(dataset=dataset, detail=True)
for block in reply:
#print block['block_name']
#print block['open_for_writing']
if block['open_for_writing']:
return False
return True
def getEventCountDataSetBlockList(dataset,blockList):
"""
Counts and adds all the events for a given lists
blocks inside a dataset
"""
# initialize API to DBS3
dbsapi = DbsApi(url=dbs3_url)
#transform from strin to list
if type(blockList) in (str, unicode):
blockList = eval(blockList)
total = 0
#get one by one block and add it so uri wont be too large
for block in blockList:
reply = dbsapi.listBlockSummaries(block_name=block)
total += reply[0]['num_event']
return total
def getEventCountDataSetFileList(dataset,fileList):
"""
Counts and adds all the events for a given lists
blocks inside a dataset
"""
# initialize API to DBS3
dbsapi = DbsApi(url=dbs3_url)
#transform from strin to list
if type(fileList) in (str, unicode):
fileList = eval(fileList)
total = 0
#get one by one block and add it so uri wont be too large
for f in fileList:
reply = dbsapi.listFiles(logical_file_name=f, detail=True)
total += reply[0]['event_count']
return total
def getEventCountDataSetRunList(dataset,runList):
"""
Counts and adds all the events for a given lists
of runs inside a dataset
"""
# initialize API to DBS3
dbsapi = DbsApi(url=dbs3_url)
# retrieve file aggregation only by the runs
#transform from strin to list
if type(runList) in (str, unicode):
runList = eval(runList)
total = 0
#get one by one run, so URI wont be too large
for run in runList:
reply = dbsapi.listFileSummaries(dataset=dataset,run_num=run)
if reply:
total += reply[0]['num_event']
return total
def getLumiCountDataSetRunList(dataset,runList):
"""
Counts and adds all the lumis for a given lists
of runs inside a dataset
"""
# initialize API to DBS3
dbsapi = DbsApi(url=dbs3_url)
# retrieve file aggregation only by the runs
#transform from strin to list
if type(runList) in (str, unicode):
runList = eval(runList)
total = 0
#get one by one run, so URI wont be too large
for run in runList:
reply = dbsapi.listFileSummaries(dataset=dataset,run_num=run)
if reply:
total += reply[0]['num_lumi']
return total
def getDatasetSize(dataset):
# initialize API to DBS3
dbsapi = DbsApi(url=dbs3_url)
# retrieve file aggregation only by the runs
#transform from strin to list
reply = dbsapi.listBlockSummaries(dataset=dataset)
return reply[0]['file_size']
def main():
args=sys.argv[1:]
if len(args) < 1:
print "usage:dbs3Client dataset dataset2 ..."
sys.exit(0)
workflow=args[0]
url='cmsweb.cern.ch'
datasets = args
for dataset in datasets:
print dataset
print " Events:", getEventCountDataSet(dataset)
print " Lumis:", getLumiCountDataSet(dataset)
info = getDatasetInfo(dataset)
print " Open Blocks: ", info[0]
print " Creation:", datetime.datetime.fromtimestamp(info[1]).strftime('%Y-%m-%d %H:%M:%S')
print " Last update:", datetime.datetime.fromtimestamp(info[2]).strftime('%Y-%m-%d %H:%M:%S')
#print " Duplicated Lumis:", duplicateRunLumi(dataset)
#print " Duplicated Lumis:", duplicateLumi(dataset)
#print " Runs", getRunsDataset(dataset))
#print " Blocks", hasAllBlocksClosed(dataset)
#print " blockEvents", getEventCountDataSetBlockList(dataset,blocklist)
sys.exit(0);
if __name__ == "__main__":
main()
sys.exit(0);