1#!/usr/bin/env python2.7 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(u'tag:yaml.org,2002:map', odict.items()) 36 37 38yaml.add_representer(collections.OrderedDict, repr_ordered_dict) 39 40 41def _rebuild_as_ordered_dict(indict, special_keys): 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 55 56def _clean_elem(indict): 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 65 66def cleaned_build_yaml_dict_as_string(indict): 67 """Takes dictionary which represents yaml file and returns the cleaned-up yaml string""" 68 js = _rebuild_as_ordered_dict(indict, _TOP_LEVEL_KEYS) 69 for grp in ['filegroups', 'libs', 'targets']: 70 if grp not in js: continue 71 js[grp] = sorted([_clean_elem(x) for x in js[grp]], 72 key=lambda x: (x.get('language', '_'), x['name'])) 73 output = yaml.dump(js, indent=2, width=80, default_flow_style=False) 74 # massage out trailing whitespace 75 lines = [] 76 for line in output.splitlines(): 77 lines.append(line.rstrip() + '\n') 78 output = ''.join(lines) 79 return output 80 81 82if __name__ == '__main__': 83 for filename in sys.argv[1:]: 84 with open(filename) as f: 85 js = yaml.load(f) 86 output = cleaned_build_yaml_dict_as_string(js) 87 if TEST: 88 with open(filename) as f: 89 if not f.read() == output: 90 raise Exception( 91 'Looks like build-cleaner.py has not been run for file "%s"?' 92 % filename) 93 else: 94 with open(filename, 'w') as f: 95 f.write(output) 96