From d8666ad3c8525be6af37d8264cd56a0cf3b8925f Mon Sep 17 00:00:00 2001 From: Oliver Smith Date: Mon, 9 Dec 2019 14:41:14 +0100 Subject: [PATCH] Drop python2 support / make it work with python3 Re-apply reverted commit Iabda95073faa2191fd117e9637e0858c589e9d9e ("Drop python2 support"), but with additional changes to make the scripts actually work with python3 and to make it build without python2. I have verified, that the contrib/jenkins.sh scripts of all Osmocom repositories (with their python3 patches on top) are working with this patch and that all Osmocom repositories with the python3 patches build in OBS (tested in own namespace). New fixes: * osmopy/obscvty.py: verify: fix compare Comparing maps in python3 does not work the same as in python2. Convert them to lists first, so the compare works as intended again. Fix error: File "/home/user/code/osmo-dev/src/osmo-python-tests/scripts/osmotestvty.py", line 57, in test_history assert(self.vty.w_verify(test_str, [t1])) AssertionError * osmopy/obscvty.py: use enc/dec with send/recv Fix error: self.socket.send("%s\r" % request) TypeError: a bytes-like object is required, not 'str' * scripts/osmotestconfig.py: use encode() before writing to file Fix error: File "/home/user/code/osmo-dev/src/osmo-python-tests/scripts/osmotestconfig.py", line 91, in copy_config tmpfile.write(open(config).read()) File "/usr/lib/python3.5/tempfile.py", line 622, in func_wrapper return func(*args, **kwargs) TypeError: a bytes-like object is required, not 'str' * debian/control: add --buildsystem=pybuild. Otherwise "--with python3" is ignored and the build fails if python2 is not installed, with: Can't exec "pyversions": No such file or directory at /usr/[...]/python_distutils.pm line 120. Related: OS#2819 Change-Id: I3ffc3519bf6c22536a49dad7a966188ddad351a7 --- contrib/jenkins.sh | 10 ---------- debian/control | 16 +--------------- debian/rules | 6 +----- osmopy/__init__.py | 2 +- osmopy/obscvty.py | 11 +++++------ osmopy/osmo_interact/__init__.py | 2 +- osmopy/osmo_interact/common.py | 5 ----- osmopy/osmoutil.py | 3 +-- scripts/osmodumpdoc.py | 3 +-- scripts/osmotestconfig.py | 5 ++--- scripts/osmotestvty.py | 3 +-- setup.py | 32 ++++++++++++++------------------ tests/test_py2.py | 7 ------- 13 files changed, 28 insertions(+), 77 deletions(-) delete mode 100644 tests/test_py2.py diff --git a/contrib/jenkins.sh b/contrib/jenkins.sh index 08908a4..9b219f9 100755 --- a/contrib/jenkins.sh +++ b/contrib/jenkins.sh @@ -4,16 +4,6 @@ set -ex COM_FLAGS='-m compileall' -# FIXME: remove once python 2 support is deprecated -PY2=python2 -PY2_LIST="osmopy scripts/osmodumpdoc.py scripts/osmotestvty.py scripts/osmotestconfig.py" -$PY2 ./setup.py install -$PY2 tests/test_py2.py -for f in $PY2_LIST -do - $PY2 $COM_FLAGS $f -done - rm -rf ./build PY3=python3 PY3_LIST="osmopy scripts/osmo_ctrl.py scripts/osmo_rate_ctr2csv.py scripts/osmodumpdoc.py scripts/osmotestvty.py scripts/osmotestconfig.py scripts/osmo_interact_ctrl.py scripts/osmo_interact_vty.py scripts/osmo_verify_transcript_ctrl.py scripts/osmo_verify_transcript_vty.py scripts/soap.py scripts/twisted_ipa.py" diff --git a/debian/control b/debian/control index 52b614c..1ad871d 100644 --- a/debian/control +++ b/debian/control @@ -2,26 +2,12 @@ Source: osmo-python-tests Section: python Priority: optional Maintainer: Harald Welte -Build-Depends: debhelper (>= 9), python, dh-python, python-setuptools, python3, python3-setuptools +Build-Depends: debhelper (>= 9), dh-python, python3, python3-setuptools Standards-Version: 3.9.8 Homepage: http://git.osmocom.org/python/osmo-python-tests/ Vcs-Git: git://git.osmocom.org/python/osmo-python-tests Vcs-Browser: http://git.osmocom.org/python/osmo-python-tests/ -Package: python2-osmopy-libs -Architecture: all -Depends: ${python:Depends}, ${misc:Depends} -Description: Python code (not only) for testing of Osmocom programs - . - This package contains the Python 2 version of osmopy libraries. - -Package: python2-osmopy-utils -Architecture: all -Depends: ${python:Depends}, ${misc:Depends}, python2-osmopy-libs -Description: Python code (not only) for testing of Osmocom programs - . - This package contains the Python 2 version of osmopy utils. - Package: python3-osmopy-libs Architecture: all Depends: ${python3:Depends}, ${misc:Depends} diff --git a/debian/rules b/debian/rules index 04b59f6..ff16a03 100755 --- a/debian/rules +++ b/debian/rules @@ -1,13 +1,9 @@ #!/usr/bin/make -f %: - dh $@ --with python2,python3 + dh $@ --with python3 --buildsystem=pybuild override_dh_auto_install: - python2 setup.py install --install-layout=deb --root=$(CURDIR)/debian/python2-osmopy-libs - rm -rf $(CURDIR)/debian/python2-osmopy-libs/usr/bin - python2 setup.py install --install-layout=deb --root=$(CURDIR)/debian/python2-osmopy-utils - rm -rf $(CURDIR)/debian/python2-osmopy-utils/usr/lib python3 setup.py install --install-layout=deb --root=$(CURDIR)/debian/python3-osmopy-libs rm -rf $(CURDIR)/debian/python3-osmopy-libs/usr/bin python3 setup.py install --install-layout=deb --root=$(CURDIR)/debian/python3-osmopy-utils diff --git a/osmopy/__init__.py b/osmopy/__init__.py index ce78caf..18362ec 100644 --- a/osmopy/__init__.py +++ b/osmopy/__init__.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 __version__ = '0.1.0' __all__ = ['obscvty', 'osmoutil', 'osmo_ipa', 'osmo_interact', 'trap_helper', 'twisted_ipa'] diff --git a/osmopy/obscvty.py b/osmopy/obscvty.py index d39d3da..6b7d56c 100755 --- a/osmopy/obscvty.py +++ b/osmopy/obscvty.py @@ -16,7 +16,6 @@ # # VTY helper code for OpenBSC # -from __future__ import print_function import re import socket import sys, subprocess @@ -176,13 +175,13 @@ class VTYInteract(object): self._connect_socket() # Now send the command - self.socket.send("%s\r" % request) + self.socket.send(("%s\r" % request).encode()) res = "" end = "" # Unfortunately, timeout and recv don't always play nicely while True: - data = self.socket.recv(4096) + data = self.socket.recv(4096).decode() res = "%s%s" % (res, data) if not res: # yes, this is ugly raise IOError("Failed to read data (did the app crash?)") @@ -205,7 +204,7 @@ class VTYInteract(object): buffer = '' data = True while data: - data = self.socket.recv(recv_buffer) + data = self.socket.recv(recv_buffer).decode() buffer += data while buffer.find(delim) != -1: @@ -244,8 +243,8 @@ class VTYInteract(object): def verify(self, command, results, close=False, loud=True, f=None): res = self.command(command, close).split('\r\n') if f: - res = map(f, res) - results = map(f, results) + res = list(map(f, res)) + results = list(map(f, results)) if loud: if res != results: diff --git a/osmopy/osmo_interact/__init__.py b/osmopy/osmo_interact/__init__.py index 4fc4fac..036238a 100644 --- a/osmopy/osmo_interact/__init__.py +++ b/osmopy/osmo_interact/__init__.py @@ -1,2 +1,2 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 __all__ = ['common', 'vty', 'ctrl'] diff --git a/osmopy/osmo_interact/common.py b/osmopy/osmo_interact/common.py index cc7e190..87eca6a 100644 --- a/osmopy/osmo_interact/common.py +++ b/osmopy/osmo_interact/common.py @@ -24,11 +24,6 @@ This implements all of application interaction, piping and verification. vty.py and ctrl.py plug VTY and CTRL interface specific bits. ''' -# Our setup.py currently wants everything to be parsable by both py2 and py3. -# IMHO that is not a good idea, but until that changes, let's just keep this -# py2 legacy shim in here so we can syntax-check this py3 module with py2. -from __future__ import print_function - import argparse import sys import os diff --git a/osmopy/osmoutil.py b/osmopy/osmoutil.py index 54a3456..ec9c8c3 100755 --- a/osmopy/osmoutil.py +++ b/osmopy/osmoutil.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # (C) 2013 by Katerina Barone-Adesi # This program is free software: you can redistribute it and/or modify @@ -14,7 +14,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from __future__ import print_function import subprocess import os import sys diff --git a/scripts/osmodumpdoc.py b/scripts/osmodumpdoc.py index d71edc8..64b088f 100755 --- a/scripts/osmodumpdoc.py +++ b/scripts/osmodumpdoc.py @@ -1,10 +1,9 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # Make sure this code is in sync with the BTS directory. # Fixes may need to be applied to both. """Start the process and dump the documentation to the doc dir.""" -from __future__ import print_function import subprocess import time import os diff --git a/scripts/osmotestconfig.py b/scripts/osmotestconfig.py index f227504..3c96ca6 100755 --- a/scripts/osmotestconfig.py +++ b/scripts/osmotestconfig.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # (C) 2013 by Katerina Barone-Adesi # This program is free software: you can redistribute it and/or modify @@ -13,7 +13,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from __future__ import print_function import os import os.path import time @@ -89,7 +88,7 @@ def copy_config(dirname, config): prefix = os.path.basename(config) tmpfile = tempfile.NamedTemporaryFile( dir=dirname, prefix=prefix, delete=False) - tmpfile.write(open(config).read()) + tmpfile.write(open(config).read().encode()) tmpfile.close() # This works around the precautions NamedTemporaryFile is made for... return tmpfile.name diff --git a/scripts/osmotestvty.py b/scripts/osmotestvty.py index 55017a5..600860f 100755 --- a/scripts/osmotestvty.py +++ b/scripts/osmotestvty.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # (C) 2013 by Katerina Barone-Adesi # This program is free software: you can redistribute it and/or modify @@ -13,7 +13,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from __future__ import print_function import os import time import unittest diff --git a/setup.py b/setup.py index 69e18b0..ab604ce 100755 --- a/setup.py +++ b/setup.py @@ -19,24 +19,20 @@ from setuptools import setup, find_packages from osmopy import __version__ import sys -if sys.version_info.major == 2: - scripts = [ - "scripts/osmodumpdoc.py", - "scripts/osmotestconfig.py", - "scripts/osmotestvty.py", - ] -elif sys.version_info.major == 3: - scripts = [ - "scripts/osmo_ctrl.py", - "scripts/osmo_rate_ctr2csv.py", - "scripts/soap.py", - "scripts/ctrl2cgi.py", - "scripts/osmo_trap2cgi.py", - "scripts/osmo_interact_vty.py", - "scripts/osmo_interact_ctrl.py", - "scripts/osmo_verify_transcript_vty.py", - "scripts/osmo_verify_transcript_ctrl.py", - ] +scripts = [ + "scripts/osmodumpdoc.py", + "scripts/osmotestvty.py", + "scripts/osmotestconfig.py", + "scripts/osmo_ctrl.py", + "scripts/osmo_rate_ctr2csv.py", + "scripts/soap.py", + "scripts/ctrl2cgi.py", + "scripts/osmo_trap2cgi.py", + "scripts/osmo_interact_vty.py", + "scripts/osmo_interact_ctrl.py", + "scripts/osmo_verify_transcript_vty.py", + "scripts/osmo_verify_transcript_ctrl.py", + ] setup( name = 'osmopython', diff --git a/tests/test_py2.py b/tests/test_py2.py deleted file mode 100644 index cac5261..0000000 --- a/tests/test_py2.py +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env python2 - -# just import a smoke test for osmopy - -import osmopy - -print '[Python2] Smoke test PASSED.'