mirror of https://github.com/grpc/grpc.git
The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#)
https://grpc.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
3.1 KiB
96 lines
3.1 KiB
9 years ago
|
#!/usr/bin/env python2.7
|
||
8 years ago
|
# Copyright 2015 gRPC authors.
|
||
10 years ago
|
#
|
||
8 years ago
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
# you may not use this file except in compliance with the License.
|
||
|
# You may obtain a copy of the License at
|
||
10 years ago
|
#
|
||
8 years ago
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||
10 years ago
|
#
|
||
8 years ago
|
# Unless required by applicable law or agreed to in writing, software
|
||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
# See the License for the specific language governing permissions and
|
||
|
# limitations under the License.
|
||
10 years ago
|
|
||
9 years ago
|
# produces cleaner build.yaml files
|
||
10 years ago
|
|
||
|
import collections
|
||
|
import os
|
||
|
import sys
|
||
9 years ago
|
import yaml
|
||
10 years ago
|
|
||
10 years ago
|
TEST = (os.environ.get('TEST', 'false') == 'true')
|
||
|
|
||
7 years ago
|
_TOP_LEVEL_KEYS = [
|
||
|
'settings', 'proto_deps', 'filegroups', 'libs', 'targets', 'vspackages'
|
||
|
]
|
||
10 years ago
|
_ELEM_KEYS = [
|
||
7 years ago
|
'name', 'gtest', 'cpu_cost', 'flaky', 'build', 'run', 'language',
|
||
|
'public_headers', 'headers', 'src', 'deps'
|
||
|
]
|
||
|
|
||
10 years ago
|
|
||
9 years ago
|
def repr_ordered_dict(dumper, odict):
|
||
7 years ago
|
return dumper.represent_mapping(u'tag:yaml.org,2002:map', odict.items())
|
||
|
|
||
9 years ago
|
|
||
|
yaml.add_representer(collections.OrderedDict, repr_ordered_dict)
|
||
|
|
||
7 years ago
|
|
||
5 years ago
|
def _rebuild_as_ordered_dict(indict, special_keys):
|
||
7 years ago
|
outdict = collections.OrderedDict()
|
||
|
for key in sorted(indict.keys()):
|
||
|
if '#' in key:
|
||
|
outdict[key] = indict[key]
|
||
|
for key in special_keys:
|
||
|
if key in indict:
|
||
|
outdict[key] = indict[key]
|
||
|
for key in sorted(indict.keys()):
|
||
|
if key in special_keys: continue
|
||
|
if '#' in key: continue
|
||
|
outdict[key] = indict[key]
|
||
|
return outdict
|
||
|
|
||
10 years ago
|
|
||
5 years ago
|
def _clean_elem(indict):
|
||
7 years ago
|
for name in ['public_headers', 'headers', 'src']:
|
||
|
if name not in indict: continue
|
||
|
inlist = indict[name]
|
||
|
protos = list(x for x in inlist if os.path.splitext(x)[1] == '.proto')
|
||
|
others = set(x for x in inlist if x not in protos)
|
||
|
indict[name] = protos + sorted(others)
|
||
5 years ago
|
return _rebuild_as_ordered_dict(indict, _ELEM_KEYS)
|
||
7 years ago
|
|
||
10 years ago
|
|
||
5 years ago
|
def cleaned_build_yaml_dict_as_string(indict):
|
||
|
"""Takes dictionary which represents yaml file and returns the cleaned-up yaml string"""
|
||
|
js = _rebuild_as_ordered_dict(indict, _TOP_LEVEL_KEYS)
|
||
7 years ago
|
for grp in ['filegroups', 'libs', 'targets']:
|
||
|
if grp not in js: continue
|
||
5 years ago
|
js[grp] = sorted([_clean_elem(x) for x in js[grp]],
|
||
5 years ago
|
key=lambda x: (x.get('language', '_'), x['name']))
|
||
7 years ago
|
output = yaml.dump(js, indent=2, width=80, default_flow_style=False)
|
||
|
# massage out trailing whitespace
|
||
|
lines = []
|
||
|
for line in output.splitlines():
|
||
|
lines.append(line.rstrip() + '\n')
|
||
|
output = ''.join(lines)
|
||
5 years ago
|
return output
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
for filename in sys.argv[1:]:
|
||
7 years ago
|
with open(filename) as f:
|
||
5 years ago
|
js = yaml.load(f)
|
||
|
output = cleaned_build_yaml_dict_as_string(js)
|
||
|
if TEST:
|
||
|
with open(filename) as f:
|
||
|
if not f.read() == output:
|
||
|
raise Exception(
|
||
|
'Looks like build-cleaner.py has not been run for file "%s"?'
|
||
|
% filename)
|
||
|
else:
|
||
|
with open(filename, 'w') as f:
|
||
|
f.write(output)
|