[英]How can I check for which command is run in setup.py?
我想知道如何根据运行哪个命令(例如install
或upload
)在setup.py中编写一些代码。
具体来说,我想拥有:
install
的特定文件,但没有其他命令。 我已经尝试阅读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.