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