CMS 3D CMS Logo

Classes | Functions | Variables
das_client Namespace Reference

Classes

class  DASOptionParser
 
class  HTTPSClientAuthHandler
 

Functions

def check_auth (key)
 
def check_glidein ()
 
def convert_time (val)
 
def extract_value (row, key, base=10)
 
def fullpath (path)
 
def get_data (host, query, idx, limit, debug, threshold=300, ckey=None, cert=None, capath=None, qcache=0, das_headers=True)
 
def get_value (data, filters, base=10)
 
def keys_attrs (lkey, oformat, host, ckey, cert, debug=0)
 
def main ()
 
def prim_value (row)
 
def print_from_cache (cache, query)
 
def print_summary (rec)
 
def size_format (uinput, ibase=0)
 
def unique_filter (rows)
 
def x509 ()
 

Variables

 __author__
 
 DAS_CLIENT
 
 EX__BASE
 
 EX_CANTCREAT
 
 EX_CONFIG
 
 EX_DATAERR
 
 EX_IOERR
 
 EX_NOHOST
 
 EX_NOINPUT
 
 EX_NOPERM
 
 EX_NOUSER
 
 EX_OK
 
 EX_OSERR
 
 EX_OSFILE
 
 EX_PROTOCOL
 
 EX_SOFTWARE
 
 EX_TEMPFAIL
 
 EX_UNAVAILABLE
 
 EX_USAGE
 

Function Documentation

◆ check_auth()

def das_client.check_auth (   key)

Definition at line 102 of file das_client.py.

References print().

Referenced by main().

102 def check_auth(key):
103  "Check if user runs das_client with key/cert and warn users to switch"
104  if not key:
105  msg = "WARNING: das_client is running without user credentials/X509 proxy, create proxy via 'voms-proxy-init -voms cms -rfc'"
106  print(msg, file=sys.stderr)
107 
def check_auth(key)
Definition: das_client.py:102
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47

◆ check_glidein()

def das_client.check_glidein ( )

Definition at line 94 of file das_client.py.

References print().

Referenced by main().

94 def check_glidein():
95  "Check glideine environment and exit if it is set"
96  glidein = os.environ.get('GLIDEIN_CMSSite', '')
97  if glidein:
98  msg = "ERROR: das_client is running from GLIDEIN environment, it is prohibited"
99  print(msg)
100  sys.exit(EX__BASE)
101 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def check_glidein()
Definition: das_client.py:94

◆ convert_time()

def das_client.convert_time (   val)

Definition at line 176 of file das_client.py.

Referenced by extract_value().

176 def convert_time(val):
177  "Convert given timestamp into human readable format"
178  if isinstance(val, int) or isinstance(val, float):
179  return time.strftime('%d/%b/%Y_%H:%M:%S_GMT', time.gmtime(val))
180  return val
181 
def convert_time(val)
Definition: das_client.py:176

◆ extract_value()

def das_client.extract_value (   row,
  key,
  base = 10 
)
Generator which extracts row[key] value

Definition at line 233 of file das_client.py.

References convert_time(), and size_format().

Referenced by get_value().

233 def extract_value(row, key, base=10):
234  """Generator which extracts row[key] value"""
235  if isinstance(row, dict) and key in row:
236  if key == 'creation_time':
237  row = convert_time(row[key])
238  elif key == 'size':
239  row = size_format(row[key], base)
240  else:
241  row = row[key]
242  yield row
243  if isinstance(row, list) or isinstance(row, GeneratorType):
244  for item in row:
245  for vvv in extract_value(item, key, base):
246  yield vvv
247 
def extract_value(row, key, base=10)
Definition: das_client.py:233
def size_format(uinput, ibase=0)
Definition: das_client.py:182
def convert_time(val)
Definition: das_client.py:176

◆ fullpath()

def das_client.fullpath (   path)

Definition at line 267 of file das_client.py.

References get_data().

