Skip to content

Instantly share code, notes, and snippets.

@vkuznet
Created March 1, 2013 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkuznet/62475d077f73d3ecf1dd to your computer and use it in GitHub Desktop.
Save vkuznet/62475d077f73d3ecf1dd to your computer and use it in GitHub Desktop.
DBS3 example to get file, lumi info for given dataset.
#!/usr/bin/env python
import httplib
import urllib2, urllib
import sys, time, types
import json
import os
class HTTPSClientAuthHandler(urllib2.HTTPSHandler):
"""
Simple HTTPS client authentication class based on provided
key/ca information
"""
def __init__(self, key=None, cert=None, debug=0):
if debug:
urllib2.HTTPSHandler.__init__(self, debuglevel=1)
else:
urllib2.HTTPSHandler.__init__(self)
self.key = key
self.cert = cert
def https_open(self, req):
"""Open request method"""
#Rather than pass in a reference to a connection class, we pass in
# a reference to a function which, for all intents and purposes,
# will behave as a constructor
return self.do_open(self.get_connection, req)
def get_connection(self, host, timeout=300):
"""Connection method"""
if self.cert:
return httplib.HTTPSConnection(host, key_file=self.key,
cert_file=self.cert)
return httplib.HTTPSConnection(host)
def file_lumi(dbs3_url, dataset, ckey, cert, debug=0):
url = dbs3_url + '/blocks/?dataset=%s' % dataset
print url
req = urllib2.Request(url)
headers = {'Accept': 'application/json;text/json'}
if headers:
for key, val in headers.items():
req.add_header(key, val)
handler = HTTPSClientAuthHandler(ckey, cert, debug)
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)
data = urllib2.urlopen(req)
blocks = json.load(data)
ncalls = 1
for row in blocks:
blk = urllib.quote(row['block_name'])
url = dbs3_url + '/filelumis?block_name=%s' % blk
req = urllib2.Request(url)
res = urllib2.urlopen(req)
val = json.load(res)
ncalls += 1
print val
print "dataset = %s" % dataset
print "nblocks = %s" % len(blocks)
print "ncalls = %s" % ncalls
def main():
"Series of DBS3 tests"
ckey = os.path.join(os.environ['HOME'], '.globus/userkey.pem')
cert = os.path.join(os.environ['HOME'], '.globus/usercert.pem')
debug = 0
dbs3_url = 'https://cmsweb.cern.ch/dbs/prod/global/DBSReader'
dataset = '/SingleMu/Run2012D-ZMu-PromptSkim-v1/RAW-RECO'
dbs3_url = 'https://cmsweb-testbed.cern.ch/dbs/prod/global/DBSReader'
dataset = '/RelValMinBias/CMSSW_3_10_1-START310_V3-v1/GEN-SIM-RECO'
dataset = '/MinimumBias/Run2010B-Apr21ReReco-v1/RECO'
# test file_lumi
# ops example: find file, lumi where dataset=dataset
time0 = time.time()
file_lumi(dbs3_url, dataset, ckey, cert)
print "Elapsed time:", (time.time()-time0)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment