Skip to content

Commit

Permalink
Add setuptools.command.build (#3256)
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri authored Jun 13, 2022
2 parents 424c7ae + 6c8fe78 commit 8f31d52
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.d/3256.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added setuptools.command.build command to match distutils.command.build -- by :user:`isuruf`
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ distutils.commands =
alias = setuptools.command.alias:alias
bdist_egg = setuptools.command.bdist_egg:bdist_egg
bdist_rpm = setuptools.command.bdist_rpm:bdist_rpm
build = setuptools.command.build:build
build_clib = setuptools.command.build_clib:build_clib
build_ext = setuptools.command.build_ext:build_ext
build_py = setuptools.command.build_py:build_py
Expand Down
24 changes: 24 additions & 0 deletions setuptools/command/build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from distutils.command.build import build as _build
import warnings

from setuptools import SetuptoolsDeprecationWarning


_ORIGINAL_SUBCOMMANDS = {"build_py", "build_clib", "build_ext", "build_scripts"}


class build(_build):
# copy to avoid sharing the object with parent class
sub_commands = _build.sub_commands[:]

def run(self):
subcommands = {cmd[0] for cmd in _build.sub_commands}
if subcommands - _ORIGINAL_SUBCOMMANDS:
msg = """
It seems that you are using `distutils.command.build.build` to add
new subcommands. Using `distutils` directly is considered deprecated,
please use `setuptools.command.build`.
"""
warnings.warn(msg, SetuptoolsDeprecationWarning)
self.sub_commands = _build.sub_commands
super().run()
63 changes: 63 additions & 0 deletions setuptools/tests/test_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from contextlib import contextmanager
from setuptools import Command, SetuptoolsDeprecationWarning
from setuptools.dist import Distribution
from setuptools.command.build import build
from distutils.command.build import build as distutils_build

import pytest


def test_distribution_gives_setuptools_build_obj(tmpdir_cwd):
"""
Check that the setuptools Distribution uses the
setuptools specific build object.
"""

dist = Distribution(dict(
script_name='setup.py',
script_args=['build'],
packages=[],
package_data={'': ['path/*']},
))
assert isinstance(dist.get_command_obj("build"), build)


@contextmanager
def _restore_sub_commands():
orig = distutils_build.sub_commands[:]
try:
yield
finally:
distutils_build.sub_commands = orig


class Subcommand(Command):
"""Dummy command to be used in tests"""

def initialize_options(self):
pass

def finalize_options(self):
pass

def run(self):
raise NotImplementedError("just to check if the command runs")


@_restore_sub_commands()
def test_subcommand_in_distutils(tmpdir_cwd):
"""
Ensure that sub commands registered in ``distutils`` run,
after instructing the users to migrate to ``setuptools``.
"""
dist = Distribution(dict(
packages=[],
cmdclass={'subcommand': Subcommand},
))
distutils_build.sub_commands.append(('subcommand', None))

warning_msg = "please use .setuptools.command.build."
with pytest.warns(SetuptoolsDeprecationWarning, match=warning_msg):
# For backward compatibility, the subcommand should run anyway:
with pytest.raises(NotImplementedError, match="the command runs"):
dist.run_command("build")

0 comments on commit 8f31d52

Please sign in to comment.