format-json 706 Bytes
#!/usr/bin/env python
r"""Command-line tool to format software release JSON for slapos.

Inspired by json.tool from python, but enforcing 2 spaces and non-sorted keys.
The files are modified in-place.

Usage::

    format-json file1.json [file2.json]

"""

import os
import sys
import json
import collections


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

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

if __name__ == '__main__':
  main()