Rev 2618: Update setup.py to just skip extensions that are not available. in http://bzr.arbash-meinel.com/branches/bzr/0.19-dev/skip_pyrex

John Arbash Meinel john at arbash-meinel.com
Sun Jul 15 15:25:57 BST 2007


At http://bzr.arbash-meinel.com/branches/bzr/0.19-dev/skip_pyrex

------------------------------------------------------------
revno: 2618
revision-id: john at arbash-meinel.com-20070715142444-c90zfgn3khpl1swt
parent: pqm at pqm.ubuntu.com-20070713074627-93zxs9uh528y0fki
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: skip_pyrex
timestamp: Sun 2007-07-15 09:24:44 -0500
message:
  Update setup.py to just skip extensions that are not available.
modified:
  setup.py                       setup.py-20050314065409-02f8a0a6e3f9bc70
-------------- next part --------------
=== modified file 'setup.py'
--- a/setup.py	2007-07-13 02:23:34 +0000
+++ b/setup.py	2007-07-15 14:24:44 +0000
@@ -149,26 +149,57 @@
 
 command_classes = {'install_scripts': my_install_scripts,
                    'build': bzr_build}
+from distutils.extension import Extension
 ext_modules = []
 try:
     from Pyrex.Distutils import build_ext
 except ImportError:
+    have_pyrex = False
     # try to build the extension from the prior generated source.
+    print
     print ("Pyrex not available, while bzr will build, "
            "you cannot modify the C extensions.")
     from distutils.command.build_ext import build_ext
-    from distutils.extension import Extension
-    #ext_modules.append(
-    #    Extension("bzrlib.modulename", ["bzrlib/foo.c"], libraries = []))
-    ext_modules.append(
-        Extension("bzrlib._knit_load_data_c", ["bzrlib/_knit_load_data_c.c"]))
 else:
-    from distutils.extension import Extension
-    #ext_modules.append(
-    #    Extension("bzrlib.modulename", ["bzrlib/foo.pyx"], libraries = []))
-    ext_modules.append(
-        Extension("bzrlib._knit_load_data_c", ["bzrlib/_knit_load_data_c.pyx"]))
+    have_pyrex = True
+# Override the build_ext if we have Pyrex available
 command_classes['build_ext'] = build_ext
+unavailable_files = []
+
+def add_pyrex_extension(module_name, **kwargs):
+    """Add a pyrex module to build.
+
+    This will use Pyrex to auto-generate the .c file if it is available.
+    Otherwise it will fall back on the .c file. If the .c file is not
+    available, it will warn, and not add anything.
+
+    You can pass any extra options to Extension through kwargs. One example is
+    'libraries = []'.
+
+    :param module_name: The python path to the module. This will be used to
+        determine the .pyx and .c files to use.
+    """
+    path = module_name.replace('.', '/')
+    pyrex_name = path + '.pyx'
+    c_name = path + '.c'
+    if have_pyrex:
+        ext_modules.append(Extension(module_name, [pyrex_name]))
+    else:
+        if not os.path.isfile(c_name):
+            unavailable_files.append(c_name)
+        else:
+            ext_modules.append(Extension(module_name, [c_name]))
+
+
+add_pyrex_extension('bzrlib._knit_load_data_c')
+
+
+if unavailable_files:
+    print 'C extension(s) not found:'
+    print '   %s' % ('\n  '.join(unavailable_files),)
+    print 'The python versions will be used instead.'
+    print
+
 
 if 'bdist_wininst' in sys.argv:
     import glob



More information about the bazaar-commits mailing list