Skip to content

Commit 161ca81

Browse files
committed
General improvements
1 parent 4c39118 commit 161ca81

3 files changed

Lines changed: 44 additions & 21 deletions

File tree

pyproject.toml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,14 @@ SKBUILD_CMAKE_BUILD_TYPE = "MinSizeRel"
125125
SKBUILD_CMAKE_BUILD_TYPE = "MinSizeRel"
126126

127127
[[tool.cibuildwheel.overrides]]
128-
# Trigger the build of the python stubs for certain platforms. In CI, this only
129-
# runs on a single platform (manylinux_x86_64), but we use * here to allow local
130-
# tests on aarch64 without running docker in emulation (i.e. new Mac silicon).
131-
# If this `select` value is updated it should be reflected in wheel.yml
128+
# Trigger the build & validation of the python stubs for certain platforms.
129+
# The local entry-point for this is `make pystubs`, which will generate the
130+
# stubs and copy them into the git repo to be committed and reviewed.
131+
# In CI, this builds the stubs and validates they match what has been committed
132+
# to the repo.
132133
select = "cp311-manylinux_*64"
133134
test-requires = "mypy~=1.15.0 stubgenlib~=0.1.0"
134135
inherit.test-command = "append"
135-
test-command = ["python {project}/src/python/generate_stubs.py '/output' '{project}/src/python'"]
136+
test-command = [
137+
"python {project}/src/python/generate_stubs.py --out-path '/output' --validate-path '{project}/src/python'",
138+
]

src/python/CMakeLists.txt

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,28 @@ set_target_properties(PyOpenImageIO PROPERTIES
2020
set_source_files_properties(${python_srcs} PROPERTIES
2121
UNITY_GROUP PyOpenImageIO)
2222

23-
message("PROC: ${CMAKE_HOST_SYSTEM_PROCESSOR}")
24-
25-
# FIXME: choose aarch vs x86_64 based on the current platform
26-
27-
add_custom_command (
28-
COMMAND cd ${CMAKE_SOURCE_DIR} &&
29-
cibuildwheel "." --output-dir "${CMAKE_BINARY_DIR}/wheelhouse" --only "cp311-manylinux_aarch64"
23+
# choose aarch vs x86_64 based on the current platform
24+
if (CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "arm64")
25+
set(_arch, "aarch64")
26+
else ()
27+
set(_arch, "x86_64")
28+
endif ()
29+
30+
# Note: this must be kept in sync with `[[tool.cibuildwheel.overrides]]` in pyproject.toml
31+
set (_python_build_id, "cp311-manylinux_${_arch}")
32+
33+
add_custom_command (COMMAND cd ${CMAKE_SOURCE_DIR} &&
34+
cibuildwheel "." --output-dir "${CMAKE_BINARY_DIR}/wheelhouse" --only
3035
OUTPUT "${CMAKE_BINARY_DIR}/wheelhouse/OpenImageIO/__init__.pyi"
31-
COMMENT "pystubs: creating python wheel"
36+
COMMENT "Generating python stubs"
3237
)
3338

34-
add_custom_command (
35-
OUTPUT "${CMAKE_SOURCE_DIR}/src/python/stubs/OpenImageIO-stubs/__init__.pyi"
36-
COMMAND ${CMAKE_COMMAND} -E copy
39+
add_custom_command (COMMAND ${CMAKE_COMMAND} -E copy
3740
"${CMAKE_BINARY_DIR}/wheelhouse/OpenImageIO/__init__.pyi"
3841
"${CMAKE_SOURCE_DIR}/src/python/stubs/OpenImageIO-stubs/__init__.pyi"
42+
OUTPUT "${CMAKE_SOURCE_DIR}/src/python/stubs/OpenImageIO-stubs/__init__.pyi"
3943
DEPENDS "${CMAKE_BINARY_DIR}/wheelhouse/OpenImageIO/__init__.pyi"
40-
COMMENT "pystubs: Copying generated stubs to source"
44+
COMMENT "Copying generated stubs to source"
4145
)
4246

4347
add_custom_target (pystubs

src/python/generate_stubs.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,30 @@ def get_colored_diff(old_text: str, new_text: str):
145145

146146

147147
if __name__ == "__main__":
148-
# FIXME: add argparse
148+
import argparse
149149
import pathlib
150150
import os
151151
import sys
152-
out_path = pathlib.Path(sys.argv[1])
153-
validate_path = pathlib.Path(sys.argv[2])
152+
153+
parser = argparse.ArgumentParser()
154+
parser.add_argument(
155+
"--out-path",
156+
default="out",
157+
help="Directory to write the stubs."
158+
)
159+
parser.add_argument(
160+
"--validate-path",
161+
default=None,
162+
help="If provided, compare the generated stub to this file. Exits with code 2 if the "
163+
"contents differ."
164+
)
165+
args = parser.parse_args()
166+
out_path = pathlib.Path(args.out_path)
154167
print(f"Stub output directory: {out_path}")
168+
155169
# perform import so we can see the traceback if it fails.
156170
import OpenImageIO
171+
157172
sys.argv[1:] = ["-p", "OpenImageIO", "-o", str(out_path)]
158173
mypy.stubgen.main()
159174
source = out_path.joinpath("OpenImageIO", "OpenImageIO.pyi")
@@ -165,7 +180,8 @@ def get_colored_diff(old_text: str, new_text: str):
165180
print(f"Renaming to {dest}")
166181
os.rename(source, dest)
167182

168-
if "GITHUB_ACTIONS" in os.environ:
183+
if "GITHUB_ACTIONS" in os.environ and args.validate_path:
184+
validate_path = pathlib.Path(args.validate_path)
169185
# in CI, we don't overwrite the output path, we merely validate that what has
170186
# been committed to the repo is what we expect.
171187
old_text = validate_path.text()

0 commit comments

Comments
 (0)