| 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 | |
| 11 | import json |
| 12 | import svn |
| 13 | |
| 14 | _global_vars = None |
| 15 | |
| borenet@google.com | 4b897fa | 2013-12-02 20:27:16 +0000 | [diff] [blame^] | 16 | |
| 17 | GLOBAL_VARS_JSON_URL = ( |
| 18 | 'http://skia.googlecode.com/svn/buildbot/site_config/global_variables.json') |
| 19 | |
| 20 | |
| 21 | class GlobalVarsRetrievalError(Exception): |
| 22 | """Exception which is raised when the global_variables.json file cannot be |
| 23 | retrieved from the Skia buildbot repository.""" |
| epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 24 | pass |
| 25 | |
| borenet@google.com | 4b897fa | 2013-12-02 20:27:16 +0000 | [diff] [blame^] | 26 | |
| 27 | class JsonDecodeError(Exception): |
| 28 | """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 |
| 30 | formatted or due to an incomplete or corrupted downloaded version of the file. |
| 31 | """ |
| 32 | pass |
| 33 | |
| 34 | |
| 35 | class NoSuchGlobalVariable(KeyError): |
| 36 | """Exception which is raised when a given variable is not found in the |
| 37 | global_variables.json file.""" |
| 38 | pass |
| 39 | |
| 40 | |
| epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 41 | def Get(var_name): |
| 42 | '''Return the value associated with this name in global_variables.json. |
| 43 | Raises NoSuchGlobalVariable if there is no variable with that name.''' |
| 44 | global _global_vars |
| 45 | if not _global_vars: |
| borenet@google.com | 4b897fa | 2013-12-02 20:27:16 +0000 | [diff] [blame^] | 46 | try: |
| 47 | global_vars_text = svn.Cat(GLOBAL_VARS_JSON_URL) |
| 48 | except Exception: |
| 49 | raise GlobalVarsRetrievalError('Failed to retrieve %s.' % |
| 50 | GLOBAL_VARS_JSON_URL) |
| 51 | try: |
| 52 | _global_vars = json.loads(global_vars_text) |
| 53 | except ValueError as e: |
| 54 | raise JsonDecodeError(e.message + '\n' + global_vars_text) |
| epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 55 | try: |
| 56 | return _global_vars[var_name]['value'] |
| 57 | except KeyError: |
| 58 | raise NoSuchGlobalVariable(var_name) |