OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 | 2 |
3 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """ | 7 """ |
8 Provides read access to buildbot's global_variables.json . | 8 Provides read access to buildbot's global_variables.json . |
9 """ | 9 """ |
10 | 10 |
11 | 11 |
12 from contextlib import closing | |
13 | |
14 import HTMLParser | 12 import HTMLParser |
15 import base64 | |
16 import json | 13 import json |
17 import re | 14 import re |
| 15 import retrieve_from_googlesource |
18 import svn | 16 import svn |
19 import sys | 17 import sys |
20 import urllib2 | |
21 | 18 |
22 | 19 |
23 _global_vars = None | 20 _global_vars = None |
24 | 21 |
25 | 22 |
26 _GLOBAL_VARS_JSON_BASE64_URL = ( | 23 SKIABOT_REPO = 'https://skia.googlesource.com/buildbot' |
27 'https://skia.googlesource.com/buildbot/+/master/' | 24 _GLOBAL_VARS_PATH = 'site_config/global_variables.json' |
28 'site_config/global_variables.json?format=TEXT') | |
29 | 25 |
30 | 26 |
31 class GlobalVarsRetrievalError(Exception): | 27 class GlobalVarsRetrievalError(Exception): |
32 """Exception which is raised when the global_variables.json file cannot be | 28 """Exception which is raised when the global_variables.json file cannot be |
33 retrieved from the Skia buildbot repository.""" | 29 retrieved from the Skia buildbot repository.""" |
34 pass | 30 pass |
35 | 31 |
36 | 32 |
37 class JsonDecodeError(Exception): | 33 class JsonDecodeError(Exception): |
38 """Exception which is raised when the global_variables.json file cannot be | 34 """Exception which is raised when the global_variables.json file cannot be |
(...skipping 15 matching lines...) Expand all Loading... |
54 Args: | 50 Args: |
55 var_name: string; the variable to look up. | 51 var_name: string; the variable to look up. |
56 Returns: | 52 Returns: |
57 The value of the variable. | 53 The value of the variable. |
58 Raises: | 54 Raises: |
59 NoSuchGlobalVariable if there is no variable with that name. | 55 NoSuchGlobalVariable if there is no variable with that name. |
60 """ | 56 """ |
61 global _global_vars | 57 global _global_vars |
62 if not _global_vars: | 58 if not _global_vars: |
63 try: | 59 try: |
64 with closing(urllib2.urlopen(_GLOBAL_VARS_JSON_BASE64_URL)) as f: | 60 global_vars_text = retrieve_from_googlesource.get(SKIABOT_REPO, |
65 global_vars_text = base64.b64decode(f.read()) | 61 _GLOBAL_VARS_PATH) |
66 except Exception as e: | 62 except Exception as e: |
67 raise GlobalVarsRetrievalError('Failed to retrieve %s:\n%s' % | 63 raise GlobalVarsRetrievalError('Failed to retrieve %s from %s:\n%s' % |
68 (_GLOBAL_VARS_JSON_BASE64_URL, str(e))) | 64 (_GLOBAL_VARS_PATH, SKIABOT_REPO, str(e))) |
69 try: | 65 try: |
70 _global_vars = json.loads(global_vars_text) | 66 _global_vars = json.loads(global_vars_text) |
71 except ValueError as e: | 67 except ValueError as e: |
72 raise JsonDecodeError(e.message + '\n' + global_vars_text) | 68 raise JsonDecodeError(e.message + '\n' + global_vars_text) |
73 try: | 69 try: |
74 return _global_vars[var_name]['value'] | 70 return _global_vars[var_name]['value'] |
75 except KeyError: | 71 except KeyError: |
76 raise NoSuchGlobalVariable(var_name) | 72 raise NoSuchGlobalVariable(var_name) |
77 | 73 |
78 | 74 |
79 if __name__ == '__main__': | 75 if __name__ == '__main__': |
80 print Get(sys.argv[1]) | 76 print Get(sys.argv[1]) |
OLD | NEW |