format-json 677 Bytes
Newer Older
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
#!/usr/bin/env python
r"""Command-line tool to format software release JSON for slapos.

Inspired by json.tool from python

Usage::

    format-json infile outfile

"""

import os
import sys
import json
import collections


def main():
  if len(sys.argv) != 3:
    raise SystemExit(sys.argv[0] + " infile outfile")

  with open(sys.argv[1], 'rb') as infile:
    try:
      obj = json.load(infile, object_pairs_hook=collections.OrderedDict)
    except ValueError, e:
      raise SystemExit(e)

  with open(sys.argv[2], 'wb') as outfile:
    json.dump(obj, outfile, sort_keys=False, indent=2, separators=(',', ': '))
    outfile.write('\n')


if __name__ == '__main__':
  main()