繁体   English   中英

如何检查setup.py中运行的命令?

[英]How can I check for which command is run in setup.py?

我想知道如何根据运行哪个命令(例如installupload )在setup.py中编写一些代码。

具体来说,我想拥有:

  1. 一种添加“ hacks”的简单方法,例如忽略install的特定文件,但没有其他命令。
  2. 在安装前添加钩子(例如运行测试)的推荐/规范方法。

我已经尝试阅读distutils文档 ,但是在细节上却很少-distutils.command [.foo]模块是完全没有文档的。

对于第一点,我可以像在此问题中提到的那样检查sys.argv ,但是当运行多个命令时,这是行不通的,例如:

python setup.py sdist bdist upload

因此它通常不适用。

您可以改写命令:

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

def run_file(path):
    with open(path, 'r') as f:
        exec(f.read())

class myinstall(install): # subclass distutils's install command
    def finalize_options(self): # called after option parsing
        # call base class function
        install.finalize_options(self)
        # super won't work because distutils under Python 2 uses old-style classes
        # ignore a module
        self.distribution.py_modules.remove('mymodule')
    def run(self): # called to run a command
        # run tests first
        run_file('path/to/test.py')
        # ^ remember to make sure the module is in sys.path
        # run the real commands
        install.run(self)

setup(
    name='abc',
    py_modules=['mymodule'],
    cmdclass={'install': myinstall}
    # ^ override the install command
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM