Skip to contentSkip to conversation

python: create installable package

PATCH Superseded John Snow 1 participant 26 messages Feb 18, 2021

v5 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

John Snow

author
#0 lore raw

v5 cover python: create installable package

This series factors the python/qemu directory as an installable package. It does not yet actually change the mechanics of how any other python source in the tree actually consumes it (yet), beyond the import path. (some import statements change in a few places.)

git: https://gitlab.com/jsnow/qemu/-/commits/python-package-mk3

The primary motivation of this series is primarily to formalize our dependencies on mypy, flake8, isort, and pylint alongside versions that are known to work. It does this using the setup.cfg and setup.py files. It also adds explicitly pinned versions (using Pipfile.lock) of these dependencies that should behave in a repeatable and known way for developers and CI environments both. Lastly, it enables those CI checks such that we can enforce Python coding quality checks via the CI tests.

An auxiliary motivation is that this package is formatted in such a way that it COULD be uploaded to https://pypi.org/project/qemu and installed independently of qemu.git with pip install qemu, but that button remains *unpushed* and this series *will not* cause any such releases. We have time to debate finer points like API guarantees and versioning even after this series is merged.

Some other things this enables that might be of interest:

With the python tooling as a proper package, you can install this package in editable or production mode to a virtual environment, your local user environment, or your system packages. The primary benefit of this is to gain access to QMP tooling regardless of CWD, without needing to battle sys.path (and confounding other python analysis tools).

For example: when developing, you may go to qemu/python/ and run make venv followed by pipenv shell to activate a virtual environment that contains the qemu python packages. These packages will always reflect the current version of the source files in the tree. When you are finished, you can simply exit the shell (^d) to remove these packages from your python environment.

When not developing, you could install a version of this package to your environment outright to gain access to the QMP and QEMUMachine classes for lightweight scripting and testing by using pip: "pip install [--user] ."

TESTING THIS SERIES:

First of all, nothing should change. Without any intervention, everything should behave exactly as it was before. The only new information here comes from how to interact with and run the linters that will be enforcing code quality standards in this subdirectory.

To test those, CD to qemu/python first, and then:

1. Try "make venv && pipenv shell" to get a venv with the package installed to it in editable mode. Ctrl+d exits this venv shell. While in this shell, any python script that uses "from qemu.[qmp|machine] import ..." should work correctly regardless of where the script is, or what your CWD is.

You will need Python 3.6 installed on your system to do this step. For
Fedora: "dnf install python36" will do the trick.

2. Try "make check" while still in the shell to run the Python linters using the venv built in the previous step. This will pull some packages from PyPI and run pytest, which will in turn execute mypy, flake8, isort and pylint with the correct arguments.

3. Having exited the shell from above, try "make venv-check". This will create and update the venv if needed, then run 'make check' within the context of that shell. It should pass as long as the above did.

4. Still outside of the venv, you may try running "make check". This will not install anything, but unless you have the right Python dependencies installed, these tests may fail for you. You might try using "pip install --user .[devel]" to install the development packages needed to run the tests successfully to your local user's python environment. Once done, you will probably want to "pip uninstall qemu" to remove that package to avoid it interfering with other things.

5. "make distclean" will delete the venv and any temporary files that may have been created by packaging, installing, testing, etc.

6. You may also (if you wish) create your own environment using a Python other than 3.6, bypassing pipenv. This may be useful for environments in which you simply do not have Python 3.6 readily available.

Using the Python of your choice:

Quoted 7 lines

make distclean # (Remove the .venv, if you created one.) /usr/bin/python3.9 -m venv .venv
source .venv/bin/activate # (or activate.[fish|csh])

make develop  # Installs linter deps to this venv
make check    # Runs tests using current env
deactivate    # Leaves venv.

This *should* pass, and if it doesn't, it's a bug. Due to dependencies changing like shifting sands, it's impossible to promise support for every last combination of dependency and python version. This is why the "make venv-check" target uses Python 3 and a very explicit set of packages instead. Still, this form *should* pass.

Changelog
=========

V5:

001/25:[----] [--] 'python/console_socket: avoid one-letter variable' 002/25:[----] [--] 'iotests/297: add --namespace-packages to mypy arguments' 003/25:[0009] [FC] 'python: create qemu packages'
004/25:[0012] [FC] 'python: create utils sub-package' 005/25:[0002] [FC] 'python: add qemu package installer' 006/25:[----] [--] 'python: add VERSION file'
007/25:[0012] [FC] 'python: add directory structure README.rst files' 008/25:[----] [--] 'python: Add pipenv support'
009/25:[----] [--] 'python: add pylint import exceptions' 010/25:[----] [--] 'python: move pylintrc into setup.cfg' 011/25:[----] [--] 'python: add pylint to pipenv'
012/25:[----] [--] 'python: move flake8 config to setup.cfg' 013/25:[----] [-C] 'python: add excluded dirs to flake8 config' 014/25:[----] [--] 'python: Add flake8 to pipenv'
015/25:[----] [-C] 'python: move mypy.ini into setup.cfg' 016/25:[----] [-C] 'python: add mypy to pipenv'
017/25:[----] [--] 'python: move .isort.cfg into setup.cfg' 018/25:[----] [--] 'python/qemu: add isort to pipenv' 019/25:[----] [--] 'python/qemu: add qemu package itself to pipenv' 020/25:[----] [-C] 'python: add devel package requirements to setuptools' 021/25:[----] [--] 'python: add pytest and tests'
022/25:[0010] [FC] 'python: add Makefile for some common tasks' 023/25:[----] [--] 'python: add .gitignore'
024/25:[----] [--] 'gitlab: add python linters to CI' 025/25:[down] 'python: add 'make develop' target'

