Nathaniel Manista | ae4fbcd | 2015-09-23 16:29:44 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 2 | # Copyright 2015 gRPC authors. |
Craig Tiller | c2c7921 | 2015-02-16 12:00:01 -0800 | [diff] [blame] | 3 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
Craig Tiller | c2c7921 | 2015-02-16 12:00:01 -0800 | [diff] [blame] | 7 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | c2c7921 | 2015-02-16 12:00:01 -0800 | [diff] [blame] | 9 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
Craig Tiller | c2c7921 | 2015-02-16 12:00:01 -0800 | [diff] [blame] | 15 | |
Craig Tiller | 1ebb7c8 | 2015-08-31 15:53:36 -0700 | [diff] [blame] | 16 | # produces cleaner build.yaml files |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 17 | |
| 18 | import collections |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 19 | import os |
| 20 | import sys |
Craig Tiller | 1ebb7c8 | 2015-08-31 15:53:36 -0700 | [diff] [blame] | 21 | import yaml |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 22 | |
Craig Tiller | d4eeff8 | 2015-01-19 20:51:28 -0800 | [diff] [blame] | 23 | TEST = (os.environ.get('TEST', 'false') == 'true') |
| 24 | |
ncteisen | 26d70b1 | 2017-12-11 16:40:56 -0800 | [diff] [blame] | 25 | _TOP_LEVEL_KEYS = [ |
| 26 | 'settings', 'proto_deps', 'filegroups', 'libs', 'targets', 'vspackages' |
| 27 | ] |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 28 | _ELEM_KEYS = [ |
ncteisen | 26d70b1 | 2017-12-11 16:40:56 -0800 | [diff] [blame] | 29 | 'name', 'gtest', 'cpu_cost', 'flaky', 'build', 'run', 'language', |
| 30 | 'public_headers', 'headers', 'src', 'deps' |
| 31 | ] |
| 32 | |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 33 | |
Craig Tiller | 1ebb7c8 | 2015-08-31 15:53:36 -0700 | [diff] [blame] | 34 | def repr_ordered_dict(dumper, odict): |
ncteisen | 26d70b1 | 2017-12-11 16:40:56 -0800 | [diff] [blame] | 35 | return dumper.represent_mapping(u'tag:yaml.org,2002:map', odict.items()) |
| 36 | |
Craig Tiller | 1ebb7c8 | 2015-08-31 15:53:36 -0700 | [diff] [blame] | 37 | |
| 38 | yaml.add_representer(collections.OrderedDict, repr_ordered_dict) |
| 39 | |
ncteisen | 26d70b1 | 2017-12-11 16:40:56 -0800 | [diff] [blame] | 40 | |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 41 | def rebuild_as_ordered_dict(indict, special_keys): |
ncteisen | 26d70b1 | 2017-12-11 16:40:56 -0800 | [diff] [blame] | 42 | outdict = collections.OrderedDict() |
| 43 | for key in sorted(indict.keys()): |
| 44 | if '#' in key: |
| 45 | outdict[key] = indict[key] |
| 46 | for key in special_keys: |
| 47 | if key in indict: |
| 48 | outdict[key] = indict[key] |
| 49 | for key in sorted(indict.keys()): |
| 50 | if key in special_keys: continue |
| 51 | if '#' in key: continue |
| 52 | outdict[key] = indict[key] |
| 53 | return outdict |
| 54 | |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 55 | |
| 56 | def clean_elem(indict): |
ncteisen | 26d70b1 | 2017-12-11 16:40:56 -0800 | [diff] [blame] | 57 | for name in ['public_headers', 'headers', 'src']: |
| 58 | if name not in indict: continue |
| 59 | inlist = indict[name] |
| 60 | protos = list(x for x in inlist if os.path.splitext(x)[1] == '.proto') |
| 61 | others = set(x for x in inlist if x not in protos) |
| 62 | indict[name] = protos + sorted(others) |
| 63 | return rebuild_as_ordered_dict(indict, _ELEM_KEYS) |
| 64 | |
Craig Tiller | 0b2a372 | 2015-01-18 11:30:07 -0800 | [diff] [blame] | 65 | |
| 66 | for filename in sys.argv[1:]: |
Craig Tiller | d4eeff8 | 2015-01-19 20:51:28 -0800 | [diff] [blame] | 67 | with open(filename) as f: |
ncteisen | 26d70b1 | 2017-12-11 16:40:56 -0800 | [diff] [blame] | 68 | js = yaml.load(f) |
| 69 | js = rebuild_as_ordered_dict(js, _TOP_LEVEL_KEYS) |
| 70 | for grp in ['filegroups', 'libs', 'targets']: |
| 71 | if grp not in js: continue |
| 72 | js[grp] = sorted( |
| 73 | [clean_elem(x) for x in js[grp]], |
| 74 | key=lambda x: (x.get('language', '_'), x['name'])) |
| 75 | output = yaml.dump(js, indent=2, width=80, default_flow_style=False) |
| 76 | # massage out trailing whitespace |
| 77 | lines = [] |
| 78 | for line in output.splitlines(): |
| 79 | lines.append(line.rstrip() + '\n') |
| 80 | output = ''.join(lines) |
| 81 | if TEST: |
| 82 | with open(filename) as f: |
| 83 | assert f.read() == output |
| 84 | else: |
| 85 | with open(filename, 'w') as f: |
| 86 | f.write(output) |