Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

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. 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?

like image 243
SpoonMeiser Avatar asked Feb 09 '10 16:02

SpoonMeiser


1 Answers

distutils use the py_compile.compile() function to compile source files. 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). distutils don't call py_compile.compile() with doraise=True, so compilation is not aborted on compilation errors.

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. For example, in your 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...
like image 179
Luper Rouch Avatar answered Nov 15 '22 06:11

Luper Rouch