Referenced by get_data(), and keys_attrs().

267 def fullpath(path):
268  "Expand path to full path"
269  if path and path[0] == '~':
270  path = path.replace('~', '')
271  path = path[1:] if path[0] == '/' else path
272  path = os.path.join(os.environ['HOME'], path)
273  return path
274 
def fullpath(path)
Definition: das_client.py:267

◆ get_data()

def das_client.get_data (   host,
  query,
  idx,
  limit,
  debug,
  threshold = 300,
  ckey = None,
  cert = None,
  capath = None,
  qcache = 0,
  das_headers = True 
)
Contact DAS server and retrieve data for given DAS query

Definition at line 276 of file das_client.py.

References fullpath(), createfilelist.int, print(), and str.

Referenced by cmsswVersionTools.PickRelValInputFiles.apply(), electronDataDiscovery.common_search(), dasFileQuery.dasFileQuery(), dataset.dasquery(), util.getRunInfo.dasQuery(), fullpath(), edmPickEvents.getFileNames_das_client(), and main().

276  cert=None, capath=None, qcache=0, das_headers=True):
277  """Contact DAS server and retrieve data for given DAS query"""
278  params = {'input':query, 'idx':idx, 'limit':limit}
279  if qcache:
280  params['qcache'] = qcache
281  path = '/das/cache'
282  pat = re.compile('http[s]{0,1}://')
283  if not pat.match(host):
284  msg = 'Invalid hostname: %s' % host
285  raise Exception(msg)
286  url = host + path
287  client = '%s (%s)' % (DAS_CLIENT, os.environ.get('USER', ''))
288  headers = {"Accept": "application/json", "User-Agent": client}
289  encoded_data = urllib.urlencode(params, doseq=True)
290  url += '?%s' % encoded_data
291  req = urllib2.Request(url=url, headers=headers)
292  if ckey and cert:
293  ckey = fullpath(ckey)
294  cert = fullpath(cert)
295  http_hdlr = HTTPSClientAuthHandler(ckey, cert, capath, debug)
296  elif cert and capath:
297  cert = fullpath(cert)
298  http_hdlr = HTTPSClientAuthHandler(ckey, cert, capath, debug)
299  else:
300  http_hdlr = urllib2.HTTPHandler(debuglevel=debug)
301  proxy_handler = urllib2.ProxyHandler({})
302  cookie_jar = cookielib.CookieJar()
303  cookie_handler = urllib2.HTTPCookieProcessor(cookie_jar)
304  try:
305  opener = urllib2.build_opener(http_hdlr, proxy_handler, cookie_handler)
306  fdesc = opener.open(req)
307  data = fdesc.read()
308  fdesc.close()
309  except urllib2.HTTPError as error:
310  print(error.read())
311  sys.exit(1)
312 
313  pat = re.compile(r'^[a-z0-9]{32}')
314  if data and isinstance(data, str) and pat.match(data) and len(data) == 32:
315  pid = data
316  else:
317  pid = None
318  iwtime = 2 # initial waiting time in seconds
319  wtime = 20 # final waiting time in seconds
320  sleep = iwtime
321  time0 = time.time()
322  while pid:
323  params.update({'pid':data})
324  encoded_data = urllib.urlencode(params, doseq=True)
325  url = host + path + '?%s' % encoded_data
326  req = urllib2.Request(url=url, headers=headers)
327  try:
328  fdesc = opener.open(req)
329  data = fdesc.read()
330  fdesc.close()
331  except urllib2.HTTPError as err:
332  return {"status":"fail", "reason":str(err)}
333  if data and isinstance(data, str) and pat.match(data) and len(data) == 32:
334  pid = data
335  else:
336  pid = None
337  time.sleep(sleep)
338  if sleep < wtime:
339  sleep *= 2
340  elif sleep == wtime:
341  sleep = iwtime # start new cycle
342  else:
343  sleep = wtime
344  if (time.time()-time0) > threshold:
345  reason = "client timeout after %s sec" % int(time.time()-time0)
346  return {"status":"fail", "reason":reason}
347  jsondict = json.loads(data)
348  return jsondict
349 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def fullpath(path)
Definition: das_client.py:267
#define str(s)

