Skip to content

Commit b92d434

Browse files
committed
Reorganize so that the built package is importable by the stub generator, before installing
1 parent 90f6acc commit b92d434

2 files changed

Lines changed: 21 additions & 14 deletions

File tree

src/cmake/pythonutils.cmake

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ macro (setup_python_module)
144144
set (PYTHON_SITE_DIR .)
145145
endif ()
146146

147-
set(PYTHON_BUILD_SITE "${CMAKE_BINARY_DIR}/lib/python/site-packages")
147+
set (PYTHON_BUILD_SITE "${CMAKE_BINARY_DIR}/lib/python/site-packages/OpenImageIO")
148148

149149
# In the build area, put it in lib/python so it doesn't clash with the
150150
# non-python libraries of the same name (which aren't prefixed by "lib"
@@ -164,8 +164,12 @@ macro (setup_python_module)
164164
if (PYTHON_VERSION_FOUND VERSION_GREATER_EQUAL "3.10")
165165
# A modern version of python is required for the necessary version of mypy
166166

167+
# copy the __init__.py file so that the package is importable on all platforms
168+
file (COPY __init__.py DESTINATION ${PYTHON_BUILD_SITE})
169+
# write the marker file
170+
file (WRITE "${PYTHON_BUILD_SITE}/py.typed" "")
171+
167172
# Run stub generation process
168-
set (_stub_file "${CMAKE_BINARY_DIR}/lib/python/site-packages/OpenImageIO.pyi")
169173
# FIXME: is this the right location to use? the source gets copied to build/src
170174
set (_stub_gen "${CMAKE_SOURCE_DIR}/src/python/generate_stubs.py")
171175

@@ -191,20 +195,18 @@ macro (setup_python_module)
191195
# endif()
192196

193197
add_custom_command (
194-
COMMAND uv run ${_stub_gen} ${PYTHON_BUILD_SITE}
195-
OUTPUT ${_stub_file}
196-
DEPENDS ${_stub_gen}
198+
COMMAND uv run --python=${Python3_EXECUTABLE} ${_stub_gen} "${CMAKE_BINARY_DIR}/lib/python/site-packages"
199+
OUTPUT "${PYTHON_BUILD_SITE}/__init__.pyi"
200+
DEPENDS ${_stub_gen} "${PYTHON_BUILD_SITE}/__init__.py"
197201
COMMENT "Creating python stubs")
198-
install (FILES ${_stub_file} DESTINATION ${PYTHON_SITE_DIR} RENAME __init__.pyi COMPONENT user)
199-
# install the marker file
200-
file (WRITE "${CMAKE_BINARY_DIR}/lib/python/site-packages/py.typed" "")
201-
install (FILES "${CMAKE_BINARY_DIR}/lib/python/site-packages/py.typed"
202-
DESTINATION ${PYTHON_SITE_DIR} COMPONENT user)
202+
203+
install (FILES "${PYTHON_BUILD_SITE}/__init__.pyi" DESTINATION ${PYTHON_SITE_DIR} COMPONENT user)
204+
install (FILES "${PYTHON_BUILD_SITE}/py.typed" DESTINATION ${PYTHON_SITE_DIR} COMPONENT user)
203205

204206
# Ensure this runs after PyOpenImageIO
205207
add_custom_target (
206208
PyOpenImageIO_stubs ALL
207-
DEPENDS ${_stub_file} "${CMAKE_BINARY_DIR}/lib/python/site-packages/py.typed")
209+
DEPENDS "${PYTHON_BUILD_SITE}/__init__.pyi" "${PYTHON_BUILD_SITE}/py.typed")
208210
add_dependencies (PyOpenImageIO_stubs PyOpenImageIO)
209211
endif()
210212

src/python/generate_stubs.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,13 @@ def set_defined_names(self, defined_names: set[str]) -> None:
119119
mypy.stubgenc.InspectionStubGenerator = InspectionStubGenerator # type: ignore[misc]
120120

121121
if __name__ == "__main__":
122+
import os
122123
import sys
123-
oiio_path = sys.argv[1]
124-
sys.path.append(oiio_path)
125-
sys.argv[1:] = ["-p", "OpenImageIO", "-o", oiio_path]
124+
out_path = sys.argv[1]
125+
print(f"Stub output directory: {out_path}")
126+
sys.path.append(out_path)
127+
sys.argv[1:] = ["-p", "OpenImageIO", "-o", out_path]
126128
mypy.stubgen.main()
129+
dest = os.path.join(out_path, "OpenImageIO", "__init__.pyi")
130+
print(f"Renaming to {dest}")
131+
os.rename(os.path.join(out_path, "OpenImageIO", "OpenImageIO.pyi"), dest)

0 commit comments

Comments
 (0)