reformat-json.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/python
  2. '''
  3. Copyright 2013 Google Inc.
  4. Use of this source code is governed by a BSD-style license that can be
  5. found in the LICENSE file.
  6. '''
  7. '''
  8. Rewrites a JSON file to use Python's standard JSON pretty-print format,
  9. so that subsequent runs of rebaseline.py will generate useful diffs
  10. (only the actual checksum differences will show up as diffs, not obscured
  11. by format differences).
  12. Should not modify the JSON contents in any meaningful way.
  13. '''
  14. # System-level imports
  15. import argparse
  16. import os
  17. import sys
  18. # Imports from within Skia
  19. #
  20. # We need to add the 'gm' directory, so that we can import gm_json.py within
  21. # that directory. That script allows us to parse the actual-results.json file
  22. # written out by the GM tool.
  23. # Make sure that the 'gm' dir is in the PYTHONPATH, but add it at the *end*
  24. # so any dirs that are already in the PYTHONPATH will be preferred.
  25. #
  26. # This assumes that the 'gm' directory has been checked out as a sibling of
  27. # the 'tools' directory containing this script, which will be the case if
  28. # 'trunk' was checked out as a single unit.
  29. GM_DIRECTORY = os.path.realpath(
  30. os.path.join(os.path.dirname(os.path.dirname(__file__)), 'gm'))
  31. if GM_DIRECTORY not in sys.path:
  32. sys.path.append(GM_DIRECTORY)
  33. import gm_json
  34. def Reformat(filename):
  35. print 'Reformatting file %s...' % filename
  36. gm_json.WriteToFile(gm_json.LoadFromFile(filename), filename)
  37. def _Main():
  38. parser = argparse.ArgumentParser(description='Reformat JSON files in-place.')
  39. parser.add_argument('filenames', metavar='FILENAME', nargs='+',
  40. help='file to reformat')
  41. args = parser.parse_args()
  42. for filename in args.filenames:
  43. Reformat(filename)
  44. sys.exit(0)
  45. if __name__ == '__main__':
  46. _Main()