Skip to content

Commit

Permalink
Merge pull request #136 from HexDecimal/fix-135
Browse files Browse the repository at this point in the history
MRG: Replicate the to-be-removed `wheel.pkginfo` module.

The plans to remove the `wheel.pkginfo` module can be negotiated with the `wheel` devs, but this PR should cause less drama.

Adds `delocate.pkginfo` to read/write these files, based on the original module.

Removes code supporting `wheel<0.32.0`.  Adds code to support the unreleased versions of `wheel>0.37.1`.

Adds type hinting to the relevant functions.

Closes #135
  • Loading branch information
matthew-brett authored Dec 31, 2021
2 parents 7967a1f + c965fe0 commit 54a30f8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 26 deletions.
4 changes: 4 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Releases

* [Unreleased]

* Ensured support for versions of wheel beyond 0.37.1. If you are using older
versions of Delcoate then you may need to to pin ``wheel==0.37.1``.
[#135](https:/matthew-brett/delocate/issues/135)

* [0.10.1] - 2021-11-23

Bugfix release.
Expand Down
29 changes: 29 additions & 0 deletions delocate/pkginfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""Tools for reading and writing PKG-INFO / METADATA without caring
about the encoding.
This is based on a copy of the old wheel.pkginfo module.
"""
from email.generator import Generator
from email.message import Message
from email.parser import Parser
from os import PathLike
from typing import Union


def read_pkg_info_bytes(bytestr: Union[bytes, str]) -> Message:
"""Parse a PKG-INFO or METADATA data string."""
if isinstance(bytestr, bytes):
bytestr = bytestr.decode("utf-8")
return Parser().parsestr(bytestr)


def read_pkg_info(path: Union[bytes, str, PathLike]) -> Message:
"""Read a PKG-INFO or METADATA file."""
with open(path, encoding="utf-8") as headers:
return Parser().parse(headers)


def write_pkg_info(path: Union[bytes, str, PathLike], message: Message) -> None:
"""Write to a PKG-INFO or METADATA file."""
with open(path, "w", encoding="utf-8") as out:
Generator(out, mangle_from_=False, maxheaderlen=0).flatten(message)
17 changes: 4 additions & 13 deletions delocate/tests/test_wheeltools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@

import os
import shutil
from email.message import Message
from os.path import basename, exists, isfile
from os.path import join as pjoin
from os.path import realpath, splitext
from typing import AnyStr

try:
from wheel.install import WheelFile
except ImportError: # As of Wheel 0.32.0
from wheel.wheelfile import WheelFile
from delocate.pkginfo import read_pkg_info_bytes
from wheel.wheelfile import WheelFile

from ..tmpdirs import InTemporaryDirectory
from ..tools import open_readable, zip2dir
Expand Down Expand Up @@ -119,15 +118,7 @@ def _filter_key(items, key):
return [(k, v) for k, v in items if k != key]


def get_info(wheelfile):
# Work round wheel API changes
try:
return wheelfile.parsed_wheel_info
except AttributeError:
pass
# Wheel 0.32.0
from wheel.pkginfo import read_pkg_info_bytes

def get_info(wheelfile: WheelFile) -> Message:
info_name = _get_wheelinfo_name(wheelfile)
return read_pkg_info_bytes(wheelfile.read(info_name))

Expand Down
16 changes: 4 additions & 12 deletions delocate/wheeltools.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@
from os.path import sep as psep
from os.path import splitext

from wheel.pkginfo import read_pkg_info, write_pkg_info
from delocate.pkginfo import read_pkg_info, write_pkg_info
from wheel.util import native, urlsafe_b64encode

try:
from wheel.install import WheelFile
except ImportError: # As of Wheel 0.32.0
from wheel.wheelfile import WheelFile
from wheel.wheelfile import WheelFile

from .tmpdirs import InTemporaryDirectory
from .tools import dir2zip, open_rw, unique_by_index, zip2dir
Expand Down Expand Up @@ -158,12 +154,8 @@ def __enter__(self):
return self


def _get_wheelinfo_name(wheelfile):
# Work round wheel API compatibility
try:
return wheelfile.wheelinfo_name
except AttributeError:
return wheelfile.dist_info_path + "/WHEEL"
def _get_wheelinfo_name(wheelfile: WheelFile):
return wheelfile.dist_info_path + "/WHEEL"


def add_platforms(in_wheel, platforms, out_path=None, clobber=False):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
install_requires=[
"machomachomangler; sys_platform == 'win32'",
"bindepend; sys_platform == 'win32'",
"wheel",
"wheel>=0.32.0",
"typing_extensions",
],
package_data={
Expand Down

0 comments on commit 54a30f8

Please sign in to comment.