blob: c3d222739665ae86a43269b66cf419caadf7f543 [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
11import json
12import svn
13
14_global_vars = None
15
borenet@google.com4b897fa2013-12-02 20:27:16 +000016
17GLOBAL_VARS_JSON_URL = (
18 'http://skia.googlecode.com/svn/buildbot/site_config/global_variables.json')
19
20
21class GlobalVarsRetrievalError(Exception):
22 """Exception which is raised when the global_variables.json file cannot be
23 retrieved from the Skia buildbot repository."""
epoger@google.comfd040112013-08-20 16:21:55 +000024 pass
25
borenet@google.com4b897fa2013-12-02 20:27:16 +000026
27class 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
35class 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.comfd040112013-08-20 16:21:55 +000041def 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.com4b897fa2013-12-02 20:27:16 +000046 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.comfd040112013-08-20 16:21:55 +000055 try:
56 return _global_vars[var_name]['value']
57 except KeyError:
58 raise NoSuchGlobalVariable(var_name)