Line Code
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 369 370 371 372 373 374 375 376 377 378 379 380 381
#!/usr/bin/env python3

"""Usage: dqm-grep -f PATTERN -e EXPRESSION [OPTIONS]

Grep contents of DQM GUI index for samples matching an expression
and contents whose name matches a wild card pattern.

The sample expression (-e option) is a boolean expression which should
yield True if the sample should be processed and False otherwise. The
expression may use the terms `run`, `dataset`, `version` and `type`,
and the function `match()` for matching regular expressions. See the
examples below for typical sample expressions.

The filter pattern (-f option) is a wild card pattern which defines
what to search. It may use wild card '*' to match any string within
one directory level, '**' to match any number of subdirectories, and
'***' to match any subdirectory or monitor element object. Names with
a trailing slash match only directories; names without trailing slash
match only plain objects. The pattern must always start with a slash.

For example the pattern '/*/EventInfo/' matches all `EventInfo` sub-
directories one level down from the root. The pattern '/*/Ev*/*Summary*'
matches all plain objects whose name contain 'Summary' in subdirectories
starting with 'Ev' one level down from the per-sample root directory.
The pattern '/CSC/***' matches all directories and objects inside the
top-level 'CSC' directory.

It is important to use sufficiently strict pattern to avoid unnecessary
- and very expensive - traversal of the full index. There can easily be
tens of thousands of directories to traverse per sample, and retrieving
them all can get very expensive.

Examples of use:

dqm-grep -f '/CSC/Event*/*Summary*' -e 'match("/StreamHLTMON/Run2011A-Express-v4/DQM", dataset)'
dqm-grep -f '/*/Event*/*Summary*' -e 'run == 168330 and match("/StreamHLTMON/Run2011A-Express-v4/DQM", dataset)'
dqm-grep -f '/*/Event*/***' -e 'run == 168330 and match("/StreamHLTMON/Run2011A-Express-v4/DQM", dataset)'
dqm-grep -f '/C*/***' -e 'run == 168330 and match("/.*/Run2011A-Express-v4/DQM", dataset)'
dqm-grep -f '/*/EventInfo/CertificationSummary' -e 'match("/StreamExpress.*/Run2011A-Express-v4/DQM", dataset)'

In order to authenticate to the target server, standard grid certificate
environment must be available. Typically this would be X509_CERT_DIR and
either X509_USER_PROXY or X509_USER_CERT and X509_USER_KEY environment
variables. If these variables are not set, the following defaults are
checked for existence. Note that if the script falls back on using a
key rather than a proxy, it will prompt for the key password.
- $X509_CERT_DIR: /etc/grid-security/certificates
- $X509_USER_KEY: $HOME/.globus/userkey.pem
- $X509_USER_CERT: $HOME/.globus/usercert.pem
"""

from DQMServices.Components.HTTP import RequestManager
from DQMServices.Components.X509 import SSLOptions
import sys, re, json, pycurl
from urllib.parse import quote
from optparse import OptionParser
from time import time

# Object types.
DIR = 0 # Directory
FILE = 1 # File / simple object.
ANY = 2 # Either; used only for filters.

# HTTP protocol `User-agent` identification string.
ident = "DQMGrep/1.0 python/%s.%s.%s" % sys.version_info[:3]

# Where to find JSON contents at a given server.
url_content = "/%(section)s/%(run)d%(dataset)s%(path)s"

# SSL/X509 options.
ssl_opts = None

# HTTP request manager for content requests.
reqman = None

# Number of HTTP requests made for content.
nreq = 0

# Found objects (per sample).
found = []

class filter:
  """One step of a search filter.

- `type`: the type of object the filter can match: `FILE`, `DIR` or `ANY`.
- `recurse`: apply the pattern recursively to subdirectories if True.
- `pattern`: the regular expression pattern as a string.
- `rx`: the regular expression as a compiled regexp object."""
  type = FILE
  recurse = False
  pattern = ""
  rx = None

  def __repr__(self):
    return "(filter pattern='%s' type=%s recurse=%s)" \
      % (self.pattern, self.type, self.recurse)

