From 7c3128f3f928b6bc0d024aab4d00a4527b76d428 Mon Sep 17 00:00:00 2001 From: James Abbatiello Date: Fri, 11 Sep 2009 17:08:45 -0400 Subject: Make `setup.py install` skip C extensions if they can't be built --- SetupTools.py | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'SetupTools.py') diff --git a/SetupTools.py b/SetupTools.py index 6f0c302..d08eef2 100644 --- a/SetupTools.py +++ b/SetupTools.py @@ -3,6 +3,7 @@ import os from os import listdir import os.path from os.path import exists, isdir, isfile, join, splitext +import sys import types import glob import string @@ -16,13 +17,45 @@ if not os.getenv('CHEETAH_INSTALL_WITHOUT_SETUPTOOLS'): from distutils.core import setup from distutils.core import Command +from distutils.command.build_ext import build_ext from distutils.command.install_data import install_data +from distutils.errors import CCompilerError, DistutilsExecError, \ + DistutilsPlatformError #imports from Cheetah ... from cheetah.FileUtils import findFiles +if sys.platform == 'win32' and sys.version_info > (2, 6): + # 2.6's distutils.msvc9compiler can raise an IOError when failing to + # find the compiler + ext_errors = (CCompilerError, DistutilsExecError, DistutilsPlatformError, + IOError) +else: + ext_errors = (CCompilerError, DistutilsExecError, DistutilsPlatformError) + ################################################## ## CLASSES ## + +class BuildFailed(Exception): + pass + +class mod_build_ext(build_ext): + """A modified version of the distutils build_ext command that raises an + exception when building of the extension fails. + """ + + def run(self): + try: + build_ext.run(self) + except DistutilsPlatformError, x: + raise BuildFailed(x) + + def build_extension(self, ext): + try: + build_ext.build_extension(self, ext) + except ext_errors, x: + raise BuildFailed(x) + class mod_install_data(install_data): """A modified version of the disutils install_data command that allows data @@ -115,11 +148,24 @@ def run_setup(configurations): # Add setup extensions cmdclasses = { + 'build_ext': mod_build_ext, 'install_data': mod_install_data, } kws['cmdclass'] = cmdclasses # Invoke distutils setup - apply(setup, (), kws) + try: + apply(setup, (), kws) + except BuildFailed, x: + print "One or more C extensions failed to build." + print "Details: %s" % x + print "Retrying without C extensions enabled." + + del kws['ext_modules'] + apply(setup, (), kws) + + print "One or more C extensions failed to build." + print "Performance enhancements will not be available." + print "Pure Python installation succeeded." -- cgit v1.2.1