diff --git a/cibuildwheel/platforms/android.py b/cibuildwheel/platforms/android.py index cb57b907a..32b01387a 100644 --- a/cibuildwheel/platforms/android.py +++ b/cibuildwheel/platforms/android.py @@ -439,7 +439,11 @@ def repair_wheel(state: BuildState, built_wheel: Path) -> Path: if state.options.repair_command: shell( prepare_command( - state.options.repair_command, wheel=built_wheel, dest_dir=repaired_wheel_dir + state.options.repair_command, + wheel=built_wheel, + dest_dir=repaired_wheel_dir, + package=state.options.package_dir, + project=".", ), env=state.build_env, ) diff --git a/cibuildwheel/platforms/linux.py b/cibuildwheel/platforms/linux.py index 8411a935a..6903079bd 100644 --- a/cibuildwheel/platforms/linux.py +++ b/cibuildwheel/platforms/linux.py @@ -320,7 +320,11 @@ def build_in_container( if build_options.repair_command: log.step("Repairing wheel...") repair_command_prepared = prepare_command( - build_options.repair_command, wheel=built_wheel, dest_dir=repaired_wheel_dir + build_options.repair_command, + wheel=built_wheel, + dest_dir=repaired_wheel_dir, + package=container_package_dir, + project=container_project_path, ) container.call(["sh", "-c", repair_command_prepared], env=env) else: diff --git a/cibuildwheel/platforms/macos.py b/cibuildwheel/platforms/macos.py index e329ccf37..763a410db 100644 --- a/cibuildwheel/platforms/macos.py +++ b/cibuildwheel/platforms/macos.py @@ -528,6 +528,8 @@ def build(options: Options, tmp_path: Path) -> None: wheel=built_wheel, dest_dir=repaired_wheel_dir, delocate_archs=delocate_archs, + package=build_options.package_dir, + project=".", ) shell(repair_command_prepared, env=env) else: diff --git a/cibuildwheel/platforms/pyodide.py b/cibuildwheel/platforms/pyodide.py index 60d707078..8b8ba2aa5 100644 --- a/cibuildwheel/platforms/pyodide.py +++ b/cibuildwheel/platforms/pyodide.py @@ -443,6 +443,8 @@ def build(options: Options, tmp_path: Path) -> None: build_options.repair_command, wheel=built_wheel, dest_dir=repaired_wheel_dir, + package=build_options.package_dir, + project=".", ) shell(repair_command_prepared, env=env) log.step_end() diff --git a/cibuildwheel/platforms/windows.py b/cibuildwheel/platforms/windows.py index 98c83b0f9..9a80aaecd 100644 --- a/cibuildwheel/platforms/windows.py +++ b/cibuildwheel/platforms/windows.py @@ -525,6 +525,8 @@ def build(options: Options, tmp_path: Path) -> None: build_options.repair_command, wheel=built_wheel, dest_dir=repaired_wheel_dir, + package=build_options.package_dir, + project=".", ) shell(repair_command_prepared, env=env) else: diff --git a/docs/options.md b/docs/options.md index 1c4e89d89..c59fea660 100644 --- a/docs/options.md +++ b/docs/options.md @@ -906,6 +906,8 @@ The following placeholders must be used inside the command and will be replaced - `{dest_dir}` for the absolute path of the directory where to create the repaired wheel - `{delocate_archs}` (macOS only) comma-separated list of architectures in the wheel. +You can use the `{package}` or `{project}` placeholders in your `repair-wheel-command` to refer to the package being built or the project root, respectively. + The command is run in a shell, so you can run multiple commands like `cmd1 && cmd2`. Platform-specific environment variables are also available:
diff --git a/test/test_custom_repair_wheel.py b/test/test_custom_repair_wheel.py index 5af012653..0b6818078 100644 --- a/test/test_custom_repair_wheel.py +++ b/test/test_custom_repair_wheel.py @@ -1,4 +1,5 @@ import subprocess +import textwrap from contextlib import nullcontext as does_not_raise import pytest @@ -56,3 +57,41 @@ def test(tmp_path, capfd): # We only produced one wheel (perhaps Pyodide) # check that it has the right name assert result[0].startswith("spam-0.1.0-py2-none-") + + +@pytest.mark.parametrize( + "repair_command", + [ + "python repair.py {wheel} {dest_dir}", + "python {package}/repair.py {wheel} {dest_dir}", + "python {project}/repair.py {wheel} {dest_dir}", + ], + ids=["no-placeholder", "package-placeholder", "project-placeholder"], +) +def test_repair_wheel_command_structure(tmp_path, repair_command): + project_dir = tmp_path / "project" + project = test_projects.new_c_project() + project.files["repair.py"] = textwrap.dedent(""" + import shutil + import sys + from pathlib import Path + + wheel = Path(sys.argv[1]) + dest_dir = Path(sys.argv[2]) + + dest_dir.mkdir(parents=True, exist_ok=True) + shutil.copy(wheel, dest_dir / "spam-repaired.whl") + """) + + # Combined test for repair wheel command formats (plain, {package}, {project}) + project.generate(project_dir) + + result = utils.cibuildwheel_run( + project_dir, + add_env={ + "CIBW_REPAIR_WHEEL_COMMAND": repair_command, + }, + single_python=True, + ) + + assert result == ["spam-repaired.whl"]