如何建设有setuptools的鸡蛋时,可以以编程方式发现错误?
-
19-09-2019 - |
题
如果我有建立鸡蛋,基本上由运行脚本
python setup.py bdist_egg --exclude-source-files
对于一些使用setuptools
来定义鸡蛋如何构建setup.py文件,有一个简单的方法来确定是否有在建设蛋任何错误?
一个情况我最近有,是,有一个模块中的语法错误。 setuptools的吐出一个消息到标准误差,但继续创建蛋,省略破碎模块。因为这是一个批处理创建多个鸡蛋的一部分,该误差被错过了,并且结果是无用的。
有没有办法编程构建一个鸡蛋时发现错误,不只是捕捉标准错误,并解析其他?
解决方案
distutils
使用py_compile.compile()
函数来编译源文件。此功能需要一个doraise
的说法,当设置为True
提高对编译错误异常(默认是打印错误到stderr)。 distutils
不调用py_compile.compile()
与doraise=True
,所以编译未被中止上编译错误。
要停止的错误,并能够检查setup.py
返回代码(这将是错误的非零),你可以修补py_compile.compile()
功能。例如,在你的setup.py
:
from setuptools import setup
import py_compile
# Replace py_compile.compile with a function that calls it with doraise=True
orig_py_compile = py_compile.compile
def doraise_py_compile(file, cfile=None, dfile=None, doraise=False):
orig_py_compile(file, cfile=cfile, dfile=dfile, doraise=True)
py_compile.compile = doraise_py_compile
# Usual setup...
不隶属于 StackOverflow