diff options
author | Robert Guo <robert.guo@10gen.com> | 2017-02-22 10:13:39 -0500 |
---|---|---|
committer | Robert Guo <robert.guo@10gen.com> | 2017-02-24 10:50:48 -0500 |
commit | 14f16f384a2ace3b5ccb45dcbfbb66f3f57e945a (patch) | |
tree | faac2e62ae5798ac27629f162b52ef4182995bb9 | |
parent | 73d37b8fc33a3c429dfd21ad305b2c355c2a9828 (diff) | |
download | mongo-14f16f384a2ace3b5ccb45dcbfbb66f3f57e945a.tar.gz |
SERVER-27627 use requests instead of urllib2 in resmoke.py
-rw-r--r-- | buildscripts/resmokelib/logging/buildlogger.py | 21 | ||||
-rw-r--r-- | buildscripts/resmokelib/logging/handlers.py | 37 | ||||
-rw-r--r-- | buildscripts/resmokelib/requirements.txt | 1 |
3 files changed, 21 insertions, 38 deletions
diff --git a/buildscripts/resmokelib/logging/buildlogger.py b/buildscripts/resmokelib/logging/buildlogger.py index e82dcf9c27b..3d3a750896a 100644 --- a/buildscripts/resmokelib/logging/buildlogger.py +++ b/buildscripts/resmokelib/logging/buildlogger.py @@ -5,7 +5,6 @@ Defines handlers for communicating with a buildlogger server. from __future__ import absolute_import import functools -import urllib2 from . import handlers from . import loggers @@ -17,7 +16,6 @@ APPEND_GLOBAL_LOGS_ENDPOINT = "/build/%(build_id)s" CREATE_TEST_ENDPOINT = "/build/%(build_id)s/test" APPEND_TEST_LOGS_ENDPOINT = "/build/%(build_id)s/test/%(test_id)s" -_BUILDLOGGER_REALM = "buildlogs" _BUILDLOGGER_CONFIG = "mci.buildlogger" _SEND_AFTER_LINES = 2000 @@ -37,20 +35,6 @@ def _log_on_error(func): def wrapper(*args, **kwargs): try: return func(*args, **kwargs) - except urllib2.HTTPError as err: - sb = [] # String builder. - sb.append("HTTP Error %s: %s" % (err.code, err.msg)) - sb.append("POST %s" % (err.filename)) - - for name in err.hdrs: - value = err.hdrs[name] - sb.append(" %s: %s" % (name, value)) - - # Try to read the response back from the server. - if hasattr(err, "read"): - sb.append(err.read()) - - loggers._BUILDLOGGER_FALLBACK.exception("\n".join(sb)) except: loggers._BUILDLOGGER_FALLBACK.exception("Encountered an error.") return None @@ -94,7 +78,6 @@ def new_build_id(config): build_num = int(config["build_num"]) handler = handlers.HTTPHandler( - realm=_BUILDLOGGER_REALM, url_root=_config.BUILDLOGGER_URL, username=username, password=password) @@ -117,7 +100,6 @@ def new_test_id(build_id, build_config, test_filename, test_command): return None handler = handlers.HTTPHandler( - realm=_BUILDLOGGER_REALM, url_root=_config.BUILDLOGGER_URL, username=build_config["username"], password=build_config["password"]) @@ -154,8 +136,7 @@ class _BaseBuildloggerHandler(handlers.BufferedHandler): username = build_config["username"] password = build_config["password"] - self.http_handler = handlers.HTTPHandler(_BUILDLOGGER_REALM, - _config.BUILDLOGGER_URL, + self.http_handler = handlers.HTTPHandler(_config.BUILDLOGGER_URL, username, password) diff --git a/buildscripts/resmokelib/logging/handlers.py b/buildscripts/resmokelib/logging/handlers.py index 3d71399bfa5..0a9f3db0755 100644 --- a/buildscripts/resmokelib/logging/handlers.py +++ b/buildscripts/resmokelib/logging/handlers.py @@ -8,13 +8,16 @@ from __future__ import absolute_import import json import logging import threading -import urllib2 + +import requests +import requests.auth from .. import utils from ..utils import timer _TIMEOUT_SECS = 10 + class BufferedHandler(logging.Handler): """ A handler class that buffers logging records in memory. Whenever @@ -141,21 +144,15 @@ class HTTPHandler(object): A class which sends data to a web server using POST requests. """ - def __init__(self, realm, url_root, username, password): + def __init__(self, url_root, username, password): """ - Initializes the handler with the necessary authenticaton + Initializes the handler with the necessary authentication credentials. """ - auth_handler = urllib2.HTTPBasicAuthHandler() - auth_handler.add_password( - realm=realm, - uri=url_root, - user=username, - passwd=password) + self.auth_handler = requests.auth.HTTPBasicAuth(username, password) self.url_root = url_root - self.url_opener = urllib2.build_opener(auth_handler, urllib2.HTTPErrorProcessor()) def _make_url(self, endpoint): return "%s/%s/" % (self.url_root.rstrip("/"), endpoint.strip("/")) @@ -176,14 +173,18 @@ class HTTPHandler(object): headers["Content-Type"] = "application/json; charset=utf-8" url = self._make_url(endpoint) - request = urllib2.Request(url=url, data=data, headers=headers) - response = self.url_opener.open(request, timeout=timeout_secs) - headers = response.info() + response = requests.post(url, data=data, headers=headers, timeout=timeout_secs, + auth=self.auth_handler) + + response.raise_for_status() + + if not response.encoding: + response.encoding = "utf-8" + + headers = response.headers - content_type = headers.gettype() - if content_type == "application/json": - encoding = headers.getparam("charset") or "utf-8" - return json.load(response, encoding=encoding) + if headers["Content-Type"].startswith("application/json"): + return response.json() - return response.read() + return response.text diff --git a/buildscripts/resmokelib/requirements.txt b/buildscripts/resmokelib/requirements.txt index 1b152893c99..d0770bc162c 100644 --- a/buildscripts/resmokelib/requirements.txt +++ b/buildscripts/resmokelib/requirements.txt @@ -1,4 +1,5 @@ pymongo >= 3.0 pypiwin32 == 219 ; sys_platform == "win32" PyYAML == 3.11 +requests >= 2.0.0 subprocess32 >= 3.2.7 ; os_name == "posix" and python_version < "3" |