blob: c3d370308edb7613072a0239976fe698befa7fee [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
commit-bot@chromium.org9f8e2822014-01-14 19:18:45 +000016import re
epoger@google.comfd040112013-08-20 16:21:55 +000017import svn
borenet@google.com6f0f5b42014-01-09 21:41:39 +000018import sys
19import urllib2
20
epoger@google.comfd040112013-08-20 16:21:55 +000021
22_global_vars = None
23
borenet@google.com4b897fa2013-12-02 20:27:16 +000024
borenet@google.com6f0f5b42014-01-09 21:41:39 +000025GLOBAL_VARS_JSON_URL = ('https://skia.googlesource.com/buildbot/+'
26 '/master/site_config/global_variables.json')
borenet@google.com4b897fa2013-12-02 20:27:16 +000027
28
29class GlobalVarsRetrievalError(Exception):
30 """Exception which is raised when the global_variables.json file cannot be
31 retrieved from the Skia buildbot repository."""
epoger@google.comfd040112013-08-20 16:21:55 +000032 pass
33
borenet@google.com4b897fa2013-12-02 20:27:16 +000034
35class JsonDecodeError(Exception):
36 """Exception which is raised when the global_variables.json file cannot be
37 interpreted as JSON. This may be due to the file itself being incorrectly
38 formatted or due to an incomplete or corrupted downloaded version of the file.
39 """
40 pass
41
42
43class NoSuchGlobalVariable(KeyError):
44 """Exception which is raised when a given variable is not found in the
45 global_variables.json file."""
46 pass
47
48
borenet@google.com6f0f5b42014-01-09 21:41:39 +000049def retrieve_from_googlesource(url):
50 """Retrieve the given file from GoogleSource's HTTP interface, trimming the
51 extraneous HTML. Intended to be a GoogleSource equivalent of "svn cat".
52
53 This just returns the unescaped contents of the first <pre> tag which matches
54 our expectations for GoogleSource's HTTP interface. If that interface changes,
55 this function will almost surely break.
56
57 Args:
58 url: string; the URL of the file to retrieve.
59 Returns:
60 The contents of the file in GoogleSource, stripped of the extra HTML from
61 the HTML interface.
62 """
63 with closing(urllib2.urlopen(url)) as f:
64 contents = f.read()
commit-bot@chromium.org9f8e2822014-01-14 19:18:45 +000065 pre_open = '<pre class="git-blob prettyprint linenums lang-(\w+)">'
borenet@google.com6f0f5b42014-01-09 21:41:39 +000066 pre_close = '</pre>'
commit-bot@chromium.org9f8e2822014-01-14 19:18:45 +000067 matched_tag = re.search(pre_open, contents).group()
68 start_index = contents.find(matched_tag)
borenet@google.com6f0f5b42014-01-09 21:41:39 +000069 end_index = contents.find(pre_close)
70 parser = HTMLParser.HTMLParser()
commit-bot@chromium.org9f8e2822014-01-14 19:18:45 +000071 return parser.unescape(contents[start_index + len(matched_tag):end_index])
borenet@google.com6f0f5b42014-01-09 21:41:39 +000072
73
epoger@google.comfd040112013-08-20 16:21:55 +000074def Get(var_name):
borenet@google.com6f0f5b42014-01-09 21:41:39 +000075 """Return the value associated with this name in global_variables.json.
76
77 Args:
78 var_name: string; the variable to look up.
79 Returns:
80 The value of the variable.
81 Raises:
82 NoSuchGlobalVariable if there is no variable with that name.
83 """
epoger@google.comfd040112013-08-20 16:21:55 +000084 global _global_vars
85 if not _global_vars:
borenet@google.com4b897fa2013-12-02 20:27:16 +000086 try:
borenet@google.com6f0f5b42014-01-09 21:41:39 +000087 global_vars_text = retrieve_from_googlesource(GLOBAL_VARS_JSON_URL)
borenet@google.com4b897fa2013-12-02 20:27:16 +000088 except Exception:
89 raise GlobalVarsRetrievalError('Failed to retrieve %s.' %
90 GLOBAL_VARS_JSON_URL)
91 try:
92 _global_vars = json.loads(global_vars_text)
93 except ValueError as e:
94 raise JsonDecodeError(e.message + '\n' + global_vars_text)
epoger@google.comfd040112013-08-20 16:21:55 +000095 try:
96 return _global_vars[var_name]['value']
97 except KeyError:
98 raise NoSuchGlobalVariable(var_name)
borenet@google.com6f0f5b42014-01-09 21:41:39 +000099
100
101if __name__ == '__main__':
102 print Get(sys.argv[1])