Skip to content

Commit

Permalink
Add post_extraction_command
Browse files Browse the repository at this point in the history
This flag is useful when one needs to do some ad-hoc operations on the
wheel files. For example, our use case pulls in a lot of third party
dependencies and some of them may have name conflict each other and we
sometimes need to remove or patch `__init__.py` files in order for
conflicting dependencies to coexist.

I am open to other suggestions or work arounds. But adding the ability
to do ad-hoc patches for each wheel_library seems to be the simplest way
to achieve this.
  • Loading branch information
tgeng committed Dec 13, 2023
1 parent 6d4a4ab commit 8e58e8d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pycross/private/tools/wheel_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import os
import shutil
import subprocess
import tempfile
from pathlib import Path
from typing import Any
Expand Down Expand Up @@ -79,6 +80,8 @@ def main(args: Any) -> None:
shutil.rmtree(link_dir, ignore_errors=True)

setup_namespace_pkg_compatibility(lib_dir)
if args.post_extraction_command:
subprocess.run(str(args.post_extraction_command.resolve()), cwd=str(lib_dir), check=True)


def parse_flags() -> Any:
Expand Down Expand Up @@ -110,6 +113,12 @@ def parse_flags() -> Any:
help="The output path.",
)

parser.add_argument(
"--post-extraction-command",
type=Path,
required=False,
)

return parser.parse_args()


Expand Down
10 changes: 10 additions & 0 deletions pycross/private/wheel_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ def _pycross_wheel_library_impl(ctx):
if ctx.attr.enable_implicit_namespace_pkgs:
args.add("--enable-implicit-namespace-pkgs")

if ctx.attr.post_extraction_command:
script = ctx.actions.declare_file(ctx.attr.name + "_post_extraction_command")
inputs.append(script)
ctx.actions.write(script, "#!/usr/bin/env bash\n" + ctx.attr.post_extraction_command, is_executable = True)
args.add("--post-extraction-command", script)

ctx.actions.run(
inputs = inputs,
outputs = [out],
Expand Down Expand Up @@ -113,6 +119,10 @@ This option is required to support some packages which cannot handle the convers
doc = "The python version required for this wheel ('PY2' or 'PY3')",
values = ["PY2", "PY3", ""],
),
"post_extraction_command": attr.string(
doc = "Bash command to run after content of the wheel gets extracted. The working directory is set to the extraction location",
default = "",
),
"_tool": attr.label(
default = Label("//pycross/private/tools:wheel_installer"),
cfg = "exec",
Expand Down

0 comments on commit 8e58e8d

Please sign in to comment.