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 | 12 from contextlib import closing |
13 | 13 |
14 import HTMLParser | 14 import HTMLParser |
15 import json | 15 import json |
| 16 import re |
16 import svn | 17 import svn |
17 import sys | 18 import sys |
18 import urllib2 | 19 import urllib2 |
19 | 20 |
20 | 21 |
21 _global_vars = None | 22 _global_vars = None |
22 | 23 |
23 | 24 |
24 GLOBAL_VARS_JSON_URL = ('https://skia.googlesource.com/buildbot/+' | 25 GLOBAL_VARS_JSON_URL = ('https://skia.googlesource.com/buildbot/+' |
25 '/master/site_config/global_variables.json') | 26 '/master/site_config/global_variables.json') |
(...skipping 28 matching lines...) Expand all Loading... |
54 this function will almost surely break. | 55 this function will almost surely break. |
55 | 56 |
56 Args: | 57 Args: |
57 url: string; the URL of the file to retrieve. | 58 url: string; the URL of the file to retrieve. |
58 Returns: | 59 Returns: |
59 The contents of the file in GoogleSource, stripped of the extra HTML from | 60 The contents of the file in GoogleSource, stripped of the extra HTML from |
60 the HTML interface. | 61 the HTML interface. |
61 """ | 62 """ |
62 with closing(urllib2.urlopen(url)) as f: | 63 with closing(urllib2.urlopen(url)) as f: |
63 contents = f.read() | 64 contents = f.read() |
64 pre_open = '<pre class="git-blob prettyprint linenums lang-json">' | 65 pre_open = '<pre class="git-blob prettyprint linenums lang-(\w+)">' |
65 pre_close = '</pre>' | 66 pre_close = '</pre>' |
66 start_index = contents.find(pre_open) | 67 matched_tag = re.search(pre_open, contents).group() |
| 68 start_index = contents.find(matched_tag) |
67 end_index = contents.find(pre_close) | 69 end_index = contents.find(pre_close) |
68 parser = HTMLParser.HTMLParser() | 70 parser = HTMLParser.HTMLParser() |
69 return parser.unescape(contents[start_index + len(pre_open):end_index]) | 71 return parser.unescape(contents[start_index + len(matched_tag):end_index]) |
70 | 72 |
71 | 73 |
72 def Get(var_name): | 74 def Get(var_name): |
73 """Return the value associated with this name in global_variables.json. | 75 """Return the value associated with this name in global_variables.json. |
74 | 76 |
75 Args: | 77 Args: |
76 var_name: string; the variable to look up. | 78 var_name: string; the variable to look up. |
77 Returns: | 79 Returns: |
78 The value of the variable. | 80 The value of the variable. |
79 Raises: | 81 Raises: |
(...skipping 11 matching lines...) Expand all Loading... |
91 except ValueError as e: | 93 except ValueError as e: |
92 raise JsonDecodeError(e.message + '\n' + global_vars_text) | 94 raise JsonDecodeError(e.message + '\n' + global_vars_text) |
93 try: | 95 try: |
94 return _global_vars[var_name]['value'] | 96 return _global_vars[var_name]['value'] |
95 except KeyError: | 97 except KeyError: |
96 raise NoSuchGlobalVariable(var_name) | 98 raise NoSuchGlobalVariable(var_name) |
97 | 99 |
98 | 100 |
99 if __name__ == '__main__': | 101 if __name__ == '__main__': |
100 print Get(sys.argv[1]) | 102 print Get(sys.argv[1]) |
OLD | NEW |