Hook to add commands to distutils build?

只谈情不闲聊 提交于 2019-12-17 18:53:33

问题


I've added a custom distutils command to a setup.py script:

from distutils.command.build_py import build_py

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

setup(..., cmdclass=cmdclass, ...)

Is there a way to make it so that when running::

python setup.py build

this first calls

python setup.py build_qt

automatically?


回答1:


You can override 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



回答2:


In order to add your own command, you can subclass the default build-command and extend its subcommands:

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

...
setup(..., cmdclass={'build': _build, ...})

Documentation (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 = []


来源:https://stackoverflow.com/questions/11331175/hook-to-add-commands-to-distutils-build

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!