From 2ca6e26cea73fa1d270f73392e8b87f3e67e6a2b Mon Sep 17 00:00:00 2001 From: Cleber Rosa Date: Thu, 11 Feb 2021 17:01:42 -0500 Subject: Python: expose QEMUMachine's temporary directory Each instance of qemu.machine.QEMUMachine currently has a "test directory", which may not have any relation to a "test", and it's really a temporary directory. Users instantiating the QEMUMachine class will be able to set the location of the directory that will *contain* the QEMUMachine unique temporary directory, so that parameter name has been changed from test_dir to base_temp_dir. A property has been added to allow users to access it without using private attributes, and with that, the directory is created on first use of the property. Signed-off-by: Cleber Rosa Message-Id: <20210211220146.2525771-3-crosa@redhat.com> Reviewed-by: Wainer dos Santos Moschetta Signed-off-by: Cleber Rosa Signed-off-by: John Snow --- python/qemu/machine.py | 24 ++++++++++++++++-------- python/qemu/qtest.py | 6 +++--- 2 files changed, 19 insertions(+), 11 deletions(-) (limited to 'python') diff --git a/python/qemu/machine.py b/python/qemu/machine.py index 6e44bda..b379fcb 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -84,7 +84,7 @@ class QEMUMachine: args: Sequence[str] = (), wrapper: Sequence[str] = (), name: Optional[str] = None, - test_dir: str = "/var/tmp", + base_temp_dir: str = "/var/tmp", monitor_address: Optional[SocketAddrT] = None, socket_scm_helper: Optional[str] = None, sock_dir: Optional[str] = None, @@ -97,10 +97,10 @@ class QEMUMachine: @param args: list of extra arguments @param wrapper: list of arguments used as prefix to qemu binary @param name: prefix for socket and log file names (default: qemu-PID) - @param test_dir: where to create socket and log file + @param base_temp_dir: default location where temporary files are created @param monitor_address: address for QMP monitor @param socket_scm_helper: helper program, required for send_fd_scm() - @param sock_dir: where to create socket (overrides test_dir for sock) + @param sock_dir: where to create socket (defaults to base_temp_dir) @param drain_console: (optional) True to drain console socket to buffer @param console_log: (optional) path to console log file @note: Qemu process is not started until launch() is used. @@ -112,8 +112,8 @@ class QEMUMachine: self._wrapper = wrapper self._name = name or "qemu-%d" % os.getpid() - self._test_dir = test_dir - self._sock_dir = sock_dir or self._test_dir + self._base_temp_dir = base_temp_dir + self._sock_dir = sock_dir or self._base_temp_dir self._socket_scm_helper = socket_scm_helper if monitor_address is not None: @@ -303,9 +303,7 @@ class QEMUMachine: return args def _pre_launch(self) -> None: - self._temp_dir = tempfile.mkdtemp(prefix="qemu-machine-", - dir=self._test_dir) - self._qemu_log_path = os.path.join(self._temp_dir, self._name + ".log") + self._qemu_log_path = os.path.join(self.temp_dir, self._name + ".log") self._qemu_log_file = open(self._qemu_log_path, 'wb') if self._console_set: @@ -744,3 +742,13 @@ class QEMUMachine: file=self._console_log_path, drain=self._drain_console) return self._console_socket + + @property + def temp_dir(self) -> str: + """ + Returns a temporary directory to be used for this machine + """ + if self._temp_dir is None: + self._temp_dir = tempfile.mkdtemp(prefix="qemu-machine-", + dir=self._base_temp_dir) + return self._temp_dir diff --git a/python/qemu/qtest.py b/python/qemu/qtest.py index 39a0cf6..78b97d1 100644 --- a/python/qemu/qtest.py +++ b/python/qemu/qtest.py @@ -112,14 +112,14 @@ class QEMUQtestMachine(QEMUMachine): binary: str, args: Sequence[str] = (), name: Optional[str] = None, - test_dir: str = "/var/tmp", + base_temp_dir: str = "/var/tmp", socket_scm_helper: Optional[str] = None, sock_dir: Optional[str] = None): if name is None: name = "qemu-%d" % os.getpid() if sock_dir is None: - sock_dir = test_dir - super().__init__(binary, args, name=name, test_dir=test_dir, + sock_dir = base_temp_dir + super().__init__(binary, args, name=name, base_temp_dir=base_temp_dir, socket_scm_helper=socket_scm_helper, sock_dir=sock_dir) self._qtest: Optional[QEMUQtestProtocol] = None -- cgit v1.1 From 976218cbe792c37c1af7840ca5113e37b5a51d95 Mon Sep 17 00:00:00 2001 From: Cleber Rosa Date: Mon, 12 Apr 2021 00:46:36 -0400 Subject: Python: add utility function for retrieving port redirection Slightly different versions for the same utility code are currently present on different locations. This unifies them all, giving preference to the version from virtiofs_submounts.py, because of the last tweaks added to it. While at it, this adds a "qemu.utils" module to host the utility function and a test. Signed-off-by: Cleber Rosa Reviewed-by: Wainer dos Santos Moschetta Reviewed-by: Eric Auger Reviewed-by: Willian Rampazzo Message-Id: <20210412044644.55083-4-crosa@redhat.com> Signed-off-by: John Snow [Squashed in below fix. --js] Signed-off-by: John Snow Signed-off-by: Cleber Rosa Message-Id: <20210601154546.130870-2-crosa@redhat.com> Signed-off-by: John Snow --- python/qemu/utils.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 python/qemu/utils.py (limited to 'python') diff --git a/python/qemu/utils.py b/python/qemu/utils.py new file mode 100644 index 0000000..5ed7892 --- /dev/null +++ b/python/qemu/utils.py @@ -0,0 +1,33 @@ +""" +QEMU utility library + +This offers miscellaneous utility functions, which may not be easily +distinguishable or numerous to be in their own module. +""" + +# Copyright (C) 2021 Red Hat Inc. +# +# Authors: +# Cleber Rosa +# +# This work is licensed under the terms of the GNU GPL, version 2. See +# the COPYING file in the top-level directory. +# + +import re +from typing import Optional + + +def get_info_usernet_hostfwd_port(info_usernet_output: str) -> Optional[int]: + """ + Returns the port given to the hostfwd parameter via info usernet + + :param info_usernet_output: output generated by hmp command "info usernet" + :return: the port number allocated by the hostfwd option + """ + for line in info_usernet_output.split('\r\n'): + regex = r'TCP.HOST_FORWARD.*127\.0\.0\.1\s+(\d+)\s+10\.' + match = re.search(regex, line) + if match is not None: + return int(match[1]) + return None -- cgit v1.1 From ee1a27235b7965bc5514555eec898f4d067fced2 Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:16:45 -0400 Subject: python/console_socket: avoid one-letter variable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes pylint warnings. Signed-off-by: John Snow Reviewed-by: Cleber Rosa Reviewed-by: Philippe Mathieu-Daudé Message-id: 20210527211715.394144-2-jsnow@redhat.com Message-id: 20210517184808.3562549-2-jsnow@redhat.com Signed-off-by: John Snow --- python/qemu/console_socket.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'python') diff --git a/python/qemu/console_socket.py b/python/qemu/console_socket.py index ac21130..87237be 100644 --- a/python/qemu/console_socket.py +++ b/python/qemu/console_socket.py @@ -46,11 +46,11 @@ class ConsoleSocket(socket.socket): self._drain_thread = self._thread_start() def __repr__(self) -> str: - s = super().__repr__() - s = s.rstrip(">") - s = "%s, logfile=%s, drain_thread=%s>" % (s, self._logfile, - self._drain_thread) - return s + tmp = super().__repr__() + tmp = tmp.rstrip(">") + tmp = "%s, logfile=%s, drain_thread=%s>" % (tmp, self._logfile, + self._drain_thread) + return tmp def _drain_fn(self) -> None: """Drains the socket and runs while the socket is open.""" -- cgit v1.1 From 07b71233a7ea77c0ec3687c3a3451865b3b899d3 Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:16:46 -0400 Subject: python/machine: use subprocess.DEVNULL instead of open(os.path.devnull) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit One less file resource to manage, and it helps quiet some pylint >= 2.8.0 warnings about not using a with-context manager for the open call. Signed-off-by: John Snow Reviewed-by: Philippe Mathieu-Daudé Reviewed-by: Cleber Rosa Message-id: 20210527211715.394144-3-jsnow@redhat.com Message-id: 20210517184808.3562549-3-jsnow@redhat.com Signed-off-by: John Snow --- python/qemu/machine.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'python') diff --git a/python/qemu/machine.py b/python/qemu/machine.py index b379fcb..5b87e9c 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -223,9 +223,8 @@ class QEMUMachine: assert fd is not None fd_param.append(str(fd)) - devnull = open(os.path.devnull, 'rb') proc = subprocess.Popen( - fd_param, stdin=devnull, stdout=subprocess.PIPE, + fd_param, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=False ) output = proc.communicate()[0] @@ -391,7 +390,6 @@ class QEMUMachine: """ Launch the VM and establish a QMP connection """ - devnull = open(os.path.devnull, 'rb') self._pre_launch() self._qemu_full_args = tuple( chain(self._wrapper, @@ -401,7 +399,7 @@ class QEMUMachine: ) LOG.debug('VM launch command: %r', ' '.join(self._qemu_full_args)) self._popen = subprocess.Popen(self._qemu_full_args, - stdin=devnull, + stdin=subprocess.DEVNULL, stdout=self._qemu_log_file, stderr=subprocess.STDOUT, shell=False, -- cgit v1.1 From 14b41797d5eb20fb9c6d0a1fe809e8422938f230 Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:16:47 -0400 Subject: python/machine: use subprocess.run instead of subprocess.Popen use run() instead of Popen() -- to assert to pylint that we are not forgetting to close a long-running program. Signed-off-by: John Snow Reviewed-by: Cleber Rosa Tested-by: Cleber Rosa Message-id: 20210527211715.394144-4-jsnow@redhat.com Message-id: 20210517184808.3562549-4-jsnow@redhat.com Signed-off-by: John Snow --- python/qemu/machine.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'python') diff --git a/python/qemu/machine.py b/python/qemu/machine.py index 5b87e9c..04e005f 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -223,13 +223,16 @@ class QEMUMachine: assert fd is not None fd_param.append(str(fd)) - proc = subprocess.Popen( - fd_param, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, close_fds=False + proc = subprocess.run( + fd_param, + stdin=subprocess.DEVNULL, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + check=False, + close_fds=False, ) - output = proc.communicate()[0] - if output: - LOG.debug(output) + if proc.stdout: + LOG.debug(proc.stdout) return proc.returncode -- cgit v1.1 From 8825fed82a1949ed74f103c2ff26c4d71d2e4845 Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:16:48 -0400 Subject: python/console_socket: Add a pylint ignore We manage cleaning up this resource ourselves. Pylint should shush. Signed-off-by: John Snow Reviewed-by: Cleber Rosa Message-id: 20210527211715.394144-5-jsnow@redhat.com Message-id: 20210517184808.3562549-5-jsnow@redhat.com Signed-off-by: John Snow --- python/qemu/console_socket.py | 1 + 1 file changed, 1 insertion(+) (limited to 'python') diff --git a/python/qemu/console_socket.py b/python/qemu/console_socket.py index 87237be..8c4ff59 100644 --- a/python/qemu/console_socket.py +++ b/python/qemu/console_socket.py @@ -39,6 +39,7 @@ class ConsoleSocket(socket.socket): self.connect(address) self._logfile = None if file: + # pylint: disable=consider-using-with self._logfile = open(file, "bw") self._open = True self._drain_thread = None -- cgit v1.1 From 63c33f3c286efe4c6474b53ae97915c9d1a6923a Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:16:49 -0400 Subject: python/machine: Disable pylint warning for open() in _pre_launch Shift the open() call later so that the pylint pragma applies *only* to that one open() call. Add a note that suggests why this is safe: the resource is unconditionally cleaned up in _post_shutdown(). _post_shutdown is called after failed launches (see launch()), and unconditionally after every call to shutdown(), and therefore also on __exit__. Signed-off-by: John Snow Reviewed-by: Wainer dos Santos Moschetta Reviewed-by: Cleber Rosa Message-id: 20210527211715.394144-6-jsnow@redhat.com Message-id: 20210517184808.3562549-6-jsnow@redhat.com Signed-off-by: John Snow --- python/qemu/machine.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'python') diff --git a/python/qemu/machine.py b/python/qemu/machine.py index 04e005f..c66bc6a 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -306,7 +306,6 @@ class QEMUMachine: def _pre_launch(self) -> None: self._qemu_log_path = os.path.join(self.temp_dir, self._name + ".log") - self._qemu_log_file = open(self._qemu_log_path, 'wb') if self._console_set: self._remove_files.append(self._console_address) @@ -321,6 +320,11 @@ class QEMUMachine: nickname=self._name ) + # NOTE: Make sure any opened resources are *definitely* freed in + # _post_shutdown()! + # pylint: disable=consider-using-with + self._qemu_log_file = open(self._qemu_log_path, 'wb') + def _post_launch(self) -> None: if self._qmp_connection: self._qmp.accept() -- cgit v1.1 From a0eae17a59fcbcdc96af2ea2a6767d758ff4a916 Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:16:50 -0400 Subject: python/machine: disable warning for Popen in _launch() We handle this resource rather meticulously in shutdown/kill/wait/__exit__ et al, through the laborious mechanisms in _do_shutdown(). Quiet this pylint warning here. Signed-off-by: John Snow Reviewed-by: Cleber Rosa Message-id: 20210527211715.394144-7-jsnow@redhat.com Message-id: 20210517184808.3562549-7-jsnow@redhat.com Signed-off-by: John Snow --- python/qemu/machine.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'python') diff --git a/python/qemu/machine.py b/python/qemu/machine.py index c66bc6a..5d72c4c 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -405,6 +405,9 @@ class QEMUMachine: self._args) ) LOG.debug('VM launch command: %r', ' '.join(self._qemu_full_args)) + + # Cleaning up of this subprocess is guaranteed by _do_shutdown. + # pylint: disable=consider-using-with self._popen = subprocess.Popen(self._qemu_full_args, stdin=subprocess.DEVNULL, stdout=self._qemu_log_file, -- cgit v1.1 From 859aeb67d7372e63bd7bb2c7d063c2a49f2507ab Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:16:51 -0400 Subject: python/machine: Trim line length to below 80 chars One more little delinting fix that snuck in. Signed-off-by: John Snow Reviewed-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Cleber Rosa Message-id: 20210527211715.394144-8-jsnow@redhat.com Signed-off-by: John Snow --- python/qemu/machine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'python') diff --git a/python/qemu/machine.py b/python/qemu/machine.py index 5d72c4c..a8837b3 100644 --- a/python/qemu/machine.py +++ b/python/qemu/machine.py @@ -97,7 +97,7 @@ class QEMUMachine: @param args: list of extra arguments @param wrapper: list of arguments used as prefix to qemu binary @param name: prefix for socket and log file names (default: qemu-PID) - @param base_temp_dir: default location where temporary files are created + @param base_temp_dir: default location where temp files are created @param monitor_address: address for QMP monitor @param socket_scm_helper: helper program, required for send_fd_scm() @param sock_dir: where to create socket (defaults to base_temp_dir) -- cgit v1.1 From beb6b57b3b1a1fe6ebc208d2edc12b504f69e29f Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:16:53 -0400 Subject: python: create qemu packages move python/qemu/*.py to python/qemu/[machine, qmp, utils]/*.py and update import directives across the tree. This is done to create a PEP420 namespace package, in which we may create subpackages. To do this, the namespace directory ("qemu") should not have any modules in it. Those files will go into new 'machine', 'qmp' and 'utils' subpackages instead. Implement machine/__init__.py making the top-level classes and functions from its various modules available directly inside the package. Change qmp.py to qmp/__init__.py similarly, such that all of the useful QMP library classes are available directly from "qemu.qmp" instead of "qemu.qmp.qmp". Signed-off-by: John Snow Reviewed-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Cleber Rosa Message-id: 20210527211715.394144-10-jsnow@redhat.com Signed-off-by: John Snow --- python/.isort.cfg | 7 + python/qemu/.flake8 | 2 - python/qemu/.isort.cfg | 7 - python/qemu/__init__.py | 11 - python/qemu/accel.py | 84 ---- python/qemu/console_socket.py | 129 ------ python/qemu/machine.py | 762 --------------------------------- python/qemu/machine/.flake8 | 2 + python/qemu/machine/__init__.py | 33 ++ python/qemu/machine/console_socket.py | 129 ++++++ python/qemu/machine/machine.py | 768 ++++++++++++++++++++++++++++++++++ python/qemu/machine/pylintrc | 58 +++ python/qemu/machine/qtest.py | 160 +++++++ python/qemu/pylintrc | 58 --- python/qemu/qmp.py | 375 ----------------- python/qemu/qmp/__init__.py | 385 +++++++++++++++++ python/qemu/qtest.py | 159 ------- python/qemu/utils.py | 33 -- python/qemu/utils/__init__.py | 45 ++ python/qemu/utils/accel.py | 84 ++++ 20 files changed, 1671 insertions(+), 1620 deletions(-) create mode 100644 python/.isort.cfg delete mode 100644 python/qemu/.flake8 delete mode 100644 python/qemu/.isort.cfg delete mode 100644 python/qemu/__init__.py delete mode 100644 python/qemu/accel.py delete mode 100644 python/qemu/console_socket.py delete mode 100644 python/qemu/machine.py create mode 100644 python/qemu/machine/.flake8 create mode 100644 python/qemu/machine/__init__.py create mode 100644 python/qemu/machine/console_socket.py create mode 100644 python/qemu/machine/machine.py create mode 100644 python/qemu/machine/pylintrc create mode 100644 python/qemu/machine/qtest.py delete mode 100644 python/qemu/pylintrc delete mode 100644 python/qemu/qmp.py create mode 100644 python/qemu/qmp/__init__.py delete mode 100644 python/qemu/qtest.py delete mode 100644 python/qemu/utils.py create mode 100644 python/qemu/utils/__init__.py create mode 100644 python/qemu/utils/accel.py (limited to 'python') diff --git a/python/.isort.cfg b/python/.isort.cfg new file mode 100644 index 0000000..6d0fd6c --- /dev/null +++ b/python/.isort.cfg @@ -0,0 +1,7 @@ +[settings] +force_grid_wrap=4 +force_sort_within_sections=True +include_trailing_comma=True +line_length=72 +lines_after_imports=2 +multi_line_output=3 \ No newline at end of file diff --git a/python/qemu/.flake8 b/python/qemu/.flake8 deleted file mode 100644 index 45d8146..0000000 --- a/python/qemu/.flake8 +++ /dev/null @@ -1,2 +0,0 @@ -[flake8] -extend-ignore = E722 # Pylint handles this, but smarter. \ No newline at end of file diff --git a/python/qemu/.isort.cfg b/python/qemu/.isort.cfg deleted file mode 100644 index 6d0fd6c..0000000 --- a/python/qemu/.isort.cfg +++ /dev/null @@ -1,7 +0,0 @@ -[settings] -force_grid_wrap=4 -force_sort_within_sections=True -include_trailing_comma=True -line_length=72 -lines_after_imports=2 -multi_line_output=3 \ No newline at end of file diff --git a/python/qemu/__init__.py b/python/qemu/__init__.py deleted file mode 100644 index 4ca06c3..0000000 --- a/python/qemu/__init__.py +++ /dev/null @@ -1,11 +0,0 @@ -# QEMU library -# -# Copyright (C) 2015-2016 Red Hat Inc. -# Copyright (C) 2012 IBM Corp. -# -# Authors: -# Fam Zheng -# -# This work is licensed under the terms of the GNU GPL, version 2. See -# the COPYING file in the top-level directory. -# diff --git a/python/qemu/accel.py b/python/qemu/accel.py deleted file mode 100644 index 297933d..0000000 --- a/python/qemu/accel.py +++ /dev/null @@ -1,84 +0,0 @@ -""" -QEMU accel module: - -This module provides utilities for discover and check the availability of -accelerators. -""" -# Copyright (C) 2015-2016 Red Hat Inc. -# Copyright (C) 2012 IBM Corp. -# -# Authors: -# Fam Zheng -# -# This work is licensed under the terms of the GNU GPL, version 2. See -# the COPYING file in the top-level directory. -# - -import logging -import os -import subprocess -from typing import List, Optional - - -LOG = logging.getLogger(__name__) - -# Mapping host architecture to any additional architectures it can -# support which often includes its 32 bit cousin. -ADDITIONAL_ARCHES = { - "x86_64": "i386", - "aarch64": "armhf", - "ppc64le": "ppc64", -} - - -def list_accel(qemu_bin: str) -> List[str]: - """ - List accelerators enabled in the QEMU binary. - - @param qemu_bin (str): path to the QEMU binary. - @raise Exception: if failed to run `qemu -accel help` - @return a list of accelerator names. - """ - if not qemu_bin: - return [] - try: - out = subprocess.check_output([qemu_bin, '-accel', 'help'], - universal_newlines=True) - except: - LOG.debug("Failed to get the list of accelerators in %s", qemu_bin) - raise - # Skip the first line which is the header. - return [acc.strip() for acc in out.splitlines()[1:]] - - -def kvm_available(target_arch: Optional[str] = None, - qemu_bin: Optional[str] = None) -> bool: - """ - Check if KVM is available using the following heuristic: - - Kernel module is present in the host; - - Target and host arches don't mismatch; - - KVM is enabled in the QEMU binary. - - @param target_arch (str): target architecture - @param qemu_bin (str): path to the QEMU binary - @return True if kvm is available, otherwise False. - """ - if not os.access("/dev/kvm", os.R_OK | os.W_OK): - return False - if target_arch: - host_arch = os.uname()[4] - if target_arch != host_arch: - if target_arch != ADDITIONAL_ARCHES.get(host_arch): - return False - if qemu_bin and "kvm" not in list_accel(qemu_bin): - return False - return True - - -def tcg_available(qemu_bin: str) -> bool: - """ - Check if TCG is available. - - @param qemu_bin (str): path to the QEMU binary - """ - return 'tcg' in list_accel(qemu_bin) diff --git a/python/qemu/console_socket.py b/python/qemu/console_socket.py deleted file mode 100644 index 8c4ff59..0000000 --- a/python/qemu/console_socket.py +++ /dev/null @@ -1,129 +0,0 @@ -""" -QEMU Console Socket Module: - -This python module implements a ConsoleSocket object, -which can drain a socket and optionally dump the bytes to file. -""" -# Copyright 2020 Linaro -# -# Authors: -# Robert Foley -# -# This code is licensed under the GPL version 2 or later. See -# the COPYING file in the top-level directory. -# - -from collections import deque -import socket -import threading -import time -from typing import Deque, Optional - - -class ConsoleSocket(socket.socket): - """ - ConsoleSocket represents a socket attached to a char device. - - Optionally (if drain==True), drains the socket and places the bytes - into an in memory buffer for later processing. - - Optionally a file path can be passed in and we will also - dump the characters to this file for debugging purposes. - """ - def __init__(self, address: str, file: Optional[str] = None, - drain: bool = False): - self._recv_timeout_sec = 300.0 - self._sleep_time = 0.5 - self._buffer: Deque[int] = deque() - socket.socket.__init__(self, socket.AF_UNIX, socket.SOCK_STREAM) - self.connect(address) - self._logfile = None - if file: - # pylint: disable=consider-using-with - self._logfile = open(file, "bw") - self._open = True - self._drain_thread = None - if drain: - self._drain_thread = self._thread_start() - - def __repr__(self) -> str: - tmp = super().__repr__() - tmp = tmp.rstrip(">") - tmp = "%s, logfile=%s, drain_thread=%s>" % (tmp, self._logfile, - self._drain_thread) - return tmp - - def _drain_fn(self) -> None: - """Drains the socket and runs while the socket is open.""" - while self._open: - try: - self._drain_socket() - except socket.timeout: - # The socket is expected to timeout since we set a - # short timeout to allow the thread to exit when - # self._open is set to False. - time.sleep(self._sleep_time) - - def _thread_start(self) -> threading.Thread: - """Kick off a thread to drain the socket.""" - # Configure socket to not block and timeout. - # This allows our drain thread to not block - # on recieve and exit smoothly. - socket.socket.setblocking(self, False) - socket.socket.settimeout(self, 1) - drain_thread = threading.Thread(target=self._drain_fn) - drain_thread.daemon = True - drain_thread.start() - return drain_thread - - def close(self) -> None: - """Close the base object and wait for the thread to terminate""" - if self._open: - self._open = False - if self._drain_thread is not None: - thread, self._drain_thread = self._drain_thread, None - thread.join() - socket.socket.close(self) - if self._logfile: - self._logfile.close() - self._logfile = None - - def _drain_socket(self) -> None: - """process arriving characters into in memory _buffer""" - data = socket.socket.recv(self, 1) - if self._logfile: - self._logfile.write(data) - self._logfile.flush() - self._buffer.extend(data) - - def recv(self, bufsize: int = 1, flags: int = 0) -> bytes: - """Return chars from in memory buffer. - Maintains the same API as socket.socket.recv. - """ - if self._drain_thread is None: - # Not buffering the socket, pass thru to socket. - return socket.socket.recv(self, bufsize, flags) - assert not flags, "Cannot pass flags to recv() in drained mode" - start_time = time.time() - while len(self._buffer) < bufsize: - time.sleep(self._sleep_time) - elapsed_sec = time.time() - start_time - if elapsed_sec > self._recv_timeout_sec: - raise socket.timeout - return bytes((self._buffer.popleft() for i in range(bufsize))) - - def setblocking(self, value: bool) -> None: - """When not draining we pass thru to the socket, - since when draining we control socket blocking. - """ - if self._drain_thread is None: - socket.socket.setblocking(self, value) - - def settimeout(self, value: Optional[float]) -> None: - """When not draining we pass thru to the socket, - since when draining we control the timeout. - """ - if value is not None: - self._recv_timeout_sec = value - if self._drain_thread is None: - socket.socket.settimeout(self, value) diff --git a/python/qemu/machine.py b/python/qemu/machine.py deleted file mode 100644 index a8837b3..0000000 --- a/python/qemu/machine.py +++ /dev/null @@ -1,762 +0,0 @@ -""" -QEMU machine module: - -The machine module primarily provides the QEMUMachine class, -which provides facilities for managing the lifetime of a QEMU VM. -""" - -# Copyright (C) 2015-2016 Red Hat Inc. -# Copyright (C) 2012 IBM Corp. -# -# Authors: -# Fam Zheng -# -# This work is licensed under the terms of the GNU GPL, version 2. See -# the COPYING file in the top-level directory. -# -# Based on qmp.py. -# - -import errno -from itertools import chain -import logging -import os -import shutil -import signal -import socket -import subprocess -import tempfile -from types import TracebackType -from typing import ( - Any, - BinaryIO, - Dict, - List, - Optional, - Sequence, - Tuple, - Type, -) - -from . import console_socket, qmp -from .qmp import QMPMessage, QMPReturnValue, SocketAddrT - - -LOG = logging.getLogger(__name__) - - -class QEMUMachineError(Exception): - """ - Exception called when an error in QEMUMachine happens. - """ - - -class QEMUMachineAddDeviceError(QEMUMachineError): - """ - Exception raised when a request to add a device can not be fulfilled - - The failures are caused by limitations, lack of information or conflicting - requests on the QEMUMachine methods. This exception does not represent - failures reported by the QEMU binary itself. - """ - - -class AbnormalShutdown(QEMUMachineError): - """ - Exception raised when a graceful shutdown was requested, but not performed. - """ - - -class QEMUMachine: - """ - A QEMU VM. - - Use this object as a context manager to ensure - the QEMU process terminates:: - - with VM(binary) as vm: - ... - # vm is guaranteed to be shut down here - """ - - def __init__(self, - binary: str, - args: Sequence[str] = (), - wrapper: Sequence[str] = (), - name: Optional[str] = None, - base_temp_dir: str = "/var/tmp", - monitor_address: Optional[SocketAddrT] = None, - socket_scm_helper: Optional[str] = None, - sock_dir: Optional[str] = None, - drain_console: bool = False, - console_log: Optional[str] = None): - ''' - Initialize a QEMUMachine - - @param binary: path to the qemu binary - @param args: list of extra arguments - @param wrapper: list of arguments used as prefix to qemu binary - @param name: prefix for socket and log file names (default: qemu-PID) - @param base_temp_dir: default location where temp files are created - @param monitor_address: address for QMP monitor - @param socket_scm_helper: helper program, required for send_fd_scm() - @param sock_dir: where to create socket (defaults to base_temp_dir) - @param drain_console: (optional) True to drain console socket to buffer - @param console_log: (optional) path to console log file - @note: Qemu process is not started until launch() is used. - ''' - # Direct user configuration - - self._binary = binary - self._args = list(args) - self._wrapper = wrapper - - self._name = name or "qemu-%d" % os.getpid() - self._base_temp_dir = base_temp_dir - self._sock_dir = sock_dir or self._base_temp_dir - self._socket_scm_helper = socket_scm_helper - - if monitor_address is not None: - self._monitor_address = monitor_address - self._remove_monitor_sockfile = False - else: - self._monitor_address = os.path.join( - self._sock_dir, f"{self._name}-monitor.sock" - ) - self._remove_monitor_sockfile = True - - self._console_log_path = console_log - if self._console_log_path: - # In order to log the console, buffering needs to be enabled. - self._drain_console = True - else: - self._drain_console = drain_console - - # Runstate - self._qemu_log_path: Optional[str] = None - self._qemu_log_file: Optional[BinaryIO] = None - self._popen: Optional['subprocess.Popen[bytes]'] = None - self._events: List[QMPMessage] = [] - self._iolog: Optional[str] = None - self._qmp_set = True # Enable QMP monitor by default. - self._qmp_connection: Optional[qmp.QEMUMonitorProtocol] = None - self._qemu_full_args: Tuple[str, ...] = () - self._temp_dir: Optional[str] = None - self._launched = False - self._machine: Optional[str] = None - self._console_index = 0 - self._console_set = False - self._console_device_type: Optional[str] = None - self._console_address = os.path.join( - self._sock_dir, f"{self._name}-console.sock" - ) - self._console_socket: Optional[socket.socket] = None - self._remove_files: List[str] = [] - self._user_killed = False - - def __enter__(self) -> 'QEMUMachine': - return self - - def __exit__(self, - exc_type: Optional[Type[BaseException]], - exc_val: Optional[BaseException], - exc_tb: Optional[TracebackType]) -> None: - self.shutdown() - - def add_monitor_null(self) -> None: - """ - This can be used to add an unused monitor instance. - """ - self._args.append('-monitor') - self._args.append('null') - - def add_fd(self, fd: int, fdset: int, - opaque: str, opts: str = '') -> 'QEMUMachine': - """ - Pass a file descriptor to the VM - """ - options = ['fd=%d' % fd, - 'set=%d' % fdset, - 'opaque=%s' % opaque] - if opts: - options.append(opts) - - # This did not exist before 3.4, but since then it is - # mandatory for our purpose - if hasattr(os, 'set_inheritable'): - os.set_inheritable(fd, True) - - self._args.append('-add-fd') - self._args.append(','.join(options)) - return self - - def send_fd_scm(self, fd: Optional[int] = None, - file_path: Optional[str] = None) -> int: - """ - Send an fd or file_path to socket_scm_helper. - - Exactly one of fd and file_path must be given. - If it is file_path, the helper will open that file and pass its own fd. - """ - # In iotest.py, the qmp should always use unix socket. - assert self._qmp.is_scm_available() - if self._socket_scm_helper is None: - raise QEMUMachineError("No path to socket_scm_helper set") - if not os.path.exists(self._socket_scm_helper): - raise QEMUMachineError("%s does not exist" % - self._socket_scm_helper) - - # This did not exist before 3.4, but since then it is - # mandatory for our purpose - if hasattr(os, 'set_inheritable'): - os.set_inheritable(self._qmp.get_sock_fd(), True) - if fd is not None: - os.set_inheritable(fd, True) - - fd_param = ["%s" % self._socket_scm_helper, - "%d" % self._qmp.get_sock_fd()] - - if file_path is not None: - assert fd is None - fd_param.append(file_path) - else: - assert fd is not None - fd_param.append(str(fd)) - - proc = subprocess.run( - fd_param, - stdin=subprocess.DEVNULL, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, - check=False, - close_fds=False, - ) - if proc.stdout: - LOG.debug(proc.stdout) - - return proc.returncode - - @staticmethod - def _remove_if_exists(path: str) -> None: - """ - Remove file object at path if it exists - """ - try: - os.remove(path) - except OSError as exception: - if exception.errno == errno.ENOENT: - return - raise - - def is_running(self) -> bool: - """Returns true if the VM is running.""" - return self._popen is not None and self._popen.poll() is None - - @property - def _subp(self) -> 'subprocess.Popen[bytes]': - if self._popen is None: - raise QEMUMachineError('Subprocess pipe not present') - return self._popen - - def exitcode(self) -> Optional[int]: - """Returns the exit code if possible, or None.""" - if self._popen is None: - return None - return self._popen.poll() - - def get_pid(self) -> Optional[int]: - """Returns the PID of the running process, or None.""" - if not self.is_running(): - return None - return self._subp.pid - - def _load_io_log(self) -> None: - if self._qemu_log_path is not None: - with open(self._qemu_log_path, "r") as iolog: - self._iolog = iolog.read() - - @property - def _base_args(self) -> List[str]: - args = ['-display', 'none', '-vga', 'none'] - - if self._qmp_set: - if isinstance(self._monitor_address, tuple): - moncdev = "socket,id=mon,host={},port={}".format( - *self._monitor_address - ) - else: - moncdev = f"socket,id=mon,path={self._monitor_address}" - args.extend(['-chardev', moncdev, '-mon', - 'chardev=mon,mode=control']) - - if self._machine is not None: - args.extend(['-machine', self._machine]) - for _ in range(self._console_index): - args.extend(['-serial', 'null']) - if self._console_set: - chardev = ('socket,id=console,path=%s,server=on,wait=off' % - self._console_address) - args.extend(['-chardev', chardev]) - if self._console_device_type is None: - args.extend(['-serial', 'chardev:console']) - else: - device = '%s,chardev=console' % self._console_device_type - args.extend(['-device', device]) - return args - - def _pre_launch(self) -> None: - self._qemu_log_path = os.path.join(self.temp_dir, self._name + ".log") - - if self._console_set: - self._remove_files.append(self._console_address) - - if self._qmp_set: - if self._remove_monitor_sockfile: - assert isinstance(self._monitor_address, str) - self._remove_files.append(self._monitor_address) - self._qmp_connection = qmp.QEMUMonitorProtocol( - self._monitor_address, - server=True, - nickname=self._name - ) - - # NOTE: Make sure any opened resources are *definitely* freed in - # _post_shutdown()! - # pylint: disable=consider-using-with - self._qemu_log_file = open(self._qemu_log_path, 'wb') - - def _post_launch(self) -> None: - if self._qmp_connection: - self._qmp.accept() - - def _post_shutdown(self) -> None: - """ - Called to cleanup the VM instance after the process has exited. - May also be called after a failed launch. - """ - # Comprehensive reset for the failed launch case: - self._early_cleanup() - - if self._qmp_connection: - self._qmp.close() - self._qmp_connection = None - - if self._qemu_log_file is not None: - self._qemu_log_file.close() - self._qemu_log_file = None - - self._load_io_log() - - self._qemu_log_path = None - - if self._temp_dir is not None: - shutil.rmtree(self._temp_dir) - self._temp_dir = None - - while len(self._remove_files) > 0: - self._remove_if_exists(self._remove_files.pop()) - - exitcode = self.exitcode() - if (exitcode is not None and exitcode < 0 - and not (self._user_killed and exitcode == -signal.SIGKILL)): - msg = 'qemu received signal %i; command: "%s"' - if self._qemu_full_args: - command = ' '.join(self._qemu_full_args) - else: - command = '' - LOG.warning(msg, -int(exitcode), command) - - self._user_killed = False - self._launched = False - - def launch(self) -> None: - """ - Launch the VM and make sure we cleanup and expose the - command line/output in case of exception - """ - - if self._launched: - raise QEMUMachineError('VM already launched') - - self._iolog = None - self._qemu_full_args = () - try: - self._launch() - self._launched = True - except: - self._post_shutdown() - - LOG.debug('Error launching VM') - if self._qemu_full_args: - LOG.debug('Command: %r', ' '.join(self._qemu_full_args)) - if self._iolog: - LOG.debug('Output: %r', self._iolog) - raise - - def _launch(self) -> None: - """ - Launch the VM and establish a QMP connection - """ - self._pre_launch() - self._qemu_full_args = tuple( - chain(self._wrapper, - [self._binary], - self._base_args, - self._args) - ) - LOG.debug('VM launch command: %r', ' '.join(self._qemu_full_args)) - - # Cleaning up of this subprocess is guaranteed by _do_shutdown. - # pylint: disable=consider-using-with - self._popen = subprocess.Popen(self._qemu_full_args, - stdin=subprocess.DEVNULL, - stdout=self._qemu_log_file, - stderr=subprocess.STDOUT, - shell=False, - close_fds=False) - self._post_launch() - - def _early_cleanup(self) -> None: - """ - Perform any cleanup that needs to happen before the VM exits. - - May be invoked by both soft and hard shutdown in failover scenarios. - Called additionally by _post_shutdown for comprehensive cleanup. - """ - # If we keep the console socket open, we may deadlock waiting - # for QEMU to exit, while QEMU is waiting for the socket to - # become writeable. - if self._console_socket is not None: - self._console_socket.close() - self._console_socket = None - - def _hard_shutdown(self) -> None: - """ - Perform early cleanup, kill the VM, and wait for it to terminate. - - :raise subprocess.Timeout: When timeout is exceeds 60 seconds - waiting for the QEMU process to terminate. - """ - self._early_cleanup() - self._subp.kill() - self._subp.wait(timeout=60) - - def _soft_shutdown(self, timeout: Optional[int], - has_quit: bool = False) -> None: - """ - Perform early cleanup, attempt to gracefully shut down the VM, and wait - for it to terminate. - - :param timeout: Timeout in seconds for graceful shutdown. - A value of None is an infinite wait. - :param has_quit: When True, don't attempt to issue 'quit' QMP command - - :raise ConnectionReset: On QMP communication errors - :raise subprocess.TimeoutExpired: When timeout is exceeded waiting for - the QEMU process to terminate. - """ - self._early_cleanup() - - if self._qmp_connection: - if not has_quit: - # Might raise ConnectionReset - self._qmp.cmd('quit') - - # May raise subprocess.TimeoutExpired - self._subp.wait(timeout=timeout) - - def _do_shutdown(self, timeout: Optional[int], - has_quit: bool = False) -> None: - """ - Attempt to shutdown the VM gracefully; fallback to a hard shutdown. - - :param timeout: Timeout in seconds for graceful shutdown. - A value of None is an infinite wait. - :param has_quit: When True, don't attempt to issue 'quit' QMP command - - :raise AbnormalShutdown: When the VM could not be shut down gracefully. - The inner exception will likely be ConnectionReset or - subprocess.TimeoutExpired. In rare cases, non-graceful termination - may result in its own exceptions, likely subprocess.TimeoutExpired. - """ - try: - self._soft_shutdown(timeout, has_quit) - except Exception as exc: - self._hard_shutdown() - raise AbnormalShutdown("Could not perform graceful shutdown") \ - from exc - - def shutdown(self, has_quit: bool = False, - hard: bool = False, - timeout: Optional[int] = 30) -> None: - """ - Terminate the VM (gracefully if possible) and perform cleanup. - Cleanup will always be performed. - - If the VM has not yet been launched, or shutdown(), wait(), or kill() - have already been called, this method does nothing. - - :param has_quit: When true, do not attempt to issue 'quit' QMP command. - :param hard: When true, do not attempt graceful shutdown, and - suppress the SIGKILL warning log message. - :param timeout: Optional timeout in seconds for graceful shutdown. - Default 30 seconds, A `None` value is an infinite wait. - """ - if not self._launched: - return - - try: - if hard: - self._user_killed = True - self._hard_shutdown() - else: - self._do_shutdown(timeout, has_quit) - finally: - self._post_shutdown() - - def kill(self) -> None: - """ - Terminate the VM forcefully, wait for it to exit, and perform cleanup. - """ - self.shutdown(hard=True) - - def wait(self, timeout: Optional[int] = 30) -> None: - """ - Wait for the VM to power off and perform post-shutdown cleanup. - - :param timeout: Optional timeout in seconds. Default 30 seconds. - A value of `None` is an infinite wait. - """ - self.shutdown(has_quit=True, timeout=timeout) - - def set_qmp_monitor(self, enabled: bool = True) -> None: - """ - Set the QMP monitor. - - @param enabled: if False, qmp monitor options will be removed from - the base arguments of the resulting QEMU command - line. Default is True. - @note: call this function before launch(). - """ - self._qmp_set = enabled - - @property - def _qmp(self) -> qmp.QEMUMonitorProtocol: - if self._qmp_connection is None: - raise QEMUMachineError("Attempt to access QMP with no connection") - return self._qmp_connection - - @classmethod - def _qmp_args(cls, _conv_keys: bool = True, **args: Any) -> Dict[str, Any]: - qmp_args = dict() - for key, value in args.items(): - if _conv_keys: - qmp_args[key.replace('_', '-')] = value - else: - qmp_args[key] = value - return qmp_args - - def qmp(self, cmd: str, - conv_keys: bool = True, - **args: Any) -> QMPMessage: - """ - Invoke a QMP command and return the response dict - """ - qmp_args = self._qmp_args(conv_keys, **args) - return self._qmp.cmd(cmd, args=qmp_args) - - def command(self, cmd: str, - conv_keys: bool = True, - **args: Any) -> QMPReturnValue: - """ - Invoke a QMP command. - On success return the response dict. - On failure raise an exception. - """ - qmp_args = self._qmp_args(conv_keys, **args) - return self._qmp.command(cmd, **qmp_args) - - def get_qmp_event(self, wait: bool = False) -> Optional[QMPMessage]: - """ - Poll for one queued QMP events and return it - """ - if self._events: - return self._events.pop(0) - return self._qmp.pull_event(wait=wait) - - def get_qmp_events(self, wait: bool = False) -> List[QMPMessage]: - """ - Poll for queued QMP events and return a list of dicts - """ - events = self._qmp.get_events(wait=wait) - events.extend(self._events) - del self._events[:] - self._qmp.clear_events() - return events - - @staticmethod - def event_match(event: Any, match: Optional[Any]) -> bool: - """ - Check if an event matches optional match criteria. - - The match criteria takes the form of a matching subdict. The event is - checked to be a superset of the subdict, recursively, with matching - values whenever the subdict values are not None. - - This has a limitation that you cannot explicitly check for None values. - - Examples, with the subdict queries on the left: - - None matches any object. - - {"foo": None} matches {"foo": {"bar": 1}} - - {"foo": None} matches {"foo": 5} - - {"foo": {"abc": None}} does not match {"foo": {"bar": 1}} - - {"foo": {"rab": 2}} matches {"foo": {"bar": 1, "rab": 2}} - """ - if match is None: - return True - - try: - for key in match: - if key in event: - if not QEMUMachine.event_match(event[key], match[key]): - return False - else: - return False - return True - except TypeError: - # either match or event wasn't iterable (not a dict) - return bool(match == event) - - def event_wait(self, name: str, - timeout: float = 60.0, - match: Optional[QMPMessage] = None) -> Optional[QMPMessage]: - """ - event_wait waits for and returns a named event from QMP with a timeout. - - name: The event to wait for. - timeout: QEMUMonitorProtocol.pull_event timeout parameter. - match: Optional match criteria. See event_match for details. - """ - return self.events_wait([(name, match)], timeout) - - def events_wait(self, - events: Sequence[Tuple[str, Any]], - timeout: float = 60.0) -> Optional[QMPMessage]: - """ - events_wait waits for and returns a single named event from QMP. - In the case of multiple qualifying events, this function returns the - first one. - - :param events: A sequence of (name, match_criteria) tuples. - The match criteria are optional and may be None. - See event_match for details. - :param timeout: Optional timeout, in seconds. - See QEMUMonitorProtocol.pull_event. - - :raise QMPTimeoutError: If timeout was non-zero and no matching events - were found. - :return: A QMP event matching the filter criteria. - If timeout was 0 and no event matched, None. - """ - def _match(event: QMPMessage) -> bool: - for name, match in events: - if event['event'] == name and self.event_match(event, match): - return True - return False - - event: Optional[QMPMessage] - - # Search cached events - for event in self._events: - if _match(event): - self._events.remove(event) - return event - - # Poll for new events - while True: - event = self._qmp.pull_event(wait=timeout) - if event is None: - # NB: None is only returned when timeout is false-ish. - # Timeouts raise QMPTimeoutError instead! - break - if _match(event): - return event - self._events.append(event) - - return None - - def get_log(self) -> Optional[str]: - """ - After self.shutdown or failed qemu execution, this returns the output - of the qemu process. - """ - return self._iolog - - def add_args(self, *args: str) -> None: - """ - Adds to the list of extra arguments to be given to the QEMU binary - """ - self._args.extend(args) - - def set_machine(self, machine_type: str) -> None: - """ - Sets the machine type - - If set, the machine type will be added to the base arguments - of the resulting QEMU command line. - """ - self._machine = machine_type - - def set_console(self, - device_type: Optional[str] = None, - console_index: int = 0) -> None: - """ - Sets the device type for a console device - - If set, the console device and a backing character device will - be added to the base arguments of the resulting QEMU command - line. - - This is a convenience method that will either use the provided - device type, or default to a "-serial chardev:console" command - line argument. - - The actual setting of command line arguments will be be done at - machine launch time, as it depends on the temporary directory - to be created. - - @param device_type: the device type, such as "isa-serial". If - None is given (the default value) a "-serial - chardev:console" command line argument will - be used instead, resorting to the machine's - default device type. - @param console_index: the index of the console device to use. - If not zero, the command line will create - 'index - 1' consoles and connect them to - the 'null' backing character device. - """ - self._console_set = True - self._console_device_type = device_type - self._console_index = console_index - - @property - def console_socket(self) -> socket.socket: - """ - Returns a socket connected to the console - """ - if self._console_socket is None: - self._console_socket = console_socket.ConsoleSocket( - self._console_address, - file=self._console_log_path, - drain=self._drain_console) - return self._console_socket - - @property - def temp_dir(self) -> str: - """ - Returns a temporary directory to be used for this machine - """ - if self._temp_dir is None: - self._temp_dir = tempfile.mkdtemp(prefix="qemu-machine-", - dir=self._base_temp_dir) - return self._temp_dir diff --git a/python/qemu/machine/.flake8 b/python/qemu/machine/.flake8 new file mode 100644 index 0000000..45d8146 --- /dev/null +++ b/python/qemu/machine/.flake8 @@ -0,0 +1,2 @@ +[flake8] +extend-ignore = E722 # Pylint handles this, but smarter. \ No newline at end of file diff --git a/python/qemu/machine/__init__.py b/python/qemu/machine/__init__.py new file mode 100644 index 0000000..98302ea --- /dev/null +++ b/python/qemu/machine/__init__.py @@ -0,0 +1,33 @@ +""" +QEMU development and testing library. + +This library provides a few high-level classes for driving QEMU from a +test suite, not intended for production use. + +- QEMUMachine: Configure and Boot a QEMU VM + - QEMUQtestMachine: VM class, with a qtest socket. + +- QEMUQtestProtocol: Connect to, send/receive qtest messages. +""" + +# Copyright (C) 2020-2021 John Snow for Red Hat Inc. +# Copyright (C) 2015-2016 Red Hat Inc. +# Copyright (C) 2012 IBM Corp. +# +# Authors: +# John Snow +# Fam Zheng +# +# This work is licensed under the terms of the GNU GPL, version 2. See +# the COPYING file in the top-level directory. +# + +from .machine import QEMUMachine +from .qtest import QEMUQtestMachine, QEMUQtestProtocol + + +__all__ = ( + 'QEMUMachine', + 'QEMUQtestProtocol', + 'QEMUQtestMachine', +) diff --git a/python/qemu/machine/console_socket.py b/python/qemu/machine/console_socket.py new file mode 100644 index 0000000..8c4ff59 --- /dev/null +++ b/python/qemu/machine/console_socket.py @@ -0,0 +1,129 @@ +""" +QEMU Console Socket Module: + +This python module implements a ConsoleSocket object, +which can drain a socket and optionally dump the bytes to file. +""" +# Copyright 2020 Linaro +# +# Authors: +# Robert Foley +# +# This code is licensed under the GPL version 2 or later. See +# the COPYING file in the top-level directory. +# + +from collections import deque +import socket +import threading +import time +from typing import Deque, Optional + + +class ConsoleSocket(socket.socket): + """ + ConsoleSocket represents a socket attached to a char device. + + Optionally (if drain==True), drains the socket and places the bytes + into an in memory buffer for later processing. + + Optionally a file path can be passed in and we will also + dump the characters to this file for debugging purposes. + """ + def __init__(self, address: str, file: Optional[str] = None, + drain: bool = False): + self._recv_timeout_sec = 300.0 + self._sleep_time = 0.5 + self._buffer: Deque[int] = deque() + socket.socket.__init__(self, socket.AF_UNIX, socket.SOCK_STREAM) + self.connect(address) + self._logfile = None + if file: + # pylint: disable=consider-using-with + self._logfile = open(file, "bw") + self._open = True + self._drain_thread = None + if drain: + self._drain_thread = self._thread_start() + + def __repr__(self) -> str: + tmp = super().__repr__() + tmp = tmp.rstrip(">") + tmp = "%s, logfile=%s, drain_thread=%s>" % (tmp, self._logfile, + self._drain_thread) + return tmp + + def _drain_fn(self) -> None: + """Drains the socket and runs while the socket is open.""" + while self._open: + try: + self._drain_socket() + except socket.timeout: + # The socket is expected to timeout since we set a + # short timeout to allow the thread to exit when + # self._open is set to False. + time.sleep(self._sleep_time) + + def _thread_start(self) -> threading.Thread: + """Kick off a thread to drain the socket.""" + # Configure socket to not block and timeout. + # This allows our drain thread to not block + # on recieve and exit smoothly. + socket.socket.setblocking(self, False) + socket.socket.settimeout(self, 1) + drain_thread = threading.Thread(target=self._drain_fn) + drain_thread.daemon = True + drain_thread.start() + return drain_thread + + def close(self) -> None: + """Close the base object and wait for the thread to terminate""" + if self._open: + self._open = False + if self._drain_thread is not None: + thread, self._drain_thread = self._drain_thread, None + thread.join() + socket.socket.close(self) + if self._logfile: + self._logfile.close() + self._logfile = None + + def _drain_socket(self) -> None: + """process arriving characters into in memory _buffer""" + data = socket.socket.recv(self, 1) + if self._logfile: + self._logfile.write(data) + self._logfile.flush() + self._buffer.extend(data) + + def recv(self, bufsize: int = 1, flags: int = 0) -> bytes: + """Return chars from in memory buffer. + Maintains the same API as socket.socket.recv. + """ + if self._drain_thread is None: + # Not buffering the socket, pass thru to socket. + return socket.socket.recv(self, bufsize, flags) + assert not flags, "Cannot pass flags to recv() in drained mode" + start_time = time.time() + while len(self._buffer) < bufsize: + time.sleep(self._sleep_time) + elapsed_sec = time.time() - start_time + if elapsed_sec > self._recv_timeout_sec: + raise socket.timeout + return bytes((self._buffer.popleft() for i in range(bufsize))) + + def setblocking(self, value: bool) -> None: + """When not draining we pass thru to the socket, + since when draining we control socket blocking. + """ + if self._drain_thread is None: + socket.socket.setblocking(self, value) + + def settimeout(self, value: Optional[float]) -> None: + """When not draining we pass thru to the socket, + since when draining we control the timeout. + """ + if value is not None: + self._recv_timeout_sec = value + if self._drain_thread is None: + socket.socket.settimeout(self, value) diff --git a/python/qemu/machine/machine.py b/python/qemu/machine/machine.py new file mode 100644 index 0000000..d33b02d --- /dev/null +++ b/python/qemu/machine/machine.py @@ -0,0 +1,768 @@ +""" +QEMU machine module: + +The machine module primarily provides the QEMUMachine class, +which provides facilities for managing the lifetime of a QEMU VM. +""" + +# Copyright (C) 2015-2016 Red Hat Inc. +# Copyright (C) 2012 IBM Corp. +# +# Authors: +# Fam Zheng +# +# This work is licensed under the terms of the GNU GPL, version 2. See +# the COPYING file in the top-level directory. +# +# Based on qmp.py. +# + +import errno +from itertools import chain +import logging +import os +import shutil +import signal +import socket +import subprocess +import tempfile +from types import TracebackType +from typing import ( + Any, + BinaryIO, + Dict, + List, + Optional, + Sequence, + Tuple, + Type, +) + +from qemu.qmp import ( + QEMUMonitorProtocol, + QMPMessage, + QMPReturnValue, + SocketAddrT, +) + +from . import console_socket + + +LOG = logging.getLogger(__name__) + + +class QEMUMachineError(Exception): + """ + Exception called when an error in QEMUMachine happens. + """ + + +class QEMUMachineAddDeviceError(QEMUMachineError): + """ + Exception raised when a request to add a device can not be fulfilled + + The failures are caused by limitations, lack of information or conflicting + requests on the QEMUMachine methods. This exception does not represent + failures reported by the QEMU binary itself. + """ + + +class AbnormalShutdown(QEMUMachineError): + """ + Exception raised when a graceful shutdown was requested, but not performed. + """ + + +class QEMUMachine: + """ + A QEMU VM. + + Use this object as a context manager to ensure + the QEMU process terminates:: + + with VM(binary) as vm: + ... + # vm is guaranteed to be shut down here + """ + + def __init__(self, + binary: str, + args: Sequence[str] = (), + wrapper: Sequence[str] = (), + name: Optional[str] = None, + base_temp_dir: str = "/var/tmp", + monitor_address: Optional[SocketAddrT] = None, + socket_scm_helper: Optional[str] = None, + sock_dir: Optional[str] = None, + drain_console: bool = False, + console_log: Optional[str] = None): + ''' + Initialize a QEMUMachine + + @param binary: path to the qemu binary + @param args: list of extra arguments + @param wrapper: list of arguments used as prefix to qemu binary + @param name: prefix for socket and log file names (default: qemu-PID) + @param base_temp_dir: default location where temp files are created + @param monitor_address: address for QMP monitor + @param socket_scm_helper: helper program, required for send_fd_scm() + @param sock_dir: where to create socket (defaults to base_temp_dir) + @param drain_console: (optional) True to drain console socket to buffer + @param console_log: (optional) path to console log file + @note: Qemu process is not started until launch() is used. + ''' + # Direct user configuration + + self._binary = binary + self._args = list(args) + self._wrapper = wrapper + + self._name = name or "qemu-%d" % os.getpid() + self._base_temp_dir = base_temp_dir + self._sock_dir = sock_dir or self._base_temp_dir + self._socket_scm_helper = socket_scm_helper + + if monitor_address is not None: + self._monitor_address = monitor_address + self._remove_monitor_sockfile = False + else: + self._monitor_address = os.path.join( + self._sock_dir, f"{self._name}-monitor.sock" + ) + self._remove_monitor_sockfile = True + + self._console_log_path = console_log + if self._console_log_path: + # In order to log the console, buffering needs to be enabled. + self._drain_console = True + else: + self._drain_console = drain_console + + # Runstate + self._qemu_log_path: Optional[str] = None + self._qemu_log_file: Optional[BinaryIO] = None + self._popen: Optional['subprocess.Popen[bytes]'] = None + self._events: List[QMPMessage] = [] + self._iolog: Optional[str] = None + self._qmp_set = True # Enable QMP monitor by default. + self._qmp_connection: Optional[QEMUMonitorProtocol] = None + self._qemu_full_args: Tuple[str, ...] = () + self._temp_dir: Optional[str] = None + self._launched = False + self._machine: Optional[str] = None + self._console_index = 0 + self._console_set = False + self._console_device_type: Optional[str] = None + self._console_address = os.path.join( + self._sock_dir, f"{self._name}-console.sock" + ) + self._console_socket: Optional[socket.socket] = None + self._remove_files: List[str] = [] + self._user_killed = False + + def __enter__(self) -> 'QEMUMachine': + return self + + def __exit__(self, + exc_type: Optional[Type[BaseException]], + exc_val: Optional[BaseException], + exc_tb: Optional[TracebackType]) -> None: + self.shutdown() + + def add_monitor_null(self) -> None: + """ + This can be used to add an unused monitor instance. + """ + self._args.append('-monitor') + self._args.append('null') + + def add_fd(self, fd: int, fdset: int, + opaque: str, opts: str = '') -> 'QEMUMachine': + """ + Pass a file descriptor to the VM + """ + options = ['fd=%d' % fd, + 'set=%d' % fdset, + 'opaque=%s' % opaque] + if opts: + options.append(opts) + + # This did not exist before 3.4, but since then it is + # mandatory for our purpose + if hasattr(os, 'set_inheritable'): + os.set_inheritable(fd, True) + + self._args.append('-add-fd') + self._args.append(','.join(options)) + return self + + def send_fd_scm(self, fd: Optional[int] = None, + file_path: Optional[str] = None) -> int: + """ + Send an fd or file_path to socket_scm_helper. + + Exactly one of fd and file_path must be given. + If it is file_path, the helper will open that file and pass its own fd. + """ + # In iotest.py, the qmp should always use unix socket. + assert self._qmp.is_scm_available() + if self._socket_scm_helper is None: + raise QEMUMachineError("No path to socket_scm_helper set") + if not os.path.exists(self._socket_scm_helper): + raise QEMUMachineError("%s does not exist" % + self._socket_scm_helper) + + # This did not exist before 3.4, but since then it is + # mandatory for our purpose + if hasattr(os, 'set_inheritable'): + os.set_inheritable(self._qmp.get_sock_fd(), True) + if fd is not None: + os.set_inheritable(fd, True) + + fd_param = ["%s" % self._socket_scm_helper, + "%d" % self._qmp.get_sock_fd()] + + if file_path is not None: + assert fd is None + fd_param.append(file_path) + else: + assert fd is not None + fd_param.append(str(fd)) + + proc = subprocess.run( + fd_param, + stdin=subprocess.DEVNULL, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + check=False, + close_fds=False, + ) + if proc.stdout: + LOG.debug(proc.stdout) + + return proc.returncode + + @staticmethod + def _remove_if_exists(path: str) -> None: + """ + Remove file object at path if it exists + """ + try: + os.remove(path) + except OSError as exception: + if exception.errno == errno.ENOENT: + return + raise + + def is_running(self) -> bool: + """Returns true if the VM is running.""" + return self._popen is not None and self._popen.poll() is None + + @property + def _subp(self) -> 'subprocess.Popen[bytes]': + if self._popen is None: + raise QEMUMachineError('Subprocess pipe not present') + return self._popen + + def exitcode(self) -> Optional[int]: + """Returns the exit code if possible, or None.""" + if self._popen is None: + return None + return self._popen.poll() + + def get_pid(self) -> Optional[int]: + """Returns the PID of the running process, or None.""" + if not self.is_running(): + return None + return self._subp.pid + + def _load_io_log(self) -> None: + if self._qemu_log_path is not None: + with open(self._qemu_log_path, "r") as iolog: + self._iolog = iolog.read() + + @property + def _base_args(self) -> List[str]: + args = ['-display', 'none', '-vga', 'none'] + + if self._qmp_set: + if isinstance(self._monitor_address, tuple): + moncdev = "socket,id=mon,host={},port={}".format( + *self._monitor_address + ) + else: + moncdev = f"socket,id=mon,path={self._monitor_address}" + args.extend(['-chardev', moncdev, '-mon', + 'chardev=mon,mode=control']) + + if self._machine is not None: + args.extend(['-machine', self._machine]) + for _ in range(self._console_index): + args.extend(['-serial', 'null']) + if self._console_set: + chardev = ('socket,id=console,path=%s,server=on,wait=off' % + self._console_address) + args.extend(['-chardev', chardev]) + if self._console_device_type is None: + args.extend(['-serial', 'chardev:console']) + else: + device = '%s,chardev=console' % self._console_device_type + args.extend(['-device', device]) + return args + + def _pre_launch(self) -> None: + self._qemu_log_path = os.path.join(self.temp_dir, self._name + ".log") + + if self._console_set: + self._remove_files.append(self._console_address) + + if self._qmp_set: + if self._remove_monitor_sockfile: + assert isinstance(self._monitor_address, str) + self._remove_files.append(self._monitor_address) + self._qmp_connection = QEMUMonitorProtocol( + self._monitor_address, + server=True, + nickname=self._name + ) + + # NOTE: Make sure any opened resources are *definitely* freed in + # _post_shutdown()! + # pylint: disable=consider-using-with + self._qemu_log_file = open(self._qemu_log_path, 'wb') + + def _post_launch(self) -> None: + if self._qmp_connection: + self._qmp.accept() + + def _post_shutdown(self) -> None: + """ + Called to cleanup the VM instance after the process has exited. + May also be called after a failed launch. + """ + # Comprehensive reset for the failed launch case: + self._early_cleanup() + + if self._qmp_connection: + self._qmp.close() + self._qmp_connection = None + + if self._qemu_log_file is not None: + self._qemu_log_file.close() + self._qemu_log_file = None + + self._load_io_log() + + self._qemu_log_path = None + + if self._temp_dir is not None: + shutil.rmtree(self._temp_dir) + self._temp_dir = None + + while len(self._remove_files) > 0: + self._remove_if_exists(self._remove_files.pop()) + + exitcode = self.exitcode() + if (exitcode is not None and exitcode < 0 + and not (self._user_killed and exitcode == -signal.SIGKILL)): + msg = 'qemu received signal %i; command: "%s"' + if self._qemu_full_args: + command = ' '.join(self._qemu_full_args) + else: + command = '' + LOG.warning(msg, -int(exitcode), command) + + self._user_killed = False + self._launched = False + + def launch(self) -> None: + """ + Launch the VM and make sure we cleanup and expose the + command line/output in case of exception + """ + + if self._launched: + raise QEMUMachineError('VM already launched') + + self._iolog = None + self._qemu_full_args = () + try: + self._launch() + self._launched = True + except: + self._post_shutdown() + + LOG.debug('Error launching VM') + if self._qemu_full_args: + LOG.debug('Command: %r', ' '.join(self._qemu_full_args)) + if self._iolog: + LOG.debug('Output: %r', self._iolog) + raise + + def _launch(self) -> None: + """ + Launch the VM and establish a QMP connection + """ + self._pre_launch() + self._qemu_full_args = tuple( + chain(self._wrapper, + [self._binary], + self._base_args, + self._args) + ) + LOG.debug('VM launch command: %r', ' '.join(self._qemu_full_args)) + + # Cleaning up of this subprocess is guaranteed by _do_shutdown. + # pylint: disable=consider-using-with + self._popen = subprocess.Popen(self._qemu_full_args, + stdin=subprocess.DEVNULL, + stdout=self._qemu_log_file, + stderr=subprocess.STDOUT, + shell=False, + close_fds=False) + self._post_launch() + + def _early_cleanup(self) -> None: + """ + Perform any cleanup that needs to happen before the VM exits. + + May be invoked by both soft and hard shutdown in failover scenarios. + Called additionally by _post_shutdown for comprehensive cleanup. + """ + # If we keep the console socket open, we may deadlock waiting + # for QEMU to exit, while QEMU is waiting for the socket to + # become writeable. + if self._console_socket is not None: + self._console_socket.close() + self._console_socket = None + + def _hard_shutdown(self) -> None: + """ + Perform early cleanup, kill the VM, and wait for it to terminate. + + :raise subprocess.Timeout: When timeout is exceeds 60 seconds + waiting for the QEMU process to terminate. + """ + self._early_cleanup() + self._subp.kill() + self._subp.wait(timeout=60) + + def _soft_shutdown(self, timeout: Optional[int], + has_quit: bool = False) -> None: + """ + Perform early cleanup, attempt to gracefully shut down the VM, and wait + for it to terminate. + + :param timeout: Timeout in seconds for graceful shutdown. + A value of None is an infinite wait. + :param has_quit: When True, don't attempt to issue 'quit' QMP command + + :raise ConnectionReset: On QMP communication errors + :raise subprocess.TimeoutExpired: When timeout is exceeded waiting for + the QEMU process to terminate. + """ + self._early_cleanup() + + if self._qmp_connection: + if not has_quit: + # Might raise ConnectionReset + self._qmp.cmd('quit') + + # May raise subprocess.TimeoutExpired + self._subp.wait(timeout=timeout) + + def _do_shutdown(self, timeout: Optional[int], + has_quit: bool = False) -> None: + """ + Attempt to shutdown the VM gracefully; fallback to a hard shutdown. + + :param timeout: Timeout in seconds for graceful shutdown. + A value of None is an infinite wait. + :param has_quit: When True, don't attempt to issue 'quit' QMP command + + :raise AbnormalShutdown: When the VM could not be shut down gracefully. + The inner exception will likely be ConnectionReset or + subprocess.TimeoutExpired. In rare cases, non-graceful termination + may result in its own exceptions, likely subprocess.TimeoutExpired. + """ + try: + self._soft_shutdown(timeout, has_quit) + except Exception as exc: + self._hard_shutdown() + raise AbnormalShutdown("Could not perform graceful shutdown") \ + from exc + + def shutdown(self, has_quit: bool = False, + hard: bool = False, + timeout: Optional[int] = 30) -> None: + """ + Terminate the VM (gracefully if possible) and perform cleanup. + Cleanup will always be performed. + + If the VM has not yet been launched, or shutdown(), wait(), or kill() + have already been called, this method does nothing. + + :param has_quit: When true, do not attempt to issue 'quit' QMP command. + :param hard: When true, do not attempt graceful shutdown, and + suppress the SIGKILL warning log message. + :param timeout: Optional timeout in seconds for graceful shutdown. + Default 30 seconds, A `None` value is an infinite wait. + """ + if not self._launched: + return + + try: + if hard: + self._user_killed = True + self._hard_shutdown() + else: + self._do_shutdown(timeout, has_quit) + finally: + self._post_shutdown() + + def kill(self) -> None: + """ + Terminate the VM forcefully, wait for it to exit, and perform cleanup. + """ + self.shutdown(hard=True) + + def wait(self, timeout: Optional[int] = 30) -> None: + """ + Wait for the VM to power off and perform post-shutdown cleanup. + + :param timeout: Optional timeout in seconds. Default 30 seconds. + A value of `None` is an infinite wait. + """ + self.shutdown(has_quit=True, timeout=timeout) + + def set_qmp_monitor(self, enabled: bool = True) -> None: + """ + Set the QMP monitor. + + @param enabled: if False, qmp monitor options will be removed from + the base arguments of the resulting QEMU command + line. Default is True. + @note: call this function before launch(). + """ + self._qmp_set = enabled + + @property + def _qmp(self) -> QEMUMonitorProtocol: + if self._qmp_connection is None: + raise QEMUMachineError("Attempt to access QMP with no connection") + return self._qmp_connection + + @classmethod + def _qmp_args(cls, _conv_keys: bool = True, **args: Any) -> Dict[str, Any]: + qmp_args = dict() + for key, value in args.items(): + if _conv_keys: + qmp_args[key.replace('_', '-')] = value + else: + qmp_args[key] = value + return qmp_args + + def qmp(self, cmd: str, + conv_keys: bool = True, + **args: Any) -> QMPMessage: + """ + Invoke a QMP command and return the response dict + """ + qmp_args = self._qmp_args(conv_keys, **args) + return self._qmp.cmd(cmd, args=qmp_args) + + def command(self, cmd: str, + conv_keys: bool = True, + **args: Any) -> QMPReturnValue: + """ + Invoke a QMP command. + On success return the response dict. + On failure raise an exception. + """ + qmp_args = self._qmp_args(conv_keys, **args) + return self._qmp.command(cmd, **qmp_args) + + def get_qmp_event(self, wait: bool = False) -> Optional[QMPMessage]: + """ + Poll for one queued QMP events and return it + """ + if self._events: + return self._events.pop(0) + return self._qmp.pull_event(wait=wait) + + def get_qmp_events(self, wait: bool = False) -> List[QMPMessage]: + """ + Poll for queued QMP events and return a list of dicts + """ + events = self._qmp.get_events(wait=wait) + events.extend(self._events) + del self._events[:] + self._qmp.clear_events() + return events + + @staticmethod + def event_match(event: Any, match: Optional[Any]) -> bool: + """ + Check if an event matches optional match criteria. + + The match criteria takes the form of a matching subdict. The event is + checked to be a superset of the subdict, recursively, with matching + values whenever the subdict values are not None. + + This has a limitation that you cannot explicitly check for None values. + + Examples, with the subdict queries on the left: + - None matches any object. + - {"foo": None} matches {"foo": {"bar": 1}} + - {"foo": None} matches {"foo": 5} + - {"foo": {"abc": None}} does not match {"foo": {"bar": 1}} + - {"foo": {"rab": 2}} matches {"foo": {"bar": 1, "rab": 2}} + """ + if match is None: + return True + + try: + for key in match: + if key in event: + if not QEMUMachine.event_match(event[key], match[key]): + return False + else: + return False + return True + except TypeError: + # either match or event wasn't iterable (not a dict) + return bool(match == event) + + def event_wait(self, name: str, + timeout: float = 60.0, + match: Optional[QMPMessage] = None) -> Optional[QMPMessage]: + """ + event_wait waits for and returns a named event from QMP with a timeout. + + name: The event to wait for. + timeout: QEMUMonitorProtocol.pull_event timeout parameter. + match: Optional match criteria. See event_match for details. + """ + return self.events_wait([(name, match)], timeout) + + def events_wait(self, + events: Sequence[Tuple[str, Any]], + timeout: float = 60.0) -> Optional[QMPMessage]: + """ + events_wait waits for and returns a single named event from QMP. + In the case of multiple qualifying events, this function returns the + first one. + + :param events: A sequence of (name, match_criteria) tuples. + The match criteria are optional and may be None. + See event_match for details. + :param timeout: Optional timeout, in seconds. + See QEMUMonitorProtocol.pull_event. + + :raise QMPTimeoutError: If timeout was non-zero and no matching events + were found. + :return: A QMP event matching the filter criteria. + If timeout was 0 and no event matched, None. + """ + def _match(event: QMPMessage) -> bool: + for name, match in events: + if event['event'] == name and self.event_match(event, match): + return True + return False + + event: Optional[QMPMessage] + + # Search cached events + for event in self._events: + if _match(event): + self._events.remove(event) + return event + + # Poll for new events + while True: + event = self._qmp.pull_event(wait=timeout) + if event is None: + # NB: None is only returned when timeout is false-ish. + # Timeouts raise QMPTimeoutError instead! + break + if _match(event): + return event + self._events.append(event) + + return None + + def get_log(self) -> Optional[str]: + """ + After self.shutdown or failed qemu execution, this returns the output + of the qemu process. + """ + return self._iolog + + def add_args(self, *args: str) -> None: + """ + Adds to the list of extra arguments to be given to the QEMU binary + """ + self._args.extend(args) + + def set_machine(self, machine_type: str) -> None: + """ + Sets the machine type + + If set, the machine type will be added to the base arguments + of the resulting QEMU command line. + """ + self._machine = machine_type + + def set_console(self, + device_type: Optional[str] = None, + console_index: int = 0) -> None: + """ + Sets the device type for a console device + + If set, the console device and a backing character device will + be added to the base arguments of the resulting QEMU command + line. + + This is a convenience method that will either use the provided + device type, or default to a "-serial chardev:console" command + line argument. + + The actual setting of command line arguments will be be done at + machine launch time, as it depends on the temporary directory + to be created. + + @param device_type: the device type, such as "isa-serial". If + None is given (the default value) a "-serial + chardev:console" command line argument will + be used instead, resorting to the machine's + default device type. + @param console_index: the index of the console device to use. + If not zero, the command line will create + 'index - 1' consoles and connect them to + the 'null' backing character device. + """ + self._console_set = True + self._console_device_type = device_type + self._console_index = console_index + + @property + def console_socket(self) -> socket.socket: + """ + Returns a socket connected to the console + """ + if self._console_socket is None: + self._console_socket = console_socket.ConsoleSocket( + self._console_address, + file=self._console_log_path, + drain=self._drain_console) + return self._console_socket + + @property + def temp_dir(self) -> str: + """ + Returns a temporary directory to be used for this machine + """ + if self._temp_dir is None: + self._temp_dir = tempfile.mkdtemp(prefix="qemu-machine-", + dir=self._base_temp_dir) + return self._temp_dir diff --git a/python/qemu/machine/pylintrc b/python/qemu/machine/pylintrc new file mode 100644 index 0000000..3f69205 --- /dev/null +++ b/python/qemu/machine/pylintrc @@ -0,0 +1,58 @@ +[MASTER] + +[MESSAGES CONTROL] + +# Disable the message, report, category or checker with the given id(s). You +# can either give multiple identifiers separated by comma (,) or put this +# option multiple times (only on the command line, not in the configuration +# file where it should appear only once). You can also use "--disable=all" to +# disable everything first and then reenable specific checks. For example, if +# you want to run only the similarities checker, you can use "--disable=all +# --enable=similarities". If you want to run only the classes checker, but have +# no Warning level messages displayed, use "--disable=all --enable=classes +# --disable=W". +disable=too-many-arguments, + too-many-instance-attributes, + too-many-public-methods, + +[REPORTS] + +[REFACTORING] + +[MISCELLANEOUS] + +[LOGGING] + +[BASIC] + +# Good variable names which should always be accepted, separated by a comma. +good-names=i, + j, + k, + ex, + Run, + _, + fd, + c, +[VARIABLES] + +[STRING] + +[SPELLING] + +[FORMAT] + +[SIMILARITIES] + +# Ignore imports when computing similarities. +ignore-imports=yes + +[TYPECHECK] + +[CLASSES] + +[IMPORTS] + +[DESIGN] + +[EXCEPTIONS] diff --git a/python/qemu/machine/qtest.py b/python/qemu/machine/qtest.py new file mode 100644 index 0000000..e893ca3 --- /dev/null +++ b/python/qemu/machine/qtest.py @@ -0,0 +1,160 @@ +""" +QEMU qtest library + +qtest offers the QEMUQtestProtocol and QEMUQTestMachine classes, which +offer a connection to QEMU's qtest protocol socket, and a qtest-enabled +subclass of QEMUMachine, respectively. +""" + +# Copyright (C) 2015 Red Hat Inc. +# +# Authors: +# Fam Zheng +# +# This work is licensed under the terms of the GNU GPL, version 2. See +# the COPYING file in the top-level directory. +# +# Based on qmp.py. +# + +import os +import socket +from typing import ( + List, + Optional, + Sequence, + TextIO, +) + +from qemu.qmp import SocketAddrT + +from .machine import QEMUMachine + + +class QEMUQtestProtocol: + """ + QEMUQtestProtocol implements a connection to a qtest socket. + + :param address: QEMU address, can be either a unix socket path (string) + or a tuple in the form ( address, port ) for a TCP + connection + :param server: server mode, listens on the socket (bool) + :raise socket.error: on socket connection errors + + .. note:: + No conection is estabalished by __init__(), this is done + by the connect() or accept() methods. + """ + def __init__(self, address: SocketAddrT, + server: bool = False): + self._address = address + self._sock = self._get_sock() + self._sockfile: Optional[TextIO] = None + if server: + self._sock.bind(self._address) + self._sock.listen(1) + + def _get_sock(self) -> socket.socket: + if isinstance(self._address, tuple): + family = socket.AF_INET + else: + family = socket.AF_UNIX + return socket.socket(family, socket.SOCK_STREAM) + + def connect(self) -> None: + """ + Connect to the qtest socket. + + @raise socket.error on socket connection errors + """ + self._sock.connect(self._address) + self._sockfile = self._sock.makefile(mode='r') + + def accept(self) -> None: + """ + Await connection from QEMU. + + @raise socket.error on socket connection errors + """ + self._sock, _ = self._sock.accept() + self._sockfile = self._sock.makefile(mode='r') + + def cmd(self, qtest_cmd: str) -> str: + """ + Send a qtest command on the wire. + + @param qtest_cmd: qtest command text to be sent + """ + assert self._sockfile is not None + self._sock.sendall((qtest_cmd + "\n").encode('utf-8')) + resp = self._sockfile.readline() + return resp + + def close(self) -> None: + """ + Close this socket. + """ + self._sock.close() + if self._sockfile: + self._sockfile.close() + self._sockfile = None + + def settimeout(self, timeout: Optional[float]) -> None: + """Set a timeout, in seconds.""" + self._sock.settimeout(timeout) + + +class QEMUQtestMachine(QEMUMachine): + """ + A QEMU VM, with a qtest socket available. + """ + + def __init__(self, + binary: str, + args: Sequence[str] = (), + name: Optional[str] = None, + base_temp_dir: str = "/var/tmp", + socket_scm_helper: Optional[str] = None, + sock_dir: Optional[str] = None): + if name is None: + name = "qemu-%d" % os.getpid() + if sock_dir is None: + sock_dir = base_temp_dir + super().__init__(binary, args, name=name, base_temp_dir=base_temp_dir, + socket_scm_helper=socket_scm_helper, + sock_dir=sock_dir) + self._qtest: Optional[QEMUQtestProtocol] = None + self._qtest_path = os.path.join(sock_dir, name + "-qtest.sock") + + @property + def _base_args(self) -> List[str]: + args = super()._base_args + args.extend([ + '-qtest', f"unix:path={self._qtest_path}", + '-accel', 'qtest' + ]) + return args + + def _pre_launch(self) -> None: + super()._pre_launch() + self._qtest = QEMUQtestProtocol(self._qtest_path, server=True) + + def _post_launch(self) -> None: + assert self._qtest is not None + super()._post_launch() + self._qtest.accept() + + def _post_shutdown(self) -> None: + super()._post_shutdown() + self._remove_if_exists(self._qtest_path) + + def qtest(self, cmd: str) -> str: + """ + Send a qtest command to the guest. + + :param cmd: qtest command to send + :return: qtest server response + """ + if self._qtest is None: + raise RuntimeError("qtest socket not available") + return self._qtest.cmd(cmd) diff --git a/python/qemu/pylintrc b/python/qemu/pylintrc deleted file mode 100644 index 3f69205..0000000 --- a/python/qemu/pylintrc +++ /dev/null @@ -1,58 +0,0 @@ -[MASTER] - -[MESSAGES CONTROL] - -# Disable the message, report, category or checker with the given id(s). You -# can either give multiple identifiers separated by comma (,) or put this -# option multiple times (only on the command line, not in the configuration -# file where it should appear only once). You can also use "--disable=all" to -# disable everything first and then reenable specific checks. For example, if -# you want to run only the similarities checker, you can use "--disable=all -# --enable=similarities". If you want to run only the classes checker, but have -# no Warning level messages displayed, use "--disable=all --enable=classes -# --disable=W". -disable=too-many-arguments, - too-many-instance-attributes, - too-many-public-methods, - -[REPORTS] - -[REFACTORING] - -[MISCELLANEOUS] - -[LOGGING] - -[BASIC] - -# Good variable names which should always be accepted, separated by a comma. -good-names=i, - j, - k, - ex, - Run, - _, - fd, - c, -[VARIABLES] - -[STRING] - -[SPELLING] - -[FORMAT] - -[SIMILARITIES] - -# Ignore imports when computing similarities. -ignore-imports=yes - -[TYPECHECK] - -[CLASSES] - -[IMPORTS] - -[DESIGN] - -[EXCEPTIONS] diff --git a/python/qemu/qmp.py b/python/qemu/qmp.py deleted file mode 100644 index 2cd4d43..0000000 --- a/python/qemu/qmp.py +++ /dev/null @@ -1,375 +0,0 @@ -""" QEMU Monitor Protocol Python class """ -# Copyright (C) 2009, 2010 Red Hat Inc. -# -# Authors: -# Luiz Capitulino -# -# This work is licensed under the terms of the GNU GPL, version 2. See -# the COPYING file in the top-level directory. - -import errno -import json -import logging -import socket -from types import TracebackType -from typing import ( - Any, - Dict, - List, - Optional, - TextIO, - Tuple, - Type, - Union, - cast, -) - - -# QMPMessage is a QMP Message of any kind. -# e.g. {'yee': 'haw'} -# -# QMPReturnValue is the inner value of return values only. -# {'return': {}} is the QMPMessage, -# {} is the QMPReturnValue. -QMPMessage = Dict[str, Any] -QMPReturnValue = Dict[str, Any] - -InternetAddrT = Tuple[str, str] -UnixAddrT = str -SocketAddrT = Union[InternetAddrT, UnixAddrT] - - -class QMPError(Exception): - """ - QMP base exception - """ - - -class QMPConnectError(QMPError): - """ - QMP connection exception - """ - - -class QMPCapabilitiesError(QMPError): - """ - QMP negotiate capabilities exception - """ - - -class QMPTimeoutError(QMPError): - """ - QMP timeout exception - """ - - -class QMPProtocolError(QMPError): - """ - QMP protocol error; unexpected response - """ - - -class QMPResponseError(QMPError): - """ - Represents erroneous QMP monitor reply - """ - def __init__(self, reply: QMPMessage): - try: - desc = reply['error']['desc'] - except KeyError: - desc = reply - super().__init__(desc) - self.reply = reply - - -class QEMUMonitorProtocol: - """ - Provide an API to connect to QEMU via QEMU Monitor Protocol (QMP) and then - allow to handle commands and events. - """ - - #: Logger object for debugging messages - logger = logging.getLogger('QMP') - - def __init__(self, address: SocketAddrT, - server: bool = False, - nickname: Optional[str] = None): - """ - Create a QEMUMonitorProtocol class. - - @param address: QEMU address, can be either a unix socket path (string) - or a tuple in the form ( address, port ) for a TCP - connection - @param server: server mode listens on the socket (bool) - @raise OSError on socket connection errors - @note No connection is established, this is done by the connect() or - accept() methods - """ - self.__events: List[QMPMessage] = [] - self.__address = address - self.__sock = self.__get_sock() - self.__sockfile: Optional[TextIO] = None - self._nickname = nickname - if self._nickname: - self.logger = logging.getLogger('QMP').getChild(self._nickname) - if server: - self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - self.__sock.bind(self.__address) - self.__sock.listen(1) - - def __get_sock(self) -> socket.socket: - if isinstance(self.__address, tuple): - family = socket.AF_INET - else: - family = socket.AF_UNIX - return socket.socket(family, socket.SOCK_STREAM) - - def __negotiate_capabilities(self) -> QMPMessage: - greeting = self.__json_read() - if greeting is None or "QMP" not in greeting: - raise QMPConnectError - # Greeting seems ok, negotiate capabilities - resp = self.cmd('qmp_capabilities') - if resp and "return" in resp: - return greeting - raise QMPCapabilitiesError - - def __json_read(self, only_event: bool = False) -> Optional[QMPMessage]: - assert self.__sockfile is not None - while True: - data = self.__sockfile.readline() - if not data: - return None - # By definition, any JSON received from QMP is a QMPMessage, - # and we are asserting only at static analysis time that it - # has a particular shape. - resp: QMPMessage = json.loads(data) - if 'event' in resp: - self.logger.debug("<<< %s", resp) - self.__events.append(resp) - if not only_event: - continue - return resp - - def __get_events(self, wait: Union[bool, float] = False) -> None: - """ - Check for new events in the stream and cache them in __events. - - @param wait (bool): block until an event is available. - @param wait (float): If wait is a float, treat it as a timeout value. - - @raise QMPTimeoutError: If a timeout float is provided and the timeout - period elapses. - @raise QMPConnectError: If wait is True but no events could be - retrieved or if some other error occurred. - """ - - # Current timeout and blocking status - current_timeout = self.__sock.gettimeout() - - # Check for new events regardless and pull them into the cache: - self.__sock.settimeout(0) # i.e. setblocking(False) - try: - self.__json_read() - except OSError as err: - # EAGAIN: No data available; not critical - if err.errno != errno.EAGAIN: - raise - finally: - self.__sock.settimeout(current_timeout) - - # Wait for new events, if needed. - # if wait is 0.0, this means "no wait" and is also implicitly false. - if not self.__events and wait: - if isinstance(wait, float): - self.__sock.settimeout(wait) - try: - ret = self.__json_read(only_event=True) - except socket.timeout as err: - raise QMPTimeoutError("Timeout waiting for event") from err - except Exception as err: - msg = "Error while reading from socket" - raise QMPConnectError(msg) from err - finally: - self.__sock.settimeout(current_timeout) - - if ret is None: - raise QMPConnectError("Error while reading from socket") - - def __enter__(self) -> 'QEMUMonitorProtocol': - # Implement context manager enter function. - return self - - def __exit__(self, - # pylint: disable=duplicate-code - # see https://github.com/PyCQA/pylint/issues/3619 - exc_type: Optional[Type[BaseException]], - exc_val: Optional[BaseException], - exc_tb: Optional[TracebackType]) -> None: - # Implement context manager exit function. - self.close() - - def connect(self, negotiate: bool = True) -> Optional[QMPMessage]: - """ - Connect to the QMP Monitor and perform capabilities negotiation. - - @return QMP greeting dict, or None if negotiate is false - @raise OSError on socket connection errors - @raise QMPConnectError if the greeting is not received - @raise QMPCapabilitiesError if fails to negotiate capabilities - """ - self.__sock.connect(self.__address) - self.__sockfile = self.__sock.makefile(mode='r') - if negotiate: - return self.__negotiate_capabilities() - return None - - def accept(self, timeout: Optional[float] = 15.0) -> QMPMessage: - """ - Await connection from QMP Monitor and perform capabilities negotiation. - - @param timeout: timeout in seconds (nonnegative float number, or - None). The value passed will set the behavior of the - underneath QMP socket as described in [1]. - Default value is set to 15.0. - @return QMP greeting dict - @raise OSError on socket connection errors - @raise QMPConnectError if the greeting is not received - @raise QMPCapabilitiesError if fails to negotiate capabilities - - [1] - https://docs.python.org/3/library/socket.html#socket.socket.settimeout - """ - self.__sock.settimeout(timeout) - self.__sock, _ = self.__sock.accept() - self.__sockfile = self.__sock.makefile(mode='r') - return self.__negotiate_capabilities() - - def cmd_obj(self, qmp_cmd: QMPMessage) -> QMPMessage: - """ - Send a QMP command to the QMP Monitor. - - @param qmp_cmd: QMP command to be sent as a Python dict - @return QMP response as a Python dict - """ - self.logger.debug(">>> %s", qmp_cmd) - self.__sock.sendall(json.dumps(qmp_cmd).encode('utf-8')) - resp = self.__json_read() - if resp is None: - raise QMPConnectError("Unexpected empty reply from server") - self.logger.debug("<<< %s", resp) - return resp - - def cmd(self, name: str, - args: Optional[Dict[str, Any]] = None, - cmd_id: Optional[Any] = None) -> QMPMessage: - """ - Build a QMP command and send it to the QMP Monitor. - - @param name: command name (string) - @param args: command arguments (dict) - @param cmd_id: command id (dict, list, string or int) - """ - qmp_cmd: QMPMessage = {'execute': name} - if args: - qmp_cmd['arguments'] = args - if cmd_id: - qmp_cmd['id'] = cmd_id - return self.cmd_obj(qmp_cmd) - - def command(self, cmd: str, **kwds: Any) -> QMPReturnValue: - """ - Build and send a QMP command to the monitor, report errors if any - """ - ret = self.cmd(cmd, kwds) - if 'error' in ret: - raise QMPResponseError(ret) - if 'return' not in ret: - raise QMPProtocolError( - "'return' key not found in QMP response '{}'".format(str(ret)) - ) - return cast(QMPReturnValue, ret['return']) - - def pull_event(self, - wait: Union[bool, float] = False) -> Optional[QMPMessage]: - """ - Pulls a single event. - - @param wait (bool): block until an event is available. - @param wait (float): If wait is a float, treat it as a timeout value. - - @raise QMPTimeoutError: If a timeout float is provided and the timeout - period elapses. - @raise QMPConnectError: If wait is True but no events could be - retrieved or if some other error occurred. - - @return The first available QMP event, or None. - """ - self.__get_events(wait) - - if self.__events: - return self.__events.pop(0) - return None - - def get_events(self, wait: bool = False) -> List[QMPMessage]: - """ - Get a list of available QMP events. - - @param wait (bool): block until an event is available. - @param wait (float): If wait is a float, treat it as a timeout value. - - @raise QMPTimeoutError: If a timeout float is provided and the timeout - period elapses. - @raise QMPConnectError: If wait is True but no events could be - retrieved or if some other error occurred. - - @return The list of available QMP events. - """ - self.__get_events(wait) - return self.__events - - def clear_events(self) -> None: - """ - Clear current list of pending events. - """ - self.__events = [] - - def close(self) -> None: - """ - Close the socket and socket file. - """ - if self.__sock: - self.__sock.close() - if self.__sockfile: - self.__sockfile.close() - - def settimeout(self, timeout: Optional[float]) -> None: - """ - Set the socket timeout. - - @param timeout (float): timeout in seconds (non-zero), or None. - @note This is a wrap around socket.settimeout - - @raise ValueError: if timeout was set to 0. - """ - if timeout == 0: - msg = "timeout cannot be 0; this engages non-blocking mode." - msg += " Use 'None' instead to disable timeouts." - raise ValueError(msg) - self.__sock.settimeout(timeout) - - def get_sock_fd(self) -> int: - """ - Get the socket file descriptor. - - @return The file descriptor number. - """ - return self.__sock.fileno() - - def is_scm_available(self) -> bool: - """ - Check if the socket allows for SCM_RIGHTS. - - @return True if SCM_RIGHTS is available, otherwise False. - """ - return self.__sock.family == socket.AF_UNIX diff --git a/python/qemu/qmp/__init__.py b/python/qemu/qmp/__init__.py new file mode 100644 index 0000000..9606248 --- /dev/null +++ b/python/qemu/qmp/__init__.py @@ -0,0 +1,385 @@ +""" +QEMU Monitor Protocol (QMP) development library & tooling. + +This package provides a fairly low-level class for communicating to QMP +protocol servers, as implemented by QEMU, the QEMU Guest Agent, and the +QEMU Storage Daemon. This library is not intended for production use. + +`QEMUMonitorProtocol` is the primary class of interest, and all errors +raised derive from `QMPError`. +""" + +# Copyright (C) 2009, 2010 Red Hat Inc. +# +# Authors: +# Luiz Capitulino +# +# This work is licensed under the terms of the GNU GPL, version 2. See +# the COPYING file in the top-level directory. + +import errno +import json +import logging +import socket +from types import TracebackType +from typing import ( + Any, + Dict, + List, + Optional, + TextIO, + Tuple, + Type, + Union, + cast, +) + + +# QMPMessage is a QMP Message of any kind. +# e.g. {'yee': 'haw'} +# +# QMPReturnValue is the inner value of return values only. +# {'return': {}} is the QMPMessage, +# {} is the QMPReturnValue. +QMPMessage = Dict[str, Any] +QMPReturnValue = Dict[str, Any] + +InternetAddrT = Tuple[str, str] +UnixAddrT = str +SocketAddrT = Union[InternetAddrT, UnixAddrT] + + +class QMPError(Exception): + """ + QMP base exception + """ + + +class QMPConnectError(QMPError): + """ + QMP connection exception + """ + + +class QMPCapabilitiesError(QMPError): + """ + QMP negotiate capabilities exception + """ + + +class QMPTimeoutError(QMPError): + """ + QMP timeout exception + """ + + +class QMPProtocolError(QMPError): + """ + QMP protocol error; unexpected response + """ + + +class QMPResponseError(QMPError): + """ + Represents erroneous QMP monitor reply + """ + def __init__(self, reply: QMPMessage): + try: + desc = reply['error']['desc'] + except KeyError: + desc = reply + super().__init__(desc) + self.reply = reply + + +class QEMUMonitorProtocol: + """ + Provide an API to connect to QEMU via QEMU Monitor Protocol (QMP) and then + allow to handle commands and events. + """ + + #: Logger object for debugging messages + logger = logging.getLogger('QMP') + + def __init__(self, address: SocketAddrT, + server: bool = False, + nickname: Optional[str] = None): + """ + Create a QEMUMonitorProtocol class. + + @param address: QEMU address, can be either a unix socket path (string) + or a tuple in the form ( address, port ) for a TCP + connection + @param server: server mode listens on the socket (bool) + @raise OSError on socket connection errors + @note No connection is established, this is done by the connect() or + accept() methods + """ + self.__events: List[QMPMessage] = [] + self.__address = address + self.__sock = self.__get_sock() + self.__sockfile: Optional[TextIO] = None + self._nickname = nickname + if self._nickname: + self.logger = logging.getLogger('QMP').getChild(self._nickname) + if server: + self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + self.__sock.bind(self.__address) + self.__sock.listen(1) + + def __get_sock(self) -> socket.socket: + if isinstance(self.__address, tuple): + family = socket.AF_INET + else: + family = socket.AF_UNIX + return socket.socket(family, socket.SOCK_STREAM) + + def __negotiate_capabilities(self) -> QMPMessage: + greeting = self.__json_read() + if greeting is None or "QMP" not in greeting: + raise QMPConnectError + # Greeting seems ok, negotiate capabilities + resp = self.cmd('qmp_capabilities') + if resp and "return" in resp: + return greeting + raise QMPCapabilitiesError + + def __json_read(self, only_event: bool = False) -> Optional[QMPMessage]: + assert self.__sockfile is not None + while True: + data = self.__sockfile.readline() + if not data: + return None + # By definition, any JSON received from QMP is a QMPMessage, + # and we are asserting only at static analysis time that it + # has a particular shape. + resp: QMPMessage = json.loads(data) + if 'event' in resp: + self.logger.debug("<<< %s", resp) + self.__events.append(resp) + if not only_event: + continue + return resp + + def __get_events(self, wait: Union[bool, float] = False) -> None: + """ + Check for new events in the stream and cache them in __events. + + @param wait (bool): block until an event is available. + @param wait (float): If wait is a float, treat it as a timeout value. + + @raise QMPTimeoutError: If a timeout float is provided and the timeout + period elapses. + @raise QMPConnectError: If wait is True but no events could be + retrieved or if some other error occurred. + """ + + # Current timeout and blocking status + current_timeout = self.__sock.gettimeout() + + # Check for new events regardless and pull them into the cache: + self.__sock.settimeout(0) # i.e. setblocking(False) + try: + self.__json_read() + except OSError as err: + # EAGAIN: No data available; not critical + if err.errno != errno.EAGAIN: + raise + finally: + self.__sock.settimeout(current_timeout) + + # Wait for new events, if needed. + # if wait is 0.0, this means "no wait" and is also implicitly false. + if not self.__events and wait: + if isinstance(wait, float): + self.__sock.settimeout(wait) + try: + ret = self.__json_read(only_event=True) + except socket.timeout as err: + raise QMPTimeoutError("Timeout waiting for event") from err + except Exception as err: + msg = "Error while reading from socket" + raise QMPConnectError(msg) from err + finally: + self.__sock.settimeout(current_timeout) + + if ret is None: + raise QMPConnectError("Error while reading from socket") + + def __enter__(self) -> 'QEMUMonitorProtocol': + # Implement context manager enter function. + return self + + def __exit__(self, + # pylint: disable=duplicate-code + # see https://github.com/PyCQA/pylint/issues/3619 + exc_type: Optional[Type[BaseException]], + exc_val: Optional[BaseException], + exc_tb: Optional[TracebackType]) -> None: + # Implement context manager exit function. + self.close() + + def connect(self, negotiate: bool = True) -> Optional[QMPMessage]: + """ + Connect to the QMP Monitor and perform capabilities negotiation. + + @return QMP greeting dict, or None if negotiate is false + @raise OSError on socket connection errors + @raise QMPConnectError if the greeting is not received + @raise QMPCapabilitiesError if fails to negotiate capabilities + """ + self.__sock.connect(self.__address) + self.__sockfile = self.__sock.makefile(mode='r') + if negotiate: + return self.__negotiate_capabilities() + return None + + def accept(self, timeout: Optional[float] = 15.0) -> QMPMessage: + """ + Await connection from QMP Monitor and perform capabilities negotiation. + + @param timeout: timeout in seconds (nonnegative float number, or + None). The value passed will set the behavior of the + underneath QMP socket as described in [1]. + Default value is set to 15.0. + @return QMP greeting dict + @raise OSError on socket connection errors + @raise QMPConnectError if the greeting is not received + @raise QMPCapabilitiesError if fails to negotiate capabilities + + [1] + https://docs.python.org/3/library/socket.html#socket.socket.settimeout + """ + self.__sock.settimeout(timeout) + self.__sock, _ = self.__sock.accept() + self.__sockfile = self.__sock.makefile(mode='r') + return self.__negotiate_capabilities() + + def cmd_obj(self, qmp_cmd: QMPMessage) -> QMPMessage: + """ + Send a QMP command to the QMP Monitor. + + @param qmp_cmd: QMP command to be sent as a Python dict + @return QMP response as a Python dict + """ + self.logger.debug(">>> %s", qmp_cmd) + self.__sock.sendall(json.dumps(qmp_cmd).encode('utf-8')) + resp = self.__json_read() + if resp is None: + raise QMPConnectError("Unexpected empty reply from server") + self.logger.debug("<<< %s", resp) + return resp + + def cmd(self, name: str, + args: Optional[Dict[str, Any]] = None, + cmd_id: Optional[Any] = None) -> QMPMessage: + """ + Build a QMP command and send it to the QMP Monitor. + + @param name: command name (string) + @param args: command arguments (dict) + @param cmd_id: command id (dict, list, string or int) + """ + qmp_cmd: QMPMessage = {'execute': name} + if args: + qmp_cmd['arguments'] = args + if cmd_id: + qmp_cmd['id'] = cmd_id + return self.cmd_obj(qmp_cmd) + + def command(self, cmd: str, **kwds: Any) -> QMPReturnValue: + """ + Build and send a QMP command to the monitor, report errors if any + """ + ret = self.cmd(cmd, kwds) + if 'error' in ret: + raise QMPResponseError(ret) + if 'return' not in ret: + raise QMPProtocolError( + "'return' key not found in QMP response '{}'".format(str(ret)) + ) + return cast(QMPReturnValue, ret['return']) + + def pull_event(self, + wait: Union[bool, float] = False) -> Optional[QMPMessage]: + """ + Pulls a single event. + + @param wait (bool): block until an event is available. + @param wait (float): If wait is a float, treat it as a timeout value. + + @raise QMPTimeoutError: If a timeout float is provided and the timeout + period elapses. + @raise QMPConnectError: If wait is True but no events could be + retrieved or if some other error occurred. + + @return The first available QMP event, or None. + """ + self.__get_events(wait) + + if self.__events: + return self.__events.pop(0) + return None + + def get_events(self, wait: bool = False) -> List[QMPMessage]: + """ + Get a list of available QMP events. + + @param wait (bool): block until an event is available. + @param wait (float): If wait is a float, treat it as a timeout value. + + @raise QMPTimeoutError: If a timeout float is provided and the timeout + period elapses. + @raise QMPConnectError: If wait is True but no events could be + retrieved or if some other error occurred. + + @return The list of available QMP events. + """ + self.__get_events(wait) + return self.__events + + def clear_events(self) -> None: + """ + Clear current list of pending events. + """ + self.__events = [] + + def close(self) -> None: + """ + Close the socket and socket file. + """ + if self.__sock: + self.__sock.close() + if self.__sockfile: + self.__sockfile.close() + + def settimeout(self, timeout: Optional[float]) -> None: + """ + Set the socket timeout. + + @param timeout (float): timeout in seconds (non-zero), or None. + @note This is a wrap around socket.settimeout + + @raise ValueError: if timeout was set to 0. + """ + if timeout == 0: + msg = "timeout cannot be 0; this engages non-blocking mode." + msg += " Use 'None' instead to disable timeouts." + raise ValueError(msg) + self.__sock.settimeout(timeout) + + def get_sock_fd(self) -> int: + """ + Get the socket file descriptor. + + @return The file descriptor number. + """ + return self.__sock.fileno() + + def is_scm_available(self) -> bool: + """ + Check if the socket allows for SCM_RIGHTS. + + @return True if SCM_RIGHTS is available, otherwise False. + """ + return self.__sock.family == socket.AF_UNIX diff --git a/python/qemu/qtest.py b/python/qemu/qtest.py deleted file mode 100644 index 78b97d1..0000000 --- a/python/qemu/qtest.py +++ /dev/null @@ -1,159 +0,0 @@ -""" -QEMU qtest library - -qtest offers the QEMUQtestProtocol and QEMUQTestMachine classes, which -offer a connection to QEMU's qtest protocol socket, and a qtest-enabled -subclass of QEMUMachine, respectively. -""" - -# Copyright (C) 2015 Red Hat Inc. -# -# Authors: -# Fam Zheng -# -# This work is licensed under the terms of the GNU GPL, version 2. See -# the COPYING file in the top-level directory. -# -# Based on qmp.py. -# - -import os -import socket -from typing import ( - List, - Optional, - Sequence, - TextIO, -) - -from .machine import QEMUMachine -from .qmp import SocketAddrT - - -class QEMUQtestProtocol: - """ - QEMUQtestProtocol implements a connection to a qtest socket. - - :param address: QEMU address, can be either a unix socket path (string) - or a tuple in the form ( address, port ) for a TCP - connection - :param server: server mode, listens on the socket (bool) - :raise socket.error: on socket connection errors - - .. note:: - No conection is estabalished by __init__(), this is done - by the connect() or accept() methods. - """ - def __init__(self, address: SocketAddrT, - server: bool = False): - self._address = address - self._sock = self._get_sock() - self._sockfile: Optional[TextIO] = None - if server: - self._sock.bind(self._address) - self._sock.listen(1) - - def _get_sock(self) -> socket.socket: - if isinstance(self._address, tuple): - family = socket.AF_INET - else: - family = socket.AF_UNIX - return socket.socket(family, socket.SOCK_STREAM) - - def connect(self) -> None: - """ - Connect to the qtest socket. - - @raise socket.error on socket connection errors - """ - self._sock.connect(self._address) - self._sockfile = self._sock.makefile(mode='r') - - def accept(self) -> None: - """ - Await connection from QEMU. - - @raise socket.error on socket connection errors - """ - self._sock, _ = self._sock.accept() - self._sockfile = self._sock.makefile(mode='r') - - def cmd(self, qtest_cmd: str) -> str: - """ - Send a qtest command on the wire. - - @param qtest_cmd: qtest command text to be sent - """ - assert self._sockfile is not None - self._sock.sendall((qtest_cmd + "\n").encode('utf-8')) - resp = self._sockfile.readline() - return resp - - def close(self) -> None: - """ - Close this socket. - """ - self._sock.close() - if self._sockfile: - self._sockfile.close() - self._sockfile = None - - def settimeout(self, timeout: Optional[float]) -> None: - """Set a timeout, in seconds.""" - self._sock.settimeout(timeout) - - -class QEMUQtestMachine(QEMUMachine): - """ - A QEMU VM, with a qtest socket available. - """ - - def __init__(self, - binary: str, - args: Sequence[str] = (), - name: Optional[str] = None, - base_temp_dir: str = "/var/tmp", - socket_scm_helper: Optional[str] = None, - sock_dir: Optional[str] = None): - if name is None: - name = "qemu-%d" % os.getpid() - if sock_dir is None: - sock_dir = base_temp_dir - super().__init__(binary, args, name=name, base_temp_dir=base_temp_dir, - socket_scm_helper=socket_scm_helper, - sock_dir=sock_dir) - self._qtest: Optional[QEMUQtestProtocol] = None - self._qtest_path = os.path.join(sock_dir, name + "-qtest.sock") - - @property - def _base_args(self) -> List[str]: - args = super()._base_args - args.extend([ - '-qtest', f"unix:path={self._qtest_path}", - '-accel', 'qtest' - ]) - return args - - def _pre_launch(self) -> None: - super()._pre_launch() - self._qtest = QEMUQtestProtocol(self._qtest_path, server=True) - - def _post_launch(self) -> None: - assert self._qtest is not None - super()._post_launch() - self._qtest.accept() - - def _post_shutdown(self) -> None: - super()._post_shutdown() - self._remove_if_exists(self._qtest_path) - - def qtest(self, cmd: str) -> str: - """ - Send a qtest command to the guest. - - :param cmd: qtest command to send - :return: qtest server response - """ - if self._qtest is None: - raise RuntimeError("qtest socket not available") - return self._qtest.cmd(cmd) diff --git a/python/qemu/utils.py b/python/qemu/utils.py deleted file mode 100644 index 5ed7892..0000000 --- a/python/qemu/utils.py +++ /dev/null @@ -1,33 +0,0 @@ -""" -QEMU utility library - -This offers miscellaneous utility functions, which may not be easily -distinguishable or numerous to be in their own module. -""" - -# Copyright (C) 2021 Red Hat Inc. -# -# Authors: -# Cleber Rosa -# -# This work is licensed under the terms of the GNU GPL, version 2. See -# the COPYING file in the top-level directory. -# - -import re -from typing import Optional - - -def get_info_usernet_hostfwd_port(info_usernet_output: str) -> Optional[int]: - """ - Returns the port given to the hostfwd parameter via info usernet - - :param info_usernet_output: output generated by hmp command "info usernet" - :return: the port number allocated by the hostfwd option - """ - for line in info_usernet_output.split('\r\n'): - regex = r'TCP.HOST_FORWARD.*127\.0\.0\.1\s+(\d+)\s+10\.' - match = re.search(regex, line) - if match is not None: - return int(match[1]) - return None diff --git a/python/qemu/utils/__init__.py b/python/qemu/utils/__init__.py new file mode 100644 index 0000000..7f1a513 --- /dev/null +++ b/python/qemu/utils/__init__.py @@ -0,0 +1,45 @@ +""" +QEMU development and testing utilities + +This package provides a small handful of utilities for performing +various tasks not directly related to the launching of a VM. +""" + +# Copyright (C) 2021 Red Hat Inc. +# +# Authors: +# John Snow +# Cleber Rosa +# +# This work is licensed under the terms of the GNU GPL, version 2. See +# the COPYING file in the top-level directory. +# + +import re +from typing import Optional + +# pylint: disable=import-error +from .accel import kvm_available, list_accel, tcg_available + + +__all__ = ( + 'get_info_usernet_hostfwd_port', + 'kvm_available', + 'list_accel', + 'tcg_available', +) + + +def get_info_usernet_hostfwd_port(info_usernet_output: str) -> Optional[int]: + """ + Returns the port given to the hostfwd parameter via info usernet + + :param info_usernet_output: output generated by hmp command "info usernet" + :return: the port number allocated by the hostfwd option + """ + for line in info_usernet_output.split('\r\n'): + regex = r'TCP.HOST_FORWARD.*127\.0\.0\.1\s+(\d+)\s+10\.' + match = re.search(regex, line) + if match is not None: + return int(match[1]) + return None diff --git a/python/qemu/utils/accel.py b/python/qemu/utils/accel.py new file mode 100644 index 0000000..297933d --- /dev/null +++ b/python/qemu/utils/accel.py @@ -0,0 +1,84 @@ +""" +QEMU accel module: + +This module provides utilities for discover and check the availability of +accelerators. +""" +# Copyright (C) 2015-2016 Red Hat Inc. +# Copyright (C) 2012 IBM Corp. +# +# Authors: +# Fam Zheng +# +# This work is licensed under the terms of the GNU GPL, version 2. See +# the COPYING file in the top-level directory. +# + +import logging +import os +import subprocess +from typing import List, Optional + + +LOG = logging.getLogger(__name__) + +# Mapping host architecture to any additional architectures it can +# support which often includes its 32 bit cousin. +ADDITIONAL_ARCHES = { + "x86_64": "i386", + "aarch64": "armhf", + "ppc64le": "ppc64", +} + + +def list_accel(qemu_bin: str) -> List[str]: + """ + List accelerators enabled in the QEMU binary. + + @param qemu_bin (str): path to the QEMU binary. + @raise Exception: if failed to run `qemu -accel help` + @return a list of accelerator names. + """ + if not qemu_bin: + return [] + try: + out = subprocess.check_output([qemu_bin, '-accel', 'help'], + universal_newlines=True) + except: + LOG.debug("Failed to get the list of accelerators in %s", qemu_bin) + raise + # Skip the first line which is the header. + return [acc.strip() for acc in out.splitlines()[1:]] + + +def kvm_available(target_arch: Optional[str] = None, + qemu_bin: Optional[str] = None) -> bool: + """ + Check if KVM is available using the following heuristic: + - Kernel module is present in the host; + - Target and host arches don't mismatch; + - KVM is enabled in the QEMU binary. + + @param target_arch (str): target architecture + @param qemu_bin (str): path to the QEMU binary + @return True if kvm is available, otherwise False. + """ + if not os.access("/dev/kvm", os.R_OK | os.W_OK): + return False + if target_arch: + host_arch = os.uname()[4] + if target_arch != host_arch: + if target_arch != ADDITIONAL_ARCHES.get(host_arch): + return False + if qemu_bin and "kvm" not in list_accel(qemu_bin): + return False + return True + + +def tcg_available(qemu_bin: str) -> bool: + """ + Check if TCG is available. + + @param qemu_bin (str): path to the QEMU binary + """ + return 'tcg' in list_accel(qemu_bin) -- cgit v1.1 From ea1213b7ccc7c24a3c704dc88bd855df45203fed Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:16:54 -0400 Subject: python: add qemu package installer Add setup.cfg and setup.py, necessary for installing a package via pip. Add a ReST document (PACKAGE.rst) explaining the basics of what this package is for and who to contact for more information. This document will be used as the landing page for the package on PyPI. List the subpackages we intend to package by name instead of using find_namespace because find_namespace will naively also packages tests, things it finds in the dist/ folder, etc. I could not figure out how to modify this behavior; adding allow/deny lists to setuptools kept changing the packaged hierarchy. This works, roll with it. I am not yet using a pyproject.toml style package manifest, because "editable" installs are not defined (yet?) by PEP-517/518. I consider editable installs crucial for development, though they have (apparently) always been somewhat poorly defined. Pip now (19.2 and later) now supports editable installs for projects using pyproject.toml manifests, but might require the use of the --no-use-pep517 flag, which somewhat defeats the point. Full support for setup.py-less editable installs was not introduced until pip 21.1.1: https://github.com/pypa/pip/pull/9547/commits/7a95720e796a5e56481c1cc20b6ce6249c50f357 For now, while the dust settles, stick with the de-facto setup.py/setup.cfg combination supported by setuptools. It will be worth re-evaluating this point again in the future when our supported build platforms all ship a fairly modern pip. Additional reading on this matter: https://github.com/pypa/packaging-problems/issues/256 https://github.com/pypa/pip/issues/6334 https://github.com/pypa/pip/issues/6375 https://github.com/pypa/pip/issues/6434 https://github.com/pypa/pip/issues/6438 Signed-off-by: John Snow Reviewed-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Cleber Rosa Message-id: 20210527211715.394144-11-jsnow@redhat.com Signed-off-by: John Snow --- python/PACKAGE.rst | 33 +++++++++++++++++++++++++++++++++ python/setup.cfg | 22 ++++++++++++++++++++++ python/setup.py | 23 +++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 python/PACKAGE.rst create mode 100644 python/setup.cfg create mode 100755 python/setup.py (limited to 'python') diff --git a/python/PACKAGE.rst b/python/PACKAGE.rst new file mode 100644 index 0000000..1bbfe1b --- /dev/null +++ b/python/PACKAGE.rst @@ -0,0 +1,33 @@ +QEMU Python Tooling +=================== + +This package provides QEMU tooling used by the QEMU project to build, +configure, and test QEMU. It is not a fully-fledged SDK and it is subject +to change at any time. + +Usage +----- + +The ``qemu.qmp`` subpackage provides a library for communicating with +QMP servers. The ``qemu.machine`` subpackage offers rudimentary +facilities for launching and managing QEMU processes. Refer to each +package's documentation +(``>>> help(qemu.qmp)``, ``>>> help(qemu.machine)``) +for more information. + +Contributing +------------ + +This package is maintained by John Snow as part of +the QEMU source tree. Contributions are welcome and follow the `QEMU +patch submission process +`_, which involves +sending patches to the QEMU development mailing list. + +John maintains a `GitLab staging branch +`_, and there is an +official `GitLab mirror `_. + +Please report bugs on the `QEMU issue tracker +`_ and tag ``@jsnow`` in +the report. diff --git a/python/setup.cfg b/python/setup.cfg new file mode 100644 index 0000000..3fa92a2 --- /dev/null +++ b/python/setup.cfg @@ -0,0 +1,22 @@ +[metadata] +name = qemu +maintainer = QEMU Developer Team +maintainer_email = qemu-devel@nongnu.org +url = https://www.qemu.org/ +download_url = https://www.qemu.org/download/ +description = QEMU Python Build, Debug and SDK tooling. +long_description = file:PACKAGE.rst +long_description_content_type = text/x-rst +classifiers = + Development Status :: 3 - Alpha + License :: OSI Approved :: GNU General Public License v2 (GPLv2) + Natural Language :: English + Operating System :: OS Independent + Programming Language :: Python :: 3 :: Only + +[options] +python_requires = >= 3.6 +packages = + qemu.qmp + qemu.machine + qemu.utils diff --git a/python/setup.py b/python/setup.py new file mode 100755 index 0000000..2014f81 --- /dev/null +++ b/python/setup.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +""" +QEMU tooling installer script +Copyright (c) 2020-2021 John Snow for Red Hat, Inc. +""" + +import setuptools +import pkg_resources + + +def main(): + """ + QEMU tooling installer + """ + + # https://medium.com/@daveshawley/safely-using-setup-cfg-for-metadata-1babbe54c108 + pkg_resources.require('setuptools>=39.2') + + setuptools.setup() + + +if __name__ == '__main__': + main() -- cgit v1.1 From 3afc32906f7bffd8e09b7d247d60b55c49665bd3 Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:16:55 -0400 Subject: python: add VERSION file Python infrastructure as it exists today is not capable reliably of single-sourcing a package version from a parent directory. The authors of pip are working to correct this, but as of today this is not possible. The problem is that when using pip to build and install a python package, it copies files over to a temporary directory and performs its build there. This loses access to any information in the parent directory, including git itself. Further, Python versions have a standard (PEP 440) that may or may not follow QEMU's versioning. In general, it does; but naturally QEMU does not follow PEP 440. To avoid any automatically-generated conflict, a manual version file is preferred. I am proposing: - Python tooling follows the QEMU version, indirectly, but with a major version of 0 to indicate that the API is not expected to be stable. This would mean version 0.5.2.0, 0.5.1.1, 0.5.3.0, etc. - In the event that a Python package needs to be updated independently of the QEMU version, a pre-release alpha version should be preferred, but *only* after inclusion to the qemu development or stable branches. e.g. 0.5.2.0a1, 0.5.2.0a2, and so on should be preferred prior to 5.2.0's release. - The Python core tooling makes absolutely no version compatibility checks or constraints. It *may* work with releases of QEMU from the past or future, but it is not required to. i.e., "qemu.machine" will, for now, remain in lock-step with QEMU. - We reserve the right to split the qemu package into independently versioned subpackages at a later date. This might allow for us to begin versioning QMP independently from QEMU at a later date, if we so choose. Implement this versioning scheme by adding a VERSION file and setting it to 0.6.0.0a1. Signed-off-by: John Snow Reviewed-by: Cleber Rosa Message-id: 20210527211715.394144-12-jsnow@redhat.com Signed-off-by: John Snow --- python/VERSION | 1 + python/setup.cfg | 1 + 2 files changed, 2 insertions(+) create mode 100644 python/VERSION (limited to 'python') diff --git a/python/VERSION b/python/VERSION new file mode 100644 index 0000000..c19f3b8 --- /dev/null +++ b/python/VERSION @@ -0,0 +1 @@ +0.6.1.0a1 diff --git a/python/setup.cfg b/python/setup.cfg index 3fa92a2..b0010e0 100644 --- a/python/setup.cfg +++ b/python/setup.cfg @@ -1,5 +1,6 @@ [metadata] name = qemu +version = file:VERSION maintainer = QEMU Developer Team maintainer_email = qemu-devel@nongnu.org url = https://www.qemu.org/ -- cgit v1.1 From 93128815af4efcaba03a5581c959bc7f98ee2725 Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:16:56 -0400 Subject: python: add directory structure README.rst files Add short readmes to python/, python/qemu/, python/qemu/machine, python/qemu/qmp, and python/qemu/utils that explain the directory hierarchy. These readmes are visible when browsing the source on e.g. gitlab/github and are designed to help new developers/users quickly make sense of the source tree. They are not designed for inclusion in a published manual. Signed-off-by: John Snow Reviewed-by: Cleber Rosa Message-id: 20210527211715.394144-13-jsnow@redhat.com Signed-off-by: John Snow --- python/README.rst | 41 +++++++++++++++++++++++++++++++++++++++++ python/qemu/README.rst | 8 ++++++++ python/qemu/machine/README.rst | 9 +++++++++ python/qemu/qmp/README.rst | 9 +++++++++ python/qemu/utils/README.rst | 7 +++++++ 5 files changed, 74 insertions(+) create mode 100644 python/README.rst create mode 100644 python/qemu/README.rst create mode 100644 python/qemu/machine/README.rst create mode 100644 python/qemu/qmp/README.rst create mode 100644 python/qemu/utils/README.rst (limited to 'python') diff --git a/python/README.rst b/python/README.rst new file mode 100644 index 0000000..38b0c83 --- /dev/null +++ b/python/README.rst @@ -0,0 +1,41 @@ +QEMU Python Tooling +=================== + +This directory houses Python tooling used by the QEMU project to build, +configure, and test QEMU. It is organized by namespace (``qemu``), and +then by package (e.g. ``qemu/machine``, ``qemu/qmp``, etc). + +``setup.py`` is used by ``pip`` to install this tooling to the current +environment. ``setup.cfg`` provides the packaging configuration used by +``setup.py`` in a setuptools specific format. You will generally invoke +it by doing one of the following: + +1. ``pip3 install .`` will install these packages to your current + environment. If you are inside a virtual environment, they will + install there. If you are not, it will attempt to install to the + global environment, which is **not recommended**. + +2. ``pip3 install --user .`` will install these packages to your user's + local python packages. If you are inside of a virtual environment, + this will fail; you likely want the first invocation above. + +If you append the ``-e`` argument, pip will install in "editable" mode; +which installs a version of the package that installs a forwarder +pointing to these files, such that the package always reflects the +latest version in your git tree. + +See `Installing packages using pip and virtual environments +`_ +for more information. + + +Files in this directory +----------------------- + +- ``qemu/`` Python package source directory. +- ``PACKAGE.rst`` is used as the README file that is visible on PyPI.org. +- ``README.rst`` you are here! +- ``VERSION`` contains the PEP-440 compliant version used to describe + this package; it is referenced by ``setup.cfg``. +- ``setup.cfg`` houses setuptools package configuration. +- ``setup.py`` is the setuptools installer used by pip; See above. diff --git a/python/qemu/README.rst b/python/qemu/README.rst new file mode 100644 index 0000000..d04943f --- /dev/null +++ b/python/qemu/README.rst @@ -0,0 +1,8 @@ +QEMU Python Namespace +===================== + +This directory serves as the root of a `Python PEP 420 implicit +namespace package `_. + +Each directory below is assumed to be an installable Python package that +is available under the ``qemu.`` namespace. diff --git a/python/qemu/machine/README.rst b/python/qemu/machine/README.rst new file mode 100644 index 0000000..ac2b4ff --- /dev/null +++ b/python/qemu/machine/README.rst @@ -0,0 +1,9 @@ +qemu.machine package +==================== + +This package provides core utilities used for testing and debugging +QEMU. It is used by the iotests, vm tests, acceptance tests, and several +other utilities in the ./scripts directory. It is not a fully-fledged +SDK and it is subject to change at any time. + +See the documentation in ``__init__.py`` for more information. diff --git a/python/qemu/qmp/README.rst b/python/qemu/qmp/README.rst new file mode 100644 index 0000000..c219514 --- /dev/null +++ b/python/qemu/qmp/README.rst @@ -0,0 +1,9 @@ +qemu.qmp package +================ + +This package provides a library used for connecting to and communicating +with QMP servers. It is used extensively by iotests, vm tests, +acceptance tests, and other utilities in the ./scripts directory. It is +not a fully-fledged SDK and is subject to change at any time. + +See the documentation in ``__init__.py`` for more information. diff --git a/python/qemu/utils/README.rst b/python/qemu/utils/README.rst new file mode 100644 index 0000000..975fbf4 --- /dev/null +++ b/python/qemu/utils/README.rst @@ -0,0 +1,7 @@ +qemu.utils package +================== + +This package provides miscellaneous utilities used for testing and +debugging QEMU. It is used primarily by the vm and acceptance tests. + +See the documentation in ``__init__.py`` for more information. -- cgit v1.1 From eae4e442caa087b2ef292a5fb6377236fa8423f2 Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:16:57 -0400 Subject: python: add MANIFEST.in When creating a source or binary distribution via 'python3 setup.py ', the VERSION and PACKAGE.rst files aren't bundled by default. Create a MANIFEST.in file that instructs the build tools to include these so that installation from these files won't fail. This is required by 'tox', as well as by the tooling needed to upload packages to PyPI. Exclude the 'README.rst' file -- that's intended as a guidebook to our source tree, not a file that needs to be distributed. Signed-off-by: John Snow Reviewed-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Cleber Rosa Message-id: 20210527211715.394144-14-jsnow@redhat.com Signed-off-by: John Snow --- python/MANIFEST.in | 3 +++ python/README.rst | 2 ++ 2 files changed, 5 insertions(+) create mode 100644 python/MANIFEST.in (limited to 'python') diff --git a/python/MANIFEST.in b/python/MANIFEST.in new file mode 100644 index 0000000..7059ad2 --- /dev/null +++ b/python/MANIFEST.in @@ -0,0 +1,3 @@ +include VERSION +include PACKAGE.rst +exclude README.rst diff --git a/python/README.rst b/python/README.rst index 38b0c83..0099646 100644 --- a/python/README.rst +++ b/python/README.rst @@ -33,6 +33,8 @@ Files in this directory ----------------------- - ``qemu/`` Python package source directory. +- ``MANIFEST.in`` is read by python setuptools, it specifies additional files + that should be included by a source distribution. - ``PACKAGE.rst`` is used as the README file that is visible on PyPI.org. - ``README.rst`` you are here! - ``VERSION`` contains the PEP-440 compliant version used to describe -- cgit v1.1 From 41c1d81cf2a9bfdb310576a716f3777e8feb1822 Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:16:58 -0400 Subject: python: Add pipenv support pipenv is a tool used for managing virtual environments with pinned, explicit dependencies. It is used for precisely recreating python virtual environments. pipenv uses two files to do this: (1) Pipfile, which is similar in purpose and scope to what setup.cfg lists. It specifies the requisite minimum to get a functional environment for using this package. (2) Pipfile.lock, which is similar in purpose to `pip freeze > requirements.txt`. It specifies a canonical virtual environment used for deployment or testing. This ensures that all users have repeatable results. The primary benefit of using this tool is to ensure *rock solid* repeatable CI results with a known set of packages. Although I endeavor to support as many versions as I can, the fluid nature of the Python toolchain often means tailoring code for fairly specific versions. Note that pipenv is *not* required to install or use this module; this is purely for the sake of repeatable testing by CI or developers. Here, a "blank" pipfile is added with no dependencies, but specifies Python 3.6 for the virtual environment. Pipfile will specify our version minimums, while Pipfile.lock specifies an exact loadout of packages that were known to operate correctly. This latter file provides the real value for easy setup of container images and CI environments. Signed-off-by: John Snow Reviewed-by: Cleber Rosa Message-id: 20210527211715.394144-15-jsnow@redhat.com Signed-off-by: John Snow --- python/Pipfile | 11 +++++++++++ python/README.rst | 3 +++ 2 files changed, 14 insertions(+) create mode 100644 python/Pipfile (limited to 'python') diff --git a/python/Pipfile b/python/Pipfile new file mode 100644 index 0000000..9534830 --- /dev/null +++ b/python/Pipfile @@ -0,0 +1,11 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] + +[packages] + +[requires] +python_version = "3.6" diff --git a/python/README.rst b/python/README.rst index 0099646..bf9bbca 100644 --- a/python/README.rst +++ b/python/README.rst @@ -36,6 +36,9 @@ Files in this directory - ``MANIFEST.in`` is read by python setuptools, it specifies additional files that should be included by a source distribution. - ``PACKAGE.rst`` is used as the README file that is visible on PyPI.org. +- ``Pipfile`` is used by Pipenv to generate ``Pipfile.lock``. +- ``Pipfile.lock`` is a set of pinned package dependencies that this package + is tested under in our CI suite. It is used by ``make venv-check``. - ``README.rst`` you are here! - ``VERSION`` contains the PEP-440 compliant version used to describe this package; it is referenced by ``setup.cfg``. -- cgit v1.1 From d1e0476958cd275419754b8acf31a9f1dc62d3dd Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:16:59 -0400 Subject: python: add pylint import exceptions Pylint 2.5.x - 2.7.x have regressions that make import checking inconsistent, see: https://github.com/PyCQA/pylint/issues/3609 https://github.com/PyCQA/pylint/issues/3624 https://github.com/PyCQA/pylint/issues/3651 Pinning to 2.4.4 is worse, because it mandates versions of shared dependencies that are too old for features we want in isort and mypy. Oh well. Signed-off-by: John Snow Reviewed-by: Cleber Rosa Message-id: 20210527211715.394144-16-jsnow@redhat.com Signed-off-by: John Snow --- python/qemu/machine/__init__.py | 3 +++ python/qemu/machine/machine.py | 2 +- python/qemu/machine/qtest.py | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) (limited to 'python') diff --git a/python/qemu/machine/__init__.py b/python/qemu/machine/__init__.py index 98302ea..728f27a 100644 --- a/python/qemu/machine/__init__.py +++ b/python/qemu/machine/__init__.py @@ -22,6 +22,9 @@ test suite, not intended for production use. # the COPYING file in the top-level directory. # +# pylint: disable=import-error +# see: https://github.com/PyCQA/pylint/issues/3624 +# see: https://github.com/PyCQA/pylint/issues/3651 from .machine import QEMUMachine from .qtest import QEMUQtestMachine, QEMUQtestProtocol diff --git a/python/qemu/machine/machine.py b/python/qemu/machine/machine.py index d33b02d..b624355 100644 --- a/python/qemu/machine/machine.py +++ b/python/qemu/machine/machine.py @@ -38,7 +38,7 @@ from typing import ( Type, ) -from qemu.qmp import ( +from qemu.qmp import ( # pylint: disable=import-error QEMUMonitorProtocol, QMPMessage, QMPReturnValue, diff --git a/python/qemu/machine/qtest.py b/python/qemu/machine/qtest.py index e893ca3..9370068 100644 --- a/python/qemu/machine/qtest.py +++ b/python/qemu/machine/qtest.py @@ -26,7 +26,7 @@ from typing import ( TextIO, ) -from qemu.qmp import SocketAddrT +from qemu.qmp import SocketAddrT # pylint: disable=import-error from .machine import QEMUMachine -- cgit v1.1 From ef42440d797a1549dd64fe2a51500ba55fe54c3f Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:17:00 -0400 Subject: python: move pylintrc into setup.cfg Delete the empty settings now that it's sharing a home with settings for other tools. pylint can now be run from this folder as "pylint qemu". Signed-off-by: John Snow Reviewed-by: Cleber Rosa Tested-by: Cleber Rosa Message-id: 20210527211715.394144-17-jsnow@redhat.com Signed-off-by: John Snow --- python/qemu/machine/pylintrc | 58 -------------------------------------------- python/setup.cfg | 29 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 58 deletions(-) delete mode 100644 python/qemu/machine/pylintrc (limited to 'python') diff --git a/python/qemu/machine/pylintrc b/python/qemu/machine/pylintrc deleted file mode 100644 index 3f69205..0000000 --- a/python/qemu/machine/pylintrc +++ /dev/null @@ -1,58 +0,0 @@ -[MASTER] - -[MESSAGES CONTROL] - -# Disable the message, report, category or checker with the given id(s). You -# can either give multiple identifiers separated by comma (,) or put this -# option multiple times (only on the command line, not in the configuration -# file where it should appear only once). You can also use "--disable=all" to -# disable everything first and then reenable specific checks. For example, if -# you want to run only the similarities checker, you can use "--disable=all -# --enable=similarities". If you want to run only the classes checker, but have -# no Warning level messages displayed, use "--disable=all --enable=classes -# --disable=W". -disable=too-many-arguments, - too-many-instance-attributes, - too-many-public-methods, - -[REPORTS] - -[REFACTORING] - -[MISCELLANEOUS] - -[LOGGING] - -[BASIC] - -# Good variable names which should always be accepted, separated by a comma. -good-names=i, - j, - k, - ex, - Run, - _, - fd, - c, -[VARIABLES] - -[STRING] - -[SPELLING] - -[FORMAT] - -[SIMILARITIES] - -# Ignore imports when computing similarities. -ignore-imports=yes - -[TYPECHECK] - -[CLASSES] - -[IMPORTS] - -[DESIGN] - -[EXCEPTIONS] diff --git a/python/setup.cfg b/python/setup.cfg index b0010e0..36b4253 100644 --- a/python/setup.cfg +++ b/python/setup.cfg @@ -21,3 +21,32 @@ packages = qemu.qmp qemu.machine qemu.utils + +[pylint.messages control] +# Disable the message, report, category or checker with the given id(s). You +# can either give multiple identifiers separated by comma (,) or put this +# option multiple times (only on the command line, not in the configuration +# file where it should appear only once). You can also use "--disable=all" to +# disable everything first and then reenable specific checks. For example, if +# you want to run only the similarities checker, you can use "--disable=all +# --enable=similarities". If you want to run only the classes checker, but have +# no Warning level messages displayed, use "--disable=all --enable=classes +# --disable=W". +disable=too-many-arguments, + too-many-instance-attributes, + too-many-public-methods, + +[pylint.basic] +# Good variable names which should always be accepted, separated by a comma. +good-names=i, + j, + k, + ex, + Run, + _, + fd, + c, + +[pylint.similarities] +# Ignore imports when computing similarities. +ignore-imports=yes -- cgit v1.1 From b4d37d8188dbff34f0bf88279eeb5b6cb6d1ff82 Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:17:01 -0400 Subject: python: add pylint to pipenv We are specifying >= pylint 2.8.x for several reasons: 1. For setup.cfg support, added in pylint 2.5.x 2. To specify a version that has incompatibly dropped bad-whitespace checks (2.6.x) 3. 2.7.x fixes "unsubscriptable" warnings in Python 3.9 4. 2.8.x adds a new, incompatible 'consider-using-with' warning that must be disabled in some cases. These pragmas cause warnings themselves in 2.7.x. Signed-off-by: John Snow Reviewed-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Cleber Rosa Tested-by: Cleber Rosa Message-id: 20210527211715.394144-18-jsnow@redhat.com Signed-off-by: John Snow --- python/Pipfile | 1 + python/Pipfile.lock | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 python/Pipfile.lock (limited to 'python') diff --git a/python/Pipfile b/python/Pipfile index 9534830..285e2c8 100644 --- a/python/Pipfile +++ b/python/Pipfile @@ -4,6 +4,7 @@ url = "https://pypi.org/simple" verify_ssl = true [dev-packages] +pylint = ">=2.8.0" [packages] diff --git a/python/Pipfile.lock b/python/Pipfile.lock new file mode 100644 index 0000000..c9debd0 --- /dev/null +++ b/python/Pipfile.lock @@ -0,0 +1,130 @@ +{ + "_meta": { + "hash": { + "sha256": "bd4fb76fcdd145bbf23c3a9dd7ad966113c5ce43ca51cc2d828aa7e73d572901" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.6" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": {}, + "develop": { + "astroid": { + "hashes": [ + "sha256:4db03ab5fc3340cf619dbc25e42c2cc3755154ce6009469766d7143d1fc2ee4e", + "sha256:8a398dfce302c13f14bab13e2b14fe385d32b73f4e4853b9bdfb64598baa1975" + ], + "markers": "python_version ~= '3.6'", + "version": "==2.5.6" + }, + "isort": { + "hashes": [ + "sha256:0a943902919f65c5684ac4e0154b1ad4fac6dcaa5d9f3426b732f1c8b5419be6", + "sha256:2bb1680aad211e3c9944dbce1d4ba09a989f04e238296c87fe2139faa26d655d" + ], + "markers": "python_version >= '3.6' and python_version < '4.0'", + "version": "==5.8.0" + }, + "lazy-object-proxy": { + "hashes": [ + "sha256:17e0967ba374fc24141738c69736da90e94419338fd4c7c7bef01ee26b339653", + "sha256:1fee665d2638491f4d6e55bd483e15ef21f6c8c2095f235fef72601021e64f61", + "sha256:22ddd618cefe54305df49e4c069fa65715be4ad0e78e8d252a33debf00f6ede2", + "sha256:24a5045889cc2729033b3e604d496c2b6f588c754f7a62027ad4437a7ecc4837", + "sha256:410283732af311b51b837894fa2f24f2c0039aa7f220135192b38fcc42bd43d3", + "sha256:4732c765372bd78a2d6b2150a6e99d00a78ec963375f236979c0626b97ed8e43", + "sha256:489000d368377571c6f982fba6497f2aa13c6d1facc40660963da62f5c379726", + "sha256:4f60460e9f1eb632584c9685bccea152f4ac2130e299784dbaf9fae9f49891b3", + "sha256:5743a5ab42ae40caa8421b320ebf3a998f89c85cdc8376d6b2e00bd12bd1b587", + "sha256:85fb7608121fd5621cc4377a8961d0b32ccf84a7285b4f1d21988b2eae2868e8", + "sha256:9698110e36e2df951c7c36b6729e96429c9c32b3331989ef19976592c5f3c77a", + "sha256:9d397bf41caad3f489e10774667310d73cb9c4258e9aed94b9ec734b34b495fd", + "sha256:b579f8acbf2bdd9ea200b1d5dea36abd93cabf56cf626ab9c744a432e15c815f", + "sha256:b865b01a2e7f96db0c5d12cfea590f98d8c5ba64ad222300d93ce6ff9138bcad", + "sha256:bf34e368e8dd976423396555078def5cfc3039ebc6fc06d1ae2c5a65eebbcde4", + "sha256:c6938967f8528b3668622a9ed3b31d145fab161a32f5891ea7b84f6b790be05b", + "sha256:d1c2676e3d840852a2de7c7d5d76407c772927addff8d742b9808fe0afccebdf", + "sha256:d7124f52f3bd259f510651450e18e0fd081ed82f3c08541dffc7b94b883aa981", + "sha256:d900d949b707778696fdf01036f58c9876a0d8bfe116e8d220cfd4b15f14e741", + "sha256:ebfd274dcd5133e0afae738e6d9da4323c3eb021b3e13052d8cbd0e457b1256e", + "sha256:ed361bb83436f117f9917d282a456f9e5009ea12fd6de8742d1a4752c3017e93", + "sha256:f5144c75445ae3ca2057faac03fda5a902eff196702b0a24daf1d6ce0650514b" + ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4, 3.5'", + "version": "==1.6.0" + }, + "mccabe": { + "hashes": [ + "sha256:ab8a6258860da4b6677da4bd2fe5dc2c659cff31b3ee4f7f5d64e79735b80d42", + "sha256:dd8d182285a0fe56bace7f45b5e7d1a6ebcbf524e8f3bd87eb0f125271b8831f" + ], + "version": "==0.6.1" + }, + "pylint": { + "hashes": [ + "sha256:586d8fa9b1891f4b725f587ef267abe2a1bad89d6b184520c7f07a253dd6e217", + "sha256:f7e2072654a6b6afdf5e2fb38147d3e2d2d43c89f648637baab63e026481279b" + ], + "index": "pypi", + "version": "==2.8.2" + }, + "toml": { + "hashes": [ + "sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b", + "sha256:b3bda1d108d5dd99f4a20d24d9c348e91c4db7ab1b749200bded2f839ccbe68f" + ], + "markers": "python_version >= '2.6' and python_version not in '3.0, 3.1, 3.2'", + "version": "==0.10.2" + }, + "typed-ast": { + "hashes": [ + "sha256:01ae5f73431d21eead5015997ab41afa53aa1fbe252f9da060be5dad2c730ace", + "sha256:067a74454df670dcaa4e59349a2e5c81e567d8d65458d480a5b3dfecec08c5ff", + "sha256:0fb71b8c643187d7492c1f8352f2c15b4c4af3f6338f21681d3681b3dc31a266", + "sha256:1b3ead4a96c9101bef08f9f7d1217c096f31667617b58de957f690c92378b528", + "sha256:2068531575a125b87a41802130fa7e29f26c09a2833fea68d9a40cf33902eba6", + "sha256:209596a4ec71d990d71d5e0d312ac935d86930e6eecff6ccc7007fe54d703808", + "sha256:2c726c276d09fc5c414693a2de063f521052d9ea7c240ce553316f70656c84d4", + "sha256:398e44cd480f4d2b7ee8d98385ca104e35c81525dd98c519acff1b79bdaac363", + "sha256:52b1eb8c83f178ab787f3a4283f68258525f8d70f778a2f6dd54d3b5e5fb4341", + "sha256:5feca99c17af94057417d744607b82dd0a664fd5e4ca98061480fd8b14b18d04", + "sha256:7538e495704e2ccda9b234b82423a4038f324f3a10c43bc088a1636180f11a41", + "sha256:760ad187b1041a154f0e4d0f6aae3e40fdb51d6de16e5c99aedadd9246450e9e", + "sha256:777a26c84bea6cd934422ac2e3b78863a37017618b6e5c08f92ef69853e765d3", + "sha256:95431a26309a21874005845c21118c83991c63ea800dd44843e42a916aec5899", + "sha256:9ad2c92ec681e02baf81fdfa056fe0d818645efa9af1f1cd5fd6f1bd2bdfd805", + "sha256:9c6d1a54552b5330bc657b7ef0eae25d00ba7ffe85d9ea8ae6540d2197a3788c", + "sha256:aee0c1256be6c07bd3e1263ff920c325b59849dc95392a05f258bb9b259cf39c", + "sha256:af3d4a73793725138d6b334d9d247ce7e5f084d96284ed23f22ee626a7b88e39", + "sha256:b36b4f3920103a25e1d5d024d155c504080959582b928e91cb608a65c3a49e1a", + "sha256:b9574c6f03f685070d859e75c7f9eeca02d6933273b5e69572e5ff9d5e3931c3", + "sha256:bff6ad71c81b3bba8fa35f0f1921fb24ff4476235a6e94a26ada2e54370e6da7", + "sha256:c190f0899e9f9f8b6b7863debfb739abcb21a5c054f911ca3596d12b8a4c4c7f", + "sha256:c907f561b1e83e93fad565bac5ba9c22d96a54e7ea0267c708bffe863cbe4075", + "sha256:cae53c389825d3b46fb37538441f75d6aecc4174f615d048321b716df2757fb0", + "sha256:dd4a21253f42b8d2b48410cb31fe501d32f8b9fbeb1f55063ad102fe9c425e40", + "sha256:dde816ca9dac1d9c01dd504ea5967821606f02e510438120091b84e852367428", + "sha256:f2362f3cb0f3172c42938946dbc5b7843c2a28aec307c49100c8b38764eb6927", + "sha256:f328adcfebed9f11301eaedfa48e15bdece9b519fb27e6a8c01aa52a17ec31b3", + "sha256:f8afcf15cc511ada719a88e013cec87c11aff7b91f019295eb4530f96fe5ef2f", + "sha256:fb1bbeac803adea29cedd70781399c99138358c26d05fcbd23c13016b7f5ec65" + ], + "markers": "implementation_name == 'cpython' and python_version < '3.8'", + "version": "==1.4.3" + }, + "wrapt": { + "hashes": [ + "sha256:b62ffa81fb85f4332a4f609cab4ac40709470da05643a082ec1eb88e6d9b97d7" + ], + "version": "==1.12.1" + } + } +} -- cgit v1.1 From 81f8c4467c1899ef1ba984c70c328ac0c32af10c Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:17:02 -0400 Subject: python: move flake8 config to setup.cfg Update the comment concerning the flake8 exception to match commit 42c0dd12, whose commit message stated: A note on the flake8 exception: flake8 will warn on *any* bare except, but pylint's is context-aware and will suppress the warning if you re-raise the exception. Signed-off-by: John Snow Reviewed-by: Cleber Rosa Message-id: 20210527211715.394144-19-jsnow@redhat.com Signed-off-by: John Snow --- python/qemu/machine/.flake8 | 2 -- python/setup.cfg | 3 +++ 2 files changed, 3 insertions(+), 2 deletions(-) delete mode 100644 python/qemu/machine/.flake8 (limited to 'python') diff --git a/python/qemu/machine/.flake8 b/python/qemu/machine/.flake8 deleted file mode 100644 index 45d8146..0000000 --- a/python/qemu/machine/.flake8 +++ /dev/null @@ -1,2 +0,0 @@ -[flake8] -extend-ignore = E722 # Pylint handles this, but smarter. \ No newline at end of file diff --git a/python/setup.cfg b/python/setup.cfg index 36b4253..52a89a0 100644 --- a/python/setup.cfg +++ b/python/setup.cfg @@ -22,6 +22,9 @@ packages = qemu.machine qemu.utils +[flake8] +extend-ignore = E722 # Prefer pylint's bare-except checks to flake8's + [pylint.messages control] # Disable the message, report, category or checker with the given id(s). You # can either give multiple identifiers separated by comma (,) or put this -- cgit v1.1 From 21d0b8667981e386cdfec18ad7d3eb4d9a33b088 Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:17:03 -0400 Subject: python: add excluded dirs to flake8 config Instruct flake8 to avoid certain well-known directories created by python tooling that it ought not check. Note that at-present, nothing actually creates a ".venv" directory; but it is in such widespread usage as a de-facto location for a developer's virtual environment that it should be excluded anyway. A forthcoming commit canonizes this with a "make venv" command. Signed-off-by: John Snow Reviewed-by: Cleber Rosa Message-id: 20210527211715.394144-20-jsnow@redhat.com Signed-off-by: John Snow --- python/setup.cfg | 2 ++ 1 file changed, 2 insertions(+) (limited to 'python') diff --git a/python/setup.cfg b/python/setup.cfg index 52a89a0..9aeab2b 100644 --- a/python/setup.cfg +++ b/python/setup.cfg @@ -24,6 +24,8 @@ packages = [flake8] extend-ignore = E722 # Prefer pylint's bare-except checks to flake8's +exclude = __pycache__, + .venv, [pylint.messages control] # Disable the message, report, category or checker with the given id(s). You -- cgit v1.1 From 6d17d910434568626c1c739b7d3d8433618b19eb Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:17:04 -0400 Subject: python: Add flake8 to pipenv flake8 3.5.x does not support the --extend-ignore syntax used in the .flake8 file to gracefully extend default ignores, so 3.6.x is our minimum requirement. There is no known upper bound. flake8 can be run from the python/ directory with no arguments. Signed-off-by: John Snow Reviewed-by: Cleber Rosa Tested-by: Cleber Rosa Message-id: 20210527211715.394144-21-jsnow@redhat.com Signed-off-by: John Snow --- python/Pipfile | 1 + python/Pipfile.lock | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) (limited to 'python') diff --git a/python/Pipfile b/python/Pipfile index 285e2c8..053f344 100644 --- a/python/Pipfile +++ b/python/Pipfile @@ -4,6 +4,7 @@ url = "https://pypi.org/simple" verify_ssl = true [dev-packages] +flake8 = ">=3.6.0" pylint = ">=2.8.0" [packages] diff --git a/python/Pipfile.lock b/python/Pipfile.lock index c9debd0..5c34019 100644 --- a/python/Pipfile.lock +++ b/python/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "bd4fb76fcdd145bbf23c3a9dd7ad966113c5ce43ca51cc2d828aa7e73d572901" + "sha256": "3c842ab9c72c40d24d146349aa144e00e4dec1c358c812cfa96489411f5b3f87" }, "pipfile-spec": 6, "requires": { @@ -25,6 +25,22 @@ "markers": "python_version ~= '3.6'", "version": "==2.5.6" }, + "flake8": { + "hashes": [ + "sha256:07528381786f2a6237b061f6e96610a4167b226cb926e2aa2b6b1d78057c576b", + "sha256:bf8fd333346d844f616e8d47905ef3a3384edae6b4e9beb0c5101e25e3110907" + ], + "index": "pypi", + "version": "==3.9.2" + }, + "importlib-metadata": { + "hashes": [ + "sha256:8c501196e49fb9df5df43833bdb1e4328f64847763ec8a50703148b73784d581", + "sha256:d7eb1dea6d6a6086f8be21784cc9e3bcfa55872b52309bc5fad53a8ea444465d" + ], + "markers": "python_version < '3.8'", + "version": "==4.0.1" + }, "isort": { "hashes": [ "sha256:0a943902919f65c5684ac4e0154b1ad4fac6dcaa5d9f3426b732f1c8b5419be6", @@ -68,6 +84,22 @@ ], "version": "==0.6.1" }, + "pycodestyle": { + "hashes": [ + "sha256:514f76d918fcc0b55c6680472f0a37970994e07bbb80725808c17089be302068", + "sha256:c389c1d06bf7904078ca03399a4816f974a1d590090fecea0c63ec26ebaf1cef" + ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", + "version": "==2.7.0" + }, + "pyflakes": { + "hashes": [ + "sha256:7893783d01b8a89811dd72d7dfd4d84ff098e5eed95cfa8905b22bbffe52efc3", + "sha256:f5bc8ecabc05bb9d291eb5203d6810b49040f6ff446a756326104746cc00c1db" + ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", + "version": "==2.3.1" + }, "pylint": { "hashes": [ "sha256:586d8fa9b1891f4b725f587ef267abe2a1bad89d6b184520c7f07a253dd6e217", @@ -120,11 +152,28 @@ "markers": "implementation_name == 'cpython' and python_version < '3.8'", "version": "==1.4.3" }, + "typing-extensions": { + "hashes": [ + "sha256:0ac0f89795dd19de6b97debb0c6af1c70987fd80a2d62d1958f7e56fcc31b497", + "sha256:50b6f157849174217d0656f99dc82fe932884fb250826c18350e159ec6cdf342", + "sha256:779383f6086d90c99ae41cf0ff39aac8a7937a9283ce0a414e5dd782f4c94a84" + ], + "markers": "python_version < '3.8'", + "version": "==3.10.0.0" + }, "wrapt": { "hashes": [ "sha256:b62ffa81fb85f4332a4f609cab4ac40709470da05643a082ec1eb88e6d9b97d7" ], "version": "==1.12.1" + }, + "zipp": { + "hashes": [ + "sha256:3607921face881ba3e026887d8150cca609d517579abe052ac81fc5aeffdbd76", + "sha256:51cb66cc54621609dd593d1787f286ee42a5c0adbb4b29abea5a63edc3e03098" + ], + "markers": "python_version >= '3.6'", + "version": "==3.4.1" } } } -- cgit v1.1 From e941c844e4446b6200ac102ef285544c406a2fcd Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:17:05 -0400 Subject: python: move mypy.ini into setup.cfg mypy supports reading its configuration values from a central project configuration file; do so. Signed-off-by: John Snow Reviewed-by: Cleber Rosa Message-id: 20210527211715.394144-22-jsnow@redhat.com Signed-off-by: John Snow --- python/mypy.ini | 4 ---- python/setup.cfg | 5 +++++ 2 files changed, 5 insertions(+), 4 deletions(-) delete mode 100644 python/mypy.ini (limited to 'python') diff --git a/python/mypy.ini b/python/mypy.ini deleted file mode 100644 index 1a581c5..0000000 --- a/python/mypy.ini +++ /dev/null @@ -1,4 +0,0 @@ -[mypy] -strict = True -python_version = 3.6 -warn_unused_configs = True diff --git a/python/setup.cfg b/python/setup.cfg index 9aeab2b..bd88b44 100644 --- a/python/setup.cfg +++ b/python/setup.cfg @@ -27,6 +27,11 @@ extend-ignore = E722 # Prefer pylint's bare-except checks to flake8's exclude = __pycache__, .venv, +[mypy] +strict = True +python_version = 3.6 +warn_unused_configs = True + [pylint.messages control] # Disable the message, report, category or checker with the given id(s). You # can either give multiple identifiers separated by comma (,) or put this -- cgit v1.1 From 0542a4c95767b2370cb6622efe723bb6197aa04c Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:17:06 -0400 Subject: python: add mypy to pipenv 0.730 appears to be about the oldest version that works with the features we want, including nice human readable output (to make sure iotest 297 passes), and type-parameterized Popen generics. 0.770, however, supports adding 'strict' to the config file, so require at least 0.770. Now that we are checking a namespace package, we need to tell mypy to allow PEP420 namespaces, so modify the mypy config as part of the move. mypy can now be run from the python root by typing 'mypy -p qemu'. A note on mypy invocation: Running it as "mypy qemu/" changes the import path detection mechanisms in mypy slightly, and it will fail. See https://github.com/python/mypy/issues/8584 for a decent entry point with more breadcrumbs on the various behaviors that contribute to this subtle difference. Signed-off-by: John Snow Reviewed-by: Cleber Rosa Tested-by: Cleber Rosa Message-id: 20210527211715.394144-23-jsnow@redhat.com Signed-off-by: John Snow --- python/Pipfile | 1 + python/Pipfile.lock | 37 ++++++++++++++++++++++++++++++++++++- python/setup.cfg | 1 + 3 files changed, 38 insertions(+), 1 deletion(-) (limited to 'python') diff --git a/python/Pipfile b/python/Pipfile index 053f344..796c628 100644 --- a/python/Pipfile +++ b/python/Pipfile @@ -5,6 +5,7 @@ verify_ssl = true [dev-packages] flake8 = ">=3.6.0" +mypy = ">=0.770" pylint = ">=2.8.0" [packages] diff --git a/python/Pipfile.lock b/python/Pipfile.lock index 5c34019..626e684 100644 --- a/python/Pipfile.lock +++ b/python/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "3c842ab9c72c40d24d146349aa144e00e4dec1c358c812cfa96489411f5b3f87" + "sha256": "14d171b3d86759e1fdfb9e55f66be4a696b6afa8f627d6c4778f8398c6a66b98" }, "pipfile-spec": 6, "requires": { @@ -84,6 +84,41 @@ ], "version": "==0.6.1" }, + "mypy": { + "hashes": [ + "sha256:0d0a87c0e7e3a9becdfbe936c981d32e5ee0ccda3e0f07e1ef2c3d1a817cf73e", + "sha256:25adde9b862f8f9aac9d2d11971f226bd4c8fbaa89fb76bdadb267ef22d10064", + "sha256:28fb5479c494b1bab244620685e2eb3c3f988d71fd5d64cc753195e8ed53df7c", + "sha256:2f9b3407c58347a452fc0736861593e105139b905cca7d097e413453a1d650b4", + "sha256:33f159443db0829d16f0a8d83d94df3109bb6dd801975fe86bacb9bf71628e97", + "sha256:3f2aca7f68580dc2508289c729bd49ee929a436208d2b2b6aab15745a70a57df", + "sha256:499c798053cdebcaa916eef8cd733e5584b5909f789de856b482cd7d069bdad8", + "sha256:4eec37370483331d13514c3f55f446fc5248d6373e7029a29ecb7b7494851e7a", + "sha256:552a815579aa1e995f39fd05dde6cd378e191b063f031f2acfe73ce9fb7f9e56", + "sha256:5873888fff1c7cf5b71efbe80e0e73153fe9212fafdf8e44adfe4c20ec9f82d7", + "sha256:61a3d5b97955422964be6b3baf05ff2ce7f26f52c85dd88db11d5e03e146a3a6", + "sha256:674e822aa665b9fd75130c6c5f5ed9564a38c6cea6a6432ce47eafb68ee578c5", + "sha256:7ce3175801d0ae5fdfa79b4f0cfed08807af4d075b402b7e294e6aa72af9aa2a", + "sha256:9743c91088d396c1a5a3c9978354b61b0382b4e3c440ce83cf77994a43e8c521", + "sha256:9f94aac67a2045ec719ffe6111df543bac7874cee01f41928f6969756e030564", + "sha256:a26f8ec704e5a7423c8824d425086705e381b4f1dfdef6e3a1edab7ba174ec49", + "sha256:abf7e0c3cf117c44d9285cc6128856106183938c68fd4944763003decdcfeb66", + "sha256:b09669bcda124e83708f34a94606e01b614fa71931d356c1f1a5297ba11f110a", + "sha256:cd07039aa5df222037005b08fbbfd69b3ab0b0bd7a07d7906de75ae52c4e3119", + "sha256:d23e0ea196702d918b60c8288561e722bf437d82cb7ef2edcd98cfa38905d506", + "sha256:d65cc1df038ef55a99e617431f0553cd77763869eebdf9042403e16089fe746c", + "sha256:d7da2e1d5f558c37d6e8c1246f1aec1e7349e4913d8fb3cb289a35de573fe2eb" + ], + "index": "pypi", + "version": "==0.812" + }, + "mypy-extensions": { + "hashes": [ + "sha256:090fedd75945a69ae91ce1303b5824f428daf5a028d2f6ab8a299250a846f15d", + "sha256:2d82818f5bb3e369420cb3c4060a7970edba416647068eb4c5343488a6c604a8" + ], + "version": "==0.4.3" + }, "pycodestyle": { "hashes": [ "sha256:514f76d918fcc0b55c6680472f0a37970994e07bbb80725808c17089be302068", diff --git a/python/setup.cfg b/python/setup.cfg index bd88b44..b485d61 100644 --- a/python/setup.cfg +++ b/python/setup.cfg @@ -31,6 +31,7 @@ exclude = __pycache__, strict = True python_version = 3.6 warn_unused_configs = True +namespace_packages = True [pylint.messages control] # Disable the message, report, category or checker with the given id(s). You -- cgit v1.1 From 158ac451b9e1029798f8fdc103fef64830e4314e Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:17:07 -0400 Subject: python: move .isort.cfg into setup.cfg Signed-off-by: John Snow Reviewed-by: Cleber Rosa Message-id: 20210527211715.394144-24-jsnow@redhat.com Signed-off-by: John Snow --- python/.isort.cfg | 7 ------- python/setup.cfg | 8 ++++++++ 2 files changed, 8 insertions(+), 7 deletions(-) delete mode 100644 python/.isort.cfg (limited to 'python') diff --git a/python/.isort.cfg b/python/.isort.cfg deleted file mode 100644 index 6d0fd6c..0000000 --- a/python/.isort.cfg +++ /dev/null @@ -1,7 +0,0 @@ -[settings] -force_grid_wrap=4 -force_sort_within_sections=True -include_trailing_comma=True -line_length=72 -lines_after_imports=2 -multi_line_output=3 \ No newline at end of file diff --git a/python/setup.cfg b/python/setup.cfg index b485d61..3f07bd2 100644 --- a/python/setup.cfg +++ b/python/setup.cfg @@ -61,3 +61,11 @@ good-names=i, [pylint.similarities] # Ignore imports when computing similarities. ignore-imports=yes + +[isort] +force_grid_wrap=4 +force_sort_within_sections=True +include_trailing_comma=True +line_length=72 +lines_after_imports=2 +multi_line_output=3 -- cgit v1.1 From 22a973cb1d365f6c506e190d26e2261a65066e15 Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:17:08 -0400 Subject: python/qemu: add isort to pipenv isort 5.0.0 through 5.0.4 has a bug that causes it to misinterpret certain "from ..." clauses that are not related to imports. isort < 5.1.1 has a bug where it does not handle comments near import statements correctly. Require 5.1.2 or greater. isort can be run (in "check" mode) with 'isort -c qemu' from the python root. isort can also be used to fix/rewrite import order automatically by using 'isort qemu'. Signed-off-by: John Snow Reviewed-by: Cleber Rosa Message-id: 20210527211715.394144-25-jsnow@redhat.com Signed-off-by: John Snow --- python/Pipfile | 1 + python/Pipfile.lock | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'python') diff --git a/python/Pipfile b/python/Pipfile index 796c628..79c74dd 100644 --- a/python/Pipfile +++ b/python/Pipfile @@ -5,6 +5,7 @@ verify_ssl = true [dev-packages] flake8 = ">=3.6.0" +isort = ">=5.1.2" mypy = ">=0.770" pylint = ">=2.8.0" diff --git a/python/Pipfile.lock b/python/Pipfile.lock index 626e684..57a5bef 100644 --- a/python/Pipfile.lock +++ b/python/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "14d171b3d86759e1fdfb9e55f66be4a696b6afa8f627d6c4778f8398c6a66b98" + "sha256": "8173290ad57aab0b722c9b4f109519de4e0dd7cd1bad1e16806b78bb169bce08" }, "pipfile-spec": 6, "requires": { @@ -46,7 +46,7 @@ "sha256:0a943902919f65c5684ac4e0154b1ad4fac6dcaa5d9f3426b732f1c8b5419be6", "sha256:2bb1680aad211e3c9944dbce1d4ba09a989f04e238296c87fe2139faa26d655d" ], - "markers": "python_version >= '3.6' and python_version < '4.0'", + "index": "pypi", "version": "==5.8.0" }, "lazy-object-proxy": { -- cgit v1.1 From a4dd49d40536b7ad70ab9c2e25a7810773ca32bc Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:17:09 -0400 Subject: python/qemu: add qemu package itself to pipenv This adds the python qemu packages themselves to the pipenv manifest. 'pipenv sync' will create a virtual environment sufficient to use the SDK. 'pipenv sync --dev' will create a virtual environment sufficient to use and test the SDK (with pylint, mypy, isort, flake8, etc.) The qemu packages are installed in 'editable' mode; all changes made to the python package inside the git tree will be reflected in the installed package without reinstallation. This includes changes made via git pull and so on. Signed-off-by: John Snow Reviewed-by: Cleber Rosa Tested-by: Cleber Rosa Message-id: 20210527211715.394144-26-jsnow@redhat.com Signed-off-by: John Snow --- python/Pipfile | 1 + python/Pipfile.lock | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) (limited to 'python') diff --git a/python/Pipfile b/python/Pipfile index 79c74dd..dbe96f7 100644 --- a/python/Pipfile +++ b/python/Pipfile @@ -10,6 +10,7 @@ mypy = ">=0.770" pylint = ">=2.8.0" [packages] +qemu = {editable = true,path = "."} [requires] python_version = "3.6" diff --git a/python/Pipfile.lock b/python/Pipfile.lock index 57a5bef..f0bf576 100644 --- a/python/Pipfile.lock +++ b/python/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "8173290ad57aab0b722c9b4f109519de4e0dd7cd1bad1e16806b78bb169bce08" + "sha256": "7c74cc4c2db3a75c954a6686411cda6fd60e464620bb6d5f1ed9a54be61db4cc" }, "pipfile-spec": 6, "requires": { @@ -15,7 +15,12 @@ } ] }, - "default": {}, + "default": { + "qemu": { + "editable": true, + "path": "." + } + }, "develop": { "astroid": { "hashes": [ -- cgit v1.1 From dbe75f55669a4e2295b0dae161b8f796e6dbaded Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:17:10 -0400 Subject: python: add devel package requirements to setuptools setuptools doesn't have a formal understanding of development requires, but it has an optional feataures section. Fine; add a "devel" feature and add the requirements to it. To avoid duplication, we can modify pipenv to install qemu[devel] instead. This enables us to run invocations like "pip install -e .[devel]" and test the package on bleeding-edge packages beyond those specified in Pipfile.lock. Importantly, this also allows us to install the qemu development packages in a non-networked mode: `pip3 install --no-index -e .[devel]` will now fail if the proper development dependencies are not already met. This can be useful for automated build scripts where fetching network packages may be undesirable. Signed-off-by: John Snow Reviewed-by: Cleber Rosa Message-id: 20210527211715.394144-27-jsnow@redhat.com Signed-off-by: John Snow --- python/PACKAGE.rst | 4 ++++ python/Pipfile | 5 +---- python/Pipfile.lock | 14 +++++++++----- python/README.rst | 4 ++++ python/setup.cfg | 9 +++++++++ 5 files changed, 27 insertions(+), 9 deletions(-) (limited to 'python') diff --git a/python/PACKAGE.rst b/python/PACKAGE.rst index 1bbfe1b..05ea778 100644 --- a/python/PACKAGE.rst +++ b/python/PACKAGE.rst @@ -31,3 +31,7 @@ official `GitLab mirror `_. Please report bugs on the `QEMU issue tracker `_ and tag ``@jsnow`` in the report. + +Optional packages necessary for running code quality analysis for this +package can be installed with the optional dependency group "devel": +``pip install qemu[devel]``. diff --git a/python/Pipfile b/python/Pipfile index dbe96f7..e7acb8c 100644 --- a/python/Pipfile +++ b/python/Pipfile @@ -4,10 +4,7 @@ url = "https://pypi.org/simple" verify_ssl = true [dev-packages] -flake8 = ">=3.6.0" -isort = ">=5.1.2" -mypy = ">=0.770" -pylint = ">=2.8.0" +qemu = {editable = true, extras = ["devel"], path = "."} [packages] qemu = {editable = true,path = "."} diff --git a/python/Pipfile.lock b/python/Pipfile.lock index f0bf576..a2cdc1c 100644 --- a/python/Pipfile.lock +++ b/python/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "7c74cc4c2db3a75c954a6686411cda6fd60e464620bb6d5f1ed9a54be61db4cc" + "sha256": "eff562a688ebc6f3ffe67494dbb804b883e2159ad81c4d55d96da9f7aec13e91" }, "pipfile-spec": 6, "requires": { @@ -35,7 +35,7 @@ "sha256:07528381786f2a6237b061f6e96610a4167b226cb926e2aa2b6b1d78057c576b", "sha256:bf8fd333346d844f616e8d47905ef3a3384edae6b4e9beb0c5101e25e3110907" ], - "index": "pypi", + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'", "version": "==3.9.2" }, "importlib-metadata": { @@ -51,7 +51,7 @@ "sha256:0a943902919f65c5684ac4e0154b1ad4fac6dcaa5d9f3426b732f1c8b5419be6", "sha256:2bb1680aad211e3c9944dbce1d4ba09a989f04e238296c87fe2139faa26d655d" ], - "index": "pypi", + "markers": "python_version >= '3.6' and python_version < '4.0'", "version": "==5.8.0" }, "lazy-object-proxy": { @@ -114,7 +114,7 @@ "sha256:d65cc1df038ef55a99e617431f0553cd77763869eebdf9042403e16089fe746c", "sha256:d7da2e1d5f558c37d6e8c1246f1aec1e7349e4913d8fb3cb289a35de573fe2eb" ], - "index": "pypi", + "markers": "python_version >= '3.5'", "version": "==0.812" }, "mypy-extensions": { @@ -145,9 +145,13 @@ "sha256:586d8fa9b1891f4b725f587ef267abe2a1bad89d6b184520c7f07a253dd6e217", "sha256:f7e2072654a6b6afdf5e2fb38147d3e2d2d43c89f648637baab63e026481279b" ], - "index": "pypi", + "markers": "python_version ~= '3.6'", "version": "==2.8.2" }, + "qemu": { + "editable": true, + "path": "." + }, "toml": { "hashes": [ "sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b", diff --git a/python/README.rst b/python/README.rst index bf9bbca..9548709 100644 --- a/python/README.rst +++ b/python/README.rst @@ -24,6 +24,10 @@ which installs a version of the package that installs a forwarder pointing to these files, such that the package always reflects the latest version in your git tree. +Installing ".[devel]" instead of "." will additionally pull in required +packages for testing this package. They are not runtime requirements, +and are not needed to simply use these libraries. + See `Installing packages using pip and virtual environments `_ for more information. diff --git a/python/setup.cfg b/python/setup.cfg index 3f07bd2..39dc135 100644 --- a/python/setup.cfg +++ b/python/setup.cfg @@ -22,6 +22,15 @@ packages = qemu.machine qemu.utils +[options.extras_require] +# Run `pipenv lock --dev` when changing these requirements. +devel = + flake8 >= 3.6.0 + isort >= 5.1.2 + mypy >= 0.770 + pylint >= 2.8.0 + + [flake8] extend-ignore = E722 # Prefer pylint's bare-except checks to flake8's exclude = __pycache__, -- cgit v1.1 From 31622b2a8ac769b3cef730d3a24ed209e3861cbc Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:17:11 -0400 Subject: python: add avocado-framework and tests Try using avocado to manage our various tests; even though right now they're only invoking shell scripts and not really running any python-native code. Create tests/, and add shell scripts which call out to mypy, flake8, pylint and isort to enforce the standards in this directory. Add avocado-framework to the setup.cfg development dependencies, and add avocado.cfg to store some preferences for how we'd like the test output to look. Finally, add avocado-framework to the Pipfile environment and lock the new dependencies. We are using avocado >= 87.0 here to take advantage of some features that Cleber has helpfully added to make the test output here *very* friendly and easy to read for developers that might chance upon the output in Gitlab CI. [Note: ALL of the dependencies get updated to the most modern versions that exist at the time of this writing. No way around it that I have seen. Not ideal, but so it goes.] Provided you have the right development dependencies (mypy, flake8, isort, pylint, and now avocado-framework) You should be able to run "avocado --config avocado.cfg run tests/" from the python folder to run all of these linters with the correct arguments. (A forthcoming commit adds the much easier 'make check'.) Signed-off-by: John Snow Reviewed-by: Cleber Rosa Tested-by: Cleber Rosa Message-id: 20210527211715.394144-28-jsnow@redhat.com Signed-off-by: John Snow --- python/Pipfile.lock | 8 ++++++++ python/README.rst | 2 ++ python/avocado.cfg | 10 ++++++++++ python/setup.cfg | 1 + python/tests/flake8.sh | 2 ++ python/tests/isort.sh | 2 ++ python/tests/mypy.sh | 2 ++ python/tests/pylint.sh | 2 ++ 8 files changed, 29 insertions(+) create mode 100644 python/avocado.cfg create mode 100755 python/tests/flake8.sh create mode 100755 python/tests/isort.sh create mode 100755 python/tests/mypy.sh create mode 100755 python/tests/pylint.sh (limited to 'python') diff --git a/python/Pipfile.lock b/python/Pipfile.lock index a2cdc1c..6e344f5 100644 --- a/python/Pipfile.lock +++ b/python/Pipfile.lock @@ -30,6 +30,14 @@ "markers": "python_version ~= '3.6'", "version": "==2.5.6" }, + "avocado-framework": { + "hashes": [ + "sha256:42aa7962df98d6b78d4efd9afa2177226dc630f3d83a2a7d5baf7a0a7da7fa1b", + "sha256:d96ae343abf890e1ef3b3a6af5ce49e35f6bded0715770c4acb325bca555c515" + ], + "markers": "python_version >= '3.6'", + "version": "==88.1" + }, "flake8": { "hashes": [ "sha256:07528381786f2a6237b061f6e96610a4167b226cb926e2aa2b6b1d78057c576b", diff --git a/python/README.rst b/python/README.rst index 9548709..6bd2c6b 100644 --- a/python/README.rst +++ b/python/README.rst @@ -37,6 +37,8 @@ Files in this directory ----------------------- - ``qemu/`` Python package source directory. +- ``tests/`` Python package tests directory. +- ``avocado.cfg`` Configuration for the Avocado test-runner. - ``MANIFEST.in`` is read by python setuptools, it specifies additional files that should be included by a source distribution. - ``PACKAGE.rst`` is used as the README file that is visible on PyPI.org. diff --git a/python/avocado.cfg b/python/avocado.cfg new file mode 100644 index 0000000..10dc6fb --- /dev/null +++ b/python/avocado.cfg @@ -0,0 +1,10 @@ +[simpletests] +# Don't show stdout/stderr in the test *summary* +status.failure_fields = ['status'] + +[job] +# Don't show the full debug.log output; only select stdout/stderr. +output.testlogs.logfiles = ['stdout', 'stderr'] + +# Show full stdout/stderr only on tests that FAIL +output.testlogs.statuses = ['FAIL'] diff --git a/python/setup.cfg b/python/setup.cfg index 39dc135..fd32519 100644 --- a/python/setup.cfg +++ b/python/setup.cfg @@ -25,6 +25,7 @@ packages = [options.extras_require] # Run `pipenv lock --dev` when changing these requirements. devel = + avocado-framework >= 87.0 flake8 >= 3.6.0 isort >= 5.1.2 mypy >= 0.770 diff --git a/python/tests/flake8.sh b/python/tests/flake8.sh new file mode 100755 index 0000000..51e0788 --- /dev/null +++ b/python/tests/flake8.sh @@ -0,0 +1,2 @@ +#!/bin/sh -e +python3 -m flake8 diff --git a/python/tests/isort.sh b/python/tests/isort.sh new file mode 100755 index 0000000..4480405 --- /dev/null +++ b/python/tests/isort.sh @@ -0,0 +1,2 @@ +#!/bin/sh -e +python3 -m isort -c qemu/ diff --git a/python/tests/mypy.sh b/python/tests/mypy.sh new file mode 100755 index 0000000..5f980f5 --- /dev/null +++ b/python/tests/mypy.sh @@ -0,0 +1,2 @@ +#!/bin/sh -e +python3 -m mypy -p qemu diff --git a/python/tests/pylint.sh b/python/tests/pylint.sh new file mode 100755 index 0000000..4b10b34 --- /dev/null +++ b/python/tests/pylint.sh @@ -0,0 +1,2 @@ +#!/bin/sh -e +python3 -m pylint qemu/ -- cgit v1.1 From 6560379facf40e66fd8fbf4578f3d28f510167d8 Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:17:12 -0400 Subject: python: add Makefile for some common tasks Add "make venv" to create the pipenv-managed virtual environment that contains our explicitly pinned dependencies. Add "make check" to run the python linters [in the host execution environment]. Add "make venv-check" which combines the above two: create/update the venv, then run the linters in that explicitly managed environment. Add "make develop" which canonizes the runes needed to get both the linting pre-requisites (the "[devel]" part), and the editable live-install (the "-e" part) of these python libraries. make clean: delete miscellaneous python packaging output possibly created by pipenv, pip, or other python packaging utilities make distclean: delete the above, the .venv, and the editable "qemu" package forwarder (qemu.egg-info) if there is one. Signed-off-by: John Snow Reviewed-by: Cleber Rosa Tested-by: Cleber Rosa Message-id: 20210527211715.394144-29-jsnow@redhat.com Signed-off-by: John Snow --- python/Makefile | 43 +++++++++++++++++++++++++++++++++++++++++++ python/PACKAGE.rst | 6 ++++++ python/README.rst | 6 ++++++ 3 files changed, 55 insertions(+) create mode 100644 python/Makefile (limited to 'python') diff --git a/python/Makefile b/python/Makefile new file mode 100644 index 0000000..a9da168 --- /dev/null +++ b/python/Makefile @@ -0,0 +1,43 @@ +.PHONY: help venv venv-check check clean distclean develop + +help: + @echo "python packaging help:" + @echo "" + @echo "make venv: Create pipenv's virtual environment." + @echo " NOTE: Requires Python 3.6 and pipenv." + @echo " Will download packages from PyPI." + @echo " Hint: (On Fedora): 'sudo dnf install python36 pipenv'" + @echo "" + @echo "make venv-check: run linters using pipenv's virtual environment." + @echo " Hint: If you don't know which test to run, run this one!" + @echo "" + @echo "make develop: Install deps for 'make check', and" + @echo " the qemu libs in editable/development mode." + @echo "" + @echo "make check: run linters using the current environment." + @echo "" + @echo "make clean: remove package build output." + @echo "" + @echo "make distclean: remove venv files, qemu package forwarder," + @echo " built distribution files, and everything" + @echo " from 'make clean'." + +venv: .venv +.venv: Pipfile.lock + @PIPENV_VENV_IN_PROJECT=1 pipenv sync --dev --keep-outdated + @touch .venv + +venv-check: venv + @pipenv run make check + +develop: + pip3 install -e .[devel] + +check: + @avocado --config avocado.cfg run tests/ + +clean: + python3 setup.py clean --all + +distclean: clean + rm -rf qemu.egg-info/ .venv/ dist/ diff --git a/python/PACKAGE.rst b/python/PACKAGE.rst index 05ea778..b0b86cc 100644 --- a/python/PACKAGE.rst +++ b/python/PACKAGE.rst @@ -35,3 +35,9 @@ the report. Optional packages necessary for running code quality analysis for this package can be installed with the optional dependency group "devel": ``pip install qemu[devel]``. + +``make develop`` can be used to install this package in editable mode +(to the current environment) *and* bring in testing dependencies in one +command. + +``make check`` can be used to run the available tests. diff --git a/python/README.rst b/python/README.rst index 6bd2c6b..dcf9938 100644 --- a/python/README.rst +++ b/python/README.rst @@ -28,6 +28,9 @@ Installing ".[devel]" instead of "." will additionally pull in required packages for testing this package. They are not runtime requirements, and are not needed to simply use these libraries. +Running ``make develop`` will pull in all testing dependencies and +install QEMU in editable mode to the current environment. + See `Installing packages using pip and virtual environments `_ for more information. @@ -39,6 +42,9 @@ Files in this directory - ``qemu/`` Python package source directory. - ``tests/`` Python package tests directory. - ``avocado.cfg`` Configuration for the Avocado test-runner. + Used by ``make check`` et al. +- ``Makefile`` provides some common testing/installation invocations. + Try ``make help`` to see available targets. - ``MANIFEST.in`` is read by python setuptools, it specifies additional files that should be included by a source distribution. - ``PACKAGE.rst`` is used as the README file that is visible on PyPI.org. -- cgit v1.1 From f9c0600f0200528921c43ccb8a8a44c81825a343 Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:17:13 -0400 Subject: python: add .gitignore Ignore *Python* build and package output (build, dist, qemu.egg-info); these files are not created as part of a QEMU build. They are created by running the commands 'python3 setup.py ' when preparing tarballs to upload to e.g. PyPI. Ignore miscellaneous cached python confetti (mypy, pylint, et al) Ignore .idea (pycharm) .vscode, and .venv (pipenv et al). Signed-off-by: John Snow Reviewed-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Cleber Rosa Message-id: 20210527211715.394144-30-jsnow@redhat.com Signed-off-by: John Snow --- python/.gitignore | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 python/.gitignore (limited to 'python') diff --git a/python/.gitignore b/python/.gitignore new file mode 100644 index 0000000..4ed144c --- /dev/null +++ b/python/.gitignore @@ -0,0 +1,15 @@ +# linter/tooling cache +.mypy_cache/ +.cache/ + +# python packaging +build/ +dist/ +qemu.egg-info/ + +# editor config +.idea/ +.vscode/ + +# virtual environments (pipenv et al) +.venv/ -- cgit v1.1 From 3c8de38c8515a300b7842d95893b9e95caaa0ad6 Mon Sep 17 00:00:00 2001 From: John Snow Date: Thu, 27 May 2021 17:17:14 -0400 Subject: python: add tox support This is intended to be a manually run, non-CI script. Use tox to test the linters against all python versions from 3.6 to 3.10. This will only work if you actually have those versions installed locally, but Fedora makes this easy: > sudo dnf install python3.6 python3.7 python3.8 python3.9 python3.10 Unlike the pipenv tests (make venv-check), this pulls "whichever" versions of the python packages, so they are unpinned and may break as time goes on. In the case that breakages are found, setup.cfg should be amended accordingly to avoid the bad dependant versions, or the code should be amended to work around the issue. With confidence that the tests pass on 3.6 through 3.10 inclusive, add the appropriate classifiers to setup.cfg to indicate which versions we claim to support. Tox 3.18.0 or above is required to use the 'allowlist_externals' option. Signed-off-by: John Snow Reviewed-by: Cleber Rosa Tested-by: Cleber Rosa Message-id: 20210527211715.394144-31-jsnow@redhat.com Signed-off-by: John Snow --- python/.gitignore | 1 + python/Makefile | 7 ++++++- python/setup.cfg | 23 ++++++++++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) (limited to 'python') diff --git a/python/.gitignore b/python/.gitignore index 4ed144c..272ed22 100644 --- a/python/.gitignore +++ b/python/.gitignore @@ -13,3 +13,4 @@ qemu.egg-info/ # virtual environments (pipenv et al) .venv/ +.tox/ diff --git a/python/Makefile b/python/Makefile index a9da168..b5621b0 100644 --- a/python/Makefile +++ b/python/Makefile @@ -16,6 +16,8 @@ help: @echo "" @echo "make check: run linters using the current environment." @echo "" + @echo "make check-tox: run linters using multiple python versions." + @echo "" @echo "make clean: remove package build output." @echo "" @echo "make distclean: remove venv files, qemu package forwarder," @@ -36,8 +38,11 @@ develop: check: @avocado --config avocado.cfg run tests/ +check-tox: + @tox + clean: python3 setup.py clean --all distclean: clean - rm -rf qemu.egg-info/ .venv/ dist/ + rm -rf qemu.egg-info/ .venv/ .tox/ dist/ diff --git a/python/setup.cfg b/python/setup.cfg index fd32519..0fcdec6 100644 --- a/python/setup.cfg +++ b/python/setup.cfg @@ -14,6 +14,11 @@ classifiers = Natural Language :: English Operating System :: OS Independent Programming Language :: Python :: 3 :: Only + Programming Language :: Python :: 3.6 + Programming Language :: Python :: 3.7 + Programming Language :: Python :: 3.8 + Programming Language :: Python :: 3.9 + Programming Language :: Python :: 3.10 [options] python_requires = >= 3.6 @@ -30,12 +35,13 @@ devel = isort >= 5.1.2 mypy >= 0.770 pylint >= 2.8.0 - + tox >= 3.18.0 [flake8] extend-ignore = E722 # Prefer pylint's bare-except checks to flake8's exclude = __pycache__, .venv, + .tox, [mypy] strict = True @@ -79,3 +85,18 @@ include_trailing_comma=True line_length=72 lines_after_imports=2 multi_line_output=3 + +# tox (https://tox.readthedocs.io/) is a tool for running tests in +# multiple virtualenvs. This configuration file will run the test suite +# on all supported python versions. To use it, "pip install tox" and +# then run "tox" from this directory. You will need all of these versions +# of python available on your system to run this test. + +[tox:tox] +envlist = py36, py37, py38, py39, py310 + +[testenv] +allowlist_externals = make +deps = .[devel] +commands = + make check -- cgit v1.1