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