OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """Contains configuration and setup of gatekeeper build database. | |
7 | |
8 The databse is a versioned JSON file built of NamedTuples. | |
9 """ | |
10 | |
11 import collections | |
12 import json | |
13 import logging | |
14 import optparse | |
15 import os | |
16 import sys | |
17 | |
18 from common import chromium_utils | |
19 from slave import gatekeeper_ng_config | |
20 | |
21 | |
22 DATA_DIR = os.path.dirname(os.path.abspath(__file__)) | |
23 | |
24 | |
25 # Bump each time there is an incompatible change in build_db. | |
26 BUILD_DB_VERSION = 1 | |
27 | |
28 | |
29 _BuildDB = collections.namedtuple('BuildDB', [ | |
30 'build_db_version', | |
31 'masters', | |
iannucci
2014/02/24 20:20:23
Add comment after these like
'masters' # {<maste
ghost stip (do not use)
2014/02/25 07:12:40
Done.
| |
32 'sections', | |
33 ]) | |
34 | |
35 | |
36 _BuildDBBuild = collections.namedtuple('BuildDBBuild', [ | |
37 'finished', | |
38 'triggered', | |
39 ]) | |
40 | |
41 | |
42 class JsonNode(object): | |
43 """Allows for serialization of NamedTuples to JSON.""" | |
44 def _asdict(self): # pylint: disable=R0201 | |
45 return {} | |
46 | |
47 # TODO(stip): recursively encode child nodes. | |
48 def asJson(self): | |
49 nodes_to_encode = filter(lambda x: hasattr(x[1], 'asJson'), | |
50 self._asdict().iteritems()) | |
iannucci
2014/02/24 20:20:23
Probably shorter as a list comp:
[(k, v) for k, v
ghost stip (do not use)
2014/02/25 07:12:40
Done.
| |
51 standard_nodes = filter(lambda x: not hasattr(x[1], 'asJson'), | |
52 self._asdict().iteritems()) | |
iannucci
2014/02/24 20:20:23
Though, you could also combine these to by just do
| |
53 newly_encoded_nodes = map(lambda x: (x[0], x[1].asJson()), nodes_to_encode) | |
54 | |
55 return dict(standard_nodes + newly_encoded_nodes) | |
56 | |
57 | |
58 class BuildDB(_BuildDB, JsonNode): | |
59 pass | |
60 | |
61 | |
62 class BuildDBBuild(_BuildDBBuild, JsonNode): | |
63 pass | |
64 | |
65 | |
66 class BadConf(Exception): | |
67 pass | |
68 | |
69 | |
70 def gen_db(**kwargs): | |
71 """Helper function to generate a default database.""" | |
72 defaults = [('build_db_version', BUILD_DB_VERSION), | |
73 ('masters', {}), | |
74 ('sections', {})] | |
75 | |
76 for key, default in defaults: | |
77 kwargs.setdefault(key, default) | |
78 | |
79 return BuildDB(**kwargs) | |
80 | |
81 def gen_build(**kwargs): | |
82 """Helper function to generate a default build.""" | |
83 defaults = [('finished', False), | |
84 ('triggered', [])] | |
85 | |
86 for key, default in defaults: | |
87 kwargs.setdefault(key, default) | |
88 | |
89 return BuildDBBuild(**kwargs) | |
90 | |
91 | |
92 def load_from_json(f): | |
93 """Load a build from a JSON stream.""" | |
94 build_db = gen_db() | |
95 | |
96 json_build_db = json.load(f) | |
97 | |
98 if json_build_db.get('build_db_version', 0) != BUILD_DB_VERSION: | |
iannucci
2014/02/24 20:20:23
could just let this default to None I think?
ghost stip (do not use)
2014/02/25 07:12:40
Done.
| |
99 raise BadConf('file is an older db version: %d (expecting %d)' % ( | |
iannucci
2014/02/24 20:20:23
%r would correctly format anything (int, str, None
ghost stip (do not use)
2014/02/25 07:12:40
Done.
| |
100 json.build_db.get('build_db_version', 0), BUILD_DB_VERSION)) | |
101 | |
102 masters = json_build_db.get('masters', {}) | |
103 # Convert build dicts into BuildDBBuilds. | |
104 build_db = gen_db() | |
105 for mastername, master in masters.iteritems(): | |
106 build_db.masters.setdefault(mastername, {}) | |
107 for buildername, builder in master.iteritems(): | |
108 build_db.masters[mastername].setdefault(buildername, {}) | |
109 for buildnumber, build in builder.iteritems(): | |
110 # Note that buildnumber is forced to be an int here, and | |
111 # we use * instead of ** -- until the serializer is recursive, | |
112 # BuildDBBuild will be written as a value list (tuple). | |
113 build_db.masters[mastername][buildername][ | |
114 int(buildnumber)] = BuildDBBuild(*build) | |
115 | |
116 return build_db | |
117 | |
118 | |
119 def get_build_db(filename): | |
120 """Open the build_db file. | |
121 | |
122 filename: the filename of the build db. | |
123 """ | |
124 build_db = gen_db() | |
125 | |
126 if os.path.isfile(filename): | |
127 print 'loading build_db from', filename | |
128 try: | |
129 with open(filename) as f: | |
130 build_db = load_from_json(f) | |
131 except BadConf as e: | |
132 new_fn = '%s.old' % filename | |
133 logging.warn('error loading %s: %s, moving to %s' % ( | |
134 filename, e, new_fn)) | |
135 chromium_utils.MoveFile(filename, new_fn) | |
136 | |
137 return build_db | |
138 | |
139 | |
140 def convert_db_to_json(build_db_data, gatekeeper_config, f): | |
141 """Converts build_db to a format suitable for JSON encoding and writes it.""" | |
142 # Remove all but the last finished build. | |
143 for builders in build_db_data.masters.values(): | |
144 for builder in builders: | |
145 unfinished = filter(lambda x: not x[1].finished, | |
146 builders[builder].iteritems()) | |
147 finished = list( | |
148 filter(lambda x: x[1].finished, builders[builder].iteritems())) | |
iannucci
2014/02/24 20:20:23
ok, seriously, you should really use list comps :p
ghost stip (do not use)
2014/02/25 07:12:40
laaaaame
| |
149 | |
150 builders[builder] = dict(unfinished) | |
151 | |
152 if finished: | |
153 max_finished = max(finished, key=lambda x: x[0]) | |
154 builders[builder][max_finished[0]] = max_finished[1] | |
155 | |
156 | |
157 build_db = gen_db(masters=build_db_data.masters) | |
158 | |
159 # Output the gatekeeper sections we're operating with, so a human reading the | |
160 # file can debug issues. This is discarded by the parser in get_build_db. | |
161 used_sections = set([]) | |
162 for masters in build_db_data.masters.values(): | |
163 for builder in masters.values(): | |
164 used_sections |= set(t for b in builder.values() for t in b.triggered) | |
165 | |
166 for master in gatekeeper_config.values(): | |
167 for section in master: | |
168 section_hash = gatekeeper_ng_config.gatekeeper_section_hash(section) | |
169 if section_hash in used_sections: | |
170 build_db.sections[section_hash] = section | |
171 | |
172 json.dump(build_db.asJson(), f, cls=gatekeeper_ng_config.SetEncoder, | |
173 sort_keys=True) | |
174 | |
175 | |
176 def save_build_db(build_db_data, gatekeeper_config, filename): | |
177 """Save the build_db file. | |
178 | |
179 build_db: dictionary to jsonize and store as build_db. | |
180 gatekeeper_config: the gatekeeper config used for this pass. | |
181 filename: the filename of the build db. | |
182 """ | |
183 print 'saving build_db to', filename | |
184 with open(filename, 'wb') as f: | |
185 convert_db_to_json(build_db_data, gatekeeper_config, f) | |
186 | |
187 | |
188 def main(): | |
189 prog_desc = 'Parses the build_db and outputs to stdout.' | |
190 usage = '%prog [options]' | |
191 parser = optparse.OptionParser(usage=(usage + '\n\n' + prog_desc)) | |
192 parser.add_option('--json', default=os.path.join(DATA_DIR, 'gatekeeper.json'), | |
193 help='location of gatekeeper configuration file') | |
194 parser.add_option('--build-db', default='build_db.json', | |
195 help='records the build status information for builders') | |
196 options, _ = parser.parse_args() | |
197 | |
198 build_db = get_build_db(options.build_db) | |
199 gatekeeper_config = gatekeeper_ng_config.load_gatekeeper_config(options.json) | |
200 | |
201 convert_db_to_json(build_db, gatekeeper_config, sys.stdout) | |
202 print | |
203 | |
204 return 0 | |
205 | |
206 | |
207 if __name__ == '__main__': | |
208 sys.exit(main()) | |
OLD | NEW |