◆ get_value()

def das_client.get_value (   data,
  filters,
  base = 10 
)
Filter data from a row for given list of filters

Definition at line 248 of file das_client.py.

References extract_value().

Referenced by cmsswVersionTools.PickRelValInputFiles.apply(), edmPickEvents.getFileNames_das_client(), and main().

248 def get_value(data, filters, base=10):
249  """Filter data from a row for given list of filters"""
250  for ftr in filters:
251  if ftr.find('>') != -1 or ftr.find('<') != -1 or ftr.find('=') != -1:
252  continue
253  row = dict(data)
254  values = []
255  keys = ftr.split('.')
256  for key in keys:
257  val = [v for v in extract_value(row, key, base)]
258  if key == keys[-1]: # we collect all values at last key
259  values += [json.dumps(i) for i in val]
260  else:
261  row = val
262  if len(values) == 1:
263  yield values[0]
264  else:
265  yield values
266 
def extract_value(row, key, base=10)
Definition: das_client.py:233
def get_value(data, filters, base=10)
Definition: das_client.py:248

◆ keys_attrs()

def das_client.keys_attrs (   lkey,
  oformat,
  host,
  ckey,
  cert,
  debug = 0 
)

Definition at line 387 of file das_client.py.

References fullpath(), and print().

Referenced by main().

387 def keys_attrs(lkey, oformat, host, ckey, cert, debug=0):
388  "Contact host for list of key/attributes pairs"
389  url = '%s/das/keys?view=json' % host
390  headers = {"Accept": "application/json", "User-Agent": DAS_CLIENT}
391  req = urllib2.Request(url=url, headers=headers)
392  if ckey and cert:
393  ckey = fullpath(ckey)
394  cert = fullpath(cert)
395  http_hdlr = HTTPSClientAuthHandler(ckey, cert, debug)
396  else:
397  http_hdlr = urllib2.HTTPHandler(debuglevel=debug)
398  proxy_handler = urllib2.ProxyHandler({})
399  cookie_jar = cookielib.CookieJar()
400  cookie_handler = urllib2.HTTPCookieProcessor(cookie_jar)
401  opener = urllib2.build_opener(http_hdlr, proxy_handler, cookie_handler)
402  fdesc = opener.open(req)
403  data = json.load(fdesc)
404  fdesc.close()
405  if oformat.lower() == 'json':
406  if lkey == 'all':
407  print(json.dumps(data))
408  else:
409  print(json.dumps({lkey:data[lkey]}))
410  return
411  for key, vdict in data.items():
412  if lkey == 'all':
413  pass
414  elif lkey != key:
415  continue
416  print()
417  print("DAS key:", key)
418  for attr, examples in vdict.items():
419  prefix = ' '
420  print('%s%s' % (prefix, attr))
421  for item in examples:
422  print('%s%s%s' % (prefix, prefix, item))
423 
def keys_attrs(lkey, oformat, host, ckey, cert, debug=0)
Definition: das_client.py:387
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def fullpath(path)
Definition: das_client.py:267

◆ main()

def das_client.main ( )
Main function

Definition at line 424 of file das_client.py.

References check_auth(), check_glidein(), spr.find(), get_data(), get_value(), createfilelist.int, join(), keys_attrs(), dqm-mbProfile.log, prim_value(), print(), print_from_cache(), print_summary(), size_format(), str, and unique_filter().

