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

Spo*_*ser 5 python error-handling compiler-errors setuptools

如果我有一个构建鸡蛋的脚本,基本上是通过运行

python setup.py bdist_egg --exclude-source-files
Run Code Online (Sandbox Code Playgroud)

对于许多setuptools用于定义如何构建蛋的setup.py文件,是否有一种简单的方法可以确定构建蛋是否有任何错误?

我最近遇到的一种情况是模块中存在语法错误.Setuptools向标准错误发出消息,但继续创建egg,省略了破坏的模块.因为这是批量创建一些鸡蛋的一部分,错误被错过,结果没用.

有没有办法在以编程方式构建一个egg时检测错误,而不仅仅是捕获标准错误并解析它?

Lup*_*uch 5

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...
Run Code Online (Sandbox Code Playgroud)