Chromium Code Reviews| Index: scripts/tools/gzjsondump.py |
| diff --git a/scripts/tools/gzjsondump.py b/scripts/tools/gzjsondump.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..7079ac4df956b3b0a3b523d4b0276fe29c233061 |
| --- /dev/null |
| +++ b/scripts/tools/gzjsondump.py |
| @@ -0,0 +1,60 @@ |
| +#!/usr/bin/env python |
|
dnj
2016/01/14 22:50:28
I wrote this b/c I am sick of writing one-off scri
iannucci
2016/01/15 04:18:18
k
|
| +# Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import argparse |
| +import base64 |
| +import json |
| +import os |
| +import sys |
| +import zlib |
| + |
| +sys.path.insert(0, |
| + os.path.join(os.path.dirname(__file__), os.pardir)) |
| + # (/path/to/build/scripts) |
| +import common.env |
| +common.env.Install() |
| + |
| +from common import chromium_utils |
| + |
| +def main(): |
| + parser = argparse.ArgumentParser() |
| + parser.add_argument('-o', '--out', default=sys.stdout, |
| + help='Write the output content here. If omitted, output will be written ' |
| + 'to STDOUT.') |
| + def add_value_arg(p): |
| + p.add_argument('value', nargs='?', |
| + help='The intput content. If omitted, content will be read from STDIN.') |
| + subparsers = parser.add_subparsers() |
| + |
| + # encode |
| + p = subparsers.add_parser('encode') |
| + def encode(value, out): |
| + obj = json.loads(value) |
| + out.write(chromium_utils.b64_gz_json_encode(obj)) |
| + p.set_defaults(func=encode) |
| + add_value_arg(p) |
| + |
| + # decode |
| + p = subparsers.add_parser('decode') |
| + def decode(value, out): |
| + obj = chromium_utils.convert_gz_json_type(value) |
| + json.dump(obj, out, sort_keys=True, separators=(',', ':')) |
| + p.set_defaults(func=decode) |
| + add_value_arg(p) |
| + |
| + opts = parser.parse_args() |
| + |
| + # Read input value. |
| + value = opts.value |
| + if not value: |
| + value = sys.stdin.read() |
| + |
| + # Generate output. |
| + opts.func(value, opts.out) |
| + return 0 |
| + |
| + |
| +if __name__ == '__main__': |
| + sys.exit(main()) |