Cleaning build directory in setup.py

ⅰ亾dé卋堺 提交于 2019-11-28 06:11:54

For pre-deletion, just delete it with distutils.dir_util.remove_tree before calling setup.

For post-delete, I assume you only want to post-delete after selected commands. Subclass the respective command, override its run method (to invoke remove_tree after calling the base run), and pass the new command into the cmdclass dictionary of setup.

Matt Ball

Does this answer it? IIRC, you'll need to use the --all flag to get rid of stuff outside of build/lib:

python setup.py clean --all

This clears the build directory before to install

python setup.py clean --all install

But according to your requirements: This will do it before, and after

python setup.py clean --all install clean --all

Here's an answer that combines the programmatic approach of Martin's answer with the functionality of Matt's answer (a clean that takes care of all possible build areas):

from distutils.core import setup
from distutils.command.clean import clean
from distutils.command.install import install

class MyInstall(install):

    # Calls the default run command, then deletes the build area
    # (equivalent to "setup clean --all").
    def run(self):
        install.run(self)
        c = clean(self.distribution)
        c.all = True
        c.finalize_options()
        c.run()

if __name__ == '__main__':

    setup(
        name="myname",
        ...
        cmdclass={'install': MyInstall}
    )
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!