Blob Blame History Raw
From 8270e433f73fe295b6b938d6bbb9e400f9e6ad4c Mon Sep 17 00:00:00 2001
From: Petr Lautrbach <plautrba@redhat.com>
Date: Sat, 29 Jun 2019 22:37:29 +0200
Subject: [PATCH 6/6] libselinux: Build selinux python package using setup.py

Originally, setup.py built selinux as a module. It caused problems with SWIG
generated python wrapper which used wrong path for importing binary module.

selinux is built as a python package now.

Fixes:
>>> import selinux
Traceback (most recent call last):
  File "/usr/lib64/python3.7/site-packages/selinux/__init__.py", line 14, in swig_import_helper
    return importlib.import_module(mname)
  File "/usr/lib64/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named '_selinux'

Signed-off-by: Petr Lautrbach <plautrba@redhat.com>
---
 libselinux/src/Makefile |  1 -
 libselinux/src/setup.py | 21 ++++++++++++++++++++-
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/libselinux/src/Makefile b/libselinux/src/Makefile
index f64f23a8..db07dcb5 100644
--- a/libselinux/src/Makefile
+++ b/libselinux/src/Makefile
@@ -171,7 +171,6 @@ install: all
 
 install-pywrap: pywrap
 	$(PYTHON) setup.py install --prefix=$(PREFIX) `test -n "$(DESTDIR)" && echo --root $(DESTDIR)`
-	install -m 644 selinux.py $(DESTDIR)$(PYTHONLIBDIR)/selinux/__init__.py
 
 install-rubywrap: rubywrap
 	test -d $(DESTDIR)$(RUBYINSTALL) || install -m 755 -d $(DESTDIR)$(RUBYINSTALL) 
diff --git a/libselinux/src/setup.py b/libselinux/src/setup.py
index b12e7869..1419aaa3 100644
--- a/libselinux/src/setup.py
+++ b/libselinux/src/setup.py
@@ -1,13 +1,29 @@
 #!/usr/bin/python3
 
 from distutils.core import Extension, setup
+from distutils.command.build_py import build_py
+
+import os
+import os.path
+
+class pre_build_py(build_py):
+    def run(self):
+
+        # create formal selinux package from selinux.py module
+        if os.path.isfile('selinux.py'):
+            if not os.path.isdir('selinux'):
+                os.mkdir('selinux')
+            os.rename('selinux.py', 'selinux/__init__py')
+
+        build_py.run(self)
 
 setup(
     name="selinux",
     version="2.9",
-    description="SELinux python 3 bindings",
+    description="SELinux python bindings",
     author="SELinux Project",
     author_email="selinux@vger.kernel.org",
+    packages=["selinux"],
     ext_modules=[
         Extension('selinux._selinux',
                   sources=['selinuxswig_python.i'],
@@ -21,4 +37,7 @@ setup(
                   libraries=['selinux'],
                   extra_link_args=['-l:libsepol.a'])
     ],
+    cmdclass={
+        'build_py': pre_build_py
+    },
 )
-- 
2.22.0