Nathaniel Manista | ae4fbcd | 2015-09-23 16:29:44 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 2 | # Copyright 2015, Google Inc. |
Craig Tiller | c2c7921 | 2015-02-16 12:00:01 -0800 | [diff] [blame] | 3 | # All rights reserved. |
| 4 | # |
| 5 | # Redistribution and use in source and binary forms, with or without |
| 6 | # modification, are permitted provided that the following conditions are |
| 7 | # met: |
| 8 | # |
| 9 | # * Redistributions of source code must retain the above copyright |
| 10 | # notice, this list of conditions and the following disclaimer. |
| 11 | # * Redistributions in binary form must reproduce the above |
| 12 | # copyright notice, this list of conditions and the following disclaimer |
| 13 | # in the documentation and/or other materials provided with the |
| 14 | # distribution. |
| 15 | # * Neither the name of Google Inc. nor the names of its |
| 16 | # contributors may be used to endorse or promote products derived from |
| 17 | # this software without specific prior written permission. |
| 18 | # |
| 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
Craig Tiller | 1ebb7c8 | 2015-08-31 15:53:36 -0700 | [diff] [blame] | 31 | # produces cleaner build.yaml files |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 32 | |
| 33 | import collections |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 34 | import os |
| 35 | import sys |
Craig Tiller | 1ebb7c8 | 2015-08-31 15:53:36 -0700 | [diff] [blame] | 36 | import yaml |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 37 | |
Craig Tiller | d4eeff8 | 2015-01-19 20:51:28 -0800 | [diff] [blame] | 38 | TEST = (os.environ.get('TEST', 'false') == 'true') |
| 39 | |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 40 | _TOP_LEVEL_KEYS = ['settings', 'proto_deps', 'filegroups', 'libs', 'targets', 'vspackages'] |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 41 | _ELEM_KEYS = [ |
Craig Tiller | ce5021b | 2015-02-18 09:25:21 -0800 | [diff] [blame] | 42 | 'name', |
Craig Tiller | ca62ff0 | 2016-02-24 22:22:57 -0800 | [diff] [blame] | 43 | 'gtest', |
Craig Tiller | 5f735a6 | 2016-01-20 09:31:15 -0800 | [diff] [blame] | 44 | 'cpu_cost', |
Craig Tiller | 4511584 | 2015-03-02 10:05:10 -0800 | [diff] [blame] | 45 | 'flaky', |
Craig Tiller | ce5021b | 2015-02-18 09:25:21 -0800 | [diff] [blame] | 46 | 'build', |
Craig Tiller | 9d085a3 | 2015-02-11 18:17:01 -0800 | [diff] [blame] | 47 | 'run', |
Craig Tiller | ce5021b | 2015-02-18 09:25:21 -0800 | [diff] [blame] | 48 | 'language', |
| 49 | 'public_headers', |
| 50 | 'headers', |
| 51 | 'src', |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 52 | 'deps'] |
| 53 | |
Craig Tiller | 1ebb7c8 | 2015-08-31 15:53:36 -0700 | [diff] [blame] | 54 | def repr_ordered_dict(dumper, odict): |
| 55 | return dumper.represent_mapping(u'tag:yaml.org,2002:map', odict.items()) |
| 56 | |
| 57 | yaml.add_representer(collections.OrderedDict, repr_ordered_dict) |
| 58 | |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 59 | def rebuild_as_ordered_dict(indict, special_keys): |
| 60 | outdict = collections.OrderedDict() |
Nicolas "Pixel" Noble | 62b8ddf | 2015-04-22 17:36:47 +0200 | [diff] [blame] | 61 | for key in sorted(indict.keys()): |
| 62 | if '#' in key: |
| 63 | outdict[key] = indict[key] |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 64 | for key in special_keys: |
| 65 | if key in indict: |
| 66 | outdict[key] = indict[key] |
| 67 | for key in sorted(indict.keys()): |
| 68 | if key in special_keys: continue |
Nicolas "Pixel" Noble | 62b8ddf | 2015-04-22 17:36:47 +0200 | [diff] [blame] | 69 | if '#' in key: continue |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 70 | outdict[key] = indict[key] |
| 71 | return outdict |
| 72 | |
| 73 | def clean_elem(indict): |
| 74 | for name in ['public_headers', 'headers', 'src']: |
| 75 | if name not in indict: continue |
| 76 | inlist = indict[name] |
Craig Tiller | 2d7e73f | 2015-01-31 20:06:02 -0800 | [diff] [blame] | 77 | protos = list(x for x in inlist if os.path.splitext(x)[1] == '.proto') |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 78 | others = set(x for x in inlist if x not in protos) |
Craig Tiller | 2d7e73f | 2015-01-31 20:06:02 -0800 | [diff] [blame] | 79 | indict[name] = protos + sorted(others) |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 80 | return rebuild_as_ordered_dict(indict, _ELEM_KEYS) |
| 81 | |
| 82 | for filename in sys.argv[1:]: |
| 83 | with open(filename) as f: |
Craig Tiller | 1ebb7c8 | 2015-08-31 15:53:36 -0700 | [diff] [blame] | 84 | js = yaml.load(f) |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 85 | js = rebuild_as_ordered_dict(js, _TOP_LEVEL_KEYS) |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 86 | for grp in ['filegroups', 'libs', 'targets']: |
| 87 | if grp not in js: continue |
| 88 | js[grp] = sorted([clean_elem(x) for x in js[grp]], |
Craig Tiller | 7d7b6c7 | 2015-01-18 12:03:17 -0800 | [diff] [blame] | 89 | key=lambda x: (x.get('language', '_'), x['name'])) |
Craig Tiller | 2583434 | 2015-09-25 08:08:24 -0700 | [diff] [blame] | 90 | output = yaml.dump(js, indent=2, width=80, default_flow_style=False) |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 91 | # massage out trailing whitespace |
| 92 | lines = [] |
| 93 | for line in output.splitlines(): |
| 94 | lines.append(line.rstrip() + '\n') |
Craig Tiller | d4eeff8 | 2015-01-19 20:51:28 -0800 | [diff] [blame] | 95 | output = ''.join(lines) |
| 96 | if TEST: |
| 97 | with open(filename) as f: |
| 98 | assert f.read() == output |
| 99 | else: |
| 100 | with open(filename, 'w') as f: |
| 101 | f.write(output) |