OLD | NEW |
---|---|
1 #!/usr/bin/python | 1 #!/usr/bin/python |
borenet
2014/01/09 19:27:28
Replace use of "svn cat" and instead use Googlesou
| |
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 | |
12 from contextlib import closing | |
13 | |
14 import HTMLParser | |
11 import json | 15 import json |
12 import svn | 16 import svn |
17 import sys | |
18 import urllib2 | |
19 | |
13 | 20 |
14 _global_vars = None | 21 _global_vars = None |
15 | 22 |
16 | 23 |
17 GLOBAL_VARS_JSON_URL = ( | 24 GLOBAL_VARS_JSON_URL = ('https://skia.googlesource.com/buildbot/+' |
18 'http://skia.googlecode.com/svn/buildbot/site_config/global_variables.json') | 25 '/master/site_config/global_variables.json') |
19 | 26 |
20 | 27 |
21 class GlobalVarsRetrievalError(Exception): | 28 class GlobalVarsRetrievalError(Exception): |
22 """Exception which is raised when the global_variables.json file cannot be | 29 """Exception which is raised when the global_variables.json file cannot be |
23 retrieved from the Skia buildbot repository.""" | 30 retrieved from the Skia buildbot repository.""" |
24 pass | 31 pass |
25 | 32 |
26 | 33 |
27 class JsonDecodeError(Exception): | 34 class JsonDecodeError(Exception): |
28 """Exception which is raised when the global_variables.json file cannot be | 35 """Exception which is raised when the global_variables.json file cannot be |
29 interpreted as JSON. This may be due to the file itself being incorrectly | 36 interpreted as JSON. This may be due to the file itself being incorrectly |
30 formatted or due to an incomplete or corrupted downloaded version of the file. | 37 formatted or due to an incomplete or corrupted downloaded version of the file. |
31 """ | 38 """ |
32 pass | 39 pass |
33 | 40 |
34 | 41 |
35 class NoSuchGlobalVariable(KeyError): | 42 class NoSuchGlobalVariable(KeyError): |
36 """Exception which is raised when a given variable is not found in the | 43 """Exception which is raised when a given variable is not found in the |
37 global_variables.json file.""" | 44 global_variables.json file.""" |
38 pass | 45 pass |
39 | 46 |
40 | 47 |
48 def retrieve_from_googlesource(url): | |
49 """Retrieve the given file from GoogleSource's HTTP interface, trimming the | |
50 extraneous HTML. Intended to be a GoogleSource equivalent of "svn cat". | |
51 | |
52 This just returns the unescaped contents of the first <pre> tag which matches | |
53 our expectations for GoogleSource's HTTP interface. If that interface changes, | |
54 this function will almost surely break. | |
55 | |
56 Args: | |
57 url: string; the URL of the file to retrieve. | |
58 Returns: | |
59 The contents of the file in GoogleSource, stripped of the extra HTML from | |
60 the HTML interface. | |
61 """ | |
62 with closing(urllib2.urlopen(url)) as f: | |
63 contents = f.read() | |
64 pre_open = '<pre class="git-blob prettyprint linenums lang-json">' | |
65 pre_close = '</pre>' | |
66 start_index = contents.find(pre_open) | |
67 end_index = contents.find(pre_close) | |
68 parser = HTMLParser.HTMLParser() | |
69 return parser.unescape(contents[start_index + len(pre_open):end_index]) | |
70 | |
71 | |
41 def Get(var_name): | 72 def Get(var_name): |
42 '''Return the value associated with this name in global_variables.json. | 73 """Return the value associated with this name in global_variables.json. |
43 Raises NoSuchGlobalVariable if there is no variable with that name.''' | 74 |
75 Args: | |
76 var_name: string; the variable to look up. | |
77 Returns: | |
78 The value of the variable. | |
79 Raises: | |
80 NoSuchGlobalVariable if there is no variable with that name. | |
81 """ | |
44 global _global_vars | 82 global _global_vars |
45 if not _global_vars: | 83 if not _global_vars: |
46 try: | 84 try: |
47 global_vars_text = svn.Cat(GLOBAL_VARS_JSON_URL) | 85 global_vars_text = retrieve_from_googlesource(GLOBAL_VARS_JSON_URL) |
48 except Exception: | 86 except Exception: |
49 raise GlobalVarsRetrievalError('Failed to retrieve %s.' % | 87 raise GlobalVarsRetrievalError('Failed to retrieve %s.' % |
50 GLOBAL_VARS_JSON_URL) | 88 GLOBAL_VARS_JSON_URL) |
51 try: | 89 try: |
52 _global_vars = json.loads(global_vars_text) | 90 _global_vars = json.loads(global_vars_text) |
53 except ValueError as e: | 91 except ValueError as e: |
54 raise JsonDecodeError(e.message + '\n' + global_vars_text) | 92 raise JsonDecodeError(e.message + '\n' + global_vars_text) |
55 try: | 93 try: |
56 return _global_vars[var_name]['value'] | 94 return _global_vars[var_name]['value'] |
57 except KeyError: | 95 except KeyError: |
58 raise NoSuchGlobalVariable(var_name) | 96 raise NoSuchGlobalVariable(var_name) |
97 | |
98 | |
99 if __name__ == '__main__': | |
100 print Get(sys.argv[1]) | |
OLD | NEW |