OLD | NEW |
---|---|
1 #!/usr/bin/python | 1 #!/usr/bin/python |
epoger
2014/05/16 18:37:42
tested:
$ tools/buildbot_globals.py android_maste
| |
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 | 12 from contextlib import closing |
13 | 13 |
14 import HTMLParser | 14 import HTMLParser |
15 import base64 | |
15 import json | 16 import json |
16 import re | 17 import re |
17 import svn | 18 import svn |
18 import sys | 19 import sys |
19 import urllib2 | 20 import urllib2 |
20 | 21 |
21 | 22 |
22 _global_vars = None | 23 _global_vars = None |
23 | 24 |
24 | 25 |
25 GLOBAL_VARS_JSON_URL = ('http://skia-tree-status.appspot.com/repo-serving/' | 26 _GLOBAL_VARS_JSON_BASE64_URL = ( |
26 'buildbot/site_config/global_variables.json') | 27 'https://skia.googlesource.com/buildbot/+/master/' |
28 'site_config/global_variables.json?format=TEXT') | |
27 | 29 |
28 | 30 |
29 class GlobalVarsRetrievalError(Exception): | 31 class GlobalVarsRetrievalError(Exception): |
30 """Exception which is raised when the global_variables.json file cannot be | 32 """Exception which is raised when the global_variables.json file cannot be |
31 retrieved from the Skia buildbot repository.""" | 33 retrieved from the Skia buildbot repository.""" |
32 pass | 34 pass |
33 | 35 |
34 | 36 |
35 class JsonDecodeError(Exception): | 37 class JsonDecodeError(Exception): |
36 """Exception which is raised when the global_variables.json file cannot be | 38 """Exception which is raised when the global_variables.json file cannot be |
37 interpreted as JSON. This may be due to the file itself being incorrectly | 39 interpreted as JSON. This may be due to the file itself being incorrectly |
38 formatted or due to an incomplete or corrupted downloaded version of the file. | 40 formatted or due to an incomplete or corrupted downloaded version of the file. |
39 """ | 41 """ |
40 pass | 42 pass |
41 | 43 |
42 | 44 |
43 class NoSuchGlobalVariable(KeyError): | 45 class NoSuchGlobalVariable(KeyError): |
44 """Exception which is raised when a given variable is not found in the | 46 """Exception which is raised when a given variable is not found in the |
45 global_variables.json file.""" | 47 global_variables.json file.""" |
46 pass | 48 pass |
47 | 49 |
48 | 50 |
49 def retrieve_from_mirror(url): | |
50 """Retrieve the given file from the Skia Buildbot repo mirror. | |
51 | |
52 Args: | |
53 url: string; the URL of the file to retrieve. | |
54 Returns: | |
55 The contents of the file in the repository. | |
56 """ | |
57 with closing(urllib2.urlopen(url)) as f: | |
58 return f.read() | |
59 | |
60 | |
61 def Get(var_name): | 51 def Get(var_name): |
62 """Return the value associated with this name in global_variables.json. | 52 """Return the value associated with this name in global_variables.json. |
63 | 53 |
64 Args: | 54 Args: |
65 var_name: string; the variable to look up. | 55 var_name: string; the variable to look up. |
66 Returns: | 56 Returns: |
67 The value of the variable. | 57 The value of the variable. |
68 Raises: | 58 Raises: |
69 NoSuchGlobalVariable if there is no variable with that name. | 59 NoSuchGlobalVariable if there is no variable with that name. |
70 """ | 60 """ |
71 global _global_vars | 61 global _global_vars |
72 if not _global_vars: | 62 if not _global_vars: |
73 try: | 63 try: |
74 global_vars_text = retrieve_from_mirror(GLOBAL_VARS_JSON_URL) | 64 with closing(urllib2.urlopen(_GLOBAL_VARS_JSON_BASE64_URL)) as f: |
65 global_vars_text = base64.b64decode(f.read()) | |
75 except Exception as e: | 66 except Exception as e: |
76 raise GlobalVarsRetrievalError('Failed to retrieve %s:\n%s' % | 67 raise GlobalVarsRetrievalError('Failed to retrieve %s:\n%s' % |
77 (GLOBAL_VARS_JSON_URL, str(e))) | 68 (_GLOBAL_VARS_JSON_BASE64_URL, str(e))) |
78 try: | 69 try: |
79 _global_vars = json.loads(global_vars_text) | 70 _global_vars = json.loads(global_vars_text) |
80 except ValueError as e: | 71 except ValueError as e: |
81 raise JsonDecodeError(e.message + '\n' + global_vars_text) | 72 raise JsonDecodeError(e.message + '\n' + global_vars_text) |
82 try: | 73 try: |
83 return _global_vars[var_name]['value'] | 74 return _global_vars[var_name]['value'] |
84 except KeyError: | 75 except KeyError: |
85 raise NoSuchGlobalVariable(var_name) | 76 raise NoSuchGlobalVariable(var_name) |
86 | 77 |
87 | 78 |
88 if __name__ == '__main__': | 79 if __name__ == '__main__': |
89 print Get(sys.argv[1]) | 80 print Get(sys.argv[1]) |
OLD | NEW |