424 def main():
425  """Main function"""
426  optmgr = DASOptionParser()
427  opts, _ = optmgr.get_opt()
428  host = opts.host
429  debug = opts.verbose
430  query = opts.query
431  idx = opts.idx
432  limit = opts.limit
433  thr = opts.threshold
434  ckey = opts.ckey
435  cert = opts.cert
436  capath = opts.capath
437  base = opts.base
438  qcache = opts.qcache
439  check_glidein()
440  check_auth(ckey)
441  if opts.keys_attrs:
442  keys_attrs(opts.keys_attrs, opts.format, host, ckey, cert, debug)
443  return
444  if not query:
445  print('Input query is missing')
446  sys.exit(EX_USAGE)
447  if opts.format == 'plain':
448  jsondict = get_data(host, query, idx, limit, debug, thr, ckey, cert, capath, qcache)
449  cli_msg = jsondict.get('client_message', None)
450  if cli_msg:
451  print("DAS CLIENT WARNING: %s" % cli_msg)
452  if 'status' not in jsondict and opts.cache:
453  print_from_cache(opts.cache, query)
454  if 'status' not in jsondict:
455  print('DAS record without status field:\n%s' % jsondict)
456  sys.exit(EX_PROTOCOL)
457  if jsondict["status"] != 'ok' and opts.cache:
458  print_from_cache(opts.cache, query)
459  if jsondict['status'] != 'ok':
460  print("status: %s, reason: %s" \
461  % (jsondict.get('status'), jsondict.get('reason', 'N/A')))
462  if opts.retry:
463  found = False
464  for attempt in xrange(1, int(opts.retry)):
465  interval = log(attempt)**5
466  print("Retry in %5.3f sec" % interval)
467  time.sleep(interval)
468  data = get_data(host, query, idx, limit, debug, thr, ckey, cert, capath, qcache)
469  jsondict = json.loads(data)
470  if jsondict.get('status', 'fail') == 'ok':
471  found = True
472  break
473  else:
474  sys.exit(EX_TEMPFAIL)
475  if not found:
476  sys.exit(EX_TEMPFAIL)
477  nres = jsondict.get('nresults', 0)
478  if not limit:
479  drange = '%s' % nres
480  else:
481  drange = '%s-%s out of %s' % (idx+1, idx+limit, nres)
482  if opts.limit:
483  msg = "\nShowing %s results" % drange
484  msg += ", for more results use --idx/--limit options\n"
485  print(msg)
486  mongo_query = jsondict.get('mongo_query', {})
487  unique = False
488  fdict = mongo_query.get('filters', {})
489  filters = fdict.get('grep', [])
490  aggregators = mongo_query.get('aggregators', [])
491  if 'unique' in fdict.keys():
492  unique = True
493  if filters and not aggregators:
494  data = jsondict['data']
495  if isinstance(data, dict):
496  rows = [r for r in get_value(data, filters, base)]
497  print(' '.join(rows))
498  elif isinstance(data, list):
499  if unique:
500  data = unique_filter(data)
501  for row in data:
502  rows = [r for r in get_value(row, filters, base)]
503  types = [type(r) for r in rows]
504  if len(types)>1: # mixed types print as is
505  print(' '.join([str(r) for r in rows]))
506  elif isinstance(rows[0], list):
507  out = set()
508  for item in rows:
509  for elem in item:
510  out.add(elem)
511  print(' '.join(out))
512  else:
513  print(' '.join(rows))
514  else:
515  print(json.dumps(jsondict))
516  elif aggregators:
517  data = jsondict['data']
518  if unique:
519  data = unique_filter(data)
520  for row in data:
521  if row['key'].find('size') != -1 and \
522  row['function'] == 'sum':
523  val = size_format(row['result']['value'], base)
524  else:
525  val = row['result']['value']
526  print('%s(%s)=%s' \
527  % (row['function'], row['key'], val))
528  else:
529  data = jsondict['data']
530  if isinstance(data, list):
531  old = None
532  val = None
533  for row in data:
534  prim_key = row.get('das', {}).get('primary_key', None)
535  if prim_key == 'summary':
536  print_summary(row)
537  return
538  val = prim_value(row)
539  if not opts.limit:
540  if val != old:
541  print(val)
542  old = val
543  else:
544  print(val)
545  if val != old and not opts.limit:
546  print(val)
547  elif isinstance(data, dict):
548  print(prim_value(data))
549  else:
550  print(data)
551  else:
552  jsondict = get_data(\
553  host, query, idx, limit, debug, thr, ckey, cert, capath, qcache)
554  print(json.dumps(jsondict))
555 
556 #
557 # main
558 #
def get_value(data, filters, base=10)
Definition: das_client.py:248
def check_auth(key)
Definition: das_client.py:102
def keys_attrs(lkey, oformat, host, ckey, cert, debug=0)
Definition: das_client.py:387
def get_data(host, query, idx, limit, debug, threshold=300, ckey=None, cert=None, capath=None, qcache=0, das_headers=True)
Definition: das_client.py:276
def prim_value(row)
Definition: das_client.py:350
def print_summary(rec)
Definition: das_client.py:365
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
def size_format(uinput, ibase=0)
Definition: das_client.py:182
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
static std::string join(char **cmd)
Definition: RemoteFile.cc:21
def print_from_cache(cache, query)
Definition: das_client.py:378
def unique_filter(rows)
Definition: das_client.py:203
def check_glidein()
Definition: das_client.py:94
#define str(s)
def main()
Definition: das_client.py:424

