From: Simon McVittie Date: Mon, 20 Aug 2018 14:00:21 +0000 (+0100) Subject: Make test scripts bilingual Python 2/Python 3 X-Git-Tag: archive/raspbian/1.0.2-1+rpi1~1^2^2~3 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=2002e1c6a36b84292adfafd946df8387c1e2e28d;p=flatpak.git Make test scripts bilingual Python 2/Python 3 Signed-off-by: Simon McVittie Forwarded: https://github.com/flatpak/flatpak/pull/1990 Gbp-Pq: Name Make-test-scripts-bilingual-Python-2-Python-3.patch --- diff --git a/tests/http-utils-test-server.py b/tests/http-utils-test-server.py index 9451e6c..ec3826d 100644 --- a/tests/http-utils-test-server.py +++ b/tests/http-utils-test-server.py @@ -1,14 +1,21 @@ -#!/usr/bin/python2 +#!/usr/bin/python from wsgiref.handlers import format_date_time from email.utils import parsedate from calendar import timegm import gzip -from urlparse import parse_qs -import BaseHTTPServer +import sys import time import zlib -from StringIO import StringIO + +if sys.version_info[0] >= 3: + from urllib.parse import parse_qs + import http.server as http_server + from io import BytesIO +else: + from urlparse import parse_qs + import BaseHTTPServer as http_server + from StringIO import StringIO as BytesIO server_start_time = int(time.time()) @@ -19,7 +26,7 @@ def parse_http_date(date): else: return None -class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): +class RequestHandler(http_server.BaseHTTPRequestHandler): def do_GET(self): parts = self.path.split('?', 1) path = parts[0] @@ -69,19 +76,28 @@ class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): if accept_encoding and accept_encoding == 'gzip': self.send_header("Content-Encoding", "gzip") - buf = StringIO() - gzfile = gzip.GzipFile(mode='w', fileobj=buf) - gzfile.write(contents) + buf = BytesIO() + gzfile = gzip.GzipFile(mode='wb', fileobj=buf) + if isinstance(contents, bytes): + gzfile.write(contents) + else: + gzfile.write(contents.encode('utf-8')) gzfile.close() contents = buf.getvalue() self.end_headers() if response == 200: - self.wfile.write(contents) + if isinstance(contents, bytes): + self.wfile.write(contents) + else: + self.wfile.write(contents.encode('utf-8')) def test(): - BaseHTTPServer.test(RequestHandler) + if sys.version_info[0] >= 3: + http_server.test(RequestHandler, port=0) + else: + http_server.test(RequestHandler) if __name__ == '__main__': test() diff --git a/tests/oci-registry-client.py b/tests/oci-registry-client.py index 033b54d..2312a99 100644 --- a/tests/oci-registry-client.py +++ b/tests/oci-registry-client.py @@ -1,9 +1,16 @@ -#!/usr/bin/python2 +#!/usr/bin/python + +from __future__ import print_function -import httplib -import urllib import sys +if sys.version_info[0] >= 3: + import http.client as http_client + import urllib.parse as urllib_parse +else: + import httplib as http_client + import urllib as urllib_parse + if sys.argv[2] == 'add': detach_icons = '--detach-icons' in sys.argv if detach_icons: @@ -11,28 +18,28 @@ if sys.argv[2] == 'add': params = {'d': sys.argv[5]} if detach_icons: params['detach-icons'] = 1 - query = urllib.urlencode(params) - conn = httplib.HTTPConnection(sys.argv[1]) + query = urllib_parse.urlencode(params) + conn = http_client.HTTPConnection(sys.argv[1]) path = "/testing/{repo}/{tag}?{query}".format(repo=sys.argv[3], tag=sys.argv[4], query=query) conn.request("POST", path) response = conn.getresponse() if response.status != 200: - print >>sys.stderr, response.read() - print >>sys.stderr, "Failed: status={}".format(response.status) + print(response.read(), file=sys.stderr) + print("Failed: status={}".format(response.status), file=sys.stderr) sys.exit(1) elif sys.argv[2] == 'delete': - conn = httplib.HTTPConnection(sys.argv[1]) + conn = http_client.HTTPConnection(sys.argv[1]) path = "/testing/{repo}/{ref}".format(repo=sys.argv[3], ref=sys.argv[4]) conn.request("DELETE", path) response = conn.getresponse() if response.status != 200: - print >>sys.stderr, response.read() - print >>sys.stderr, "Failed: status={}".format(response.status) + print(response.read(), file=sys.stderr) + print("Failed: status={}".format(response.status), file=sys.stderr) sys.exit(1) else: - print >>sys.stderr, "Usage: oci-registry-client.py [add|remove] ARGS" + print("Usage: oci-registry-client.py [add|remove] ARGS", file=sys.stderr) sys.exit(1) diff --git a/tests/oci-registry-server.py b/tests/oci-registry-server.py index f31eecd..aa3045e 100644 --- a/tests/oci-registry-server.py +++ b/tests/oci-registry-server.py @@ -1,14 +1,21 @@ -#!/usr/bin/python2 +#!/usr/bin/python + +from __future__ import print_function -import BaseHTTPServer import base64 import hashlib import json import os import sys -from urlparse import parse_qs import time +if sys.version_info[0] >= 3: + from urllib.parse import parse_qs + import http.server as http_server +else: + from urlparse import parse_qs + import BaseHTTPServer as http_server + repositories = {} icons = {} @@ -56,7 +63,7 @@ def parse_http_date(date): else: return None -class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): +class RequestHandler(http_server.BaseHTTPRequestHandler): def check_route(self, route): parts = self.path.split('?', 1) path = parts[0].split('/') @@ -64,7 +71,7 @@ class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): result = [] route_path = route.split('/') - print(route_path, path) + print((route_path, path)) if len(route_path) != len(path): return False @@ -126,9 +133,13 @@ class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): if response == 200: if response_file: - with open(response_file) as f: + with open(response_file, 'rb') as f: response_string = f.read() - self.wfile.write(response_string) + + if isinstance(response_string, bytes): + self.wfile.write(response_string) + else: + self.wfile.write(response_string.encode('utf-8')) def do_POST(self): if self.check_route('/testing/@repo_name/@tag'): @@ -227,7 +238,10 @@ class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): return def test(): - BaseHTTPServer.test(RequestHandler) + if sys.version_info[0] >= 3: + http_server.test(RequestHandler, port=0) + else: + http_server.test(RequestHandler) if __name__ == '__main__': test() diff --git a/tests/test-http-utils.sh b/tests/test-http-utils.sh index 1f46cee..45c2c9d 100755 --- a/tests/test-http-utils.sh +++ b/tests/test-http-utils.sh @@ -21,7 +21,7 @@ set -euo pipefail . $(dirname $0)/libtest.sh -$(dirname $0)/test-webserver.sh "" "python2 $test_srcdir/http-utils-test-server.py 0" +$(dirname $0)/test-webserver.sh "" "python $test_srcdir/http-utils-test-server.py 0" FLATPAK_HTTP_PID=$(cat httpd-pid) mv httpd-port httpd-port-main port=$(cat httpd-port-main) diff --git a/tests/test-oci-registry.sh b/tests/test-oci-registry.sh index cda9396..d32f7e6 100755 --- a/tests/test-oci-registry.sh +++ b/tests/test-oci-registry.sh @@ -27,11 +27,11 @@ echo "1..13" # Start the fake registry server -$(dirname $0)/test-webserver.sh "" "python2 $test_srcdir/oci-registry-server.py 0" +$(dirname $0)/test-webserver.sh "" "python $test_srcdir/oci-registry-server.py 0" FLATPAK_HTTP_PID=$(cat httpd-pid) mv httpd-port httpd-port-main port=$(cat httpd-port-main) -client="python2 $test_srcdir/oci-registry-client.py 127.0.0.1:$port" +client="python $test_srcdir/oci-registry-client.py 127.0.0.1:$port" setup_repo_no_add oci