挂钩添加命令到distutils构建?

ast*_*rog 12 python distutils

我在setup.py脚本中添加了自定义distutils命令:

from distutils.command.build_py import build_py

cmdclass = {}
cmdclass['build_qt'] = BuildQt
cmdclass['build_py'] = build_py

setup(..., cmdclass=cmdclass, ...)
Run Code Online (Sandbox Code Playgroud)

有没有办法让它在运行时::

python setup.py build
Run Code Online (Sandbox Code Playgroud)

这第一次打电话

python setup.py build_qt
Run Code Online (Sandbox Code Playgroud)

自动?

eca*_*mur 15

你可以覆盖build:

from distutils.command.build import build

class my_build(build):
    def run(self):
        self.run_command("build_qt")
        build.run(self)

cmdclass['build'] = my_build
Run Code Online (Sandbox Code Playgroud)


Hor*_*tor 5

为了添加您自己的命令,您可以对默认build命令进行子类化并扩展其子命令:

class _build(build):
     sub_commands = [('build_qt', None)] + build.sub_commands

...
setup(..., cmdclass={'build': _build, ...})
Run Code Online (Sandbox Code Playgroud)

文档(distutils.cmd.Command):

# 'sub_commands' formalizes the notion of a "family" of commands,
# eg. "install" as the parent with sub-commands "install_lib",
# "install_headers", etc.  The parent of a family of commands
# defines 'sub_commands' as a class attribute; it's a list of
#    (command_name : string, predicate : unbound_method | string | None)
# tuples, where 'predicate' is a method of the parent command that
# determines whether the corresponding command is applicable in the
# current situation.  (Eg. we "install_headers" is only applicable if
# we have any C header files to install.)  If 'predicate' is None,
# that command is always applicable.
#
# 'sub_commands' is usually defined at the *end* of a class, because
# predicates can be unbound methods, so they must already have been
# defined.  The canonical example is the "install" command.
sub_commands = []
Run Code Online (Sandbox Code Playgroud)