From: Martin Belanger Date: Wed, 27 Oct 2021 12:20:10 +0000 (-0400) Subject: Add Python tests to meson framework. X-Git-Tag: v1.0-rc0~65^2 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=b37d353fd237234333e00d7a901f2eae9d157508;p=users%2Fsagi%2Flibnvme.git Add Python tests to meson framework. Python has this strict directory layout requirement. A Python package is a directory that must contain a __init__.py file in addition to containing modules. To be able to run Python tests and load modules directly from the build directory, the Python package (libnvme) and module (nvme.py) must be organized in the build directory the same way they would be in the install directory. And the PYTHONPATH must be set to point to the build directory. In other words, like this: .build <- PYTHONPATH points to here. \_ libnvme <- Package \_ nvme.py <- Module \_ __init__.py \_ _nvme.cpython-38-x86_64-linux-gnu.so This patch changes the directory layout so that meson will organize the Python package and module as shown above. The patch also defines Python tests and sets the PYTHONPATH to allow importing directly from the build directory. Signed-off-by: Martin Belanger --- diff --git a/.gitignore b/.gitignore index 29a202e3..d52a35ac 100644 --- a/.gitignore +++ b/.gitignore @@ -19,7 +19,9 @@ examples/display-columnar examples/telemetry-listen examples/discover-loop +config.h ccan/config.h +ccan/ccan-config.h ccan/tools/configurator/configurator doc/_build diff --git a/Makefile b/Makefile index 6cc83386..d416c2db 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ INSTALL=install default: all python: all - @$(MAKE) -C pynvme python + @$(MAKE) -C libnvme python all: $(NAME).pc @$(MAKE) -C src @@ -55,7 +55,7 @@ install-tests: @$(MAKE) -C test install prefix=$(DESTDIR)$(prefix) datadir=$(DESTDIR)$(datadir) install-python: - @$(MAKE) -C pynvme install prefix=$(DESTDIR)$(prefix) + @$(MAKE) -C libnvme install prefix=$(DESTDIR)$(prefix) clean: @rm -f config-host.mak config-host.h cscope.out $(NAME).pc diff --git a/pynvme/.gitignore b/libnvme/.gitignore similarity index 67% rename from pynvme/.gitignore rename to libnvme/.gitignore index 44ac4fef..e943f50c 100644 --- a/pynvme/.gitignore +++ b/libnvme/.gitignore @@ -1,3 +1,4 @@ build/ +__pycache__/ nvme.py nvme_wrap.c diff --git a/pynvme/Makefile b/libnvme/Makefile similarity index 100% rename from pynvme/Makefile rename to libnvme/Makefile diff --git a/pynvme/README.md b/libnvme/README.md similarity index 100% rename from pynvme/README.md rename to libnvme/README.md diff --git a/pynvme/__init__.py b/libnvme/__init__.py similarity index 100% rename from pynvme/__init__.py rename to libnvme/__init__.py diff --git a/libnvme/meson.build b/libnvme/meson.build new file mode 100644 index 00000000..554d4e3e --- /dev/null +++ b/libnvme/meson.build @@ -0,0 +1,70 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later +# +# This file is part of libnvme. +# Copyright (c) 2021 Dell Inc. +# +# Authors: Martin Belanger +# + +want_python = get_option('python') +if want_python != 'false' + python3 = import('python').find_installation('python3') + py3_dep = python3.dependency(required: want_python == 'true') + swig = find_program('swig', required: want_python == 'true') + have_python_support = py3_dep.found() and swig.found() +else + have_python_support = false +endif + +if have_python_support + pymod_swig = custom_target( + 'nvme.py', + input: ['nvme.i', config_h], + output: ['nvme.py', 'nvme_wrap.c'], + command: [swig, '-python', '-py3', '-o', '@OUTPUT1@', '@INPUT0@'], + install: true, + install_dir: [python3.get_install_dir(pure: false, subdir: 'libnvme'), false], + ) + + pynvme_clib = python3.extension_module( + '_nvme', + pymod_swig[1], + dependencies : py3_dep, + include_directories: incdir, + link_with: libnvme, + install: true, + subdir: 'libnvme', + ) + + # Little hack to copy file __init__.py to the build directory. + # This is needed to create the proper directory layout to run the tests. + # It's a hack because we don't really "configure" file __init__.py and we + # could simply install directly from the source tree with: + # python3.install_sources(['__init__.py', ], pure:false, subdir:'libnvme') + # However, since we need __init__.py in the build directory to run the tests + # we resort to this hack to copy it. + configure_file( + input: '__init__.py', + output: '__init__.py', + copy: true, + install_dir: python3.get_install_dir(pure: false, subdir: 'libnvme'), + ) + + # Set the PYTHONPATH so that we can run the + # tests directly from the build directory. + test_env = environment() + test_env.append('MALLOC_PERTURB_', '0') + test_env.append('PYTHONPATH', meson.build_root()) + + # Test section + test('[Python] import libnvme', python3, args: ['-c', 'from libnvme import nvme'], env: test_env) + + py_tests = [ + [ 'create ctrl object', files('tests/create-ctrl-obj.py') ], + ] + foreach test: py_tests + description = test[0] + py_script = test[1] + test('[Python] ' + description, python3, args: [py_script, ], env: test_env) + endforeach +endif diff --git a/pynvme/nvme.i b/libnvme/nvme.i similarity index 100% rename from pynvme/nvme.i rename to libnvme/nvme.i diff --git a/pynvme/setup.py b/libnvme/setup.py similarity index 100% rename from pynvme/setup.py rename to libnvme/setup.py diff --git a/libnvme/tests/create-ctrl-obj.py b/libnvme/tests/create-ctrl-obj.py new file mode 100755 index 00000000..186c6013 --- /dev/null +++ b/libnvme/tests/create-ctrl-obj.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python3 +import sys +import pprint +from libnvme import nvme + +root = nvme.root() +root.log_level('debug') + +host = nvme.host(root) +subsysnqn = nvme.NVME_DISC_SUBSYS_NAME +transport = 'loop' +traddr = '127.0.0.1' +trsvcid = '8009' +ctrl = nvme.ctrl(subsysnqn=subsysnqn, transport=transport, traddr=traddr, trsvcid=trsvcid) diff --git a/meson.build b/meson.build index ea7c5165..06f19128 100644 --- a/meson.build +++ b/meson.build @@ -209,7 +209,7 @@ incdir = include_directories(['.', 'ccan', 'src']) ################################################################################ subdir('src') -subdir('pynvme') +subdir('libnvme') subdir('test') subdir('examples') subdir('doc') diff --git a/pynvme/meson.build b/pynvme/meson.build deleted file mode 100644 index bde1536b..00000000 --- a/pynvme/meson.build +++ /dev/null @@ -1,44 +0,0 @@ -# SPDX-License-Identifier: LGPL-2.1-or-later -# -# This file is part of libnvme. -# Copyright (c) 2021 Dell Inc. -# -# Authors: Martin Belanger -# - -want_python = get_option('python') -if want_python != 'false' - python3 = import('python').find_installation('python3') - py3_dep = python3.dependency(required: want_python == 'true') - swig = find_program('swig', required: want_python == 'true') - have_python_support = py3_dep.found() and swig.found() -else - have_python_support = false -endif - -if have_python_support - pymod_swig = custom_target( - 'nvme.py', - input: ['nvme.i', config_h], - output: ['nvme.py', 'nvme_wrap.c'], - command: [swig, '-python', '-py3', '-o', '@OUTPUT1@', '@INPUT0@'], - install: true, - install_dir: [python3.get_install_dir(pure: false, subdir: 'libnvme'), false], - ) - - pynvme_clib = python3.extension_module( - '_nvme', - pymod_swig[1], - dependencies : py3_dep, - include_directories: incdir, - link_with: libnvme, - install: true, - subdir: 'libnvme', - ) - - python3.install_sources( - ['__init__.py', ], - pure: false, - subdir: 'libnvme', - ) -endif