2 import os, sys, time, re
4 import urllib, urllib2, httplib, cookielib
6 from optparse
import OptionParser
7 from types
import GeneratorType
10 DAS_CLIENT =
'das-client/1.1::python/%s.%s' % sys.version_info[:2]
12 """Contact DAS server and retrieve data for given DAS query""" 13 params = {
'input':query,
'idx':0,
'limit':0}
15 url =
"https://cmsweb.cern.ch" + path
16 client =
'%s (%s)' % (DAS_CLIENT, os.environ.get(
'USER',
''))
17 headers = {
"Accept":
"application/json",
"User-Agent": client}
18 encoded_data = urllib.urlencode(params, doseq=
True)
19 url +=
'?%s' % encoded_data
20 req = urllib2.Request(url=url, headers=headers)
21 http_hdlr = urllib2.HTTPHandler(debuglevel=0)
22 proxy_handler = urllib2.ProxyHandler({})
23 cookie_jar = cookielib.CookieJar()
24 cookie_handler = urllib2.HTTPCookieProcessor(cookie_jar)
25 opener = urllib2.build_opener(http_hdlr, proxy_handler, cookie_handler)
26 fdesc = opener.open(req)
30 pat = re.compile(
r'^[a-z0-9]{32}')
31 if data
and isinstance(data, str)
and pat.match(data)
and len(data) == 32: pid = data
38 params.update({
'pid':data})
39 encoded_data = urllib.urlencode(params, doseq=
True)
40 url =
"https://cmsweb.cern.ch" + path +
'?%s' % encoded_data
41 req = urllib2.Request(url=url, headers=headers)
43 fdesc = opener.open(req)
46 except urllib2.HTTPError
as err:
47 return {
"status":
"fail",
"reason":str(err)}
48 if data
and isinstance(data, str)
and pat.match(data)
and len(data) == 32: pid = data
51 if sleep < wtime: sleep *= 2
52 elif sleep == wtime: sleep = iwtime
54 if (time.time()-time0) > 300:
55 return {
"status":
"fail",
"reason":(
"client timeout after %s sec" % int(time.time()-time0))}
56 jsondict = json.loads(data)
57 if (
'status' not in jsondict)
or jsondict[
'status'] !=
'ok':
58 print(
'DAS record with bad status or without status field:\n')
59 pprint.pprint(jsondict)
66 if isinstance(data, list):
68 for i
in range(0, len(data)):
69 if key
in data[i].keys():
70 if found_key
and value!=data[i][key]:
72 print "ERROR: Found multiple instances of key \'%s\'." % key
79 print "WARNING: Returning NULL. Could not find key \'%s\' in list." % key
82 elif isinstance(data, dict):
83 if key
in data.keys():
87 print "WARNING: Returning NULL. Could not find key \'%s\' in dict." % key
92 print "WARNING: Returning NULL. Dictionary is of neither type list or dict:" 97 jsondict =
get_data(
'lumi file='+file)
98 data = jsondict[
'data']
106 if (run_number==
"NULL"): run_number = -1
107 if (verbose):
print "Found run %s in file %s." % (run_number, file)
108 rundict[run_number] = []
111 rundict[run_number].append(-1)
114 rundict[run_number].extend([i
for i
in range(ll[0],ll[1]+1)])
117 print "Contents of %s" % file
118 pprint.pprint(rundict)
122 return rundict.keys()
124 def getFilesInfo(dataset, wanted_keys = ['name','size','nevents'], verbose = False):
125 jsondict =
get_data(
'file dataset='+dataset)
128 for entry
in jsondict[
'data']:
129 orig_fdict = entry[
'file']
133 for key
in wanted_keys:
135 if (key==
'size' or key==
'nevents')
and skim_fdict[key]==
"NULL": skim_fdict[key] = -1
136 if verbose:
print skim_fdict[key],
138 fdicts.append(skim_fdict)
142 def getDatasetInfo(dataset, wanted_keys = ['name','size','nevents','nfiles'], verbose = False):
143 jsondict_ds =
get_data(
'dataset='+dataset)
146 orig_dsdict =
findKeyValue(jsondict_ds[
'data'],
'dataset')
148 for key
in wanted_keys:
150 if verbose:
print skim_dsdict[key],
156 def getDatasetsInfo(dataset, wanted_keys = ['name','size','nevents','nfiles'], verbose = False):
159 jsondict =
get_data(
'dataset='+dataset)
162 for entry
in jsondict[
'data']:
165 skim_dsdict =
getDatasetInfo(dsname, wanted_keys = wanted_keys, verbose = verbose)
166 dsdicts.append(skim_dsdict)
168 skim_dsdict =
getDatasetInfo(dataset, wanted_keys = wanted_keys, verbose = verbose)
169 dsdicts.append(skim_dsdict)
def getFilesInfo(dataset, wanted_keys=['name', size, nevents, verbose=False)
def getDatasetsInfo(dataset, wanted_keys=['name', size, nevents, nfiles, verbose=False)
def getDatasetInfo(dataset, wanted_keys=['name', size, nevents, nfiles, verbose=False)
def findKeyValue(data, key)
def getFileRunInfo(file, getlumis=False, verbose=False)