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