◆ prim_value()

def das_client.prim_value (   row)
Extract primary key value from DAS record

Definition at line 350 of file das_client.py.

Referenced by main().

350 def prim_value(row):
351  """Extract primary key value from DAS record"""
352  prim_key = row['das']['primary_key']
353  if prim_key == 'summary':
354  return row.get(prim_key, None)
355  key, att = prim_key.split('.')
356  if isinstance(row[key], list):
357  for item in row[key]:
358  if att in item:
359  return item[att]
360  else:
361  if key in row:
362  if att in row[key]:
363  return row[key][att]
364 
def prim_value(row)
Definition: das_client.py:350

◆ print_from_cache()

def das_client.print_from_cache (   cache,
  query 
)

Definition at line 378 of file das_client.py.

References beamvalidation.exit(), join(), print(), and fileinputsource_cfi.read.

Referenced by main().

378 def print_from_cache(cache, query):
379  "print the list of files reading it from cache"
380  data = open(cache).read()
381  jsondict = json.loads(data)
382  if query in jsondict:
383  print("\n".join(jsondict[query]))
384  exit(0)
385  exit(1)
386 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
static std::string join(char **cmd)
Definition: RemoteFile.cc:21
def print_from_cache(cache, query)
Definition: das_client.py:378
def exit(msg="")

◆ print_summary()

def das_client.print_summary (   rec)

Definition at line 365 of file das_client.py.

References SiStripPI.max, and print().

Referenced by main().

365 def print_summary(rec):
366  "Print summary record information on stdout"
367  if 'summary' not in rec:
368  msg = 'Summary information is not found in record:\n', rec
369  raise Exception(msg)
370  for row in rec['summary']:
371  keys = [k for k in row.keys()]
372  maxlen = max([len(k) for k in keys])
373  for key, val in row.items():
374  pkey = '%s%s' % (key, ' '*(maxlen-len(key)))
375  print('%s: %s' % (pkey, val))
376  print()
377 
def print_summary(rec)
Definition: das_client.py:365
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47

◆ size_format()

def das_client.size_format (   uinput,
  ibase = 0 
)
Format file size utility, it converts file size into KB, MB, GB, TB, PB units

Definition at line 182 of file das_client.py.

References nano_mu_digi_cff.float.

Referenced by extract_value(), and main().

