views:

74

answers:

1

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?

+4  A: 

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...
Luper Rouch
Excellent work there! This seems like the sort of thing you'd expect setuptools/distutils to support directly.
SpoonMeiser