Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setuptools.command.build #3256

Merged
merged 8 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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
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")