From fcd6b38bab8517d83e1ed48eef1bca9a9a190f57 Mon Sep 17 00:00:00 2001 From: Richard Purdie Date: Fri, 20 May 2016 11:57:44 +0100 Subject: classes/lib: Complete transition to python3 This patch contains all the other misc pieces of the transition to python3 which didn't make sense to be broken into individual patches. Signed-off-by: Richard Purdie --- meta/lib/oeqa/controllers/masterimage.py | 4 +--- meta/lib/oeqa/oetest.py | 10 +++++++++- meta/lib/oeqa/selftest/_toaster.py | 4 ++-- meta/lib/oeqa/selftest/recipetool.py | 4 ++-- meta/lib/oeqa/selftest/sstatetests.py | 6 +++--- meta/lib/oeqa/targetcontrol.py | 4 +--- meta/lib/oeqa/utils/commands.py | 4 ++-- meta/lib/oeqa/utils/decorators.py | 17 ++++++++++++++++- meta/lib/oeqa/utils/dump.py | 2 +- meta/lib/oeqa/utils/httpserver.py | 6 +++--- meta/lib/oeqa/utils/logparser.py | 2 +- meta/lib/oeqa/utils/qemurunner.py | 22 ++++++++++++---------- meta/lib/oeqa/utils/qemutinyrunner.py | 2 +- meta/lib/oeqa/utils/targetbuild.py | 4 +--- meta/lib/oeqa/utils/testexport.py | 2 +- 15 files changed, 56 insertions(+), 37 deletions(-) (limited to 'meta/lib/oeqa') diff --git a/meta/lib/oeqa/controllers/masterimage.py b/meta/lib/oeqa/controllers/masterimage.py index 522f9ebd76..4cb75539ee 100644 --- a/meta/lib/oeqa/controllers/masterimage.py +++ b/meta/lib/oeqa/controllers/masterimage.py @@ -24,9 +24,7 @@ from oeqa.utils import CommandError from abc import ABCMeta, abstractmethod -class MasterImageHardwareTarget(oeqa.targetcontrol.BaseTarget): - - __metaclass__ = ABCMeta +class MasterImageHardwareTarget(oeqa.targetcontrol.BaseTarget, metaclass=ABCMeta): supported_image_fstypes = ['tar.gz', 'tar.bz2'] diff --git a/meta/lib/oeqa/oetest.py b/meta/lib/oeqa/oetest.py index 4211ffc379..b4cf34b720 100644 --- a/meta/lib/oeqa/oetest.py +++ b/meta/lib/oeqa/oetest.py @@ -13,6 +13,7 @@ import inspect import subprocess import signal import shutil +import functools try: import bb except ImportError: @@ -340,7 +341,14 @@ class TestContext(object): for index, suite in enumerate(suites): set_suite_depth(suite) suite.index = index - suites.sort(cmp=lambda a,b: cmp((a.depth, a.index), (b.depth, b.index))) + + def cmp(a, b): + return (a > b) - (a < b) + + def cmpfunc(a, b): + return cmp((a.depth, a.index), (b.depth, b.index)) + + suites.sort(key=functools.cmp_to_key(cmpfunc)) self.suite = testloader.suiteClass(suites) diff --git a/meta/lib/oeqa/selftest/_toaster.py b/meta/lib/oeqa/selftest/_toaster.py index c424659fdc..15ea9df9ef 100644 --- a/meta/lib/oeqa/selftest/_toaster.py +++ b/meta/lib/oeqa/selftest/_toaster.py @@ -2,7 +2,7 @@ import unittest import os import sys import shlex, subprocess -import urllib, commands, time, getpass, re, json, shlex +import urllib.request, urllib.parse, urllib.error, subprocess, time, getpass, re, json, shlex import oeqa.utils.ftools as ftools from oeqa.selftest.base import oeSelfTest @@ -290,7 +290,7 @@ class Toaster_DB_Tests(ToasterSetup): layers = Layer.objects.values('id', 'layer_index_url') cnt_err = [] for layer in layers: - resp = urllib.urlopen(layer['layer_index_url']) + resp = urllib.request.urlopen(layer['layer_index_url']) if (resp.getcode() != 200): cnt_err.append(layer['id']) self.assertEqual(len(cnt_err), 0, msg = 'Errors for layer id: %s' % cnt_err) diff --git a/meta/lib/oeqa/selftest/recipetool.py b/meta/lib/oeqa/selftest/recipetool.py index e72911b0aa..a93d18e275 100644 --- a/meta/lib/oeqa/selftest/recipetool.py +++ b/meta/lib/oeqa/selftest/recipetool.py @@ -1,7 +1,7 @@ import os import logging import tempfile -import urlparse +import urllib.parse from oeqa.utils.commands import runCmd, bitbake, get_bb_var, create_temp_layer from oeqa.utils.decorators import testcase @@ -471,7 +471,7 @@ class RecipetoolAppendsrcBase(RecipetoolBase): '''Return the first file:// in SRC_URI for the specified recipe.''' src_uri = get_bb_var('SRC_URI', recipe).split() for uri in src_uri: - p = urlparse.urlparse(uri) + p = urllib.parse.urlparse(uri) if p.scheme == 'file': return p.netloc + p.path diff --git a/meta/lib/oeqa/selftest/sstatetests.py b/meta/lib/oeqa/selftest/sstatetests.py index cc64c6cb68..07212ac4f1 100644 --- a/meta/lib/oeqa/selftest/sstatetests.py +++ b/meta/lib/oeqa/selftest/sstatetests.py @@ -264,7 +264,7 @@ SDKMACHINE = "i686" files2 = get_files(topdir + "/tmp-sstatesamehash2/stamps/") files2 = [x.replace("tmp-sstatesamehash2", "tmp-sstatesamehash").replace("i686-linux", "x86_64-linux").replace("i686" + targetvendor + "-linux", "x86_64" + targetvendor + "-linux", ) for x in files2] self.maxDiff = None - self.assertItemsEqual(files1, files2) + self.assertCountEqual(files1, files2) @testcase(1271) @@ -298,7 +298,7 @@ NATIVELSBSTRING = \"DistroB\" files2 = get_files(topdir + "/tmp-sstatesamehash2/stamps/") files2 = [x.replace("tmp-sstatesamehash2", "tmp-sstatesamehash") for x in files2] self.maxDiff = None - self.assertItemsEqual(files1, files2) + self.assertCountEqual(files1, files2) @testcase(1368) def test_sstate_allarch_samesigs(self): @@ -393,7 +393,7 @@ DEFAULTTUNE_virtclass-multilib-lib32 = "x86" files2 = get_files(topdir + "/tmp-sstatesamehash2/stamps") files2 = [x.replace("tmp-sstatesamehash2", "tmp-sstatesamehash") for x in files2] self.maxDiff = None - self.assertItemsEqual(files1, files2) + self.assertCountEqual(files1, files2) def test_sstate_noop_samesigs(self): diff --git a/meta/lib/oeqa/targetcontrol.py b/meta/lib/oeqa/targetcontrol.py index 5422a617c4..1c57efaaef 100644 --- a/meta/lib/oeqa/targetcontrol.py +++ b/meta/lib/oeqa/targetcontrol.py @@ -43,9 +43,7 @@ def get_target_controller(d): return controller(d) -class BaseTarget(object): - - __metaclass__ = ABCMeta +class BaseTarget(object, metaclass=ABCMeta): supported_image_fstypes = [] diff --git a/meta/lib/oeqa/utils/commands.py b/meta/lib/oeqa/utils/commands.py index 9a7c1d1375..18fe39ecfe 100644 --- a/meta/lib/oeqa/utils/commands.py +++ b/meta/lib/oeqa/utils/commands.py @@ -41,7 +41,7 @@ class Command(object): self.data = data self.options = dict(self.defaultopts) - if isinstance(self.cmd, basestring): + if isinstance(self.cmd, str): self.options["shell"] = True if self.data: self.options['stdin'] = subprocess.PIPE @@ -123,7 +123,7 @@ def bitbake(command, ignore_status=False, timeout=None, postconfig=None, **optio else: extra_args = "" - if isinstance(command, basestring): + if isinstance(command, str): cmd = "bitbake " + extra_args + " " + command else: cmd = [ "bitbake" ] + [a for a in (command + extra_args.split(" ")) if a not in [""]] diff --git a/meta/lib/oeqa/utils/decorators.py b/meta/lib/oeqa/utils/decorators.py index 6fb09db417..0b23565485 100644 --- a/meta/lib/oeqa/utils/decorators.py +++ b/meta/lib/oeqa/utils/decorators.py @@ -115,6 +115,8 @@ class NoParsingFilter(logging.Filter): def filter(self, record): return record.levelno == 100 +import inspect + def LogResults(original_class): orig_method = original_class.run @@ -124,6 +126,19 @@ def LogResults(original_class): logfile = os.path.join(os.getcwd(),'results-'+caller+'.'+timestamp+'.log') linkfile = os.path.join(os.getcwd(),'results-'+caller+'.log') + def get_class_that_defined_method(meth): + if inspect.ismethod(meth): + for cls in inspect.getmro(meth.__self__.__class__): + if cls.__dict__.get(meth.__name__) is meth: + return cls + meth = meth.__func__ # fallback to __qualname__ parsing + if inspect.isfunction(meth): + cls = getattr(inspect.getmodule(meth), + meth.__qualname__.split('.', 1)[0].rsplit('.', 1)[0]) + if isinstance(cls, type): + return cls + return None + #rewrite the run method of unittest.TestCase to add testcase logging def run(self, result, *args, **kws): orig_method(self, result, *args, **kws) @@ -135,7 +150,7 @@ def LogResults(original_class): except AttributeError: test_case = self._testMethodName - class_name = str(testMethod.im_class).split("'")[1] + class_name = str(get_class_that_defined_method(testMethod)).split("'")[1] #create custom logging level for filtering. custom_log_level = 100 diff --git a/meta/lib/oeqa/utils/dump.py b/meta/lib/oeqa/utils/dump.py index 63a591d366..71422a9aea 100644 --- a/meta/lib/oeqa/utils/dump.py +++ b/meta/lib/oeqa/utils/dump.py @@ -3,7 +3,7 @@ import sys import errno import datetime import itertools -from commands import runCmd +from .commands import runCmd def get_host_dumper(d): cmds = d.getVar("testimage_dump_host", True) diff --git a/meta/lib/oeqa/utils/httpserver.py b/meta/lib/oeqa/utils/httpserver.py index 76518d8ef9..bd76f36468 100644 --- a/meta/lib/oeqa/utils/httpserver.py +++ b/meta/lib/oeqa/utils/httpserver.py @@ -1,8 +1,8 @@ -import SimpleHTTPServer +import http.server import multiprocessing import os -class HTTPServer(SimpleHTTPServer.BaseHTTPServer.HTTPServer): +class HTTPServer(http.server.HTTPServer): def server_start(self, root_dir): import signal @@ -10,7 +10,7 @@ class HTTPServer(SimpleHTTPServer.BaseHTTPServer.HTTPServer): os.chdir(root_dir) self.serve_forever() -class HTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): +class HTTPRequestHandler(http.server.SimpleHTTPRequestHandler): def log_message(self, format_str, *args): pass diff --git a/meta/lib/oeqa/utils/logparser.py b/meta/lib/oeqa/utils/logparser.py index 87b50354cd..b377dcd271 100644 --- a/meta/lib/oeqa/utils/logparser.py +++ b/meta/lib/oeqa/utils/logparser.py @@ -3,7 +3,7 @@ import sys import os import re -import ftools +from . import ftools # A parser that can be used to identify weather a line is a test result or a section statement. diff --git a/meta/lib/oeqa/utils/qemurunner.py b/meta/lib/oeqa/utils/qemurunner.py index 3d60433cae..e408fbbf3a 100644 --- a/meta/lib/oeqa/utils/qemurunner.py +++ b/meta/lib/oeqa/utils/qemurunner.py @@ -23,8 +23,8 @@ logger = logging.getLogger("BitBake.QemuRunner") # Get Unicode non printable control chars control_range = list(range(0,32))+list(range(127,160)) -control_chars = [unichr(x) for x in control_range - if unichr(x) not in string.printable] +control_chars = [chr(x) for x in control_range + if chr(x) not in string.printable] re_control_char = re.compile('[%s]' % re.escape("".join(control_chars))) class QemuRunner: @@ -220,6 +220,7 @@ class QemuRunner: stopread = False qemusock = None bootlog = '' + data = b'' while time.time() < endtime and not stopread: sread, swrite, serror = select.select(socklist, [], [], 5) for sock in sread: @@ -283,13 +284,14 @@ class QemuRunner: if hasattr(self, "origchldhandler"): signal.signal(signal.SIGCHLD, self.origchldhandler) if self.runqemu: - os.kill(self.monitorpid, signal.SIGKILL) - logger.info("Sending SIGTERM to runqemu") - try: - os.killpg(os.getpgid(self.runqemu.pid), signal.SIGTERM) - except OSError as e: - if e.errno != errno.ESRCH: - raise + if hasattr(self, "monitorpid"): + os.kill(self.monitorpid, signal.SIGKILL) + logger.info("Sending SIGTERM to runqemu") + try: + os.killpg(os.getpgid(self.runqemu.pid), signal.SIGTERM) + except OSError as e: + if e.errno != errno.ESRCH: + raise endtime = time.time() + self.runqemutime while self.runqemu.poll() is None and time.time() < endtime: time.sleep(1) @@ -448,7 +450,7 @@ class LoggingThread(threading.Thread): def stop(self): self.logger.info("Stopping logging thread") if self.running: - os.write(self.writepipe, "stop") + os.write(self.writepipe, bytes("stop", "utf-8")) def teardown(self): self.logger.info("Tearing down logging thread") diff --git a/meta/lib/oeqa/utils/qemutinyrunner.py b/meta/lib/oeqa/utils/qemutinyrunner.py index 054ab0ec5d..c823157ad6 100644 --- a/meta/lib/oeqa/utils/qemutinyrunner.py +++ b/meta/lib/oeqa/utils/qemutinyrunner.py @@ -13,7 +13,7 @@ import re import socket import select import bb -from qemurunner import QemuRunner +from .qemurunner import QemuRunner class QemuTinyRunner(QemuRunner): diff --git a/meta/lib/oeqa/utils/targetbuild.py b/meta/lib/oeqa/utils/targetbuild.py index f850d78df1..d538f6b65f 100644 --- a/meta/lib/oeqa/utils/targetbuild.py +++ b/meta/lib/oeqa/utils/targetbuild.py @@ -10,9 +10,7 @@ import bb.utils import subprocess from abc import ABCMeta, abstractmethod -class BuildProject(): - - __metaclass__ = ABCMeta +class BuildProject(metaclass=ABCMeta): def __init__(self, d, uri, foldername=None, tmpdir="/tmp/"): self.d = d diff --git a/meta/lib/oeqa/utils/testexport.py b/meta/lib/oeqa/utils/testexport.py index 4fbf4bdcb3..57be2ca449 100644 --- a/meta/lib/oeqa/utils/testexport.py +++ b/meta/lib/oeqa/utils/testexport.py @@ -6,7 +6,7 @@ import os, re, glob as g, shutil as sh,sys from time import sleep -from commands import runCmd +from .commands import runCmd from difflib import SequenceMatcher as SM try: -- cgit 1.2.3-korg