Nathaniel Manista | ae4fbcd | 2015-09-23 16:29:44 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 |
Craig Tiller | 0eef9ee | 2016-01-20 09:56:21 -0800 | [diff] [blame] | 2 | # Copyright 2015-2016, 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 | 5f735a6 | 2016-01-20 09:31:15 -0800 | [diff] [blame] | 43 | 'cpu_cost', |
Craig Tiller | 4511584 | 2015-03-02 10:05:10 -0800 | [diff] [blame] | 44 | 'flaky', |
Craig Tiller | ce5021b | 2015-02-18 09:25:21 -0800 | [diff] [blame] | 45 | 'build', |
Craig Tiller | 9d085a3 | 2015-02-11 18:17:01 -0800 | [diff] [blame] | 46 | 'run', |
Craig Tiller | ce5021b | 2015-02-18 09:25:21 -0800 | [diff] [blame] | 47 | 'language', |
| 48 | 'public_headers', |
| 49 | 'headers', |
| 50 | 'src', |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 51 | 'deps'] |
| 52 | |
Craig Tiller | 1ebb7c8 | 2015-08-31 15:53:36 -0700 | [diff] [blame] | 53 | def repr_ordered_dict(dumper, odict): |
| 54 | return dumper.represent_mapping(u'tag:yaml.org,2002:map', odict.items()) |
| 55 | |
| 56 | yaml.add_representer(collections.OrderedDict, repr_ordered_dict) |
| 57 | |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 58 | def rebuild_as_ordered_dict(indict, special_keys): |
| 59 | outdict = collections.OrderedDict() |
Nicolas "Pixel" Noble | 62b8ddf | 2015-04-22 17:36:47 +0200 | [diff] [blame] | 60 | for key in sorted(indict.keys()): |
| 61 | if '#' in key: |
| 62 | outdict[key] = indict[key] |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 63 | for key in special_keys: |
| 64 | if key in indict: |
| 65 | outdict[key] = indict[key] |
| 66 | for key in sorted(indict.keys()): |
| 67 | if key in special_keys: continue |
Nicolas "Pixel" Noble | 62b8ddf | 2015-04-22 17:36:47 +0200 | [diff] [blame] | 68 | if '#' in key: continue |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 69 | outdict[key] = indict[key] |
| 70 | return outdict |
| 71 | |
| 72 | def clean_elem(indict): |
| 73 | for name in ['public_headers', 'headers', 'src']: |
| 74 | if name not in indict: continue |
| 75 | inlist = indict[name] |
Craig Tiller | 2d7e73f | 2015-01-31 20:06:02 -0800 | [diff] [blame] | 76 | 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] | 77 | others = set(x for x in inlist if x not in protos) |
Craig Tiller | 2d7e73f | 2015-01-31 20:06:02 -0800 | [diff] [blame] | 78 | indict[name] = protos + sorted(others) |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 79 | return rebuild_as_ordered_dict(indict, _ELEM_KEYS) |
| 80 | |
| 81 | for filename in sys.argv[1:]: |
| 82 | with open(filename) as f: |
Craig Tiller | 1ebb7c8 | 2015-08-31 15:53:36 -0700 | [diff] [blame] | 83 | js = yaml.load(f) |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 84 | js = rebuild_as_ordered_dict(js, _TOP_LEVEL_KEYS) |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 85 | for grp in ['filegroups', 'libs', 'targets']: |
| 86 | if grp not in js: continue |
| 87 | js[grp] = sorted([clean_elem(x) for x in js[grp]], |
Craig Tiller | 7d7b6c7 | 2015-01-18 12:03:17 -0800 | [diff] [blame] | 88 | key=lambda x: (x.get('language', '_'), x['name'])) |
Craig Tiller | 2583434 | 2015-09-25 08:08:24 -0700 | [diff] [blame] | 89 | output = yaml.dump(js, indent=2, width=80, default_flow_style=False) |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 90 | # massage out trailing whitespace |
| 91 | lines = [] |
| 92 | for line in output.splitlines(): |
| 93 | lines.append(line.rstrip() + '\n') |
Craig Tiller | d4eeff8 | 2015-01-19 20:51:28 -0800 | [diff] [blame] | 94 | output = ''.join(lines) |
| 95 | if TEST: |
| 96 | with open(filename) as f: |
| 97 | assert f.read() == output |
| 98 | else: |
| 99 | with open(filename, 'w') as f: |
| 100 | f.write(output) |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 101 | |