def pattern_to_filter(pattern):
  """Converts a search pattern into a search filter.

The pattern must be of the form of path with '*' wild card pattern, for
example "/*/EventInfo/*Summary". A single star matches any string except
slashes, i.e. matching within a single directory. A double star matches
directories recursively.

A name with trailing slash matches directory. A name without the trailing
slash matches non-directories. A triple star will match either directory
or non-directory.

The patterns match against the full path, and therefore must always start
with a slash. If you want to search entire tree, use "/**/Name*".

The pattern "/*/EventInfo/" matches folders named 'EventInfo' one level
down from the top; it will not recurse further down in the tree as it is
known matches deeper inside are not possible. "/*/EventInfo/**/*Summary"
pattern matches any non-directory object name ending in "Summary" anywhere
inside "EventInfo" one level down from the top.

Returns a list of `filter` expressions representing the pattern, each of
which represents one or more levels of matching/recursion.
"""
  filters = []

  # Check the pattern starts with '/'
  if not pattern.startswith("/"):
    raise ValueError("pattern must start with slash")

  # Process pattern as directory search specs, but collapse
  # repeated slashes into one slash first.
  for part in re.sub("/+", "/", pattern).split('/')[1:]:
    if filters and filters[-1].type == FILE:
      filters[-1].type = DIR
    f = filter()
    filters.append(f)
    for term in re.split("([*]+)", part):
      if term == "***":
        f.pattern += ".*"
        f.recurse = True
        f.type = ANY
      elif term == "**":
        f.pattern += ".*"
        f.recurse = True
        f.type = DIR
      elif term == "*":
        f.pattern += "[^/]*"
        f.type = FILE
      elif term:
        f.pattern += re.escape(term)
        f.type = FILE
    if f.pattern != ".*":
      f.pattern = "^%s$" % f.pattern
    f.rx = re.compile(f.pattern)

  return filters

def should_process_sample(s, expr):
  """Evaluate sample predicate expression `expr` against sample `s`.
Returns True if the sample should be processed, False otherwise."""
  try:
    s['match'] = lambda rx, str: re.match(rx, str)
    s['run'] = int(s['run'])
    val = eval(expr, {}, s)
    del s['match']
    return val
  except:
    return False

def find_matching_samples(options):
  """Generator which returns all samples at target sever which
match the requested predicate expression."""
  all_samples = {}

  def req_error(c, url, errmsg, errno):
    print("%s: failed to retrieve samples: %s (%d)" \
      % (options.server, errmsg, errno), file=sys.stderr)
    sys.exit(1)

  def req_done(c):
    json_decoder = json.decoder.JSONDecoder()
    all_samples['result'] = json_decoder.decode(c.buffer.getvalue().decode('utf-8'))

  reqman = RequestManager(ssl_opts = ssl_opts,
                          user_agent = ident,
                          request_respond = req_done,
                          request_error = req_error)
  reqman.put((options.server + "/samples",))
  reqman.process()

  if not all_samples:
    print("%s: no samples" % options.server, file=sys.stderr)
    sys.exit(1)

  for sample_type in all_samples['result']['samples']:
    for sample in sample_type['items']:
      if should_process_sample(sample, options.sample_expr):
        yield sample

def request_init(c, options, sample, filters, pos, path):
  """`RequestManager` callback to initialise JSON contents request."""
  sample.update(path = path)
  c.url = options.server + quote(url_content % sample)
  c.setopt(pycurl.URL, c.url)
  if False and options.verbose:
    print(c.url)

def report_error(c, task, errmsg, errno):
  """`RequestManager` callback to report JSON contents request errors."""
  print("FAILED to retrieve %s: %s (%d)" % (task, errmsg, errno), file=sys.stderr)

def match_filters(item, filters, poslist):
  """Match filter list created by `pattern_to_filter` against an object.

The input arguments are:
- `item`: JSON for the object from the server.
- `filters`: List of all filters; not modified in any way.
- `poslist`: The list of positions in `filters` where to search.

The searching initially begins with `poslist` equal to [0], i.e. the first
filter. The function builds a new poslist to use for subdirectories. For
each non-recursive filter, the old filter is effectively removed from the
list and the next filter (if any) is added back. Recursive filters stay in
the list, so effectively the `poslist` maintains a NFA search stack for all
active search positions.

The function returns a tuple consisting of:
- `name`: the name of the object that was matched, subdirectory or plain
- `matched`: True if the entire filter chain has matched for this object
- `descend`: True if this was a subdirectory the filters require to descend
into; note that this is different from `matched`, basically non-terminal
match on subdirectory objects
- `poslist`: new filter position list for searching any subdirectories;
this will be empty if the filter list has been exhausted with or without
match.
"""
  newposlist = []
  descend = False
  matched = False
  name = None

  for idx in poslist:
    assert idx < len(filters)
    f = filters[idx]
    fmatched = False
    if 'subdir' in item \
       and (f.type == DIR or f.type == ANY) \
       and f.rx.match(item['subdir']):
      descend = fmatched = True
      name = item['subdir']
    elif 'obj' in item \
         and (f.type == FILE or f.type == ANY) \
         and f.rx.match(item['obj']):
      fmatched = True
      name = item['obj']

    if fmatched:
      if idx == len(filters)-1:
        matched = True
      if f.recurse:
        newposlist.append(idx)
      if idx < len(filters) - 1:
        newposlist.append(idx+1)

  return name, matched, descend, newposlist

