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