Skip to content

Commit

Permalink
Support passing dist-info-dir to bdist_wheel, enabling setuptools to …
Browse files Browse the repository at this point in the history
…handle prepare_metadata_for_build_wheel correctly as per PEP-517. Closes #611
  • Loading branch information
pelson committed Apr 24, 2024
1 parent 0a4f40e commit 2f77339
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/wheel/bdist_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ class bdist_wheel(Command):
None,
"Python tag (cp32|cp33|cpNN) for abi3 wheel tag" " (default: false)",
),
(
"dist-info-dir=",
None,
"directory where a pre-generated dist-info can be found (e.g. as a "
"result of calling the PEP517 'prepare_metadata_for_build_wheel' "
"method)"),
]

boolean_options = ["keep-temp", "skip-build", "relative", "universal"]
Expand All @@ -231,6 +237,7 @@ def initialize_options(self):
self.format = "zip"
self.keep_temp = False
self.dist_dir = None
self.dist_info_dir = None
self.egginfo_dir = None
self.root_is_pure = None
self.skip_build = None
Expand All @@ -249,8 +256,9 @@ def finalize_options(self):
bdist_base = self.get_finalized_command("bdist").bdist_base
self.bdist_dir = os.path.join(bdist_base, "wheel")

egg_info = self.distribution.get_command_obj("egg_info")
egg_info.ensure_finalized() # needed for correct `wheel_dist_name`
if self.dist_info_dir is None:
egg_info = self.distribution.get_command_obj("egg_info")
egg_info.ensure_finalized() # needed for correct `wheel_dist_name`

Check warning on line 261 in src/wheel/bdist_wheel.py

View check run for this annotation

Codecov / codecov/patch

src/wheel/bdist_wheel.py#L259-L261

Added lines #L259 - L261 were not covered by tests

self.data_dir = self.wheel_dist_name + ".data"
self.plat_name_supplied = self.plat_name is not None
Expand Down Expand Up @@ -412,12 +420,21 @@ def run(self):
)

self.set_undefined_options("install_egg_info", ("target", "egginfo_dir"))

distinfo_dirname = (
f"{safer_name(self.distribution.get_name())}-"
f"{safer_version(self.distribution.get_version())}.dist-info"
)
distinfo_dir = os.path.join(self.bdist_dir, distinfo_dirname)
self.egg2dist(self.egginfo_dir, distinfo_dir)
if self.dist_info_dir:

Check warning on line 429 in src/wheel/bdist_wheel.py

View check run for this annotation

Codecov / codecov/patch

src/wheel/bdist_wheel.py#L429

Added line #L429 was not covered by tests
# Use the given dist-info directly.
shutil.copytree(self.dist_info_dir, distinfo_dir)

Check warning on line 431 in src/wheel/bdist_wheel.py

View check run for this annotation

Codecov / codecov/patch

src/wheel/bdist_wheel.py#L431

Added line #L431 was not covered by tests
# Egg info is still generated, so remove it now to avoid it getting
# copied into the wheel.
shutil.rmtree(self.egginfo_dir)

Check warning on line 434 in src/wheel/bdist_wheel.py

View check run for this annotation

Codecov / codecov/patch

src/wheel/bdist_wheel.py#L434

Added line #L434 was not covered by tests
else:
# Convert the generated egg-info into dist-info.
self.egg2dist(self.egginfo_dir, distinfo_dir)

Check warning on line 437 in src/wheel/bdist_wheel.py

View check run for this annotation

Codecov / codecov/patch

src/wheel/bdist_wheel.py#L437

Added line #L437 was not covered by tests

self.write_wheelfile(distinfo_dir)

Expand Down
23 changes: 23 additions & 0 deletions tests/test_bdist_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,29 @@ def license_paths(self):
assert "UTF-8 描述 説明" in metadata


def test_dist_info_provided(dummy_dist, monkeypatch, tmp_path):
monkeypatch.chdir(dummy_dist)
distinfo = tmp_path / "dummy_dist.dist-info"
distinfo.mkdir()
(distinfo / 'METADATA').write_text("name: helloworld", encoding="utf-8")

# We don't control the metadata. According to PEP-517, "The hook MAY also
# create other files inside this directory, and a build frontend MUST
# preserve".
(distinfo / 'FOO').write_text("bar", encoding="utf-8")
subprocess.check_call(
[
sys.executable, "setup.py", "bdist_wheel",
"-b", str(tmp_path), "--universal",
"--dist-info-dir", str(distinfo),
],
)
with WheelFile("dist/dummy_dist-1.0-py2.py3-none-any.whl") as wf:
with wf.open("dummy_dist-1.0.dist-info/METADATA") as fh:
assert fh.read() == b'name: helloworld'
assert 'dummy_dist-1.0.dist-info/FOO' in list(wf.namelist())


def test_licenses_default(dummy_dist, monkeypatch, tmp_path):
monkeypatch.chdir(dummy_dist)
subprocess.check_call(
Expand Down

0 comments on commit 2f77339

Please sign in to comment.