epoger@google.com | 727b33f | 2013-07-19 15:45:22 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
Eric Boren | a1db799 | 2021-03-25 09:04:43 -0400 | [diff] [blame] | 3 | |
epoger@google.com | 727b33f | 2013-07-19 15:45:22 +0000 | [diff] [blame] | 4 | ''' |
| 5 | Copyright 2013 Google Inc. |
| 6 | |
| 7 | Use of this source code is governed by a BSD-style license that can be |
| 8 | found in the LICENSE file. |
| 9 | ''' |
| 10 | |
| 11 | ''' |
| 12 | Rewrites a JSON file to use Python's standard JSON pretty-print format, |
| 13 | so that subsequent runs of rebaseline.py will generate useful diffs |
| 14 | (only the actual checksum differences will show up as diffs, not obscured |
| 15 | by format differences). |
| 16 | |
| 17 | Should not modify the JSON contents in any meaningful way. |
| 18 | ''' |
| 19 | |
Eric Boren | a1db799 | 2021-03-25 09:04:43 -0400 | [diff] [blame] | 20 | |
epoger@google.com | 727b33f | 2013-07-19 15:45:22 +0000 | [diff] [blame] | 21 | # System-level imports |
Eric Boren | a1db799 | 2021-03-25 09:04:43 -0400 | [diff] [blame] | 22 | from __future__ import print_function |
epoger@google.com | 727b33f | 2013-07-19 15:45:22 +0000 | [diff] [blame] | 23 | import argparse |
| 24 | import os |
| 25 | import sys |
| 26 | |
Eric Boren | a1db799 | 2021-03-25 09:04:43 -0400 | [diff] [blame] | 27 | |
epoger@google.com | 727b33f | 2013-07-19 15:45:22 +0000 | [diff] [blame] | 28 | # 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. |
| 39 | GM_DIRECTORY = os.path.realpath( |
| 40 | os.path.join(os.path.dirname(os.path.dirname(__file__)), 'gm')) |
| 41 | if GM_DIRECTORY not in sys.path: |
| 42 | sys.path.append(GM_DIRECTORY) |
| 43 | import gm_json |
| 44 | |
| 45 | def Reformat(filename): |
Eric Boren | a1db799 | 2021-03-25 09:04:43 -0400 | [diff] [blame] | 46 | print('Reformatting file %s...' % filename) |
epoger@google.com | 727b33f | 2013-07-19 15:45:22 +0000 | [diff] [blame] | 47 | gm_json.WriteToFile(gm_json.LoadFromFile(filename), filename) |
| 48 | |
| 49 | def _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 | |
| 58 | if __name__ == '__main__': |
| 59 | _Main() |