Commit 6695817c authored by Jérome Perrin's avatar Jérome Perrin

update for python3 >= 3.9

drop support for python 2
parent 943a005d
This diff is collapsed.
......@@ -3,10 +3,10 @@ import sys
import json
import bz2
import gzip
from StringIO import StringIO
import io
import tempfile
import apachedex
from . import lzma
import lzma
class ApacheDEXTestCase(unittest.TestCase):
......@@ -15,8 +15,10 @@ class ApacheDEXTestCase(unittest.TestCase):
self._original_sys_stdin = sys.stdin
self._original_sys_stderr = sys.stderr
self._original_sys_stdout = sys.stdout
sys.stderr = StringIO()
sys.stdout = StringIO()
self._stderr_bytes = io.BytesIO()
sys.stderr = io.TextIOWrapper(self._stderr_bytes, write_through=True)
self._stdout_bytes = io.BytesIO()
sys.stdout = io.TextIOWrapper(self._stdout_bytes, write_through=True)
def tearDown(self):
sys.argv = self._original_sys_argv
......@@ -25,17 +27,28 @@ class ApacheDEXTestCase(unittest.TestCase):
sys.stdout = self._original_sys_stdout
class TestFiles(ApacheDEXTestCase):
def test(self):
with tempfile.NamedTemporaryFile() as fin, tempfile.NamedTemporaryFile() as fout:
fin.write(
b'''127.0.0.1 - - [14/Jul/2017:09:41:41 +0200] "GET / HTTP/1.1" 200 7499 "https://example.org/" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36" 1754'''
)
fin.flush()
sys.argv = ['apachedex', '--base=/', fin.name, '--out', fout.name]
apachedex.main()
class TestMalformedInput(ApacheDEXTestCase):
def test_timestamp_mixed_in_timestamp(self):
sys.argv = ['apachedex', '--base=/', '-']
sys.stdin = StringIO(
sys.stdin = io.StringIO(
# this first line is valid, but second is not
'''127.0.0.1 - - [14/Jul/2017:09:41:41 +0200] "GET / HTTP/1.1" 200 7499 "https://example.org/" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36" 1754
127.0.0.1 - - [14/Jul/2017:127.0.0.1 - - [14/Jul/2017:09:41:41 +0200] "GET / HTTP/1.1" 200 7499 "https://example.org/" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36" 1754''')
apachedex.main()
self.assertNotIn('Malformed line at -:1', sys.stderr.getvalue())
self.assertIn('Malformed line at -:2', sys.stderr.getvalue())
self.assertNotIn(b'Malformed line at -:1', self._stderr_bytes.getvalue())
self.assertIn(b'Malformed line at -:2', self._stderr_bytes.getvalue())
class TestCharacterEncoding(ApacheDEXTestCase):
......@@ -48,7 +61,7 @@ class TestCharacterEncoding(ApacheDEXTestCase):
fin.flush()
sys.argv = ['apachedex', '--base=/', fin.name, '-f', 'json', '-o', fout.name]
apachedex.main()
self.assertNotIn('Malformed line', sys.stderr.getvalue())
self.assertNotIn(b'Malformed line', self._stderr_bytes.getvalue())
with open(fout.name) as f:
self.assertTrue(json.load(f))
......@@ -74,7 +87,7 @@ class EncodedInputTestMixin:
fin.flush()
sys.argv = ['apachedex', '--base=/', fin.name, '-f', 'json', '-o', fout.name]
apachedex.main()
self.assertNotIn('Malformed line', sys.stderr.getvalue())
self.assertNotIn(b'Malformed line', self._stderr_bytes.getvalue())
with open(fout.name) as f:
self.assertTrue(json.load(f))
......@@ -86,20 +99,15 @@ class TestBzip2Encoding(ApacheDEXTestCase, EncodedInputTestMixin):
class TestZlibEncoding(ApacheDEXTestCase, EncodedInputTestMixin):
def _getInputData(self):
f = StringIO()
f = io.BytesIO()
with gzip.GzipFile(mode="w", fileobj=f) as gzfile:
gzfile.write(self.DEFAULT_LINE)
return f.getvalue()
if lzma is not None:
class TestLzmaEncoding(ApacheDEXTestCase, EncodedInputTestMixin):
def _getInputData(self):
return lzma.compress(self.DEFAULT_LINE)
else:
class TestLzmaEncoding(ApacheDEXTestCase):
def test(self):
self.skipTest("lzma not available")
class TestLzmaEncoding(ApacheDEXTestCase, EncodedInputTestMixin):
def _getInputData(self):
return lzma.compress(self.DEFAULT_LINE)
class TestTimeEnconding(ApacheDEXTestCase):
......@@ -107,7 +115,7 @@ class TestTimeEnconding(ApacheDEXTestCase):
def test_seconds_timing(self):
with tempfile.NamedTemporaryFile() as fout:
sys.argv = ['apachedex', '--base=/', '-', '--logformat', '%h %l %u %t "%r" %>s %O "%{Referer}i" "%{User-Agent}i" %T', '-f', 'json', '-o', fout.name]
sys.stdin = StringIO(
sys.stdin = io.StringIO(
'''127.0.0.1 - - [14/Jul/2017:09:41:41 +0200] "GET / HTTP/1.1" 200 7499 "https://example.org/" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36" 1''')
apachedex.main()
......@@ -119,7 +127,7 @@ class TestTimeEnconding(ApacheDEXTestCase):
def test_milliseconds_timing(self):
with tempfile.NamedTemporaryFile() as fout:
sys.argv = ['apachedex', '--base=/', '-', '--logformat', '%h %l %u %t "%r" %>s %O "%{Referer}i" "%{User-Agent}i" %D', '-f', 'json', '-o', fout.name]
sys.stdin = StringIO(
sys.stdin = io.StringIO(
'''127.0.0.1 - - [14/Jul/2017:09:41:41 +0200] "GET / HTTP/1.1" 200 7499 "https://example.org/" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36" 1000000''')
apachedex.main()
......@@ -131,7 +139,7 @@ class TestTimeEnconding(ApacheDEXTestCase):
def test_microseconds_timing(self):
with tempfile.NamedTemporaryFile() as fout:
sys.argv = ['apachedex', '--base=/', '-', '--logformat', '%h %l %u %t "%r" %>s %O "%{Referer}i" "%{User-Agent}i" %{ms}T', '-f', 'json', '-o', fout.name]
sys.stdin = StringIO(
sys.stdin = io.StringIO(
'''127.0.0.1 - - [14/Jul/2017:09:41:41 +0200] "GET / HTTP/1.1" 200 7499 "https://example.org/" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36" 1000
''')
......
......@@ -64,14 +64,13 @@ setup(
long_description=".. contents::\n\n" + description,
author='Vincent Pelletier',
author_email='vincent@nexedi.com',
url='http://git.erp5.org/gitweb/apachedex.git',
url='https://lab.nexedi.com/nexedi/apachedex.git',
license='GPL 2+',
platforms=['any'],
classifiers=[
'Intended Audience :: Developers',
'License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)',
'Operating System :: OS Independent',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: Implementation :: PyPy',
'Programming Language :: Python :: Implementation :: CPython',
......@@ -90,5 +89,4 @@ setup(
},
test_suite='apachedex.tests',
zip_safe=True,
use_2to3=True,
)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment