OLD | NEW |
| (Empty) |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 import json | |
5 import logging | |
6 | |
7 from infra.services.gnumbd.support.util import cached_property | |
8 from infra.services.gnumbd.support.git import INVALID | |
9 | |
10 LOGGER = logging.getLogger(__name__) | |
11 | |
12 class ConfigRef(object): | |
13 CONVERT = { | |
14 'interval': lambda self, val: float(val), | |
15 'pending_tag_prefix': lambda self, val: str(val), | |
16 'pending_ref_prefix': lambda self, val: str(val), | |
17 'enabled_refglobs': lambda self, val: map(str, list(val)), | |
18 } | |
19 DEFAULTS = { | |
20 'interval': 5.0, | |
21 'pending_tag_prefix': 'refs/pending-tags', | |
22 'pending_ref_prefix': 'refs/pending', | |
23 'enabled_refglobs': [], | |
24 } | |
25 | |
26 def __init__(self, ref, filename='config.json'): | |
27 self.ref = ref | |
28 self.repo = ref.repo | |
29 self.filename = filename | |
30 | |
31 def __getitem__(self, key): | |
32 return self.current[key] | |
33 | |
34 @cached_property | |
35 def current(self): | |
36 cur = self.ref.commit | |
37 | |
38 while cur is not None and cur is not INVALID: | |
39 LOGGER.debug('Evaluating config at %s:%s', cur.hsh, self.filename) | |
40 try: | |
41 data = self.repo.run('cat-file', 'blob', | |
42 '%s:%s' % (cur.hsh, self.filename)) | |
43 data = json.loads(data) | |
44 if not isinstance(data, dict): | |
45 LOGGER.error('Non-dict config: %r', data) | |
46 continue | |
47 | |
48 ret = {} | |
49 for k, def_v in self.DEFAULTS.iteritems(): | |
50 ret[k] = self.CONVERT[k](self, data.get(k, def_v)) | |
51 | |
52 LOGGER.debug('Using configuration at %s: %r', cur.hsh, ret) | |
53 return ret | |
54 except Exception: | |
55 LOGGER.exception('Caught exception while processing') | |
56 finally: | |
57 cur = cur.parent | |
58 LOGGER.warn('Using default config: %r', self.DEFAULTS) | |
59 return dict(self.DEFAULTS) | |
60 | |
61 def evaluate(self): | |
62 del self.current | |
63 return self.current | |
OLD | NEW |