简体   繁体   English

使用setuptools构建鸡蛋时,如何以编程方式检测错误?

[英]How can I detect errors programatically when building an egg with setuptools?

If I have a script that builds eggs, basically by running 如果我有一个构建鸡蛋的脚本,基本上是通过运行

python setup.py bdist_egg --exclude-source-files

for a number of setup.py files that use setuptools to define how eggs are built, is there an easy way to determine if there were any errors in building the egg? 对于许多使用setuptools定义鸡蛋构造方式的setup.py文件,是否有一种简单的方法来确定在建造鸡蛋时是否存在任何错误?

A situation I had recently, was that there was a syntax error in a module. 我最近遇到的一种情况是模块中存在语法错误。 Setuptools spat out a message onto standard error, but continued to create the egg, omitting to broken module. Setuptools向标准错误显示一条消息,但继续创建鸡蛋,省略了损坏的模块。 Because this was part of a batch creating a number of eggs, the error was missed, and the result was useless. 因为这是创建大量蛋的批处理的一部分,所以错误被遗漏了,结果是无用的。

Is there a way to detect errors when building an egg programatically, other than just capturing standard error and parsing that? 除了以标准错误捕获和解析之外,还可以通过编程方式构建鸡蛋时检测错误吗?

distutils use the py_compile.compile() function to compile source files. distutils使用py_compile.compile()函数来编译源文件。 This function takes a doraise argument, that when set to True raises an exception on compilation errors (the default is to print the errors to stderr). 此函数带有doraise参数,该参数设置为True会引发编译错误异常(默认是将错误打印到stderr)。 distutils don't call py_compile.compile() with doraise=True , so compilation is not aborted on compilation errors. distutils不会使用doraise=True调用py_compile.compile() ,因此不会因编译错误而中止编译。

To stop on errors and be able to check the setup.py return code (it will be nonzero on errors), you could patch the py_compile.compile() function. 要停止错误并能够检查setup.py返回码(错误时将返回非零值),可以修补py_compile.compile()函数。 For example, in your setup.py : 例如,在您的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...

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

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