2013-08-20 16:21:55 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# Copyright (c) 2013 The Chromium Authors. All rights reserved.
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Provides read access to buildbot's global_variables.json .
|
|
|
|
"""
|
|
|
|
|
2014-01-09 21:41:39 +00:00
|
|
|
|
|
|
|
from contextlib import closing
|
|
|
|
|
|
|
|
import HTMLParser
|
2014-05-16 19:19:31 +00:00
|
|
|
import base64
|
2013-08-20 16:21:55 +00:00
|
|
|
import json
|
2014-01-14 19:18:45 +00:00
|
|
|
import re
|
2013-08-20 16:21:55 +00:00
|
|
|
import svn
|
2014-01-09 21:41:39 +00:00
|
|
|
import sys
|
|
|
|
import urllib2
|
|
|
|
|
2013-08-20 16:21:55 +00:00
|
|
|
|
|
|
|
_global_vars = None
|
|
|
|
|
2013-12-02 20:27:16 +00:00
|
|
|
|
2014-05-16 19:19:31 +00:00
|
|
|
_GLOBAL_VARS_JSON_BASE64_URL = (
|
|
|
|
'https://skia.googlesource.com/buildbot/+/master/'
|
|
|
|
'site_config/global_variables.json?format=TEXT')
|
2013-12-02 20:27:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GlobalVarsRetrievalError(Exception):
|
|
|
|
"""Exception which is raised when the global_variables.json file cannot be
|
|
|
|
retrieved from the Skia buildbot repository."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class JsonDecodeError(Exception):
|
|
|
|
"""Exception which is raised when the global_variables.json file cannot be
|
|
|
|
interpreted as JSON. This may be due to the file itself being incorrectly
|
|
|
|
formatted or due to an incomplete or corrupted downloaded version of the file.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2013-08-20 16:21:55 +00:00
|
|
|
class NoSuchGlobalVariable(KeyError):
|
2013-12-02 20:27:16 +00:00
|
|
|
"""Exception which is raised when a given variable is not found in the
|
|
|
|
global_variables.json file."""
|
2013-08-20 16:21:55 +00:00
|
|
|
pass
|
|
|
|
|
2013-12-02 20:27:16 +00:00
|
|
|
|
2013-08-20 16:21:55 +00:00
|
|
|
def Get(var_name):
|
2014-01-09 21:41:39 +00:00
|
|
|
"""Return the value associated with this name in global_variables.json.
|
2014-05-16 19:19:31 +00:00
|
|
|
|
2014-01-09 21:41:39 +00:00
|
|
|
Args:
|
|
|
|
var_name: string; the variable to look up.
|
|
|
|
Returns:
|
|
|
|
The value of the variable.
|
|
|
|
Raises:
|
|
|
|
NoSuchGlobalVariable if there is no variable with that name.
|
|
|
|
"""
|
2013-08-20 16:21:55 +00:00
|
|
|
global _global_vars
|
|
|
|
if not _global_vars:
|
2013-12-02 20:27:16 +00:00
|
|
|
try:
|
2014-05-16 19:19:31 +00:00
|
|
|
with closing(urllib2.urlopen(_GLOBAL_VARS_JSON_BASE64_URL)) as f:
|
|
|
|
global_vars_text = base64.b64decode(f.read())
|
2014-04-29 17:11:19 +00:00
|
|
|
except Exception as e:
|
|
|
|
raise GlobalVarsRetrievalError('Failed to retrieve %s:\n%s' %
|
2014-05-16 19:19:31 +00:00
|
|
|
(_GLOBAL_VARS_JSON_BASE64_URL, str(e)))
|
2013-12-02 20:27:16 +00:00
|
|
|
try:
|
|
|
|
_global_vars = json.loads(global_vars_text)
|
|
|
|
except ValueError as e:
|
|
|
|
raise JsonDecodeError(e.message + '\n' + global_vars_text)
|
2013-08-20 16:21:55 +00:00
|
|
|
try:
|
|
|
|
return _global_vars[var_name]['value']
|
|
|
|
except KeyError:
|
|
|
|
raise NoSuchGlobalVariable(var_name)
|
2014-01-09 21:41:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
print Get(sys.argv[1])
|