| epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 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 |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | """ |
| 8 | Provides read access to buildbot's global_variables.json . |
| 9 | """ |
| 10 | |
| borenet@google.com | 6f0f5b4 | 2014-01-09 21:41:39 +0000 | [diff] [blame^] | 11 | |
| 12 | from contextlib import closing |
| 13 | |
| 14 | import HTMLParser |
| epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 15 | import json |
| 16 | import svn |
| borenet@google.com | 6f0f5b4 | 2014-01-09 21:41:39 +0000 | [diff] [blame^] | 17 | import sys |
| 18 | import urllib2 |
| 19 | |
| epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 20 | |
| 21 | _global_vars = None |
| 22 | |
| borenet@google.com | 4b897fa | 2013-12-02 20:27:16 +0000 | [diff] [blame] | 23 | |
| borenet@google.com | 6f0f5b4 | 2014-01-09 21:41:39 +0000 | [diff] [blame^] | 24 | GLOBAL_VARS_JSON_URL = ('https://skia.googlesource.com/buildbot/+' |
| 25 | '/master/site_config/global_variables.json') |
| borenet@google.com | 4b897fa | 2013-12-02 20:27:16 +0000 | [diff] [blame] | 26 | |
| 27 | |
| 28 | class GlobalVarsRetrievalError(Exception): |
| 29 | """Exception which is raised when the global_variables.json file cannot be |
| 30 | retrieved from the Skia buildbot repository.""" |
| epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 31 | pass |
| 32 | |
| borenet@google.com | 4b897fa | 2013-12-02 20:27:16 +0000 | [diff] [blame] | 33 | |
| 34 | class JsonDecodeError(Exception): |
| 35 | """Exception which is raised when the global_variables.json file cannot be |
| 36 | interpreted as JSON. This may be due to the file itself being incorrectly |
| 37 | formatted or due to an incomplete or corrupted downloaded version of the file. |
| 38 | """ |
| 39 | pass |
| 40 | |
| 41 | |
| 42 | class NoSuchGlobalVariable(KeyError): |
| 43 | """Exception which is raised when a given variable is not found in the |
| 44 | global_variables.json file.""" |
| 45 | pass |
| 46 | |
| 47 | |
| borenet@google.com | 6f0f5b4 | 2014-01-09 21:41:39 +0000 | [diff] [blame^] | 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 | |
| epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 72 | def Get(var_name): |
| borenet@google.com | 6f0f5b4 | 2014-01-09 21:41:39 +0000 | [diff] [blame^] | 73 | """Return the value associated with this name in global_variables.json. |
| 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 | """ |
| epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 82 | global _global_vars |
| 83 | if not _global_vars: |
| borenet@google.com | 4b897fa | 2013-12-02 20:27:16 +0000 | [diff] [blame] | 84 | try: |
| borenet@google.com | 6f0f5b4 | 2014-01-09 21:41:39 +0000 | [diff] [blame^] | 85 | global_vars_text = retrieve_from_googlesource(GLOBAL_VARS_JSON_URL) |
| borenet@google.com | 4b897fa | 2013-12-02 20:27:16 +0000 | [diff] [blame] | 86 | except Exception: |
| 87 | raise GlobalVarsRetrievalError('Failed to retrieve %s.' % |
| 88 | GLOBAL_VARS_JSON_URL) |
| 89 | try: |
| 90 | _global_vars = json.loads(global_vars_text) |
| 91 | except ValueError as e: |
| 92 | raise JsonDecodeError(e.message + '\n' + global_vars_text) |
| epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 93 | try: |
| 94 | return _global_vars[var_name]['value'] |
| 95 | except KeyError: |
| 96 | raise NoSuchGlobalVariable(var_name) |
| borenet@google.com | 6f0f5b4 | 2014-01-09 21:41:39 +0000 | [diff] [blame^] | 97 | |
| 98 | |
| 99 | if __name__ == '__main__': |
| 100 | print Get(sys.argv[1]) |