How do I subclass the build command?

江枫思渺然 提交于 2019-12-09 03:08:13

问题


The subject is self-descriptive: I need to subclass the setup.py build command in order to perform additional build steps. However I've failed to find any build command class to inherit from. I've been trying:

class BuildCommandProxy(setuptools.command.build):
    pass

and

class BuildCommandProxy(distutils.command.build):
    pass

and even:

class BuildCommandProxy(setuptools.distutils.command.build):
    pass

without any success.

UPDATE: looking for how to implement something like this with setuptools.

UPDATE2: I have some custom command implementation:

class CustomCommand(setuptools.Command):
    # ...

What I would like to implement is to pass this command to cmdclass like this:

cmdclass={
    "build": CustomCommand,
}

and then invoke the original build in CustomCommand.run after doing some custom steps.


回答1:


Setuptools does not override the distutils build command itself; only the build_py and build_ext subcommands.

So, to create your own subclass you need to import from the distutils.command.build module, which contains a build class (subclass of Command):

import distutils.command.build

class BuildCommandProxy(distutils.command.build.build):
    pass



回答2:


For completeness, here is a full example of how to add custom build operations:

import distutils.command.build

# Override build command
class BuildCommand(distutils.command.build.build):

    def run(self):
        # Run the original build command
        distutils.command.build.build.run(self)
        # Custom build stuff goes here

# Replace the build command with ours
setup(...,
      cmdclass={"build": BuildCommand})


来源:https://stackoverflow.com/questions/14544587/how-do-i-subclass-the-build-command

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