blob: f09a01fc57f0d58f6bd47ee21f4cb8a48cdb2dd2 [file] [log] [blame]
Nathaniel Manistaae4fbcd2015-09-23 16:29:44 +00001#!/usr/bin/env python2.7
Craig Tiller6169d5f2016-03-31 07:46:18 -07002# Copyright 2015, 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 Tillerca62ff02016-02-24 22:22:57 -080043 'gtest',
Craig Tiller5f735a62016-01-20 09:31:15 -080044 'cpu_cost',
Craig Tiller45115842015-03-02 10:05:10 -080045 'flaky',
Craig Tillerce5021b2015-02-18 09:25:21 -080046 'build',
Craig Tiller9d085a32015-02-11 18:17:01 -080047 'run',
Craig Tillerce5021b2015-02-18 09:25:21 -080048 'language',
49 'public_headers',
50 'headers',
51 'src',
Craig Tiller0b2a3722015-01-18 11:30:07 -080052 'deps']
53
Craig Tiller1ebb7c82015-08-31 15:53:36 -070054def repr_ordered_dict(dumper, odict):
55 return dumper.represent_mapping(u'tag:yaml.org,2002:map', odict.items())
56
57yaml.add_representer(collections.OrderedDict, repr_ordered_dict)
58
Craig Tiller0b2a3722015-01-18 11:30:07 -080059def rebuild_as_ordered_dict(indict, special_keys):
60 outdict = collections.OrderedDict()
Nicolas "Pixel" Noble62b8ddf2015-04-22 17:36:47 +020061 for key in sorted(indict.keys()):
62 if '#' in key:
63 outdict[key] = indict[key]
Craig Tiller0b2a3722015-01-18 11:30:07 -080064 for key in special_keys:
65 if key in indict:
66 outdict[key] = indict[key]
67 for key in sorted(indict.keys()):
68 if key in special_keys: continue
Nicolas "Pixel" Noble62b8ddf2015-04-22 17:36:47 +020069 if '#' in key: continue
Craig Tiller0b2a3722015-01-18 11:30:07 -080070 outdict[key] = indict[key]
71 return outdict
72
73def clean_elem(indict):
74 for name in ['public_headers', 'headers', 'src']:
75 if name not in indict: continue
76 inlist = indict[name]
Craig Tiller2d7e73f2015-01-31 20:06:02 -080077 protos = list(x for x in inlist if os.path.splitext(x)[1] == '.proto')
Craig Tiller0b2a3722015-01-18 11:30:07 -080078 others = set(x for x in inlist if x not in protos)
Craig Tiller2d7e73f2015-01-31 20:06:02 -080079 indict[name] = protos + sorted(others)
Craig Tiller0b2a3722015-01-18 11:30:07 -080080 return rebuild_as_ordered_dict(indict, _ELEM_KEYS)
81
82for filename in sys.argv[1:]:
83 with open(filename) as f:
Craig Tiller1ebb7c82015-08-31 15:53:36 -070084 js = yaml.load(f)
Craig Tiller0b2a3722015-01-18 11:30:07 -080085 js = rebuild_as_ordered_dict(js, _TOP_LEVEL_KEYS)
Craig Tiller0b2a3722015-01-18 11:30:07 -080086 for grp in ['filegroups', 'libs', 'targets']:
87 if grp not in js: continue
88 js[grp] = sorted([clean_elem(x) for x in js[grp]],
Craig Tiller7d7b6c72015-01-18 12:03:17 -080089 key=lambda x: (x.get('language', '_'), x['name']))
Craig Tiller25834342015-09-25 08:08:24 -070090 output = yaml.dump(js, indent=2, width=80, default_flow_style=False)
Craig Tiller0b2a3722015-01-18 11:30:07 -080091 # massage out trailing whitespace
92 lines = []
93 for line in output.splitlines():
94 lines.append(line.rstrip() + '\n')
Craig Tillerd4eeff82015-01-19 20:51:28 -080095 output = ''.join(lines)
96 if TEST:
97 with open(filename) as f:
98 assert f.read() == output
99 else:
100 with open(filename, 'w') as f:
101 f.write(output)