diff --git a/distutils/spawn.py b/distutils/spawn.py index 2116136b..429d1ccb 100644 --- a/distutils/spawn.py +++ b/distutils/spawn.py @@ -2,17 +2,16 @@ Provides the 'spawn()' function, a front-end to various platform- specific functions for launching another program in a sub-process. -Also provides the 'find_executable()' to search the path for a given -executable name. """ from __future__ import annotations import os import platform +import shutil import subprocess - -from typing import Mapping +import sys +import warnings from typing import Mapping @@ -64,7 +63,7 @@ def spawn(cmd, search_path=True, verbose=False, dry_run=False, env=None): return if search_path: - executable = find_executable(cmd[0]) + executable = shutil.which(cmd[0]) if executable is not None: cmd[0] = executable @@ -86,6 +85,9 @@ def find_executable(executable, path=None): A string listing directories separated by 'os.pathsep'; defaults to os.environ['PATH']. Returns the complete filename or None if not found. """ + warnings.warn( + 'Use shutil.which instead of find_executable', DeprecationWarning, stacklevel=2 + ) _, ext = os.path.splitext(executable) if (sys.platform == 'win32') and (ext != '.exe'): executable = executable + '.exe' diff --git a/distutils/tests/__init__.py b/distutils/tests/__init__.py index 20dfe8f1..16b68b4e 100644 --- a/distutils/tests/__init__.py +++ b/distutils/tests/__init__.py @@ -7,6 +7,7 @@ by import rather than matching pre-defined names. """ +import shutil from typing import Sequence @@ -19,7 +20,7 @@ def missing_compiler_executable(cmd_names: Sequence[str] = []): # pragma: no co missing. """ - from distutils import ccompiler, errors, spawn, sysconfig + from distutils import ccompiler, errors, sysconfig compiler = ccompiler.new_compiler() sysconfig.customize_compiler(compiler) @@ -37,5 +38,5 @@ def missing_compiler_executable(cmd_names: Sequence[str] = []): # pragma: no co assert cmd is not None, "the '%s' executable is not configured" % name elif not cmd: continue - if spawn.find_executable(cmd[0]) is None: + if shutil.which(cmd[0]) is None: return cmd[0] diff --git a/distutils/tests/test_archive_util.py b/distutils/tests/test_archive_util.py index 02af2aa0..abbcd36c 100644 --- a/distutils/tests/test_archive_util.py +++ b/distutils/tests/test_archive_util.py @@ -135,7 +135,7 @@ def _create_files(self): return tmpdir @pytest.mark.usefixtures('needs_zlib') - @pytest.mark.skipif("not (find_executable('tar') and find_executable('gzip'))") + @pytest.mark.skipif("not (shutil.which('tar') and shutil.which('gzip'))") def test_tarfile_vs_tar(self): tmpdir = self._create_files() tmpdir2 = self.mkdtemp() @@ -190,7 +190,7 @@ def test_tarfile_vs_tar(self): tarball = base_name + '.tar' assert os.path.exists(tarball) - @pytest.mark.skipif("not find_executable('compress')") + @pytest.mark.skipif("not shutil.which('compress')") def test_compress_deprecated(self): tmpdir = self._create_files() base_name = os.path.join(self.mkdtemp(), 'archive') diff --git a/distutils/tests/test_bdist_rpm.py b/distutils/tests/test_bdist_rpm.py index a5cb42c3..28edda4d 100644 --- a/distutils/tests/test_bdist_rpm.py +++ b/distutils/tests/test_bdist_rpm.py @@ -1,10 +1,10 @@ """Tests for distutils.command.bdist_rpm.""" import os +import shutil # noqa: F401 import sys from distutils.command.bdist_rpm import bdist_rpm from distutils.core import Distribution -from distutils.spawn import find_executable # noqa: F401 from distutils.tests import support import pytest @@ -43,8 +43,8 @@ class TestBuildRpm( ): @mac_woes @requires_zlib() - @pytest.mark.skipif("not find_executable('rpm')") - @pytest.mark.skipif("not find_executable('rpmbuild')") + @pytest.mark.skipif("not shutil.which('rpm')") + @pytest.mark.skipif("not shutil.which('rpmbuild')") def test_quiet(self): # let's create a package tmp_dir = self.mkdtemp() @@ -86,8 +86,8 @@ def test_quiet(self): @mac_woes @requires_zlib() # https://bugs.python.org/issue1533164 - @pytest.mark.skipif("not find_executable('rpm')") - @pytest.mark.skipif("not find_executable('rpmbuild')") + @pytest.mark.skipif("not shutil.which('rpm')") + @pytest.mark.skipif("not shutil.which('rpmbuild')") def test_no_optimize_flag(self): # let's create a package that breaks bdist_rpm tmp_dir = self.mkdtemp() diff --git a/distutils/tests/test_sdist.py b/distutils/tests/test_sdist.py index a85997f1..6a1aa518 100644 --- a/distutils/tests/test_sdist.py +++ b/distutils/tests/test_sdist.py @@ -2,6 +2,7 @@ import os import pathlib +import shutil # noqa: F401 import tarfile import warnings import zipfile @@ -10,7 +11,6 @@ from distutils.core import Distribution from distutils.errors import DistutilsOptionError from distutils.filelist import FileList -from distutils.spawn import find_executable # noqa: F401 from distutils.tests.test_config import BasePyPIRCCommandTestCase from os.path import join from textwrap import dedent @@ -137,8 +137,8 @@ def test_prune_file_list(self): assert sorted(content) == ['fake-1.0/' + x for x in expected] @pytest.mark.usefixtures('needs_zlib') - @pytest.mark.skipif("not find_executable('tar')") - @pytest.mark.skipif("not find_executable('gzip')") + @pytest.mark.skipif("not shutil.which('tar')") + @pytest.mark.skipif("not shutil.which('gzip')") def test_make_distribution(self): # now building a sdist dist, cmd = self.get_cmd() @@ -434,8 +434,8 @@ def test_manual_manifest(self): @pytest.mark.usefixtures('needs_zlib') @require_unix_id @require_uid_0 - @pytest.mark.skipif("not find_executable('tar')") - @pytest.mark.skipif("not find_executable('gzip')") + @pytest.mark.skipif("not shutil.which('tar')") + @pytest.mark.skipif("not shutil.which('gzip')") def test_make_distribution_owner_group(self): # now building a sdist dist, cmd = self.get_cmd() diff --git a/pytest.ini b/pytest.ini index f9b1d1fc..b53e0d93 100644 --- a/pytest.ini +++ b/pytest.ini @@ -35,5 +35,8 @@ filterwarnings= # suppress warnings in deprecated compilers ignore:(bcpp|msvc9?)compiler is deprecated - # suppress well know deprecation warning + # suppress well known deprecation warning ignore:distutils.log.Log is deprecated + + # suppress known deprecation + ignore:Use shutil.which instead of find_executable:DeprecationWarning