OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Loads gatekeeper configuration files for use with gatekeeper_ng.py. | 6 """Loads gatekeeper configuration files for use with gatekeeper_ng.py. |
7 | 7 |
8 The gatekeeper json configuration file has two main sections: 'masters' | 8 The gatekeeper json configuration file has two main sections: 'masters' |
9 and 'categories.' The following shows the breakdown of a possible config, | 9 and 'categories.' The following shows the breakdown of a possible config, |
10 but note that all nodes are optional (including the root 'masters' and | 10 but note that all nodes are optional (including the root 'masters' and |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 | 240 |
241 | 241 |
242 def inject_hashes(gatekeeper_config): | 242 def inject_hashes(gatekeeper_config): |
243 new_config = copy.deepcopy(gatekeeper_config) | 243 new_config = copy.deepcopy(gatekeeper_config) |
244 for master in new_config.values(): | 244 for master in new_config.values(): |
245 for section in master: | 245 for section in master: |
246 section['section_hash'] = gatekeeper_section_hash(section) | 246 section['section_hash'] = gatekeeper_section_hash(section) |
247 return new_config | 247 return new_config |
248 | 248 |
249 | 249 |
| 250 # Python's sets aren't JSON-encodable, so we convert them to lists here. |
| 251 class SetEncoder(json.JSONEncoder): |
| 252 # pylint: disable=E0202 |
| 253 def default(self, obj): |
| 254 if isinstance(obj, set): |
| 255 return sorted(list(obj)) |
| 256 return json.JSONEncoder.default(self, obj) |
| 257 |
| 258 |
250 def flatten_to_json(gatekeeper_config, stream): | 259 def flatten_to_json(gatekeeper_config, stream): |
251 # Python's sets aren't JSON-encodable, so we convert them to lists here. | |
252 class SetEncoder(json.JSONEncoder): | |
253 # pylint: disable=E0202 | |
254 def default(self, obj): | |
255 if isinstance(obj, set): | |
256 return sorted(list(obj)) | |
257 return json.JSONEncoder.default(self, obj) | |
258 | |
259 json.dump(gatekeeper_config, stream, cls=SetEncoder, sort_keys=True) | 260 json.dump(gatekeeper_config, stream, cls=SetEncoder, sort_keys=True) |
260 | 261 |
261 | 262 |
262 def main(): | 263 def main(): |
263 prog_desc = 'Reads gatekeeper.json and emits a flattened config.' | 264 prog_desc = 'Reads gatekeeper.json and emits a flattened config.' |
264 usage = '%prog [options]' | 265 usage = '%prog [options]' |
265 parser = optparse.OptionParser(usage=(usage + '\n\n' + prog_desc)) | 266 parser = optparse.OptionParser(usage=(usage + '\n\n' + prog_desc)) |
266 parser.add_option('--json', default=os.path.join(DATA_DIR, 'gatekeeper.json'), | 267 parser.add_option('--json', default=os.path.join(DATA_DIR, 'gatekeeper.json'), |
267 help='location of gatekeeper configuration file') | 268 help='location of gatekeeper configuration file') |
268 parser.add_option('--no-hashes', action='store_true', | 269 parser.add_option('--no-hashes', action='store_true', |
269 help='don\'t insert gatekeeper section hashes') | 270 help='don\'t insert gatekeeper section hashes') |
270 options, _ = parser.parse_args() | 271 options, _ = parser.parse_args() |
271 | 272 |
272 gatekeeper_config = load_gatekeeper_config(options.json) | 273 gatekeeper_config = load_gatekeeper_config(options.json) |
273 | 274 |
274 if not options.no_hashes: | 275 if not options.no_hashes: |
275 gatekeeper_config = inject_hashes(gatekeeper_config) | 276 gatekeeper_config = inject_hashes(gatekeeper_config) |
276 | 277 |
277 flatten_to_json(gatekeeper_config, sys.stdout) | 278 flatten_to_json(gatekeeper_config, sys.stdout) |
278 print | 279 print |
279 | 280 |
280 return 0 | 281 return 0 |
281 | 282 |
282 | 283 |
283 if __name__ == '__main__': | 284 if __name__ == '__main__': |
284 sys.exit(main()) | 285 sys.exit(main()) |
OLD | NEW |