diff options
author | Robert Guo <robert.guo@10gen.com> | 2017-02-24 11:48:27 -0500 |
---|---|---|
committer | Robert Guo <robert.guo@10gen.com> | 2017-02-24 11:48:46 -0500 |
commit | 973b8b9da39db84073e98d4979ec3a8d6179b217 (patch) | |
tree | a3c32f4c142008269c3600940d61f70409246709 /buildscripts/resmokelib/logging | |
parent | a70ed6af85014f3a62351095cb19231beaee68e9 (diff) | |
download | mongo-973b8b9da39db84073e98d4979ec3a8d6179b217.tar.gz |
Revert "SERVER-27627 use requests instead of urllib2 in resmoke.py"
This reverts commit 14f16f384a2ace3b5ccb45dcbfbb66f3f57e945a.
Diffstat (limited to 'buildscripts/resmokelib/logging')
-rw-r--r-- | buildscripts/resmokelib/logging/buildlogger.py | 21 | ||||
-rw-r--r-- | buildscripts/resmokelib/logging/handlers.py | 37 |
2 files changed, 38 insertions, 20 deletions
diff --git a/buildscripts/resmokelib/logging/buildlogger.py b/buildscripts/resmokelib/logging/buildlogger.py index 3d3a750896a..e82dcf9c27b 100644 --- a/buildscripts/resmokelib/logging/buildlogger.py +++ b/buildscripts/resmokelib/logging/buildlogger.py @@ -5,6 +5,7 @@ Defines handlers for communicating with a buildlogger server. from __future__ import absolute_import import functools +import urllib2 from . import handlers from . import loggers @@ -16,6 +17,7 @@ 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 @@ -35,6 +37,20 @@ 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 @@ -78,6 +94,7 @@ 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) @@ -100,6 +117,7 @@ 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"]) @@ -136,7 +154,8 @@ class _BaseBuildloggerHandler(handlers.BufferedHandler): username = build_config["username"] password = build_config["password"] - self.http_handler = handlers.HTTPHandler(_config.BUILDLOGGER_URL, + self.http_handler = handlers.HTTPHandler(_BUILDLOGGER_REALM, + _config.BUILDLOGGER_URL, username, password) diff --git a/buildscripts/resmokelib/logging/handlers.py b/buildscripts/resmokelib/logging/handlers.py index 0a9f3db0755..3d71399bfa5 100644 --- a/buildscripts/resmokelib/logging/handlers.py +++ b/buildscripts/resmokelib/logging/handlers.py @@ -8,16 +8,13 @@ from __future__ import absolute_import import json import logging import threading - -import requests -import requests.auth +import urllib2 from .. import utils from ..utils import timer _TIMEOUT_SECS = 10 - class BufferedHandler(logging.Handler): """ A handler class that buffers logging records in memory. Whenever @@ -144,15 +141,21 @@ class HTTPHandler(object): A class which sends data to a web server using POST requests. """ - def __init__(self, url_root, username, password): + def __init__(self, realm, url_root, username, password): """ - Initializes the handler with the necessary authentication + Initializes the handler with the necessary authenticaton credentials. """ - self.auth_handler = requests.auth.HTTPBasicAuth(username, password) + auth_handler = urllib2.HTTPBasicAuthHandler() + auth_handler.add_password( + realm=realm, + uri=url_root, + user=username, + passwd=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("/")) @@ -173,18 +176,14 @@ 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 = 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 + response = self.url_opener.open(request, timeout=timeout_secs) + headers = response.info() - if headers["Content-Type"].startswith("application/json"): - return response.json() + content_type = headers.gettype() + if content_type == "application/json": + encoding = headers.getparam("charset") or "utf-8" + return json.load(response, encoding=encoding) - return response.text + return response.read() |