blob: 8d0e43897b7290d14f129afa964b8e34952f5bdf [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
12from contextlib import closing
13
14import HTMLParser
epoger@google.comfd040112013-08-20 16:21:55 +000015import json
16import svn
borenet@google.com6f0f5b42014-01-09 21:41:39 +000017import sys
18import urllib2
19
epoger@google.comfd040112013-08-20 16:21:55 +000020
21_global_vars = None
22
borenet@google.com4b897fa2013-12-02 20:27:16 +000023
borenet@google.com6f0f5b42014-01-09 21:41:39 +000024GLOBAL_VARS_JSON_URL = ('https://skia.googlesource.com/buildbot/+'
25 '/master/site_config/global_variables.json')
borenet@google.com4b897fa2013-12-02 20:27:16 +000026
27
28class GlobalVarsRetrievalError(Exception):
29 """Exception which is raised when the global_variables.json file cannot be
30 retrieved from the Skia buildbot repository."""
epoger@google.comfd040112013-08-20 16:21:55 +000031 pass
32
borenet@google.com4b897fa2013-12-02 20:27:16 +000033
34class JsonDecodeError(Exception):
35 """Exception which is raised when the global_variables.json file cannot be
36 interpreted as JSON. This may be due to the file itself being incorrectly
37 formatted or due to an incomplete or corrupted downloaded version of the file.
38 """
39 pass
40
41
42class NoSuchGlobalVariable(KeyError):
43 """Exception which is raised when a given variable is not found in the
44 global_variables.json file."""
45 pass
46
47
borenet@google.com6f0f5b42014-01-09 21:41:39 +000048def retrieve_from_googlesource(url):
49 """Retrieve the given file from GoogleSource's HTTP interface, trimming the
50 extraneous HTML. Intended to be a GoogleSource equivalent of "svn cat".
51
52 This just returns the unescaped contents of the first <pre> tag which matches
53 our expectations for GoogleSource's HTTP interface. If that interface changes,
54 this function will almost surely break.
55
56 Args:
57 url: string; the URL of the file to retrieve.
58 Returns:
59 The contents of the file in GoogleSource, stripped of the extra HTML from
60 the HTML interface.
61 """
62 with closing(urllib2.urlopen(url)) as f:
63 contents = f.read()
64 pre_open = '<pre class="git-blob prettyprint linenums lang-json">'
65 pre_close = '</pre>'
66 start_index = contents.find(pre_open)
67 end_index = contents.find(pre_close)
68 parser = HTMLParser.HTMLParser()
69 return parser.unescape(contents[start_index + len(pre_open):end_index])
70
71
epoger@google.comfd040112013-08-20 16:21:55 +000072def Get(var_name):
borenet@google.com6f0f5b42014-01-09 21:41:39 +000073 """Return the value associated with this name in global_variables.json.
74
75 Args:
76 var_name: string; the variable to look up.
77 Returns:
78 The value of the variable.
79 Raises:
80 NoSuchGlobalVariable if there is no variable with that name.
81 """
epoger@google.comfd040112013-08-20 16:21:55 +000082 global _global_vars
83 if not _global_vars:
borenet@google.com4b897fa2013-12-02 20:27:16 +000084 try:
borenet@google.com6f0f5b42014-01-09 21:41:39 +000085 global_vars_text = retrieve_from_googlesource(GLOBAL_VARS_JSON_URL)
borenet@google.com4b897fa2013-12-02 20:27:16 +000086 except Exception:
87 raise GlobalVarsRetrievalError('Failed to retrieve %s.' %
88 GLOBAL_VARS_JSON_URL)
89 try:
90 _global_vars = json.loads(global_vars_text)
91 except ValueError as e:
92 raise JsonDecodeError(e.message + '\n' + global_vars_text)
epoger@google.comfd040112013-08-20 16:21:55 +000093 try:
94 return _global_vars[var_name]['value']
95 except KeyError:
96 raise NoSuchGlobalVariable(var_name)
borenet@google.com6f0f5b42014-01-09 21:41:39 +000097
98
99if __name__ == '__main__':
100 print Get(sys.argv[1])