osmo-python-tests/osmopy/osmoutil.py

108 lines
3.1 KiB
Python
Raw Normal View History

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
2019-12-09 13:41:14 +00:00
#!/usr/bin/env python3
# (C) 2013 by Katerina Barone-Adesi <kat.obsc@gmail.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import subprocess
import os
import sys
import importlib
import time
import unittest
"""Run a command, with stdout and stderr directed to devnull"""
devnull = None
def popen_devnull(cmd, verbose=True):
global devnull
if devnull is None:
if verbose:
print("Opening /dev/null")
devnull = open(os.devnull, 'w')
if verbose:
print("Launching: PWD=%s %s" % (os.getcwd(), ' '.join([repr(c) for c in cmd])))
return subprocess.Popen(cmd, stdout=devnull, stderr=devnull)
"""End a process.
If the process doesn't appear to exist (for instance, is None), do nothing"""
def end_proc(proc):
if not proc:
return
proc.terminate()
time_to_wait_for_term = 5
wait_step = 0.001
waited_time = 0
while True:
# poll returns None if proc is still running
rc = proc.poll()
if rc is not None:
break
waited_time += wait_step
# make wait_step approach 1.0
wait_step = (1. + 5. * wait_step) / 6.
if waited_time >= time_to_wait_for_term:
break
time.sleep(wait_step)
if proc.poll() is None:
# termination seems to be slower than that, let's just kill
proc.kill()
print("Killed child process")
elif waited_time > .002:
print("Terminating took %.3fs" % waited_time)
proc.wait()
"""Add a directory to sys.path, try to import a config file."""
def importappconf_or_quit(dirname, confname, p_set):
if dirname not in sys.path:
sys.path.append(dirname)
try:
return importlib.import_module(confname)
except ImportError as e:
if p_set:
print("osmoappdesc not found in %s" % dirname, file=sys.stderr)
else:
print("set osmoappdesc location with -p <dir>", file=sys.stderr)
sys.exit(1)
def pick_tests(suite, *name_snippets):
'''for unittest: Non-standard way of picking only selected tests to run,
by name. Kind of stupid of python unittest to not provide this feature
more easily.'''
new_tests = []
for t in suite._tests:
if isinstance(t, unittest.suite.TestSuite):
pick_tests(t, *name_snippets)
new_tests.append(t)
continue
if not isinstance(t, unittest.TestCase):
new_tests.append(t)
continue
if any([n.lower() in t._testMethodName.lower() for n in name_snippets]):
new_tests.append(t)
suite._tests = new_tests