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 |
commit-bot@chromium.org | 9f8e282 | 2014-01-14 19:18:45 +0000 | [diff] [blame] | 16 | import re |
epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 17 | import svn |
borenet@google.com | 6f0f5b4 | 2014-01-09 21:41:39 +0000 | [diff] [blame] | 18 | import sys |
| 19 | import urllib2 |
| 20 | |
epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 21 | |
| 22 | _global_vars = None |
| 23 | |
borenet@google.com | 4b897fa | 2013-12-02 20:27:16 +0000 | [diff] [blame] | 24 | |
commit-bot@chromium.org | 95ead5b | 2014-04-29 17:11:19 +0000 | [diff] [blame^] | 25 | GLOBAL_VARS_JSON_URL = ('http://skia-tree-status.appspot.com/repo-serving/' |
| 26 | 'buildbot/site_config/global_variables.json') |
borenet@google.com | 4b897fa | 2013-12-02 20:27:16 +0000 | [diff] [blame] | 27 | |
| 28 | |
| 29 | class GlobalVarsRetrievalError(Exception): |
| 30 | """Exception which is raised when the global_variables.json file cannot be |
| 31 | retrieved from the Skia buildbot repository.""" |
epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 32 | pass |
| 33 | |
borenet@google.com | 4b897fa | 2013-12-02 20:27:16 +0000 | [diff] [blame] | 34 | |
| 35 | class JsonDecodeError(Exception): |
| 36 | """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 |
| 38 | formatted or due to an incomplete or corrupted downloaded version of the file. |
| 39 | """ |
| 40 | pass |
| 41 | |
| 42 | |
| 43 | class NoSuchGlobalVariable(KeyError): |
| 44 | """Exception which is raised when a given variable is not found in the |
| 45 | global_variables.json file.""" |
| 46 | pass |
| 47 | |
| 48 | |
commit-bot@chromium.org | 95ead5b | 2014-04-29 17:11:19 +0000 | [diff] [blame^] | 49 | def retrieve_from_mirror(url): |
| 50 | """Retrieve the given file from the Skia Buildbot repo mirror. |
borenet@google.com | 6f0f5b4 | 2014-01-09 21:41:39 +0000 | [diff] [blame] | 51 | |
| 52 | Args: |
| 53 | url: string; the URL of the file to retrieve. |
| 54 | Returns: |
commit-bot@chromium.org | 95ead5b | 2014-04-29 17:11:19 +0000 | [diff] [blame^] | 55 | The contents of the file in the repository. |
borenet@google.com | 6f0f5b4 | 2014-01-09 21:41:39 +0000 | [diff] [blame] | 56 | """ |
| 57 | with closing(urllib2.urlopen(url)) as f: |
commit-bot@chromium.org | 95ead5b | 2014-04-29 17:11:19 +0000 | [diff] [blame^] | 58 | return f.read() |
borenet@google.com | 6f0f5b4 | 2014-01-09 21:41:39 +0000 | [diff] [blame] | 59 | |
| 60 | |
epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 61 | def Get(var_name): |
borenet@google.com | 6f0f5b4 | 2014-01-09 21:41:39 +0000 | [diff] [blame] | 62 | """Return the value associated with this name in global_variables.json. |
| 63 | |
| 64 | Args: |
| 65 | var_name: string; the variable to look up. |
| 66 | Returns: |
| 67 | The value of the variable. |
| 68 | Raises: |
| 69 | NoSuchGlobalVariable if there is no variable with that name. |
| 70 | """ |
epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 71 | global _global_vars |
| 72 | if not _global_vars: |
borenet@google.com | 4b897fa | 2013-12-02 20:27:16 +0000 | [diff] [blame] | 73 | try: |
commit-bot@chromium.org | 95ead5b | 2014-04-29 17:11:19 +0000 | [diff] [blame^] | 74 | global_vars_text = retrieve_from_mirror(GLOBAL_VARS_JSON_URL) |
| 75 | except Exception as e: |
| 76 | raise GlobalVarsRetrievalError('Failed to retrieve %s:\n%s' % |
| 77 | (GLOBAL_VARS_JSON_URL, str(e))) |
borenet@google.com | 4b897fa | 2013-12-02 20:27:16 +0000 | [diff] [blame] | 78 | try: |
| 79 | _global_vars = json.loads(global_vars_text) |
| 80 | except ValueError as e: |
| 81 | raise JsonDecodeError(e.message + '\n' + global_vars_text) |
epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 82 | try: |
| 83 | return _global_vars[var_name]['value'] |
| 84 | except KeyError: |
| 85 | raise NoSuchGlobalVariable(var_name) |
borenet@google.com | 6f0f5b4 | 2014-01-09 21:41:39 +0000 | [diff] [blame] | 86 | |
| 87 | |
| 88 | if __name__ == '__main__': |
| 89 | print Get(sys.argv[1]) |