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