Skip to content

Commit 5a61475

Browse files
authored
Merge pull request #6236 from pradyunsg/fix/pip-wheel-locations
Copy the correct file during PEP 517's build process
2 parents b5dd279 + 51086b3 commit 5a61475

File tree

3 files changed

+61
-15
lines changed

3 files changed

+61
-15
lines changed

news/6196.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ensure the correct wheel file is copied when building PEP 517 distribution is built.

src/pip/_internal/wheel.py

+25-15
Original file line numberDiff line numberDiff line change
@@ -818,15 +818,14 @@ def _build_one_inside_env(self, req, output_dir, python_tag=None):
818818
builder = self._build_one_pep517
819819
else:
820820
builder = self._build_one_legacy
821-
if builder(req, temp_dir.path, python_tag=python_tag):
821+
wheel_path = builder(req, temp_dir.path, python_tag=python_tag)
822+
if wheel_path is not None:
823+
wheel_name = os.path.basename(wheel_path)
824+
dest_path = os.path.join(output_dir, wheel_name)
822825
try:
823-
wheel_name = os.listdir(temp_dir.path)[0]
824-
wheel_path = os.path.join(output_dir, wheel_name)
825-
shutil.move(
826-
os.path.join(temp_dir.path, wheel_name), wheel_path
827-
)
826+
shutil.move(wheel_path, dest_path)
828827
logger.info('Stored in directory: %s', output_dir)
829-
return wheel_path
828+
return dest_path
830829
except Exception:
831830
pass
832831
# Ignore return, we can't do anything else useful.
@@ -844,29 +843,39 @@ def _base_setup_args(self, req):
844843
] + list(self.global_options)
845844

846845
def _build_one_pep517(self, req, tempd, python_tag=None):
846+
"""Build one InstallRequirement using the PEP 517 build process.
847+
848+
Returns path to wheel if successfully built. Otherwise, returns None.
849+
"""
847850
assert req.metadata_directory is not None
848851
try:
849852
req.spin_message = 'Building wheel for %s (PEP 517)' % (req.name,)
850853
logger.debug('Destination directory: %s', tempd)
851-
wheelname = req.pep517_backend.build_wheel(
854+
wheel_name = req.pep517_backend.build_wheel(
852855
tempd,
853856
metadata_directory=req.metadata_directory
854857
)
855858
if python_tag:
856859
# General PEP 517 backends don't necessarily support
857860
# a "--python-tag" option, so we rename the wheel
858861
# file directly.
859-
newname = replace_python_tag(wheelname, python_tag)
862+
new_name = replace_python_tag(wheel_name, python_tag)
860863
os.rename(
861-
os.path.join(tempd, wheelname),
862-
os.path.join(tempd, newname)
864+
os.path.join(tempd, wheel_name),
865+
os.path.join(tempd, new_name)
863866
)
864-
return True
867+
# Reassign to simplify the return at the end of function
868+
wheel_name = new_name
865869
except Exception:
866870
logger.error('Failed building wheel for %s', req.name)
867-
return False
871+
return None
872+
return os.path.join(tempd, wheel_name)
868873

869874
def _build_one_legacy(self, req, tempd, python_tag=None):
875+
"""Build one InstallRequirement using the "legacy" build process.
876+
877+
Returns path to wheel if successfully built. Otherwise, returns None.
878+
"""
870879
base_args = self._base_setup_args(req)
871880

872881
spin_message = 'Building wheel for %s (setup.py)' % (req.name,)
@@ -881,11 +890,12 @@ def _build_one_legacy(self, req, tempd, python_tag=None):
881890
try:
882891
call_subprocess(wheel_args, cwd=req.setup_py_dir,
883892
show_stdout=False, spinner=spinner)
884-
return True
885893
except Exception:
886894
spinner.finish("error")
887895
logger.error('Failed building wheel for %s', req.name)
888-
return False
896+
return None
897+
# listdir's return value is sorted to be deterministic
898+
return os.path.join(tempd, sorted(os.listdir(tempd))[0])
889899

890900
def _clean_one(self, req):
891901
base_args = self._base_setup_args(req)

tests/functional/test_wheel.py

+35
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ def auto_with_wheel(with_wheel):
1414
pass
1515

1616

17+
def add_files_to_dist_directory(folder):
18+
(folder / 'dist').makedirs()
19+
(folder / 'dist' / 'a_name-0.0.1.tar.gz').write("hello")
20+
# Not adding a wheel file since that confuses setuptools' backend.
21+
# (folder / 'dist' / 'a_name-0.0.1-py2.py3-none-any.whl').write("hello")
22+
23+
1724
def test_wheel_exit_status_code_when_no_requirements(script):
1825
"""
1926
Test wheel exit status code when no requirements specified
@@ -210,3 +217,31 @@ def test_pip_wheel_with_user_set_in_config(script, data, common_wheels):
210217
'--no-index', '-f', common_wheels
211218
)
212219
assert "Successfully built withpyproject" in result.stdout, result.stdout
220+
221+
222+
def test_pep517_wheels_are_not_confused_with_other_files(script, tmpdir, data):
223+
"""Check correct wheels are copied. (#6196)
224+
"""
225+
pkg_to_wheel = data.src / 'withpyproject'
226+
add_files_to_dist_directory(pkg_to_wheel)
227+
228+
result = script.pip('wheel', pkg_to_wheel, '-w', script.scratch_path)
229+
assert "Installing build dependencies" in result.stdout, result.stdout
230+
231+
wheel_file_name = 'withpyproject-0.0.1-py%s-none-any.whl' % pyversion[0]
232+
wheel_file_path = script.scratch / wheel_file_name
233+
assert wheel_file_path in result.files_created, result.stdout
234+
235+
236+
def test_legacy_wheels_are_not_confused_with_other_files(script, tmpdir, data):
237+
"""Check correct wheels are copied. (#6196)
238+
"""
239+
pkg_to_wheel = data.src / 'simplewheel-1.0'
240+
add_files_to_dist_directory(pkg_to_wheel)
241+
242+
result = script.pip('wheel', pkg_to_wheel, '-w', script.scratch_path)
243+
assert "Installing build dependencies" not in result.stdout, result.stdout
244+
245+
wheel_file_name = 'simplewheel-1.0-py%s-none-any.whl' % pyversion[0]
246+
wheel_file_path = script.scratch / wheel_file_name
247+
assert wheel_file_path in result.files_created, result.stdout

0 commit comments

Comments
 (0)