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 66for filename in sys.argv[1:]: 67 with open(filename) as f: 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) 87