| 1 |
#!/usr/bin/env python3 |
| 2 |
# Copyright 2015 gRPC authors. |
| 3 |
# |
| 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 |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 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. |
| 15 |
|
| 16 |
# produces cleaner build.yaml files |
| 17 |
|
| 18 |
import collections |
| 19 |
import os |
| 20 |
import sys |
| 21 |
|
| 22 |
import yaml |
| 23 |
|
| 24 |
TEST = (os.environ.get('TEST', 'false') == 'true') |
| 25 |
|
| 26 |
_TOP_LEVEL_KEYS = [ |
| 27 |
'settings', 'proto_deps', 'filegroups', 'libs', 'targets', 'vspackages' |
| 28 |
] |
| 29 |
_ELEM_KEYS = [ |
| 30 |
'name', 'gtest', 'cpu_cost', 'flaky', 'build', 'run', 'language', |
| 31 |
'public_headers', 'headers', 'src', 'deps' |
| 32 |
] |
| 33 |
|
| 34 |
|
| 35 |
def repr_ordered_dict(dumper, odict): |
| 36 |
return dumper.represent_mapping('tag:yaml.org,2002:map', |
| 37 |
list(odict.items())) |
| 38 |
|
| 39 |
|
| 40 |
yaml.add_representer(collections.OrderedDict, repr_ordered_dict) |
| 41 |
|
| 42 |
|
| 43 |
def _rebuild_as_ordered_dict(indict, special_keys): |
| 44 |
outdict = collections.OrderedDict() |
| 45 |
for key in sorted(indict.keys()): |
| 46 |
if '#' in key: |
| 47 |
outdict[key] = indict[key] |
| 48 |
for key in special_keys: |
| 49 |
if key in indict: |
| 50 |
outdict[key] = indict[key] |
| 51 |
for key in sorted(indict.keys()): |
| 52 |
if key in special_keys: |
| 53 |
continue |
| 54 |
if '#' in key: |
| 55 |
continue |
| 56 |
outdict[key] = indict[key] |
| 57 |
return outdict |
| 58 |
|
| 59 |
|
| 60 |
def _clean_elem(indict): |
| 61 |
for name in ['public_headers', 'headers', 'src']: |
| 62 |
if name not in indict: |
| 63 |
continue |
| 64 |
inlist = indict[name] |
| 65 |
protos = list(x for x in inlist if os.path.splitext(x)[1] == '.proto') |
| 66 |
others = set(x for x in inlist if x not in protos) |
| 67 |
indict[name] = protos + sorted(others) |
| 68 |
return _rebuild_as_ordered_dict(indict, _ELEM_KEYS) |
| 69 |
|
| 70 |
|
| 71 |
def cleaned_build_yaml_dict_as_string(indict): |
| 72 |
"""Takes dictionary which represents yaml file and returns the cleaned-up yaml string""" |
| 73 |
js = _rebuild_as_ordered_dict(indict, _TOP_LEVEL_KEYS) |
| 74 |
for grp in ['filegroups', 'libs', 'targets']: |
| 75 |
if grp not in js: |
| 76 |
continue |
| 77 |
js[grp] = sorted([_clean_elem(x) for x in js[grp]], |
| 78 |
key=lambda x: (x.get('language', '_'), x['name'])) |
| 79 |
output = yaml.dump(js, indent=2, width=80, default_flow_style=False) |
| 80 |
# massage out trailing whitespace |
| 81 |
lines = [] |
| 82 |
for line in output.splitlines(): |
| 83 |
lines.append(line.rstrip() + '\n') |
| 84 |
output = ''.join(lines) |
| 85 |
return output |
| 86 |
|
| 87 |
|
| 88 |
if __name__ == '__main__': |
| 89 |
for filename in sys.argv[1:]: |
| 90 |
with open(filename) as f: |
| 91 |
js = yaml.load(f, Loader=yaml.FullLoader) |
| 92 |
output = cleaned_build_yaml_dict_as_string(js) |
| 93 |
if TEST: |
| 94 |
with open(filename) as f: |
| 95 |
if not f.read() == output: |
| 96 |
raise Exception( |
| 97 |
'Looks like build-cleaner.py has not been run for file "%s"?' |
| 98 |
% filename) |
| 99 |
else: |
| 100 |
with open(filename, 'w') as f: |
| 101 |
f.write(output) |