buildbot_globals.py revision 4b897fa1f6f2f3c2319699545c6e3b8d3c82db17
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"""
8Provides read access to buildbot's global_variables.json .
9"""
10
11import json
12import svn
13
14_global_vars = None
15
16
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."""
24  pass
25
26
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
41def 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:
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)
55  try:
56    return _global_vars[var_name]['value']
57  except KeyError:
58    raise NoSuchGlobalVariable(var_name)
59