def process(c):
  """`RequestManager` callback to handle JSON content response.

This gets called once per every directory which has been successfully
retrieved from the server. It basically applies `match_filters` to all
objects found and requests subdirectories if necessary, and adds to
`found` objects which matched the entire filter expression.

If verbosity has been requested, also shows simple progress bar on the
search progress, one dot for every ten directories retrieved."""
  global found, nreq
  options, sample, filters, pos, path = c.task
  json_decoder = json.decoder.JSONDecoder()

  nreq += 1
  if options.verbose and nreq % 10 == 0:
    sys.stdout.write(".")
    sys.stdout.flush()
    if nreq % 750 == 0:
      print()

  reply = c.buffer.getvalue().decode('utf-8')
  reply = re.sub(r'("value": ")"([A-Za-z0-9_]+")"', r'\1\2', reply)
  reply = re.sub(r'("(?:mean|rms|min|max)":) nan,', r'\1 "NaN",', reply)
  reply = json_decoder.decode(reply)

  seen = set()
  for item in reply['contents']:
    name, match, descend, newpos = match_filters(item, filters, pos)
    if match:
      found.append((path + name, item))
    if descend and name not in seen:
      reqman.put((options, sample, filters, newpos, path + name + "/"))
    seen.update((name,))

# Parse command line options.
op = OptionParser(usage = __doc__)
op.add_option("-e", "--samples", dest = "sample_expr", metavar = "EXPRESSION",
              help = "Evaluate EXPRESSION to decide which samples to scan")
op.add_option("-f", "--filter", dest = "glob",
              type = "string", action = "store", metavar = "PATTERN",
              default = "/*/EventInfo/*Summary",
              help = "Filter monitor elements matching PATTERN")
op.add_option("-s", "--server", dest = "server",
              type = "string", action = "store", metavar = "SERVER",
              default = "https://cmsweb.cern.ch/dqm/offline/data/json",
              help = "Pull content from SERVER")
op.add_option("-n", "--connections", dest = "connections",
              type = "int", action = "store", metavar = "NUM",
              default = 10, help = "Use NUM concurrent connections")
op.add_option("-v", "--verbose", dest = "verbose",
              action = "store_true", default = False,
              help = "Show verbose scan information")
options, args = op.parse_args()
if args:
  print("Too many arguments", file=sys.stderr)
  sys.exit(1)
if not options.sample_expr:
  print("Sample predicate expression required", file=sys.stderr)
  sys.exit(1)
if not options.glob:
  print("Monitor element filter expression required", file=sys.stderr)
  sys.exit(1)
if not options.server:
  print("Server contact string required", file=sys.stderr)
  sys.exit(1)

# Get SSL X509 parametres.
ssl_opts = SSLOptions()
if options.verbose:
  print("Using SSL cert dir", ssl_opts.ca_path)
  print("Using SSL private key", ssl_opts.key_file)
  print("Using SSL public key", ssl_opts.cert_file)

# Convert glob pattern into a filter expression.
filters = pattern_to_filter(options.glob)

# Start a request manager for contents.
reqman = RequestManager(num_connections = options.connections,
                        ssl_opts = ssl_opts,
                        user_agent = ident,
                        request_init = request_init,
                        request_respond = process,
                        request_error = report_error)

# Process all samples matching the predicate.
ntotreq = 0
nfound = 0
start = time()
for sample in find_matching_samples(options):
  nreq = 0
  found = []
  sample['section'] = 'archive'
  if options.verbose:
    print("Scanning %s" % sample)
  reqman.put((options, sample, filters, [0], "/"))
  reqman.process()
  if options.verbose:
    print()
  if found:
    print("%(section)s/%(run)d%(dataset)s:" % sample)
    found.sort()
    for path, item in found:
      if 'subdir' in item:
        print(" %s/" % path)
      elif 'value' in item:
        print(" %s = %s" % (path, item['value']))
      else:
        print(" %s = [%s # %d]" % (path, item['properties']['type'], item['nentries']))
  nfound += len(found)
  ntotreq += nreq
end = time()

# Provide final summary.
if options.verbose:
  print("\nFound %d objects in %d directories in %.3f seconds" % (nfound, ntotreq, end - start))