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