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