From b44d39e30e79f42a57b08b13a7b2e969300e3faa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Achard?= Date: Sun, 12 Mar 2023 20:37:46 +0000 Subject: [PATCH] Fix python packaging MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémi Achard --- MANIFEST.in | 3 ++ pyproject.toml | 2 +- setup.py | 9 +++--- src/bindings/python/CMakeLists.txt | 32 ++++++++++++------- src/bindings/python/{ => package}/__init__.py | 10 +++++- tests/python/OpenColorIOTest.py | 12 +++++++ tests/python/OpenColorIOTestSuite.py | 12 ++----- tests/python/TransformsTest.py | 2 +- 8 files changed, 55 insertions(+), 27 deletions(-) rename src/bindings/python/{ => package}/__init__.py (82%) diff --git a/MANIFEST.in b/MANIFEST.in index d72188ba8f..cf2223275b 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -6,3 +6,6 @@ graft share graft src graft tests global-include CMakeLists.txt *.cmake *.md +global-exclude */__pycache__/* +global-exclude *.pyc +global-exclude .DS_Store diff --git a/pyproject.toml b/pyproject.toml index ff83ac1a0d..e19afc34bf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,7 +17,7 @@ build-backend = "setuptools.build_meta" [tool.cibuildwheel] build-verbosity = "1" -test-command = "python -m PyOpenColorIOTests.OpenColorIOTestSuite" +test-command = "python -m PyOpenColorIO.tests.OpenColorIOTestSuite" test-requires = ["numpy"] manylinux-x86_64-image = "manylinux2014" diff --git a/setup.py b/setup.py index 80f4b29e1f..614752a527 100644 --- a/setup.py +++ b/setup.py @@ -155,11 +155,12 @@ def build_extension(self, ext): setup( version=get_version(), package_dir={ - 'PyOpenColorIOTests': 'tests/python', - 'PyOpenColorIOTests.data': 'tests/data', + 'PyOpenColorIO': 'src/bindings/python/package', + 'PyOpenColorIO.tests': 'tests/python', + 'PyOpenColorIO.data': 'tests/data', }, - packages=['PyOpenColorIOTests', 'PyOpenColorIOTests.data'], - ext_modules=[CMakeExtension("PyOpenColorIO")], + packages=['PyOpenColorIO', 'PyOpenColorIO.tests', 'PyOpenColorIO.data'], + ext_modules=[CMakeExtension("PyOpenColorIO.PyOpenColorIO")], cmdclass={"build_ext": CMakeBuild}, include_package_data=True ) diff --git a/src/bindings/python/CMakeLists.txt b/src/bindings/python/CMakeLists.txt index 8588700a0c..436299cd95 100644 --- a/src/bindings/python/CMakeLists.txt +++ b/src/bindings/python/CMakeLists.txt @@ -131,13 +131,6 @@ if(WIN32) ) endif() -# NOTE: Depending of the compiler version pybind11 2.4.3 does not compile with C++17 so revert to c++11 - -set(APP_CXX_STANDARD ${CMAKE_CXX_STANDARD}) -if(${CMAKE_CXX_STANDARD} GREATER_EQUAL 17) - set(APP_CXX_STANDARD 11) -endif() - set(CUSTOM_COMPILE_FLAGS ${PLATFORM_COMPILE_OPTIONS}) set(CUSTOM_LINK_FLAGS ${PLATFORM_LINK_OPTIONS}) @@ -159,7 +152,6 @@ set_target_properties(PyOpenColorIO PROPERTIES COMPILE_OPTIONS "${CUSTOM_COMPILE_FLAGS}" LINK_OPTIONS "${CUSTOM_LINK_FLAGS}" - CXX_STANDARD ${APP_CXX_STANDARD} ) if(NOT BUILD_SHARED_LIBS) @@ -183,10 +175,10 @@ if (UNIX AND NOT CMAKE_SKIP_RPATH) # dynamic library based on the default installation directory structure. if (APPLE) set_target_properties(PyOpenColorIO PROPERTIES - INSTALL_RPATH "@loader_path/../..;${CMAKE_INSTALL_RPATH}") + INSTALL_RPATH "@loader_path/../../..;${CMAKE_INSTALL_RPATH}") else() set_target_properties(PyOpenColorIO PROPERTIES - INSTALL_RPATH "$ORIGIN/../..;${CMAKE_INSTALL_RPATH}") + INSTALL_RPATH "$ORIGIN/../../..;${CMAKE_INSTALL_RPATH}") endif() endif() @@ -227,6 +219,24 @@ target_compile_definitions(PyOpenColorIO PY_VERSION_PATCH=${Python_VERSION_PATCH} ) +############################################################################### +# Build layout +# Mirrors the installation, using PyOpenColorIO folder and __init__.py file. +# When building the Python wheel, do not override the target directory. +set(_PyOpenColorIO_BUILD_PACKAGE_DIR "${CMAKE_CURRENT_BINARY_DIR}/PyOpenColorIO") +if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY) + set_target_properties(PyOpenColorIO PROPERTIES + LIBRARY_OUTPUT_DIRECTORY "${_PyOpenColorIO_BUILD_PACKAGE_DIR}" + # For Windows compatibility + LIBRARY_OUTPUT_DIRECTORY_DEBUG "${_PyOpenColorIO_BUILD_PACKAGE_DIR}" + LIBRARY_OUTPUT_DIRECTORY_RELEASE "${_PyOpenColorIO_BUILD_PACKAGE_DIR}" + ) +endif() + +file(COPY package/__init__.py DESTINATION "${_PyOpenColorIO_BUILD_PACKAGE_DIR}") + +############################################################################### +# Install layout # Set to site-package location. if(WIN32) set(_Python_VARIANT_PATH "${CMAKE_INSTALL_LIBDIR}/site-packages") @@ -245,4 +255,4 @@ install(TARGETS PyOpenColorIO LIBRARY DESTINATION ${_PyOpenColorIO_SITE_PACKAGE_DIR} ) -install(FILES __init__.py DESTINATION ${_PyOpenColorIO_SITE_PACKAGE_DIR}) \ No newline at end of file +install(FILES package/__init__.py DESTINATION ${_PyOpenColorIO_SITE_PACKAGE_DIR}) \ No newline at end of file diff --git a/src/bindings/python/__init__.py b/src/bindings/python/package/__init__.py similarity index 82% rename from src/bindings/python/__init__.py rename to src/bindings/python/package/__init__.py index 3ce17e051e..3ac093f5c3 100644 --- a/src/bindings/python/__init__.py +++ b/src/bindings/python/package/__init__.py @@ -21,4 +21,12 @@ if os.path.exists(path) and path != ".": os.add_dll_directory(path) -from .PyOpenColorIO import * \ No newline at end of file +del os, sys, platform + +# +# Import compiled module. +# + +from .PyOpenColorIO import __author__, __email__, __license__, __copyright__, __version__, __status__, __doc__ + +from .PyOpenColorIO import * diff --git a/tests/python/OpenColorIOTest.py b/tests/python/OpenColorIOTest.py index 0b6af12a24..8a7016290c 100644 --- a/tests/python/OpenColorIOTest.py +++ b/tests/python/OpenColorIOTest.py @@ -11,6 +11,18 @@ class OpenColorIOTest(unittest.TestCase): + def test_attributes(self): + """ + Test Global attributes. + """ + self.assertTrue(hasattr(OCIO, "__author__")) + self.assertTrue(hasattr(OCIO, "__email__")) + self.assertTrue(hasattr(OCIO, "__license__")) + self.assertTrue(hasattr(OCIO, "__copyright__")) + self.assertTrue(hasattr(OCIO, "__version__")) + self.assertTrue(hasattr(OCIO, "__status__")) + self.assertTrue(hasattr(OCIO, "__doc__")) + def test_env_variable(self): """ Test Get/SetEnvVariable(). diff --git a/tests/python/OpenColorIOTestSuite.py b/tests/python/OpenColorIOTestSuite.py index a9aaa5c930..995706f0a4 100755 --- a/tests/python/OpenColorIOTestSuite.py +++ b/tests/python/OpenColorIOTestSuite.py @@ -24,14 +24,8 @@ # Note: Only when compiling within Microsoft Visual Studio editor i.e. not on command line. if len(sys.argv) == 3: opencolorio_dir = os.path.join(opencolorio_dir, sys.argv[2]) - pyopencolorio_dir = os.path.join(pyopencolorio_dir, sys.argv[2]) - - # Python 3.8+ does no longer look for DLLs in PATH environment variable - if hasattr(os, 'add_dll_directory'): - os.add_dll_directory(opencolorio_dir) - else: - os.environ['PATH'] = '{0};{1}'.format( - opencolorio_dir, os.getenv('PATH', '')) + # PyOpenColorIO __init__.py file handle os.add_dll_directory() + os.environ['PATH'] = '{0};{1}'.format(opencolorio_dir, os.getenv('PATH', '')) elif sys.platform == 'darwin': # On OSX we must add the main library location to DYLD_LIBRARY_PATH os.environ['DYLD_LIBRARY_PATH'] = '{0}:{1}'.format( @@ -41,7 +35,7 @@ # Else it probably means direct invocation from installed package else: here = os.path.dirname(__file__) - os.environ["TEST_DATAFILES_DIR"] = os.path.join(here, 'data', 'files') + os.environ["TEST_DATAFILES_DIR"] = os.path.join(os.path.dirname(here), 'data', 'files') sys.path.insert(0, here) import PyOpenColorIO as OCIO diff --git a/tests/python/TransformsTest.py b/tests/python/TransformsTest.py index d4b1b35962..9354eec6e1 100644 --- a/tests/python/TransformsTest.py +++ b/tests/python/TransformsTest.py @@ -34,7 +34,7 @@ def all_transforms_as_group(self): # Ensure we only catch and filter for this specific error self.assertEqual( str(e), - 'PyOpenColorIO.Transform: No constructor defined!', + 'PyOpenColorIO.PyOpenColorIO.Transform: No constructor defined!', 'Unintended Error Raised: {0}'.format(e) )