diff --git a/changelog.d/3256.change.rst b/changelog.d/3256.change.rst new file mode 100644 index 0000000000..46ead2d2be --- /dev/null +++ b/changelog.d/3256.change.rst @@ -0,0 +1 @@ +Added setuptools.command.build command to match distutils.command.build -- by :user:`isuruf` diff --git a/setup.cfg b/setup.cfg index 78c088a15b..af55965d8a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -105,6 +105,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 diff --git a/setuptools/command/build.py b/setuptools/command/build.py new file mode 100644 index 0000000000..7ab60ce6b2 --- /dev/null +++ b/setuptools/command/build.py @@ -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() diff --git a/setuptools/tests/test_build.py b/setuptools/tests/test_build.py new file mode 100644 index 0000000000..cefb3d343c --- /dev/null +++ b/setuptools/tests/test_build.py @@ -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")