|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from __future__ import print_function |
| 4 | + |
| 5 | +import os |
| 6 | +import argparse |
| 7 | +import json |
| 8 | +from json.decoder import WHITESPACE |
| 9 | +from hubblestack.hec.dq import DiskQueue |
| 10 | + |
| 11 | +def get_args(*a): |
| 12 | + parser = argparse.ArgumentParser(description='hubble disk queue info extractor') |
| 13 | + parser.add_argument("cachedir", nargs='+') |
| 14 | + parser.add_argument('-p', '--peek', '--print', action='store_true', |
| 15 | + help='attempt to dump all the bytes found in queue in the order they would be dequeued') |
| 16 | + parser.add_argument('-e', '--evil-decode', action='store_true', |
| 17 | + help='attempt to decode all json found in queue (implies -p) and pretty-print them') |
| 18 | + parser.add_argument('-c', '--with-color', action='store_true', |
| 19 | + help='attempt to pull in pygments and colorize the output of -e (implies -p and -e)') |
| 20 | + parser.add_argument('-m', '--show-meta', action='store_true', |
| 21 | + help='attempt to show meta-data as a comment in peek mode or a spurious _META_ field in evil mode') |
| 22 | + args = parser.parse_args(*a) |
| 23 | + if args.with_color: |
| 24 | + args.evil_decode = True |
| 25 | + if args.evil_decode: |
| 26 | + args.peek = True |
| 27 | + return args |
| 28 | + |
| 29 | +def show_info(dirname): |
| 30 | + dq = DiskQueue(dirname) |
| 31 | + print("QUEUE={} ITEMS={} SIZE={}".format(dirname, dq.cn, dq.sz)) |
| 32 | + |
| 33 | +def evil_decode(docbytes): |
| 34 | + decoder = json.JSONDecoder() |
| 35 | + idx = WHITESPACE.match(docbytes, 0).end() |
| 36 | + while idx < len(docbytes): |
| 37 | + try: |
| 38 | + obj, end = decoder.raw_decode(docbytes, idx) |
| 39 | + yield obj |
| 40 | + idx = WHITESPACE.match(docbytes, end).end() |
| 41 | + except ValueError as e: |
| 42 | + print('docbytes:\n', docbytes) |
| 43 | + raise |
| 44 | + |
| 45 | +def read_entries(dirname, evil=False, color=False, meta=False): |
| 46 | + dq = DiskQueue(dirname) |
| 47 | + for item,meta in dq.iter_peek(): |
| 48 | + if evil: |
| 49 | + for obj in evil_decode(item): |
| 50 | + if meta: |
| 51 | + obj['_META_'] = meta |
| 52 | + obj = json.dumps(obj, sort_keys=True, indent=2) |
| 53 | + if color: |
| 54 | + try: |
| 55 | + from pygments import highlight, lexers, formatters |
| 56 | + obj = highlight(obj, lexers.JsonLexer(), |
| 57 | + formatters.TerminalFormatter()) |
| 58 | + except ImportError: |
| 59 | + pass |
| 60 | + print(obj) |
| 61 | + else: |
| 62 | + if meta: |
| 63 | + print('# ', json.dumps(meta)) |
| 64 | + print(item) |
| 65 | + |
| 66 | +def main(args): |
| 67 | + for dirname in args.cachedir: |
| 68 | + try: |
| 69 | + if not os.path.isdir(dirname): |
| 70 | + raise Exception("directory does not exist") |
| 71 | + if args.peek: |
| 72 | + read_entries(dirname, evil=args.evil_decode, color=args.with_color, meta=args.show_meta) |
| 73 | + else: |
| 74 | + show_info(dirname) |
| 75 | + except Exception as e: |
| 76 | + print("# Exception while reading {}: {}".format(dirname, repr(e))) |
| 77 | + |
| 78 | +if __name__ == '__main__': |
| 79 | + try: |
| 80 | + main(get_args()) |
| 81 | + except KeyboardInterrupt: |
| 82 | + pass |
0 commit comments