182 def size_format(uinput, ibase=0):
183  """
184  Format file size utility, it converts file size into KB, MB, GB, TB, PB units
185  """
186  if not ibase:
187  return uinput
188  try:
189  num = float(uinput)
190  except Exception as _exc:
191  return uinput
192  if ibase == 2.: # power of 2
193  base = 1024.
194  xlist = ['', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB']
195  else: # default base is 10
196  base = 1000.
197  xlist = ['', 'KB', 'MB', 'GB', 'TB', 'PB']
198  for xxx in xlist:
199  if num < base:
200  return "%3.1f%s" % (num, xxx)
201  num /= base
202 
def size_format(uinput, ibase=0)
Definition: das_client.py:182

◆ unique_filter()

def das_client.unique_filter (   rows)
Unique filter drop duplicate rows.

Definition at line 203 of file das_client.py.

Referenced by main().

203 def unique_filter(rows):
204  """
205  Unique filter drop duplicate rows.
206  """
207  old_row = {}
208  row = None
209  for row in rows:
210  row_data = dict(row)
211  try:
212  del row_data['_id']
213  del row_data['das']
214  del row_data['das_id']
215  del row_data['cache_id']
216  except:
217  pass
218  old_data = dict(old_row)
219  try:
220  del old_data['_id']
221  del old_data['das']
222  del old_data['das_id']
223  del old_data['cache_id']
224  except:
225  pass
226  if row_data == old_data:
227  continue
228  if old_row:
229  yield old_row
230  old_row = row
231  yield row
232 
def unique_filter(rows)
Definition: das_client.py:203

◆ x509()

def das_client.x509 ( )

Definition at line 85 of file das_client.py.

85 def x509():
86  "Helper function to get x509 either from env or tmp file"
87  proxy = os.environ.get('X509_USER_PROXY', '')
88  if not proxy:
89  proxy = '/tmp/x509up_u%s' % pwd.getpwuid( os.getuid() ).pw_uid
90  if not os.path.isfile(proxy):
91  return ''
92  return proxy
93 
def x509()
Definition: das_client.py:85

Variable Documentation

◆ __author__

das_client.__author__
private

Definition at line 9 of file das_client.py.

◆ DAS_CLIENT

das_client.DAS_CLIENT

Definition at line 18 of file das_client.py.

◆ EX__BASE

das_client.EX__BASE

Definition at line 35 of file das_client.py.

◆ EX_CANTCREAT

das_client.EX_CANTCREAT

Definition at line 45 of file das_client.py.

◆ EX_CONFIG

das_client.EX_CONFIG

Definition at line 50 of file das_client.py.

◆ EX_DATAERR

das_client.EX_DATAERR

Definition at line 37 of file das_client.py.

◆ EX_IOERR

das_client.EX_IOERR

Definition at line 46 of file das_client.py.

◆ EX_NOHOST

das_client.EX_NOHOST

Definition at line 40 of file das_client.py.

◆ EX_NOINPUT

das_client.EX_NOINPUT

Definition at line 38 of file das_client.py.

◆ EX_NOPERM

das_client.EX_NOPERM

Definition at line 49 of file das_client.py.

◆ EX_NOUSER

das_client.EX_NOUSER

Definition at line 39 of file das_client.py.

◆ EX_OK

das_client.EX_OK

Definition at line 34 of file das_client.py.

◆ EX_OSERR

das_client.EX_OSERR

Definition at line 43 of file das_client.py.

◆ EX_OSFILE

das_client.EX_OSFILE

Definition at line 44 of file das_client.py.

◆ EX_PROTOCOL

das_client.EX_PROTOCOL

Definition at line 48 of file das_client.py.

◆ EX_SOFTWARE

das_client.EX_SOFTWARE

Definition at line 42 of file das_client.py.

◆ EX_TEMPFAIL

das_client.EX_TEMPFAIL

Definition at line 47 of file das_client.py.

◆ EX_UNAVAILABLE

das_client.EX_UNAVAILABLE

Definition at line 41 of file das_client.py.

◆ EX_USAGE

das_client.EX_USAGE

Definition at line 36 of file das_client.py.