From patchwork Tue Nov 27 10:50:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cleber Rosa X-Patchwork-Id: 10700245 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1513A181D for ; Tue, 27 Nov 2018 10:51:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0481B2ACBD for ; Tue, 27 Nov 2018 10:51:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EB6FB2ACC1; Tue, 27 Nov 2018 10:51:41 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 39B5D2ACC3 for ; Tue, 27 Nov 2018 10:51:41 +0000 (UTC) Received: from localhost ([::1]:41351 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gRaxw-0003af-43 for patchwork-qemu-devel@patchwork.kernel.org; Tue, 27 Nov 2018 05:51:40 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43246) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gRaxE-0002sM-Pc for qemu-devel@nongnu.org; Tue, 27 Nov 2018 05:50:58 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gRaxC-0008W7-VU for qemu-devel@nongnu.org; Tue, 27 Nov 2018 05:50:56 -0500 Received: from mx1.redhat.com ([209.132.183.28]:56504) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gRax6-00084q-HS; Tue, 27 Nov 2018 05:50:48 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E0E5D58E39; Tue, 27 Nov 2018 10:50:45 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-125-50.rdu2.redhat.com [10.10.125.50]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BC98F5D6A9; Tue, 27 Nov 2018 10:50:36 +0000 (UTC) From: Cleber Rosa To: qemu-devel@nongnu.org Date: Tue, 27 Nov 2018 05:50:34 -0500 Message-Id: <20181127105034.21045-1-crosa@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Tue, 27 Nov 2018 10:50:46 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC PATCH] Introduce Python module structure X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Fam Zheng , Eduardo Habkost , qemu-block@nongnu.org, =?utf-8?q?Ale?= =?utf-8?q?x_Benn=C3=A9e?= , Cleber Rosa , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Wainer dos Santos Moschetta , Max Reitz , Caio Carrara , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP This is a simple move of Python code that wraps common QEMU functionality, and are used by a number of different tests and scripts. By treating that code as a real Python module, we can more easily, among other things: * reuse more code * apply a more consistent style * add tests to that code * generate documentation Signed-off-by: Cleber Rosa --- configure | 1 + scripts/qemu.py => python/qemu/__init__.py | 11 ++++++----- {scripts/qmp => python/qemu}/qmp.py | 0 {scripts => python/qemu}/qtest.py | 5 +++-- scripts/device-crash-test | 5 +++++ scripts/qmp/__init__.py | 0 tests/acceptance/avocado_qemu/__init__.py | 9 +++++---- tests/migration/guestperf/engine.py | 10 +++++++--- tests/qemu-iotests/iotests.py | 8 ++++++-- tests/vm/basevm.py | 9 +++++++-- 10 files changed, 40 insertions(+), 18 deletions(-) rename scripts/qemu.py => python/qemu/__init__.py (98%) rename {scripts/qmp => python/qemu}/qmp.py (100%) rename {scripts => python/qemu}/qtest.py (97%) delete mode 100644 scripts/qmp/__init__.py diff --git a/configure b/configure index 0a3c6a72c3..2b64c51009 100755 --- a/configure +++ b/configure @@ -7510,6 +7510,7 @@ LINKS="$LINKS pc-bios/qemu-icon.bmp" LINKS="$LINKS .gdbinit scripts" # scripts needed by relative path in .gdbinit LINKS="$LINKS tests/acceptance tests/data" LINKS="$LINKS tests/qemu-iotests/check" +LINKS="$LINKS python" for bios_file in \ $source_path/pc-bios/*.bin \ $source_path/pc-bios/*.lid \ diff --git a/scripts/qemu.py b/python/qemu/__init__.py similarity index 98% rename from scripts/qemu.py rename to python/qemu/__init__.py index 6e3b0e6771..2ae5e1f632 100644 --- a/scripts/qemu.py +++ b/python/qemu/__init__.py @@ -16,12 +16,13 @@ import errno import logging import os import subprocess -import qmp.qmp import re import shutil import socket import tempfile +from . import qmp + LOG = logging.getLogger(__name__) @@ -58,7 +59,7 @@ class QEMUMachineAddDeviceError(QEMUMachineError): failures reported by the QEMU binary itself. """ -class MonitorResponseError(qmp.qmp.QMPError): +class MonitorResponseError(qmp.QMPError): """ Represents erroneous QMP monitor reply """ @@ -259,8 +260,8 @@ class QEMUMachine(object): self._qemu_log_path = os.path.join(self._temp_dir, self._name + ".log") self._qemu_log_file = open(self._qemu_log_path, 'wb') - self._qmp = qmp.qmp.QEMUMonitorProtocol(self._vm_monitor, - server=True) + self._qmp = qmp.QEMUMonitorProtocol(self._vm_monitor, + server=True) def _post_launch(self): self._qmp.accept() @@ -376,7 +377,7 @@ class QEMUMachine(object): """ reply = self.qmp(cmd, conv_keys, **args) if reply is None: - raise qmp.qmp.QMPError("Monitor is closed") + raise qmp.QMPError("Monitor is closed") if "error" in reply: raise MonitorResponseError(reply) return reply["return"] diff --git a/scripts/qmp/qmp.py b/python/qemu/qmp.py similarity index 100% rename from scripts/qmp/qmp.py rename to python/qemu/qmp.py diff --git a/scripts/qtest.py b/python/qemu/qtest.py similarity index 97% rename from scripts/qtest.py rename to python/qemu/qtest.py index adf1fe3f26..bff79cdd13 100644 --- a/scripts/qtest.py +++ b/python/qemu/qtest.py @@ -13,7 +13,8 @@ import socket import os -import qemu + +from . import QEMUMachine class QEMUQtestProtocol(object): @@ -73,7 +74,7 @@ class QEMUQtestProtocol(object): self._sock.settimeout(timeout) -class QEMUQtestMachine(qemu.QEMUMachine): +class QEMUQtestMachine(QEMUMachine): '''A QEMU VM''' def __init__(self, binary, args=None, name=None, test_dir="/var/tmp", diff --git a/scripts/device-crash-test b/scripts/device-crash-test index e93a7c0c84..c75ae0ecbc 100755 --- a/scripts/device-crash-test +++ b/scripts/device-crash-test @@ -35,6 +35,11 @@ import random import argparse from itertools import chain +THIS_DIR = os.path.dirname(os.path.abspath(__file__)) +TOP_DIR = os.path.dirname(THIS_DIR) +PYTHON_MODULE_PATH = os.path.join(TOP_DIR, 'python') +sys.path.append(PYTHON_MODULE_PATH) + from qemu import QEMUMachine logger = logging.getLogger('device-crash-test') diff --git a/scripts/qmp/__init__.py b/scripts/qmp/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/tests/acceptance/avocado_qemu/__init__.py b/tests/acceptance/avocado_qemu/__init__.py index 1e54fd5932..4f3e426ebd 100644 --- a/tests/acceptance/avocado_qemu/__init__.py +++ b/tests/acceptance/avocado_qemu/__init__.py @@ -13,9 +13,10 @@ import sys import avocado -SRC_ROOT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) -SRC_ROOT_DIR = os.path.abspath(os.path.dirname(SRC_ROOT_DIR)) -sys.path.append(os.path.join(SRC_ROOT_DIR, 'scripts')) +THIS_DIR = os.path.dirname(os.path.abspath(__file__)) +TOP_DIR = os.path.dirname(os.path.dirname(os.path.dirname(THIS_DIR))) +PYTHON_MODULE_PATH = os.path.join(TOP_DIR, 'python') +sys.path.append(PYTHON_MODULE_PATH) from qemu import QEMUMachine @@ -34,7 +35,7 @@ def pick_default_qemu_bin(): if is_readable_executable_file(qemu_bin_relative_path): return qemu_bin_relative_path - qemu_bin_from_src_dir_path = os.path.join(SRC_ROOT_DIR, + qemu_bin_from_src_dir_path = os.path.join(TOP_DIR, qemu_bin_relative_path) if is_readable_executable_file(qemu_bin_from_src_dir_path): return qemu_bin_from_src_dir_path diff --git a/tests/migration/guestperf/engine.py b/tests/migration/guestperf/engine.py index 398e3f2706..73c9b66821 100644 --- a/tests/migration/guestperf/engine.py +++ b/tests/migration/guestperf/engine.py @@ -24,13 +24,17 @@ import re import sys import time -sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..', 'scripts')) -import qemu -import qmp.qmp from guestperf.progress import Progress, ProgressStats from guestperf.report import Report from guestperf.timings import TimingRecord, Timings +THIS_DIR = os.path.dirname(os.path.abspath(__file__)) +TOP_DIR = os.path.dirname(os.path.dirname(os.path.dirname(THIS_DIR))) +PYTHON_MODULE_PATH = os.path.join(TOP_DIR, 'python') +sys.path.append(PYTHON_MODULE_PATH) + +import qemu + class Engine(object): diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py index d537538ba0..92fddd2a58 100644 --- a/tests/qemu-iotests/iotests.py +++ b/tests/qemu-iotests/iotests.py @@ -31,8 +31,12 @@ import logging import atexit import io -sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'scripts')) -import qtest +THIS_DIR = os.path.dirname(os.path.abspath(__file__)) +TOP_DIR = os.path.dirname(os.path.dirname(THIS_DIR)) +PYTHON_MODULE_PATH = os.path.join(TOP_DIR, 'python') +sys.path.append(PYTHON_MODULE_PATH) + +from qemu import qtest # This will not work if arguments contain spaces but is necessary if we diff --git a/tests/vm/basevm.py b/tests/vm/basevm.py index 5caf77d6b8..464234a7b2 100755 --- a/tests/vm/basevm.py +++ b/tests/vm/basevm.py @@ -17,8 +17,6 @@ import sys import logging import time import datetime -sys.path.append(os.path.join(os.path.dirname(__file__), "..", "..", "scripts")) -from qemu import QEMUMachine, kvm_available import subprocess import hashlib import optparse @@ -28,6 +26,13 @@ import shutil import multiprocessing import traceback +THIS_DIR = os.path.dirname(os.path.abspath(__file__)) +TOP_DIR = os.path.dirname(os.path.dirname(THIS_DIR)) +PYTHON_MODULE_PATH = os.path.join(TOP_DIR, 'python') +sys.path.append(PYTHON_MODULE_PATH) + +from qemu import QEMUMachine, kvm_available + SSH_KEY = open(os.path.join(os.path.dirname(__file__), "..", "keys", "id_rsa")).read() SSH_PUB_KEY = open(os.path.join(os.path.dirname(__file__),