Browse Source

Support extension build fails.

Dulwich is pure python, with optional C implementation of some routines
for performance.

On systems which can not build the extensions, they default back to the
pure python implementations.

The builder can specify pure python by adding the flag
'--without-speedups' to the invocation of setup.py.
Hal Wine 15 years ago
parent
commit
6c7ebf7ff4
1 changed files with 67 additions and 9 deletions
  1. 67 9
      setup.py

+ 67 - 9
setup.py

@@ -2,11 +2,16 @@
 # Setup file for bzr-git
 # Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
 
+from distutils.cmd import Command
+from distutils.command.build_ext import build_ext
+from distutils.errors import CCompilerError, DistutilsPlatformError
 try:
-    from setuptools import setup
+    from setuptools import setup, Extension, Feature
+    from setuptools.command.bdist_egg import bdist_egg
 except ImportError:
-    from distutils.core import setup
-from distutils.extension import Extension
+    from distutils.core import setup, Extension
+    Feature = None
+    bdist_egg = None
 
 dulwich_version_string = '0.4.2'
 
@@ -16,6 +21,63 @@ import sys
 if sys.platform == 'win32':
     include_dirs.append('dulwich')
 
+#sys.path.append(os.path.join('doc', 'common'))
+
+_speedup_available = False
+
+class optional_build_ext(build_ext):
+    # This class allows C extension building to fail.
+    def run(self):
+        try:
+            print "trying run"
+            build_ext.run(self)
+        except DistutilsPlatformError, e:
+            self._unavailable(e)
+
+    def build_extension(self, ext):
+        try:
+            print "trying build_extension %s" % (ext,)
+            build_ext.build_extension(self, ext)
+            global _speedup_available
+            _speedup_available = True
+        except CCompilerError, e:
+            self._unavailable(e)
+
+    def _unavailable(self, exc):
+        print('*' * 70)
+        print("""WARNING:
+An optional C extension could not be compiled, speedups will not be
+available.""")
+        print('*' * 70)
+        print(exc)
+
+if Feature:
+    speedups = Feature(
+        "optional C speed-enhancements",
+        standard = True,
+        ext_modules=[
+            Extension('dulwich._objects', ['dulwich/_objects.c'],
+                      include_dirs=include_dirs),
+            Extension('dulwich._pack', ['dulwich/_pack.c'],
+                      include_dirs=include_dirs),
+            ],
+    )
+else:
+    speedups = None
+
+
+# Setuptools need some help figuring out if the egg is "zip_safe" or not
+if bdist_egg:
+    class my_bdist_egg(bdist_egg):
+        def zip_safe(self):
+            return not _speedup_available and bdist_egg.zip_safe(self)
+
+
+cmdclass = {'build_ext': optional_build_ext}
+if bdist_egg:
+    cmdclass['bdist_egg'] = my_bdist_egg
+
+
 
 setup(name='dulwich',
       description='Pure-Python Git Library',
@@ -33,10 +95,6 @@ setup(name='dulwich',
       """,
       packages=['dulwich', 'dulwich.tests'],
       scripts=['bin/dulwich', 'bin/dul-daemon'],
-      ext_modules=[
-          Extension('dulwich._objects', ['dulwich/_objects.c'],
-                    include_dirs=include_dirs),
-          Extension('dulwich._pack', ['dulwich/_pack.c'],
-                    include_dirs=include_dirs),
-          ],
+      features = {'speedups': speedups},
+      cmdclass = cmdclass,
       )