blob: e110ae9c6f330b6c3f440ad55c300b7583c9427b [file] [log] [blame]
epoger@google.comfd040112013-08-20 16:21:55 +00001#!/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"""
8Provides read access to buildbot's global_variables.json .
9"""
10
borenet@google.com6f0f5b42014-01-09 21:41:39 +000011
epoger@google.comfd040112013-08-20 16:21:55 +000012import json
borenet2e81e512014-06-05 07:32:15 -070013import retrieve_from_googlesource
borenet@google.com6f0f5b42014-01-09 21:41:39 +000014import sys
borenet@google.com6f0f5b42014-01-09 21:41:39 +000015
epoger@google.comfd040112013-08-20 16:21:55 +000016
17_global_vars = None
18
borenet@google.com4b897fa2013-12-02 20:27:16 +000019
borenet2e81e512014-06-05 07:32:15 -070020SKIABOT_REPO = 'https://skia.googlesource.com/buildbot'
21_GLOBAL_VARS_PATH = 'site_config/global_variables.json'
borenet@google.com4b897fa2013-12-02 20:27:16 +000022
23
24class GlobalVarsRetrievalError(Exception):
25 """Exception which is raised when the global_variables.json file cannot be
26 retrieved from the Skia buildbot repository."""
epoger@google.comfd040112013-08-20 16:21:55 +000027 pass
28
borenet@google.com4b897fa2013-12-02 20:27:16 +000029
30class JsonDecodeError(Exception):
31 """Exception which is raised when the global_variables.json file cannot be
32 interpreted as JSON. This may be due to the file itself being incorrectly
33 formatted or due to an incomplete or corrupted downloaded version of the file.
34 """
35 pass
36
37
38class NoSuchGlobalVariable(KeyError):
39 """Exception which is raised when a given variable is not found in the
40 global_variables.json file."""
41 pass
42
43
epoger@google.comfd040112013-08-20 16:21:55 +000044def Get(var_name):
borenet@google.com6f0f5b42014-01-09 21:41:39 +000045 """Return the value associated with this name in global_variables.json.
commit-bot@chromium.org90a17672014-05-16 19:19:31 +000046
borenet@google.com6f0f5b42014-01-09 21:41:39 +000047 Args:
48 var_name: string; the variable to look up.
49 Returns:
50 The value of the variable.
51 Raises:
52 NoSuchGlobalVariable if there is no variable with that name.
53 """
epoger@google.comfd040112013-08-20 16:21:55 +000054 global _global_vars
55 if not _global_vars:
borenet@google.com4b897fa2013-12-02 20:27:16 +000056 try:
borenet2e81e512014-06-05 07:32:15 -070057 global_vars_text = retrieve_from_googlesource.get(SKIABOT_REPO,
58 _GLOBAL_VARS_PATH)
commit-bot@chromium.org95ead5b2014-04-29 17:11:19 +000059 except Exception as e:
borenet2e81e512014-06-05 07:32:15 -070060 raise GlobalVarsRetrievalError('Failed to retrieve %s from %s:\n%s' %
61 (_GLOBAL_VARS_PATH, SKIABOT_REPO, str(e)))
borenet@google.com4b897fa2013-12-02 20:27:16 +000062 try:
63 _global_vars = json.loads(global_vars_text)
64 except ValueError as e:
65 raise JsonDecodeError(e.message + '\n' + global_vars_text)
epoger@google.comfd040112013-08-20 16:21:55 +000066 try:
67 return _global_vars[var_name]['value']
68 except KeyError:
69 raise NoSuchGlobalVariable(var_name)
borenet@google.com6f0f5b42014-01-09 21:41:39 +000070
71
72if __name__ == '__main__':
73 print Get(sys.argv[1])