]> www.infradead.org Git - users/sagi/libnvme.git/commitdiff
Add Python tests to meson framework.
authorMartin Belanger <martin.belanger@dell.com>
Wed, 27 Oct 2021 12:20:10 +0000 (08:20 -0400)
committerMartin Belanger <martin.belanger@dell.com>
Wed, 27 Oct 2021 12:20:10 +0000 (08:20 -0400)
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 <martin.belanger@dell.com>
12 files changed:
.gitignore
Makefile
libnvme/.gitignore [moved from pynvme/.gitignore with 67% similarity]
libnvme/Makefile [moved from pynvme/Makefile with 100% similarity]
libnvme/README.md [moved from pynvme/README.md with 100% similarity]
libnvme/__init__.py [moved from pynvme/__init__.py with 100% similarity]
libnvme/meson.build [new file with mode: 0644]
libnvme/nvme.i [moved from pynvme/nvme.i with 100% similarity]
libnvme/setup.py [moved from pynvme/setup.py with 100% similarity]
libnvme/tests/create-ctrl-obj.py [new file with mode: 0755]
meson.build
pynvme/meson.build [deleted file]

index 29a202e3163c6b71f642f4c46215c57508c2a2f3..d52a35ac5b7c9e4dae64b699d059a1c9ae70e56c 100644 (file)
@@ -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
index 6cc8338645e156e778595f35a5ed1553260517f5..d416c2db9d79f10a1f14d6260352d82ef4268d04 100644 (file)
--- 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
similarity index 67%
rename from pynvme/.gitignore
rename to libnvme/.gitignore
index 44ac4fefc01e7e7214692783d39bed926acbf552..e943f50cdf5277b069adae022a6246d1141d4c1a 100644 (file)
@@ -1,3 +1,4 @@
 build/
+__pycache__/
 nvme.py
 nvme_wrap.c
similarity index 100%
rename from pynvme/Makefile
rename to libnvme/Makefile
similarity index 100%
rename from pynvme/README.md
rename to libnvme/README.md
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 (file)
index 0000000..554d4e3
--- /dev/null
@@ -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 <Martin.Belanger@dell.com>
+#
+
+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
similarity index 100%
rename from pynvme/nvme.i
rename to libnvme/nvme.i
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 (executable)
index 0000000..186c601
--- /dev/null
@@ -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)
index ea7c51651586d1fed60ab44158c3ee84725259f6..06f191283f4f23ffb300ebf8557331223bb85964 100644 (file)
@@ -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 (file)
index bde1536..0000000
+++ /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 <Martin.Belanger@dell.com>
-#
-
-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