From 723b18e6478f55296166dc200cdf1bc21e3ae1a5 Mon Sep 17 00:00:00 2001 From: Darrel Herbst Date: Tue, 12 Jan 2021 12:27:12 -0500 Subject: [PATCH 1/4] Replaced urllib with requests --- openlibrary/utils/httpserver.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/openlibrary/utils/httpserver.py b/openlibrary/utils/httpserver.py index 86910054a1d..7816441876a 100644 --- a/openlibrary/utils/httpserver.py +++ b/openlibrary/utils/httpserver.py @@ -3,27 +3,23 @@ >>> server = HTTPServer(port=8090) >>> server.request('/hello/world', method='GET').should_return('hello, world!', headers={'content-type': 'text/plain'}) - >>> response = urllib.request.urlopen('http://0.0.0.0:8090/hello/world') - >>> response.read() + >>> response = requests.get('http://0.0.0.0:8090/hello/world') + >>> response.text() 'hello, world!' - >>> response.info().getheader('Content-Type') + >>> response.headers['Content-Type'] 'text/plain' >>> server.stop() - >>> urllib.request.urlopen('http://0.0.0.0:8090/hello/world') + >>> requests.get('http://0.0.0.0:8090/hello/world') Traceback (most recent call last): ... IOError: [Errno socket error] (61, 'Connection refused') """ -from __future__ import print_function -try: # Python 3 - from cheroot.wsgi import Server as CherryPyWSGIServer -except ImportError: # Python 2 - from web.wsgiserver import CherryPyWSGIServer +from cheroot.wsgi import Server as CherryPyWSGIServer +import requests # for doctest import threading import time - -from six.moves import urllib +from urllib.parse import urlencode class HTTPServer: @@ -51,7 +47,7 @@ def request(self, path, method='GET', query=None): response = Respose() if isinstance(query, dict): - query = urllib.parse.urlencode(query) + query = urlencode(query) self.mappings[path, method, query] = response return response @@ -66,6 +62,7 @@ def __call__(self, environ, start_response): return Respose()(start_response) + class Respose: def __init__(self): self.status = '404 Not Found' @@ -82,6 +79,7 @@ def __call__(self, start_response): start_response(self.status, self.headers.items()) return self.data + if __name__ == "__main__": import doctest doctest.testmod() From 75fa4e45bc56b28d0eb6fb40fdc61e6272d2b950 Mon Sep 17 00:00:00 2001 From: Darrel Herbst Date: Tue, 12 Jan 2021 12:32:35 -0500 Subject: [PATCH 2/4] Delete openlibrary.utils.httpserver because it is no longer used. --- openlibrary/utils/httpserver.py | 85 --------------------------------- scripts/run_doctests.sh | 1 - 2 files changed, 86 deletions(-) delete mode 100644 openlibrary/utils/httpserver.py diff --git a/openlibrary/utils/httpserver.py b/openlibrary/utils/httpserver.py deleted file mode 100644 index 7816441876a..00000000000 --- a/openlibrary/utils/httpserver.py +++ /dev/null @@ -1,85 +0,0 @@ -"""http server for testing. - - >>> server = HTTPServer(port=8090) - >>> server.request('/hello/world', method='GET').should_return('hello, world!', headers={'content-type': 'text/plain'}) - - >>> response = requests.get('http://0.0.0.0:8090/hello/world') - >>> response.text() - 'hello, world!' - >>> response.headers['Content-Type'] - 'text/plain' - - >>> server.stop() - >>> requests.get('http://0.0.0.0:8090/hello/world') - Traceback (most recent call last): - ... - IOError: [Errno socket error] (61, 'Connection refused') -""" -from cheroot.wsgi import Server as CherryPyWSGIServer -import requests # for doctest -import threading -import time -from urllib.parse import urlencode - - -class HTTPServer: - def __init__(self, port=8090): - self.mappings = {} - self.port = port - self.server = CherryPyWSGIServer(("0.0.0.0", port), self, server_name="localhost") - self.started = False - self.t = threading.Thread(target=self._start) - self.t.start() - time.sleep(0.1) - - def _start(self): - try: - self.server.start() - except Exception as e: - print('ERROR: failed to start server', str(e)) - - def stop(self): - self.server.stop() - self.t.join() - - def request(self, path, method='GET', query=None): - query = query or {} - response = Respose() - - if isinstance(query, dict): - query = urlencode(query) - - self.mappings[path, method, query] = response - return response - - def __call__(self, environ, start_response): - _method = environ.get('REQUEST_METHOD', 'GET') - _path = environ.get('PATH_INFO') - - for (path, method, query_string), response in self.mappings.items(): - if _path == path and _method == method: - return response(start_response) - - return Respose()(start_response) - - -class Respose: - def __init__(self): - self.status = '404 Not Found' - self.data = "not found" - self.headers = {} - - def should_return(self, data, status="200 OK", headers=None): - headers = headers or {} - self.status = status - self.data = data - self.headers = headers - - def __call__(self, start_response): - start_response(self.status, self.headers.items()) - return self.data - - -if __name__ == "__main__": - import doctest - doctest.testmod() diff --git a/scripts/run_doctests.sh b/scripts/run_doctests.sh index fedb45e47dd..3945d19de99 100755 --- a/scripts/run_doctests.sh +++ b/scripts/run_doctests.sh @@ -54,7 +54,6 @@ pytest --doctest-modules \ --ignore=openlibrary/solr/update_work.py \ --ignore=openlibrary/tests/catalog/test_get_ia.py \ --ignore=openlibrary/utils/form.py \ - --ignore=openlibrary/utils/httpserver.py \ --ignore=openlibrary/utils/schema.py \ --ignore=openlibrary/utils/solr.py \ --ignore=scripts \ From 659cb5ec388e5e292e8885ec2a5e35969c1e2620 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 15 Jan 2021 01:22:51 +0100 Subject: [PATCH 3/4] Upgrade to Python 3.9.1 --- docker/Dockerfile.olbase | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docker/Dockerfile.olbase b/docker/Dockerfile.olbase index 148703bc0e2..a10dc5ab063 100644 --- a/docker/Dockerfile.olbase +++ b/docker/Dockerfile.olbase @@ -44,15 +44,15 @@ ENV PATH $PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH RUN pyenv update && pyenv install 2.7.6 RUN pyenv update && pyenv install 3.8.6 -RUN pyenv update && pyenv install 3.9.0 -RUN pyenv global 3.8.6 3.9.0 2.7.6 +RUN pyenv update && pyenv install 3.9.1 +RUN pyenv global 3.8.6 3.9.1 2.7.6 RUN pyenv rehash # Update Python 2's pip to match production (Jan 2020) RUN python2 -m pip install --upgrade --disable-pip-version-check pip==19.3.1 # Install wheel before other requirements to reduce Docker build time. -RUN python3.8 -m pip install --upgrade pip wheel==0.35.1 -RUN python3.9 -m pip install --upgrade pip wheel==0.35.1 +RUN python3.8 -m pip install --upgrade pip wheel +RUN python3.9 -m pip install --upgrade pip wheel USER root # Add pyenv to root's bashrc as well From 3e7dbabe23a8a16ba92d5d54cea71d5e807b54ca Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 15 Jan 2021 01:23:21 +0100 Subject: [PATCH 4/4] Upgrade to Python 3.9.1 --- .python-version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.python-version b/.python-version index 2c49607562e..4ae69d94755 100644 --- a/.python-version +++ b/.python-version @@ -1,3 +1,3 @@ 3.8.6 -3.9.0 +3.9.1 2.7.6