簡體   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