- Shuffled flake8 patch to the flake8 region of the patchset
(Didn't move the pylint patch down: It was annoying and stands on its own.) Added RB/TB tags where applicable, see chart below this section.
03: Changed copyright year
Updated import fixes for latest origin/master
04: Updated import fixes for latest origin/master
05: Updated commit message ("ReST" vs "rst")
Added information on PEP517/518 adoption to commit message.
07: Make clear that "qemu.qmp", "qemu.machine" are not exhaustive.
Replace "amend" by "append"
Drastically shorten python/qemu/utils/README.rst
22: Update Makefile with new Hint for which test you should run if unsure.
Fix typo (qemu.egg.info => qemu.egg-info)
Have "make venv" touch .venv to avoid redundant checking where possible Update help text for what "make distclean" is removing.
25: New, just to facilitate creating your own venv, in the case
that you cannot easily install python 3.6, but still want
to run tests on your local machine easily.

Current Status
==============

+ [01] python-console_socket-avoid    # [RB] CR [SOB] JS
+ [02] iotests-297-add-namespace      # [RB] CR [SOB] JS
+ [03] python-create-qemu-packages    # [SOB] JS
+ [04] python-create-utils-sub        # [SOB] JS
+ [05] python-add-qemu-package        # [SOB] JS
+ [06] python-add-version-file        # [RB] CR [SOB] JS
+ [07] python-add-directory-structure # [SOB] JS
+ [08] python-add-pipenv-support      # [RB] CR [SOB] JS
+ [09] python-add-pylint-import       # [RB] CR [SOB] JS
+ [10] python-move-pylintrc-into      # [RB] CR [TB] CR [SOB] JS
+ [11] python-add-pylint-to-pipenv    # [RB] CR [TB] CR [SOB] JS
+ [12] python-move-flake8-config-to   # [RB] CR [SOB] JS
+ [13] python-add-excluded-dirs-to    # [SOB] JS
+ [14] python-add-flake8-to-pipenv    # [RB] CR [TB] CR [SOB] JS
+ [15] python-move-mypy-ini-into      # [SOB] JS
+ [16] python-add-mypy-to-pipenv      # [RB] CR [TB] CR [SOB] JS
+ [17] python-move-isort-cfg-into     # [RB] CR [SOB] JS
+ [18] python-qemu-add-isort-to       # [RB] CR,PM [TB] CR [SOB] JS
+ [19] python-qemu-add-qemu-package   # [RB] CR [TB] CR [SOB] JS
+ [20] python-add-devel-package       # [SOB] JS
+ [21] python-add-pytest-and-tests    # [SOB] JS
+ [22] python-add-makefile-for-some   # [SOB] JS
+ [23] python-add-gitignore           # [SOB] JS
+ [24] gitlab-add-python-linters-to   # [SOB] JS
Quoted 1 line

[25] python-add-make-develop-target # [SOB] JS

Looking for reviews on:

+ [03] python-create-qemu-packages    # python: create qemu packages
+ [04] python-create-utils-sub        # python: create utils sub-package
+ [05] python-add-qemu-package        # python: add qemu package installer
+ [07] python-add-directory-structure # python: add directory structure README.rst files
+ [13] python-add-excluded-dirs-to    # python: add excluded dirs to flake8 config
+ [15] python-move-mypy-ini-into      # python: move mypy.ini into setup.cfg
+ [20] python-add-devel-package       # python: add devel package requirements to setuptools
+ [21] python-add-pytest-and-tests    # python: add pytest and tests
+ [22] python-add-makefile-for-some   # python: add Makefile for some common tasks
+ [23] python-add-gitignore           # python: add .gitignore
+ [24] gitlab-add-python-linters-to   # gitlab: add python linters to CI
Quoted 1 line

[25] python-add-make-develop-target # python: add 'make develop' target

Misc Notes
==========

Reviewer notes:

- The VERSION hack may be imperfect, but at 0.x and without uploading it to PyPI, we have *all* the time in the world to fine-tune it later.
- The CI integration may not be perfect, but it is better than *nothing*, so I think it's worth doing even in an imperfect state.
- Patch 25 is just a bonus, because I can't stop myself from making changes.

John Snow (25):
python/console_socket: avoid one-letter variable iotests/297: add --namespace-packages to mypy arguments
python: create qemu packages
python: create utils sub-package
python: add qemu package installer
python: add VERSION file
python: add directory structure README.rst files
python: Add pipenv support
python: add pylint import exceptions
python: move pylintrc into setup.cfg
python: add pylint to pipenv
python: move flake8 config to setup.cfg
python: add excluded dirs to flake8 config
python: Add flake8 to pipenv
python: move mypy.ini into setup.cfg
python: add mypy to pipenv
python: move .isort.cfg into setup.cfg python/qemu: add isort to pipenv
python/qemu: add qemu package itself to pipenv
python: add devel package requirements to setuptools
python: add pytest and tests
python: add Makefile for some common tasks
python: add .gitignore
gitlab: add python linters to CI
python: add 'make develop' target

 python/PACKAGE.rst                          |  32 +++
 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 +
 .gitlab-ci.yml                              |  10 +
 python/.gitignore                           |   9 +
 python/Makefile                             |  42 +++
 python/Pipfile                              |  13 +
 python/Pipfile.lock                         | 285 ++++++++++++++++++++
 python/VERSION                              |   1 +
 python/mypy.ini                             |   4 -
 python/qemu/.flake8                         |   2 -
 python/qemu/.isort.cfg                      |   7 -
 python/qemu/__init__.py                     |  11 -
 python/qemu/machine/__init__.py             |  36 +++
 python/qemu/{ => machine}/console_socket.py |  10 +-
 python/qemu/{ => machine}/machine.py        |  16 +-
 python/qemu/{ => machine}/qtest.py          |   3 +-
 python/qemu/pylintrc                        |  58 ----
 python/qemu/{qmp.py => qmp/__init__.py}     |  12 +-
 python/qemu/utils/__init__.py               |  23 ++
 python/qemu/{ => utils}/accel.py            |   0
 python/setup.cfg                            |  82 ++++++
 python/setup.py                             |  23 ++
 python/tests/test_lint.py                   |  28 ++
 tests/acceptance/avocado_qemu/__init__.py   |   4 +-
 tests/acceptance/virtio-gpu.py              |   2 +-
 tests/docker/dockerfiles/fedora.docker      |   2 +
 tests/qemu-iotests/297                      |   1 +
 tests/qemu-iotests/300                      |   4 +-
 tests/qemu-iotests/iotests.py               |   2 +-
 tests/vm/aarch64vm.py                       |   2 +-
 tests/vm/basevm.py                          |   2 +-
 35 files changed, 698 insertions(+), 102 deletions(-)
 create mode 100644 python/PACKAGE.rst
 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
 create mode 100644 python/.gitignore
 create mode 100644 python/Makefile
 create mode 100644 python/Pipfile
 create mode 100644 python/Pipfile.lock
 create mode 100644 python/VERSION
 delete mode 100644 python/mypy.ini
 delete mode 100644 python/qemu/.flake8
 delete mode 100644 python/qemu/.isort.cfg
 delete mode 100644 python/qemu/__init__.py
 create mode 100644 python/qemu/machine/__init__.py
 rename python/qemu/{ => machine}/console_socket.py (95%)
 rename python/qemu/{ => machine}/machine.py (98%)
 rename python/qemu/{ => machine}/qtest.py (98%)
 delete mode 100644 python/qemu/pylintrc
 rename python/qemu/{qmp.py => qmp/__init__.py} (96%)
 create mode 100644 python/qemu/utils/__init__.py
 rename python/qemu/{ => utils}/accel.py (100%)
 create mode 100644 python/setup.cfg
 create mode 100755 python/setup.py
 create mode 100644 python/tests/test_lint.py
Signature · 1 line
2.29.2
author
#1 lore raw

PATCH v5 1/25 python/console_socket: avoid one-letter variable

Fixes pylint warnings.

---
 python/qemu/console_socket.py | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
python/qemu/console_socket.py +5 -5
index ac21130e446..87237bebef7 100644
@@ -46,11 +46,11 @@ def __init__(self, address: str, file: Optional[str] = None,
             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."""
Signature · 1 line
2.29.2
author
#2 lore raw

PATCH v5 2/25 iotests/297: add --namespace-packages to mypy arguments

In reply to John Snow

mypy is kind of weird about how it handles imports. For legacy reasons, it won't load PEP 420 namespaces, because of logic implemented prior to that becoming a standard.

So, if you plan on using any, you have to pass
--namespace-packages. Alright, fine.

---
 tests/qemu-iotests/297 | 1 +
 1 file changed, 1 insertion(+)
tests/qemu-iotests/297 +1 -0
index a37910b42d9..433b7323368 100755
@@ -95,6 +95,7 @@ def run_linters():
                             '--warn-redundant-casts',
                             '--warn-unused-ignores',
                             '--no-implicit-reexport',
+                            '--namespace-packages',
                             filename),
                            env=env,
                            check=False,
Signature · 1 line
2.29.2
author
#3 lore raw

PATCH v5 3/25 python: create qemu packages

In reply to John Snow

move python/qemu/*.py to python/qemu/[machine, qmp]/*.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' and 'qmp' 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".

---

Note for reviewers: in the next patch, I add a utils sub-package and move qemu/machine/accel.py to qemu/utils/accel.py. If we like it that way, we can squash it in here if we want, or just leave it as its own follow-up patch, I am just leaving it as something that will be easy to un-do or change for now.

---
 python/{qemu => }/.isort.cfg                |  0
 python/qemu/__init__.py                     | 11 ------
 python/qemu/{ => machine}/.flake8           |  0
 python/qemu/machine/__init__.py             | 41 +++++++++++++++++++++
 python/qemu/{ => machine}/accel.py          |  0
 python/qemu/{ => machine}/console_socket.py |  0
 python/qemu/{ => machine}/machine.py        | 16 +++++---
 python/qemu/{ => machine}/pylintrc          |  0
 python/qemu/{ => machine}/qtest.py          |  3 +-
 python/qemu/{qmp.py => qmp/__init__.py}     | 12 +++++-
 tests/acceptance/avocado_qemu/__init__.py   |  4 +-
 tests/acceptance/virtio-gpu.py              |  2 +-
 tests/qemu-iotests/300                      |  4 +-
 tests/qemu-iotests/iotests.py               |  2 +-
 tests/vm/aarch64vm.py                       |  2 +-
 tests/vm/basevm.py                          |  3 +-
 16 files changed, 73 insertions(+), 27 deletions(-)
 rename python/{qemu => }/.isort.cfg (100%)
 delete mode 100644 python/qemu/__init__.py
 rename python/qemu/{ => machine}/.flake8 (100%)
 create mode 100644 python/qemu/machine/__init__.py
 rename python/qemu/{ => machine}/accel.py (100%)
 rename python/qemu/{ => machine}/console_socket.py (100%)
 rename python/qemu/{ => machine}/machine.py (98%)
 rename python/qemu/{ => machine}/pylintrc (100%)
 rename python/qemu/{ => machine}/qtest.py (99%)
 rename python/qemu/{qmp.py => qmp/__init__.py} (96%)
python/.isort.cfg renamed from python/qemu/.isort.cfg +0 -0
similarity index 100%
rename from python/qemu/.isort.cfg
rename to python/.isort.cfg
python/qemu/__init__.py deleted +0 -11
deleted file mode 100644
index 4ca06c34a41..00000000000
@@ -1,11 +0,0 @@
-# QEMU library
-#
-# Copyright (C) 2015-2016 Red Hat Inc.
-# Copyright (C) 2012 IBM Corp.
-#
-# Authors:
-#  Fam Zheng <[email protected]>
-#
-# This work is licensed under the terms of the GNU GPL, version 2.  See
-# the COPYING file in the top-level directory.
-#
python/qemu/machine/.flake8 renamed from python/qemu/.flake8 +0 -0
similarity index 100%
rename from python/qemu/.flake8
rename to python/qemu/machine/.flake8
python/qemu/machine/__init__.py new file +41 -0

41 lines not on this page · raw message

python/qemu/machine/accel.py renamed from python/qemu/accel.py +0 -0
similarity index 100%
rename from python/qemu/accel.py
rename to python/qemu/machine/accel.py
python/qemu/machine/console_socket.py renamed from python/qemu/console_socket.py +0 -0
similarity index 100%
rename from python/qemu/console_socket.py
rename to python/qemu/machine/console_socket.py
python/qemu/machine/machine.py renamed from python/qemu/machine.py +11 -5
similarity index 98%
rename from python/qemu/machine.py
rename to python/qemu/machine/machine.py
index 6e44bda337e..cc0d2bb23f9 100644
@@ -38,8 +38,14 @@
     Type,
 )
 
-from . import console_socket, qmp
-from .qmp import QMPMessage, QMPReturnValue, SocketAddrT
+from qemu.qmp import (
+    QEMUMonitorProtocol,
+    QMPMessage,
+    QMPReturnValue,
+    SocketAddrT,
+)
+
+from . import console_socket
 
 
 LOG = logging.getLogger(__name__)
@@ -139,7 +145,7 @@ def __init__(self,
         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._qmp_connection: Optional[QEMUMonitorProtocol] = None
         self._qemu_full_args: Tuple[str, ...] = ()
         self._temp_dir: Optional[str] = None
         self._launched = False
@@ -315,7 +321,7 @@ def _pre_launch(self) -> None:
             if self._remove_monitor_sockfile:
                 assert isinstance(self._monitor_address, str)
                 self._remove_files.append(self._monitor_address)
-            self._qmp_connection = qmp.QEMUMonitorProtocol(
+            self._qmp_connection = QEMUMonitorProtocol(
                 self._monitor_address,
                 server=True,
                 nickname=self._name
@@ -535,7 +541,7 @@ def set_qmp_monitor(self, enabled: bool = True) -> None:
         self._qmp_set = enabled
 
     @property
-    def _qmp(self) -> qmp.QEMUMonitorProtocol:
+    def _qmp(self) -> QEMUMonitorProtocol:
         if self._qmp_connection is None:
             raise QEMUMachineError("Attempt to access QMP with no connection")
         return self._qmp_connection
python/qemu/machine/pylintrc renamed from python/qemu/pylintrc +0 -0
similarity index 100%
rename from python/qemu/pylintrc
rename to python/qemu/machine/pylintrc
python/qemu/machine/qtest.py renamed from python/qemu/qtest.py +2 -1
similarity index 99%
rename from python/qemu/qtest.py
rename to python/qemu/machine/qtest.py
index 39a0cf62fe9..53926e434a7 100644
@@ -26,8 +26,9 @@
     TextIO,
 )
 
+from qemu.qmp import SocketAddrT
+
 from .machine import QEMUMachine
-from .qmp import SocketAddrT
 
 
 class QEMUQtestProtocol:
python/qemu/qmp/__init__.py renamed from python/qemu/qmp.py +11 -1
similarity index 96%
rename from python/qemu/qmp.py
rename to python/qemu/qmp/__init__.py
index 2cd4d43036c..9606248a3d2 100644
@@ -1,4 +1,14 @@
-""" QEMU Monitor Protocol Python class """
+"""
+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:
tests/acceptance/avocado_qemu/__init__.py +2 -2
index df167b142cc..cdd597ae0c4 100644
@@ -40,9 +40,9 @@
 
 sys.path.append(os.path.join(SOURCE_DIR, 'python'))
 
-from qemu.accel import kvm_available
-from qemu.accel import tcg_available
 from qemu.machine import QEMUMachine
+from qemu.machine import kvm_available
+from qemu.machine import tcg_available
 
 def is_readable_executable_file(path):
     return os.path.isfile(path) and os.access(path, os.R_OK | os.X_OK)
tests/acceptance/virtio-gpu.py +1 -1
index ab1a4c1a718..a2f044d3f00 100644
@@ -10,7 +10,7 @@
 from avocado_qemu import exec_command_and_wait_for_pattern
 from avocado_qemu import is_readable_executable_file
 
-from qemu.accel import kvm_available
+from qemu.machine import kvm_available
 
 import os
 import socket
tests/qemu-iotests/300 +2 -2
index 63036f6a6e1..5b9883bd950 100755
@@ -28,7 +28,7 @@ import iotests
 
 # Import qemu after iotests.py has amended sys.path
 # pylint: disable=wrong-import-order
-import qemu
+from qemu.machine import machine
 
 BlockBitmapMapping = List[Dict[str, Union[str, List[Dict[str, str]]]]]
 
@@ -466,7 +466,7 @@ class TestBlockBitmapMappingErrors(TestDirtyBitmapMigration):
         # the failed migration
         try:
             self.vm_b.shutdown()
-        except qemu.machine.AbnormalShutdown:
+        except machine.AbnormalShutdown:
             pass
 
     def test_aliased_bitmap_name_too_long(self) -> None:
tests/qemu-iotests/iotests.py +1 -1
index 4e758308f24..0bfcb03508a 100644
@@ -37,7 +37,7 @@
 
 # pylint: disable=import-error, wrong-import-position
 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python'))
-from qemu import qtest
+from qemu.machine import qtest
 from qemu.qmp import QMPMessage
 
 # Use this logger for logging messages directly from the iotests module
tests/vm/aarch64vm.py +1 -1
index d70ab843b6b..39ff99b0859 100644
@@ -14,7 +14,7 @@
 import sys
 import subprocess
 import basevm
-from qemu.accel import kvm_available
+from qemu.machine import kvm_available
 
 # This is the config needed for current version of QEMU.
 # This works for both kvm and tcg.
tests/vm/basevm.py +1 -2
index 00f1d5ca8da..12d08cf2b1b 100644
@@ -19,8 +19,7 @@
 import time
 import datetime
 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python'))
-from qemu.accel import kvm_available
-from qemu.machine import QEMUMachine
+from qemu.machine import kvm_available, QEMUMachine
 import subprocess
 import hashlib
 import argparse
Signature · 1 line
2.29.2
author
#4 lore raw

PATCH v5 4/25 python: create utils sub-package

In reply to John Snow

Create a space for miscellaneous things that don't belong strictly in "qemu.machine" nor "qemu.qmp" packages.

---
 python/qemu/machine/__init__.py           |  8 --------
 python/qemu/utils/__init__.py             | 23 +++++++++++++++++++++++
 python/qemu/{machine => utils}/accel.py   |  0
 tests/acceptance/avocado_qemu/__init__.py |  4 ++--
 tests/acceptance/virtio-gpu.py            |  2 +-
 tests/vm/aarch64vm.py                     |  2 +-
 tests/vm/basevm.py                        |  3 ++-
 7 files changed, 29 insertions(+), 13 deletions(-)
 create mode 100644 python/qemu/utils/__init__.py
 rename python/qemu/{machine => utils}/accel.py (100%)
python/qemu/machine/__init__.py +0 -8
index 0ac6c1e36e3..98302ea31e7 100644
@@ -8,10 +8,6 @@
  - QEMUQtestMachine: VM class, with a qtest socket.
 
 - QEMUQtestProtocol: Connect to, send/receive qtest messages.
-
-- list_accel: List available accelerators
-- kvm_available: Probe for KVM support
-- tcg_available: Probe for TCG support
 """
 
 # Copyright (C) 2020-2021 John Snow for Red Hat Inc.
@@ -26,15 +22,11 @@
 # the COPYING file in the top-level directory.
 #
 
-from .accel import kvm_available, list_accel, tcg_available
 from .machine import QEMUMachine
 from .qtest import QEMUQtestMachine, QEMUQtestProtocol
 
 
 __all__ = (
-    'list_accel',
-    'kvm_available',
-    'tcg_available',
     'QEMUMachine',
     'QEMUQtestProtocol',
     'QEMUQtestMachine',
python/qemu/utils/__init__.py new file +23 -0
new file mode 100644
index 00000000000..edf807a93e5
@@ -0,0 +1,23 @@
+"""
+QEMU development and testing utilities
+
+This library provides a small handful of utilities for performing various tasks
+not directly related to the launching of a VM.
+
+The only module included at present is accel; its public functions are
+repeated here for your convenience:
+
+- list_accel: List available accelerators
+- kvm_available: Probe for KVM support
+- tcg_available: Prove for TCG support
+"""
+
+# pylint: disable=import-error
+from .accel import kvm_available, list_accel, tcg_available
+
+
+__all__ = (
+    'list_accel',
+    'kvm_available',
+    'tcg_available',
+)
python/qemu/utils/accel.py renamed from python/qemu/machine/accel.py +0 -0
similarity index 100%
rename from python/qemu/machine/accel.py
rename to python/qemu/utils/accel.py
tests/acceptance/avocado_qemu/__init__.py +2 -2
index cdd597ae0c4..5e072e738cf 100644
@@ -41,8 +41,8 @@
 sys.path.append(os.path.join(SOURCE_DIR, 'python'))
 
 from qemu.machine import QEMUMachine
-from qemu.machine import kvm_available
-from qemu.machine import tcg_available
+from qemu.utils import kvm_available
+from qemu.utils import tcg_available
 
 def is_readable_executable_file(path):
     return os.path.isfile(path) and os.access(path, os.R_OK | os.X_OK)
tests/acceptance/virtio-gpu.py +1 -1
index a2f044d3f00..7023c0953db 100644
@@ -10,7 +10,7 @@
 from avocado_qemu import exec_command_and_wait_for_pattern
 from avocado_qemu import is_readable_executable_file
 
-from qemu.machine import kvm_available
+from qemu.utils import kvm_available
 
 import os
 import socket
tests/vm/aarch64vm.py +1 -1
index 39ff99b0859..b00cce07eb8 100644
@@ -14,7 +14,7 @@
 import sys
 import subprocess
 import basevm
-from qemu.machine import kvm_available
+from qemu.utils import kvm_available
 
 # This is the config needed for current version of QEMU.
 # This works for both kvm and tcg.
tests/vm/basevm.py +2 -1
index 12d08cf2b1b..a3867fdf88e 100644
@@ -19,7 +19,8 @@
 import time
 import datetime
 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python'))
-from qemu.machine import kvm_available, QEMUMachine
+from qemu.machine import QEMUMachine
+from qemu.utils import kvm_available
 import subprocess
 import hashlib
 import argparse
Signature · 1 line
2.29.2
author
#5 lore raw

PATCH v5 5/25 python: add qemu package installer

In reply to John Snow

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.

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.

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

---
 python/PACKAGE.rst | 32 ++++++++++++++++++++++++++++++++
 python/setup.cfg   | 19 +++++++++++++++++++
 python/setup.py    | 23 +++++++++++++++++++++++
 3 files changed, 74 insertions(+)
 create mode 100644 python/PACKAGE.rst
 create mode 100644 python/setup.cfg
 create mode 100755 python/setup.py
python/PACKAGE.rst new file +32 -0
new file mode 100644
index 00000000000..0e714c87eb3
@@ -0,0 +1,32 @@
+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 <[email protected]> as part of
+the QEMU source tree. Contributions are welcome and follow the `QEMU
+patch submission process
+<https://wiki.qemu.org/Contribute/SubmitAPatch>`_, which involves
+sending patches to the QEMU development mailing list.
+
+John maintains a `GitLab staging branch
+<https://gitlab.com/jsnow/qemu/-/tree/python>`_, and there is an
+official `GitLab mirror <https://gitlab.com/qemu-project/qemu>`_.
+
+Please report bugs by sending a mail to [email protected] and CC
[email protected].
python/setup.cfg new file +19 -0
new file mode 100644
index 00000000000..dd71640fc2f
@@ -0,0 +1,19 @@
+[metadata]
+name = qemu
+maintainer = QEMU Developer Team
+maintainer_email = [email protected]
+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 = find_namespace:
python/setup.py new file +23 -0
new file mode 100755
index 00000000000..2014f81b757
@@ -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()
Signature · 1 line
2.29.2
author
#6 lore raw

PATCH v5 6/25 python: add VERSION file

In reply to John Snow

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.

---
 python/VERSION   | 1 +
 python/setup.cfg | 1 +
 2 files changed, 2 insertions(+)
 create mode 100644 python/VERSION
python/VERSION new file +1 -0
new file mode 100644
index 00000000000..ffc91e96185
@@ -0,0 +1 @@
+0.6.0.0a1
python/setup.cfg +1 -0
index dd71640fc2f..e7f8ab23815 100644
@@ -1,5 +1,6 @@
 [metadata]
 name = qemu
+version = file:VERSION
 maintainer = QEMU Developer Team
 maintainer_email = [email protected]
 url = https://www.qemu.org/
Signature · 1 line
2.29.2
author
#7 lore raw

PATCH v5 7/25 python: add directory structure README.rst files

In reply to John Snow

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.

---
 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
python/README.rst new file +41 -0
new file mode 100644
index 00000000000..7a0dc5dff4a
@@ -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.
+
+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
+<https://packaging.python.org/guides/installing-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.
python/qemu/README.rst new file +8 -0
new file mode 100644
index 00000000000..d04943f526c
@@ -0,0 +1,8 @@
+QEMU Python Namespace
+=====================
+
+This directory serves as the root of a `Python PEP 420 implicit
+namespace package <https://www.python.org/dev/peps/pep-0420/>`_.
+
+Each directory below is assumed to be an installable Python package that
+is available under the ``qemu.<package>`` namespace.
python/qemu/machine/README.rst new file +9 -0
new file mode 100644
index 00000000000..ac2b4fffb42
@@ -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.
python/qemu/qmp/README.rst new file +9 -0
new file mode 100644
index 00000000000..c21951491cf
@@ -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.
python/qemu/utils/README.rst new file +7 -0
new file mode 100644
index 00000000000..975fbf4d7de
@@ -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.
Signature · 1 line
2.29.2
author
#8 lore raw

PATCH v5 8/25 python: Add pipenv support

In reply to John Snow

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.py 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 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.

---
 python/Pipfile | 11 +++++++++++
 1 file changed, 11 insertions(+)
 create mode 100644 python/Pipfile
python/Pipfile new file +11 -0
new file mode 100644
index 00000000000..9534830b5eb
@@ -0,0 +1,11 @@
+[[source]]
+name = "pypi"
+url = "https://pypi.org/simple"
+verify_ssl = true
+
+[dev-packages]
+
+[packages]
+
+[requires]
+python_version = "3.6"
Signature · 1 line
2.29.2
author
#9 lore raw

PATCH v5 9/25 python: add pylint import exceptions

In reply to John Snow

Pylint 2.5.x and 2.6.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.

---
 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(-)
python/qemu/machine/__init__.py +3 -0
index 98302ea31e7..728f27adbed 100644
@@ -22,6 +22,9 @@
 # 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
 
python/qemu/machine/machine.py +1 -1
index cc0d2bb23f9..498d49e7f04 100644
@@ -38,7 +38,7 @@
     Type,
 )
 
-from qemu.qmp import (
+from qemu.qmp import (  # pylint: disable=import-error
     QEMUMonitorProtocol,
     QMPMessage,
     QMPReturnValue,
python/qemu/machine/qtest.py +1 -1
index 53926e434a7..c3adf4e3012 100644
@@ -26,7 +26,7 @@
     TextIO,
 )
 
-from qemu.qmp import SocketAddrT
+from qemu.qmp import SocketAddrT  # pylint: disable=import-error
 
 from .machine import QEMUMachine
 
Signature · 1 line
2.29.2
author
#10 lore raw

PATCH v5 10/25 python: move pylintrc into setup.cfg

In reply to John Snow

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".

---
 python/qemu/machine/pylintrc | 58 ------------------------------------
 python/setup.cfg             | 29 ++++++++++++++++++
 2 files changed, 29 insertions(+), 58 deletions(-)
 delete mode 100644 python/qemu/machine/pylintrc
python/qemu/machine/pylintrc deleted +0 -58
deleted file mode 100644
index 3f69205000d..00000000000
@@ -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]
python/setup.cfg +29 -0
index e7f8ab23815..20b24372a4a 100644
@@ -18,3 +18,32 @@ classifiers =
 [options]
 python_requires = >= 3.6
 packages = find_namespace:
+
+[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
Signature · 1 line
2.29.2
author
#11 lore raw

PATCH v5 11/25 python: add pylint to pipenv

In reply to John Snow

We are specifying >= pylint 2.6.x for two reasons:

1. For setup.cfg support, added in pylint 2.5.x
2. To clarify that we are using a version that has incompatibly dropped bad-whitespace checks.

---
 python/Pipfile      |   1 +
 python/Pipfile.lock | 137 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 138 insertions(+)
 create mode 100644 python/Pipfile.lock
python/Pipfile +1 -0
index 9534830b5eb..1e58986c895 100644
@@ -4,6 +4,7 @@ url = "https://pypi.org/simple"
 verify_ssl = true
 
 [dev-packages]
+pylint = ">=2.6.0"
 
 [packages]
 
python/Pipfile.lock new file +137 -0

137 lines not on this page · raw message

Signature · 1 line
2.29.2
author
#12 lore raw

PATCH v5 12/25 python: move flake8 config to setup.cfg

In reply to John Snow

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.

---
 python/qemu/machine/.flake8 | 2 --
 python/setup.cfg            | 3 +++
 2 files changed, 3 insertions(+), 2 deletions(-)
 delete mode 100644 python/qemu/machine/.flake8
python/qemu/machine/.flake8 deleted +0 -2
deleted file mode 100644
index 45d8146f3f5..00000000000
@@ -1,2 +0,0 @@
-[flake8]
-extend-ignore = E722  # Pylint handles this, but smarter.
\ No newline at end of file
python/setup.cfg +3 -0
index 20b24372a4a..9ecb2902006 100644
@@ -19,6 +19,9 @@ classifiers =
 python_requires = >= 3.6
 packages = find_namespace:
 
+[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
Signature · 1 line
2.29.2
author
#13 lore raw

PATCH v5 13/25 python: add excluded dirs to flake8 config

In reply to John Snow

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.

---
 python/setup.cfg | 2 ++
 1 file changed, 2 insertions(+)
python/setup.cfg +2 -0
index 9ecb2902006..f21a1c42fc0 100644
@@ -21,6 +21,8 @@ packages = find_namespace:
 
 [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
Signature · 1 line
2.29.2
author
#14 lore raw

PATCH v5 14/25 python: Add flake8 to pipenv

In reply to John Snow

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.

---
 python/Pipfile      |  1 +
 python/Pipfile.lock | 51 ++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 51 insertions(+), 1 deletion(-)
python/Pipfile +1 -0
index 1e58986c895..d1f7045f680 100644
@@ -4,6 +4,7 @@ url = "https://pypi.org/simple"
 verify_ssl = true
 
 [dev-packages]
+flake8 = ">=3.6.0"
 pylint = ">=2.6.0"
 
 [packages]
python/Pipfile.lock +50 -1

80 lines not on this page · raw message

Signature · 1 line
2.29.2
author
#15 lore raw

PATCH v5 15/25 python: move mypy.ini into setup.cfg

In reply to John Snow

mypy supports reading its configuration values from a central project configuration file; do so.

---
 python/mypy.ini  | 4 ----
 python/setup.cfg | 5 +++++
 2 files changed, 5 insertions(+), 4 deletions(-)
 delete mode 100644 python/mypy.ini
python/mypy.ini deleted +0 -4
deleted file mode 100644
index 1a581c5f1ea..00000000000
@@ -1,4 +0,0 @@
-[mypy]
-strict = True
-python_version = 3.6
-warn_unused_configs = True
python/setup.cfg +5 -0
index f21a1c42fc0..a3fcf67f8d2 100644
@@ -24,6 +24,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
Signature · 1 line
2.29.2
author
#16 lore raw

PATCH v5 16/25 python: add mypy to pipenv

In reply to John Snow

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.

---
 python/Pipfile      |  1 +
 python/Pipfile.lock | 37 ++++++++++++++++++++++++++++++++++++-
 python/setup.cfg    |  1 +
 3 files changed, 38 insertions(+), 1 deletion(-)
python/Pipfile +1 -0
index d1f7045f680..51c537b0d10 100644
@@ -5,6 +5,7 @@ verify_ssl = true
 
 [dev-packages]
 flake8 = ">=3.6.0"
+mypy = ">=0.770"
 pylint = ">=2.6.0"
 
 [packages]
python/Pipfile.lock +36 -1
index 869b0bdf67f..5f8aab117c3 100644
@@ -1,7 +1,7 @@
 {
     "_meta": {
         "hash": {
-            "sha256": "9f6d4857a6c72ad40fc1ec1e58cdb76f187a2986ac4156f0027e5eb798ec69a9"
+            "sha256": "65f010a8f2e55e9870d2b7e0d8af129516097d23abf2504f396552748b067ade"
         },
         "pipfile-spec": 6,
         "requires": {
@@ -83,6 +83,41 @@
             ],
             "version": "==0.6.1"
         },
+        "mypy": {
+            "hashes": [
+                "sha256:0d2fc8beb99cd88f2d7e20d69131353053fbecea17904ee6f0348759302c52fa",
+                "sha256:2b216eacca0ec0ee124af9429bfd858d5619a0725ee5f88057e6e076f9eb1a7b",
+                "sha256:319ee5c248a7c3f94477f92a729b7ab06bf8a6d04447ef3aa8c9ba2aa47c6dcf",
+                "sha256:3e0c159a7853e3521e3f582adb1f3eac66d0b0639d434278e2867af3a8c62653",
+                "sha256:5615785d3e2f4f03ab7697983d82c4b98af5c321614f51b8f1034eb9ebe48363",
+                "sha256:5ff616787122774f510caeb7b980542a7cc2222be3f00837a304ea85cd56e488",
+                "sha256:6f8425fecd2ba6007e526209bb985ce7f49ed0d2ac1cc1a44f243380a06a84fb",
+                "sha256:74f5aa50d0866bc6fb8e213441c41e466c86678c800700b87b012ed11c0a13e0",
+                "sha256:90b6f46dc2181d74f80617deca611925d7e63007cf416397358aa42efb593e07",
+                "sha256:947126195bfe4709c360e89b40114c6746ae248f04d379dca6f6ab677aa07641",
+                "sha256:a301da58d566aca05f8f449403c710c50a9860782148332322decf73a603280b",
+                "sha256:aa9d4901f3ee1a986a3a79fe079ffbf7f999478c281376f48faa31daaa814e86",
+                "sha256:b9150db14a48a8fa114189bfe49baccdff89da8c6639c2717750c7ae62316738",
+                "sha256:b95068a3ce3b50332c40e31a955653be245666a4bc7819d3c8898aa9fb9ea496",
+                "sha256:ca7ad5aed210841f1e77f5f2f7d725b62c78fa77519312042c719ed2ab937876",
+                "sha256:d16c54b0dffb861dc6318a8730952265876d90c5101085a4bc56913e8521ba19",
+                "sha256:e0202e37756ed09daf4b0ba64ad2c245d357659e014c3f51d8cd0681ba66940a",
+                "sha256:e1c84c65ff6d69fb42958ece5b1255394714e0aac4df5ffe151bc4fe19c7600a",
+                "sha256:e32b7b282c4ed4e378bba8b8dfa08e1cfa6f6574067ef22f86bee5b1039de0c9",
+                "sha256:e3b8432f8df19e3c11235c4563a7250666dc9aa7cdda58d21b4177b20256ca9f",
+                "sha256:e497a544391f733eca922fdcb326d19e894789cd4ff61d48b4b195776476c5cf",
+                "sha256:f5fdf935a46aa20aa937f2478480ebf4be9186e98e49cc3843af9a5795a49a25"
+            ],
+            "index": "pypi",
+            "version": "==0.800"
+        },
+        "mypy-extensions": {
+            "hashes": [
+                "sha256:090fedd75945a69ae91ce1303b5824f428daf5a028d2f6ab8a299250a846f15d",
+                "sha256:2d82818f5bb3e369420cb3c4060a7970edba416647068eb4c5343488a6c604a8"
+            ],
+            "version": "==0.4.3"
+        },
         "pycodestyle": {
             "hashes": [
                 "sha256:2295e7b2f6b5bd100585ebcb1f616591b652db8a741695b3d8f5d28bdc934367",
python/setup.cfg +1 -0
index a3fcf67f8d2..cdd8477292b 100644
@@ -28,6 +28,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
Signature · 1 line
2.29.2
author
#17 lore raw

PATCH v5 17/25 python: move .isort.cfg into setup.cfg

In reply to John Snow
---
 python/.isort.cfg | 7 -------
 python/setup.cfg  | 8 ++++++++
 2 files changed, 8 insertions(+), 7 deletions(-)
 delete mode 100644 python/.isort.cfg
python/.isort.cfg deleted +0 -7
deleted file mode 100644
index 6d0fd6cc0d3..00000000000
@@ -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
python/setup.cfg +8 -0
index cdd8477292b..8a2c200a581 100644
@@ -58,3 +58,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
Signature · 1 line
2.29.2
author
#18 lore raw

PATCH v5 18/25 python/qemu: add isort to pipenv

In reply to John Snow

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.

Require 5.0.5 or greater.

isort can be run with 'isort -c qemu' from the python root.

---
 python/Pipfile      | 1 +
 python/Pipfile.lock | 4 ++--
 2 files changed, 3 insertions(+), 2 deletions(-)
python/Pipfile +1 -0
index 51c537b0d10..75b96f29d87 100644
@@ -5,6 +5,7 @@ verify_ssl = true
 
 [dev-packages]
 flake8 = ">=3.6.0"
+isort = ">=5.0.5"
 mypy = ">=0.770"
 pylint = ">=2.6.0"
 
python/Pipfile.lock +2 -2
index 5f8aab117c3..201e735c27a 100644
@@ -1,7 +1,7 @@
 {
     "_meta": {
         "hash": {
-            "sha256": "65f010a8f2e55e9870d2b7e0d8af129516097d23abf2504f396552748b067ade"
+            "sha256": "b89c7a1b8a414f2a4cd708964123fb427d55419ee0b39e088bf2e7d4fbc11979"
         },
         "pipfile-spec": 6,
         "requires": {
@@ -46,7 +46,7 @@
                 "sha256:c729845434366216d320e936b8ad6f9d681aab72dc7cbc2d51bedc3582f3ad1e",
                 "sha256:fff4f0c04e1825522ce6949973e83110a6e907750cd92d128b0d14aaaadbffdc"
             ],
-            "markers": "python_version >= '3.6' and python_version < '4.0'",
+            "index": "pypi",
             "version": "==5.7.0"
         },
         "lazy-object-proxy": {
Signature · 1 line
2.29.2
author
#19 lore raw

PATCH v5 19/25 python/qemu: add qemu package itself to pipenv

In reply to John Snow

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.

---
 python/Pipfile      | 1 +
 python/Pipfile.lock | 9 +++++++--
 2 files changed, 8 insertions(+), 2 deletions(-)
python/Pipfile +1 -0
index 75b96f29d87..214fb175e7a 100644
@@ -10,6 +10,7 @@ mypy = ">=0.770"
 pylint = ">=2.6.0"
 
 [packages]
+qemu = {editable = true,path = "."}
 
 [requires]
 python_version = "3.6"
python/Pipfile.lock +7 -2
index 201e735c27a..4b4402f49e5 100644
@@ -1,7 +1,7 @@
 {
     "_meta": {
         "hash": {
-            "sha256": "b89c7a1b8a414f2a4cd708964123fb427d55419ee0b39e088bf2e7d4fbc11979"
+            "sha256": "e38d142c3fadc2f2ed849e86f7ebd14e25974dc12228751490aef5a9ee074f2f"
         },
         "pipfile-spec": 6,
         "requires": {
@@ -15,7 +15,12 @@
             }
         ]
     },
-    "default": {},
+    "default": {
+        "qemu": {
+            "editable": true,
+            "path": "."
+        }
+    },
     "develop": {
         "astroid": {
             "hashes": [
Signature · 1 line
2.29.2
author
#20 lore raw

PATCH v5 20/25 python: add devel package requirements to setuptools

In reply to John Snow

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.

---
 python/Pipfile      |  5 +----
 python/Pipfile.lock | 14 +++++++++-----
 python/setup.cfg    |  9 +++++++++
 3 files changed, 19 insertions(+), 9 deletions(-)
python/Pipfile +1 -4
index 214fb175e7a..e7acb8cefa4 100644
@@ -4,10 +4,7 @@ url = "https://pypi.org/simple"
 verify_ssl = true
 
 [dev-packages]
-flake8 = ">=3.6.0"
-isort = ">=5.0.5"
-mypy = ">=0.770"
-pylint = ">=2.6.0"
+qemu = {editable = true, extras = ["devel"], path = "."}
 
 [packages]
 qemu = {editable = true,path = "."}
python/Pipfile.lock +9 -5
index 4b4402f49e5..85b3124a491 100644
@@ -1,7 +1,7 @@
 {
     "_meta": {
         "hash": {
-            "sha256": "e38d142c3fadc2f2ed849e86f7ebd14e25974dc12228751490aef5a9ee074f2f"
+            "sha256": "eff562a688ebc6f3ffe67494dbb804b883e2159ad81c4d55d96da9f7aec13e91"
         },
         "pipfile-spec": 6,
         "requires": {
@@ -35,7 +35,7 @@
                 "sha256:749dbbd6bfd0cf1318af27bf97a14e28e5ff548ef8e5b1566ccfb25a11e7c839",
                 "sha256:aadae8761ec651813c24be05c6f7b4680857ef6afaae4651a4eccaef97ce6c3b"
             ],
-            "index": "pypi",
+            "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'",
             "version": "==3.8.4"
         },
         "importlib-metadata": {
@@ -51,7 +51,7 @@
                 "sha256:c729845434366216d320e936b8ad6f9d681aab72dc7cbc2d51bedc3582f3ad1e",
                 "sha256:fff4f0c04e1825522ce6949973e83110a6e907750cd92d128b0d14aaaadbffdc"
             ],
-            "index": "pypi",
+            "markers": "python_version >= '3.6' and python_version < '4.0'",
             "version": "==5.7.0"
         },
         "lazy-object-proxy": {
@@ -113,7 +113,7 @@
                 "sha256:e497a544391f733eca922fdcb326d19e894789cd4ff61d48b4b195776476c5cf",
                 "sha256:f5fdf935a46aa20aa937f2478480ebf4be9186e98e49cc3843af9a5795a49a25"
             ],
-            "index": "pypi",
+            "markers": "python_version >= '3.5'",
             "version": "==0.800"
         },
         "mypy-extensions": {
@@ -144,9 +144,13 @@
                 "sha256:bb4a908c9dadbc3aac18860550e870f58e1a02c9f2c204fdf5693d73be061210",
                 "sha256:bfe68f020f8a0fece830a22dd4d5dddb4ecc6137db04face4c3420a46a52239f"
             ],
-            "index": "pypi",
+            "markers": "python_version >= '3.5'",
             "version": "==2.6.0"
         },
+        "qemu": {
+            "editable": true,
+            "path": "."
+        },
         "six": {
             "hashes": [
                 "sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259",
python/setup.cfg +9 -0
index 8a2c200a581..6eb9f62d5d9 100644
@@ -19,6 +19,15 @@ classifiers =
 python_requires = >= 3.6
 packages = find_namespace:
 
+[options.extras_require]
+# Run `pipenv lock` when changing these requirements.
+devel =
+    flake8 >= 3.6.0
+    isort >= 5.0.5
+    mypy >= 0.770
+    pylint >= 2.6.0
+
+
 [flake8]
 extend-ignore = E722  # Prefer pylint's bare-except checks to flake8's
 exclude = __pycache__,
Signature · 1 line
2.29.2
author
#21 lore raw

PATCH v5 21/25 python: add pytest and tests

In reply to John Snow

Try using pytest to manage our various tests; even though right now they're only invoking binaries and not really running any python-native code.

Create tests/, and add test_lint.py which calls out to mypy, flake8, pylint and isort to enforce the standards in this directory.

Add pytest to the setup.cfg development dependencies; add a pytest configuration section as well; run it in verbose mode.

Finally, add pytest to the Pipfile environment and lock the new dependencies.

Provided you have the right development dependencies (mypy, flake8, isort, pylint, and now pytest) You should be able to run "pytest" from the python folder to run all of these linters with the correct arguments.

---
 python/Pipfile.lock       | 57 ++++++++++++++++++++++++++++++++++++++-
 python/setup.cfg          |  5 ++++
 python/tests/test_lint.py | 28 +++++++++++++++++++
 3 files changed, 89 insertions(+), 1 deletion(-)
 create mode 100644 python/tests/test_lint.py
python/Pipfile.lock +56 -1
index 85b3124a491..0fa1a14a9b2 100644
@@ -30,6 +30,14 @@
             "markers": "python_version >= '3.5'",
             "version": "==2.4.2"
         },
+        "attrs": {
+            "hashes": [
+                "sha256:31b2eced602aa8423c2aea9c76a724617ed67cf9513173fd3a4f03e3a929c7e6",
+                "sha256:832aa3cde19744e49938b91fea06d69ecb9e649c93ba974535d08ad92164f700"
+            ],
+            "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'",
+            "version": "==20.3.0"
+        },
         "flake8": {
             "hashes": [
                 "sha256:749dbbd6bfd0cf1318af27bf97a14e28e5ff548ef8e5b1566ccfb25a11e7c839",
@@ -43,9 +51,16 @@
                 "sha256:ace61d5fc652dc280e7b6b4ff732a9c2d40db2c0f92bc6cb74e07b73d53a1771",
                 "sha256:fa5daa4477a7414ae34e95942e4dd07f62adf589143c875c133c1e53c4eff38d"
             ],
-            "markers": "python_version < '3.8'",
+            "markers": "python_version < '3.8' and python_version < '3.8'",
             "version": "==3.4.0"
         },
+        "iniconfig": {
+            "hashes": [
+                "sha256:011e24c64b7f47f6ebd835bb12a743f2fbe9a26d4cecaa7f53bc4f35ee9da8b3",
+                "sha256:bc3af051d7d14b2ee5ef9969666def0cd1a000e121eaea580d4a313df4b37f32"
+            ],
+            "version": "==1.1.1"
+        },
         "isort": {
             "hashes": [
                 "sha256:c729845434366216d320e936b8ad6f9d681aab72dc7cbc2d51bedc3582f3ad1e",
@@ -123,6 +138,30 @@
             ],
             "version": "==0.4.3"
         },
+        "packaging": {
+            "hashes": [
+                "sha256:5b327ac1320dc863dca72f4514ecc086f31186744b84a230374cc1fd776feae5",
+                "sha256:67714da7f7bc052e064859c05c595155bd1ee9f69f76557e21f051443c20947a"
+            ],
+            "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'",
+            "version": "==20.9"
+        },
+        "pluggy": {
+            "hashes": [
+                "sha256:15b2acde666561e1298d71b523007ed7364de07029219b604cf808bfa1c765b0",
+                "sha256:966c145cd83c96502c3c3868f50408687b38434af77734af1e9ca461a4081d2d"
+            ],
+            "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'",
+            "version": "==0.13.1"
+        },
+        "py": {
+            "hashes": [
+                "sha256:21b81bda15b66ef5e1a777a21c4dcd9c20ad3efd0b3f817e7a809035269e1bd3",
+                "sha256:3b80836aa6d1feeaa108e046da6423ab8f6ceda6468545ae8d02d9d58d18818a"
+            ],
+            "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'",
+            "version": "==1.10.0"
+        },
         "pycodestyle": {
             "hashes": [
                 "sha256:2295e7b2f6b5bd100585ebcb1f616591b652db8a741695b3d8f5d28bdc934367",
@@ -147,6 +186,22 @@
             "markers": "python_version >= '3.5'",
             "version": "==2.6.0"
         },
+        "pyparsing": {
+            "hashes": [
+                "sha256:c203ec8783bf771a155b207279b9bccb8dea02d8f0c9e5f8ead507bc3246ecc1",
+                "sha256:ef9d7589ef3c200abe66653d3f1ab1033c3c419ae9b9bdb1240a85b024efc88b"
+            ],
+            "markers": "python_version >= '2.6' and python_version not in '3.0, 3.1, 3.2'",
+            "version": "==2.4.7"
+        },
+        "pytest": {
+            "hashes": [
+                "sha256:9d1edf9e7d0b84d72ea3dbcdfd22b35fb543a5e8f2a60092dd578936bf63d7f9",
+                "sha256:b574b57423e818210672e07ca1fa90aaf194a4f63f3ab909a2c67ebb22913839"
+            ],
+            "markers": "python_version >= '3.6'",
+            "version": "==6.2.2"
+        },
         "qemu": {
             "editable": true,
             "path": "."
python/setup.cfg +5 -0
index 6eb9f62d5d9..2c12d9ab89b 100644
@@ -26,6 +26,7 @@ devel =
     isort >= 5.0.5
     mypy >= 0.770
     pylint >= 2.6.0
+    pytest >= 3.0.0
 
 
 [flake8]
@@ -75,3 +76,7 @@ include_trailing_comma=True
 line_length=72
 lines_after_imports=2
 multi_line_output=3
+
+[tool:pytest]
+addopts = -v
+console_output_style = count
python/tests/test_lint.py new file +28 -0
new file mode 100644
index 00000000000..38fefa01c66
@@ -0,0 +1,28 @@
+import subprocess
+
+
+def _external_test(command_line: str) -> None:
+    args = command_line.split(' ')
+    try:
+        subprocess.run(args, check=True)
+    except subprocess.CalledProcessError as err:
+        # Re-contextualize to avoid pytest showing error context inside
+        # the subprocess module itself
+        raise Exception("'{:s}' returned {:d}".format(
+            ' '.join(args), err.returncode)) from None
+
+
+def test_isort() -> None:
+    _external_test('isort -c qemu')
+
+
+def test_flake8() -> None:
+    _external_test('flake8')
+
+
+def test_pylint() -> None:
+    _external_test('pylint qemu')
+
+
+def test_mypy() -> None:
+    _external_test('mypy -p qemu')
Signature · 1 line
2.29.2
author
#22 lore raw

PATCH v5 22/25 python: add Makefile for some common tasks

In reply to John Snow

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.

make clean: delete miscellaneous build 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.

---
 python/Makefile | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
 create mode 100644 python/Makefile
python/Makefile new file +37 -0
new file mode 100644
index 00000000000..47940846400
@@ -0,0 +1,37 @@
+.PHONY: help venv venv-check check clean distclean
+
+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 check:      run linters using the current environment."
+	@echo "    Hint: Install deps with: 'pip install \".[devel]\"'"
+	@echo ""
+	@echo "make clean:      remove build output."
+	@echo ""
+	@echo "make distclean:  remove venv files, qemu package forwarder, and"
+	@echo "                 everything 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
+
+check:
+	@pytest
+
+clean:
+	rm -rf build/ dist/
+
+distclean: clean
+	rm -rf qemu.egg-info/ .venv
Signature · 1 line
2.29.2
author
#23 lore raw

PATCH v5 23/25 python: add .gitignore

In reply to John Snow

Ignore *Python* build and package output (build, dist, qemu.egg-info); these files are not created as part of a QEMU build.

Ignore miscellaneous cached python confetti (__pycache__, *.pyc, .mypy_cache).

Ignore .idea (pycharm) and .venv (pipenv et al).

---
 python/.gitignore | 9 +++++++++
 1 file changed, 9 insertions(+)
 create mode 100644 python/.gitignore
python/.gitignore new file +9 -0
new file mode 100644
index 00000000000..78c522768bc
@@ -0,0 +1,9 @@
+*.pyc
+.idea/
+.mypy_cache/
+.pytest_cache/
+.venv/
+__pycache__/
+build/
+dist/
+qemu.egg-info/
Signature · 1 line
2.29.2
author
#24 lore raw

PATCH v5 24/25 gitlab: add python linters to CI

In reply to John Snow

Add python3.6 to the fedora container image: we need it to run the linters against that explicit version to make sure we don't break our minimum version promise.

Add pipenv so that we can fetch precise versions of pip packages we need to guarantee test reproducability.

---
 .gitlab-ci.yml                         | 10 ++++++++++
 tests/docker/dockerfiles/fedora.docker |  2 ++
 2 files changed, 12 insertions(+)
.gitlab-ci.yml +10 -0
index 7adb9a4cef0..8dc4de9b168 100644
@@ -665,6 +665,16 @@ check-patch:
     GIT_DEPTH: 1000
   allow_failure: true
 
+
+check-python:
+  stage: build
+  image: $CI_REGISTRY_IMAGE/qemu/fedora:latest
+  script:
+    - cd python
+    - make venv-check
+  variables:
+    GIT_DEPTH: 1000
+
 check-dco:
   stage: build
   image: $CI_REGISTRY_IMAGE/qemu/centos8:latest
tests/docker/dockerfiles/fedora.docker +2 -0
index 0d7602abbeb..1262b8c5e03 100644
@@ -84,6 +84,7 @@ ENV PACKAGES \
     numactl-devel \
     perl \
     perl-Test-Harness \
+    pipenv \
     pixman-devel \
     python3 \
     python3-PyYAML \
@@ -93,6 +94,7 @@ ENV PACKAGES \
     python3-pip \
     python3-sphinx \
     python3-virtualenv \
+    python3.6 \
     rdma-core-devel \
     SDL2-devel \
     snappy-devel \
Signature · 1 line
2.29.2
author
#25 lore raw

PATCH v5 25/25 python: add 'make develop' target

In reply to John Snow

Add a shortcut that 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.

---
 python/Makefile | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
python/Makefile +7 -2
index 47940846400..2699b77b072 100644
@@ -1,4 +1,4 @@
-.PHONY: help venv venv-check check clean distclean
+.PHONY: help venv venv-check check clean distclean develop
 
 help:
 	@echo "python packaging help:"
@@ -11,8 +11,10 @@ help:
 	@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 "    Hint: Install deps with: 'pip install \".[devel]\"'"
 	@echo ""
 	@echo "make clean:      remove build output."
 	@echo ""
@@ -27,6 +29,9 @@ venv: .venv
 venv-check: venv
 	@pipenv run make check
 
+develop:
+	pip3 install -e .[devel]
+
 check:
 	@pytest
 
Signature · 1 line
2.29.2