Skip to content

Commit

Permalink
Deprecate find_executable.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Apr 21, 2024
1 parent a7816b2 commit 190e240
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 20 deletions.
12 changes: 7 additions & 5 deletions distutils/spawn.py
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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'
Expand Down
5 changes: 3 additions & 2 deletions distutils/tests/__init__.py
Expand Up @@ -7,6 +7,7 @@
by import rather than matching pre-defined names.
"""

import shutil
from typing import Sequence


Expand All @@ -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)
Expand All @@ -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]
4 changes: 2 additions & 2 deletions distutils/tests/test_archive_util.py
Expand Up @@ -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()
Expand Down Expand Up @@ -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')
Expand Down
10 changes: 5 additions & 5 deletions 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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
10 changes: 5 additions & 5 deletions distutils/tests/test_sdist.py
Expand Up @@ -2,6 +2,7 @@

import os
import pathlib
import shutil # noqa: F401
import tarfile
import warnings
import zipfile
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion pytest.ini
Expand Up @@ -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

0 comments on commit 190e240

Please sign in to comment.