blob: a6b86fb2423090ea02b94f4b6ff50f3be28db812 [file] [log] [blame]
Nathaniel Manistaae4fbcd2015-09-23 16:29:44 +00001#!/usr/bin/env python2.7
Jan Tattermusch7897ae92017-06-07 22:57:36 +02002# Copyright 2015 gRPC authors.
Craig Tillerc2c79212015-02-16 12:00:01 -08003#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02004# 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
Craig Tillerc2c79212015-02-16 12:00:01 -08007#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02008# http://www.apache.org/licenses/LICENSE-2.0
Craig Tillerc2c79212015-02-16 12:00:01 -08009#
Jan Tattermusch7897ae92017-06-07 22:57:36 +020010# 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.
Craig Tillerc2c79212015-02-16 12:00:01 -080015
Craig Tiller1ebb7c82015-08-31 15:53:36 -070016# produces cleaner build.yaml files
Craig Tiller0b2a3722015-01-18 11:30:07 -080017
18import collections
Craig Tiller0b2a3722015-01-18 11:30:07 -080019import os
20import sys
Craig Tiller1ebb7c82015-08-31 15:53:36 -070021import yaml
Craig Tiller0b2a3722015-01-18 11:30:07 -080022
Craig Tillerd4eeff82015-01-19 20:51:28 -080023TEST = (os.environ.get('TEST', 'false') == 'true')
24
ncteisen26d70b12017-12-11 16:40:56 -080025_TOP_LEVEL_KEYS = [
26 'settings', 'proto_deps', 'filegroups', 'libs', 'targets', 'vspackages'
27]
Craig Tiller0b2a3722015-01-18 11:30:07 -080028_ELEM_KEYS = [
ncteisen26d70b12017-12-11 16:40:56 -080029 'name', 'gtest', 'cpu_cost', 'flaky', 'build', 'run', 'language',
30 'public_headers', 'headers', 'src', 'deps'
31]
32
Craig Tiller0b2a3722015-01-18 11:30:07 -080033
Craig Tiller1ebb7c82015-08-31 15:53:36 -070034def repr_ordered_dict(dumper, odict):
ncteisen26d70b12017-12-11 16:40:56 -080035 return dumper.represent_mapping(u'tag:yaml.org,2002:map', odict.items())
36
Craig Tiller1ebb7c82015-08-31 15:53:36 -070037
38yaml.add_representer(collections.OrderedDict, repr_ordered_dict)
39
ncteisen26d70b12017-12-11 16:40:56 -080040
Craig Tiller0b2a3722015-01-18 11:30:07 -080041def rebuild_as_ordered_dict(indict, special_keys):
ncteisen26d70b12017-12-11 16:40:56 -080042 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
Craig Tiller0b2a3722015-01-18 11:30:07 -080055
56def clean_elem(indict):
ncteisen26d70b12017-12-11 16:40:56 -080057 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
Craig Tiller0b2a3722015-01-18 11:30:07 -080065
66for filename in sys.argv[1:]:
Craig Tillerd4eeff82015-01-19 20:51:28 -080067 with open(filename) as f:
ncteisen26d70b12017-12-11 16:40:56 -080068 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)