Skip to content

Commit

Permalink
Refactor patch_wheel function
Browse files Browse the repository at this point in the history
Remove `raise ValueError` on missing patch file.
This was always extraneous since the open call would've raised `FileNotFoundError` on its own.

Normally I wouldn't add an undocumented raise to the changelog, but I'm being safe.

Convert to pathlib and use str types for Popen.
  • Loading branch information
HexDecimal committed Oct 15, 2024
1 parent d2115f4 commit 85e4719
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ rules on making a good Changelog.

## [Unreleased]

### Changed

- `patch_wheel` function raises `FileNotFoundError` instead of `ValueError` on
missing patch files.

### Removed

- Dropped support for Python 3.7 and Python 3.8.
Expand Down
31 changes: 13 additions & 18 deletions delocate/delocating.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,9 @@ def delocate_wheel(


def patch_wheel(
in_wheel: str, patch_fname: str, out_wheel: str | None = None
in_wheel: str | os.PathLike[str],
patch_fname: str | os.PathLike[str],
out_wheel: str | os.PathLike[str] | None = None,
) -> None:
"""Apply ``-p1`` style patch in `patch_fname` to contents of `in_wheel`.
Expand All @@ -1101,32 +1103,25 @@ def patch_wheel(
Parameters
----------
in_wheel : str
in_wheel : str or PathLike
Filename of wheel to process
patch_fname : str
patch_fname : str or PathLike
Filename of patch file. Will be applied with ``patch -p1 <
patch_fname``
out_wheel : None or str
out_wheel : None or str or PathLike
Filename of patched wheel to write. If None, overwrite `in_wheel`
"""
in_wheel = abspath(in_wheel)
patch_fname = abspath(patch_fname)
if out_wheel is None:
out_wheel = in_wheel
else:
out_wheel = abspath(out_wheel)
if not exists(patch_fname):
raise ValueError(f"patch file {patch_fname} does not exist")
in_wheel = Path(in_wheel).resolve(strict=True)
patch_fname = Path(patch_fname).resolve(strict=True)
out_wheel = Path(out_wheel).resolve() if out_wheel is not None else in_wheel
with InWheel(in_wheel, out_wheel):
with open(patch_fname, "rb") as fobj:
with open(patch_fname, "rb") as f:
patch_proc = Popen(
["patch", "-p1"], stdin=fobj, stdout=PIPE, stderr=PIPE
["patch", "-p1"], stdin=f, stdout=PIPE, stderr=PIPE, text=True
)
stdout, stderr = patch_proc.communicate()
stdout, _stderr = patch_proc.communicate()
if patch_proc.returncode != 0:
raise RuntimeError(
"Patch failed with stdout:\n" + stdout.decode("latin1")
)
raise RuntimeError(f"Patch failed with stdout:\n{stdout}")


_ARCH_LOOKUP = {"intel": ["i386", "x86_64"], "universal2": ["x86_64", "arm64"]}
Expand Down

0 comments on commit 85e4719

Please sign in to comment.