blob: 49a364412350c0aa6bb02e61c1b426ce9bc8fc29 [file] [log] [blame]
Nathaniel Manistaae4fbcd2015-09-23 16:29:44 +00001#!/usr/bin/env python2.7
Craig Tiller0eef9ee2016-01-20 09:56:21 -08002# Copyright 2015-2016, Google Inc.
Craig Tillerc2c79212015-02-16 12:00:01 -08003# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9# * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15# * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
Craig Tiller1ebb7c82015-08-31 15:53:36 -070031# produces cleaner build.yaml files
Craig Tiller0b2a3722015-01-18 11:30:07 -080032
33import collections
Craig Tiller0b2a3722015-01-18 11:30:07 -080034import os
35import sys
Craig Tiller1ebb7c82015-08-31 15:53:36 -070036import yaml
Craig Tiller0b2a3722015-01-18 11:30:07 -080037
Craig Tillerd4eeff82015-01-19 20:51:28 -080038TEST = (os.environ.get('TEST', 'false') == 'true')
39
Craig Tiller1b4e3302015-12-17 16:35:00 -080040_TOP_LEVEL_KEYS = ['settings', 'proto_deps', 'filegroups', 'libs', 'targets', 'vspackages']
Craig Tiller0b2a3722015-01-18 11:30:07 -080041_ELEM_KEYS = [
Craig Tillerce5021b2015-02-18 09:25:21 -080042 'name',
Craig Tiller5f735a62016-01-20 09:31:15 -080043 'cpu_cost',
Craig Tiller45115842015-03-02 10:05:10 -080044 'flaky',
Craig Tillerce5021b2015-02-18 09:25:21 -080045 'build',
Craig Tiller9d085a32015-02-11 18:17:01 -080046 'run',
Craig Tillerce5021b2015-02-18 09:25:21 -080047 'language',
48 'public_headers',
49 'headers',
50 'src',
Craig Tiller0b2a3722015-01-18 11:30:07 -080051 'deps']
52
Craig Tiller1ebb7c82015-08-31 15:53:36 -070053def repr_ordered_dict(dumper, odict):
54 return dumper.represent_mapping(u'tag:yaml.org,2002:map', odict.items())
55
56yaml.add_representer(collections.OrderedDict, repr_ordered_dict)
57
Craig Tiller0b2a3722015-01-18 11:30:07 -080058def rebuild_as_ordered_dict(indict, special_keys):
59 outdict = collections.OrderedDict()
Nicolas "Pixel" Noble62b8ddf2015-04-22 17:36:47 +020060 for key in sorted(indict.keys()):
61 if '#' in key:
62 outdict[key] = indict[key]
Craig Tiller0b2a3722015-01-18 11:30:07 -080063 for key in special_keys:
64 if key in indict:
65 outdict[key] = indict[key]
66 for key in sorted(indict.keys()):
67 if key in special_keys: continue
Nicolas "Pixel" Noble62b8ddf2015-04-22 17:36:47 +020068 if '#' in key: continue
Craig Tiller0b2a3722015-01-18 11:30:07 -080069 outdict[key] = indict[key]
70 return outdict
71
72def clean_elem(indict):
73 for name in ['public_headers', 'headers', 'src']:
74 if name not in indict: continue
75 inlist = indict[name]
Craig Tiller2d7e73f2015-01-31 20:06:02 -080076 protos = list(x for x in inlist if os.path.splitext(x)[1] == '.proto')
Craig Tiller0b2a3722015-01-18 11:30:07 -080077 others = set(x for x in inlist if x not in protos)
Craig Tiller2d7e73f2015-01-31 20:06:02 -080078 indict[name] = protos + sorted(others)
Craig Tiller0b2a3722015-01-18 11:30:07 -080079 return rebuild_as_ordered_dict(indict, _ELEM_KEYS)
80
81for filename in sys.argv[1:]:
82 with open(filename) as f:
Craig Tiller1ebb7c82015-08-31 15:53:36 -070083 js = yaml.load(f)
Craig Tiller0b2a3722015-01-18 11:30:07 -080084 js = rebuild_as_ordered_dict(js, _TOP_LEVEL_KEYS)
Craig Tiller0b2a3722015-01-18 11:30:07 -080085 for grp in ['filegroups', 'libs', 'targets']:
86 if grp not in js: continue
87 js[grp] = sorted([clean_elem(x) for x in js[grp]],
Craig Tiller7d7b6c72015-01-18 12:03:17 -080088 key=lambda x: (x.get('language', '_'), x['name']))
Craig Tiller25834342015-09-25 08:08:24 -070089 output = yaml.dump(js, indent=2, width=80, default_flow_style=False)
Craig Tiller0b2a3722015-01-18 11:30:07 -080090 # massage out trailing whitespace
91 lines = []
92 for line in output.splitlines():
93 lines.append(line.rstrip() + '\n')
Craig Tillerd4eeff82015-01-19 20:51:28 -080094 output = ''.join(lines)
95 if TEST:
96 with open(filename) as f:
97 assert f.read() == output
98 else:
99 with open(filename, 'w') as f:
100 f.write(output)
Craig Tiller0b2a3722015-01-18 11:30:07 -0800101