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 | |
borenet@google.com | 6f0f5b4 | 2014-01-09 21:41:39 +0000 | [diff] [blame] | 12 | import HTMLParser |
epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 13 | import json |
commit-bot@chromium.org | 9f8e282 | 2014-01-14 19:18:45 +0000 | [diff] [blame] | 14 | import re |
borenet | 2e81e51 | 2014-06-05 07:32:15 -0700 | [diff] [blame] | 15 | import retrieve_from_googlesource |
epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 16 | import svn |
borenet@google.com | 6f0f5b4 | 2014-01-09 21:41:39 +0000 | [diff] [blame] | 17 | import sys |
borenet@google.com | 6f0f5b4 | 2014-01-09 21:41:39 +0000 | [diff] [blame] | 18 | |
epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 19 | |
| 20 | _global_vars = None |
| 21 | |
borenet@google.com | 4b897fa | 2013-12-02 20:27:16 +0000 | [diff] [blame] | 22 | |
borenet | 2e81e51 | 2014-06-05 07:32:15 -0700 | [diff] [blame] | 23 | SKIABOT_REPO = 'https://skia.googlesource.com/buildbot' |
| 24 | _GLOBAL_VARS_PATH = 'site_config/global_variables.json' |
borenet@google.com | 4b897fa | 2013-12-02 20:27:16 +0000 | [diff] [blame] | 25 | |
| 26 | |
| 27 | class GlobalVarsRetrievalError(Exception): |
| 28 | """Exception which is raised when the global_variables.json file cannot be |
| 29 | retrieved from the Skia buildbot repository.""" |
epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 30 | pass |
| 31 | |
borenet@google.com | 4b897fa | 2013-12-02 20:27:16 +0000 | [diff] [blame] | 32 | |
| 33 | class JsonDecodeError(Exception): |
| 34 | """Exception which is raised when the global_variables.json file cannot be |
| 35 | interpreted as JSON. This may be due to the file itself being incorrectly |
| 36 | formatted or due to an incomplete or corrupted downloaded version of the file. |
| 37 | """ |
| 38 | pass |
| 39 | |
| 40 | |
| 41 | class NoSuchGlobalVariable(KeyError): |
| 42 | """Exception which is raised when a given variable is not found in the |
| 43 | global_variables.json file.""" |
| 44 | pass |
| 45 | |
| 46 | |
epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 47 | def Get(var_name): |
borenet@google.com | 6f0f5b4 | 2014-01-09 21:41:39 +0000 | [diff] [blame] | 48 | """Return the value associated with this name in global_variables.json. |
commit-bot@chromium.org | 90a1767 | 2014-05-16 19:19:31 +0000 | [diff] [blame] | 49 | |
borenet@google.com | 6f0f5b4 | 2014-01-09 21:41:39 +0000 | [diff] [blame] | 50 | Args: |
| 51 | var_name: string; the variable to look up. |
| 52 | Returns: |
| 53 | The value of the variable. |
| 54 | Raises: |
| 55 | NoSuchGlobalVariable if there is no variable with that name. |
| 56 | """ |
epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 57 | global _global_vars |
| 58 | if not _global_vars: |
borenet@google.com | 4b897fa | 2013-12-02 20:27:16 +0000 | [diff] [blame] | 59 | try: |
borenet | 2e81e51 | 2014-06-05 07:32:15 -0700 | [diff] [blame] | 60 | global_vars_text = retrieve_from_googlesource.get(SKIABOT_REPO, |
| 61 | _GLOBAL_VARS_PATH) |
commit-bot@chromium.org | 95ead5b | 2014-04-29 17:11:19 +0000 | [diff] [blame] | 62 | except Exception as e: |
borenet | 2e81e51 | 2014-06-05 07:32:15 -0700 | [diff] [blame] | 63 | raise GlobalVarsRetrievalError('Failed to retrieve %s from %s:\n%s' % |
| 64 | (_GLOBAL_VARS_PATH, SKIABOT_REPO, str(e))) |
borenet@google.com | 4b897fa | 2013-12-02 20:27:16 +0000 | [diff] [blame] | 65 | try: |
| 66 | _global_vars = json.loads(global_vars_text) |
| 67 | except ValueError as e: |
| 68 | raise JsonDecodeError(e.message + '\n' + global_vars_text) |
epoger@google.com | fd04011 | 2013-08-20 16:21:55 +0000 | [diff] [blame] | 69 | try: |
| 70 | return _global_vars[var_name]['value'] |
| 71 | except KeyError: |
| 72 | raise NoSuchGlobalVariable(var_name) |
borenet@google.com | 6f0f5b4 | 2014-01-09 21:41:39 +0000 | [diff] [blame] | 73 | |
| 74 | |
| 75 | if __name__ == '__main__': |
| 76 | print Get(sys.argv[1]) |