Advice from distutils experts?

Robert Kern robert.kern at gmail.com
Sat Mar 4 18:11:47 EST 2006


olsongt at verizon.net wrote:
> I'm doing something a little wierd in one of my projects.  I'm
> generating a C source file based on information extracted from python's
> header files.  Although I can just generate the file and check the
> result into source control, I'd rather have the file generated during
> the install process instead of doing it manually.
> 
> So I'd like to:
>     + have the file generated when a unix user runs setup.py install.
>     + have the file generated when I create a windows binary
> distribution.
>     + not have the file generated when I create source distribution
> .tgz's and .zip's.
> 
> I'd appreciate any pointers on the best way to hook into disutils to do
> this.  And if the answer is "Your crazy, generate the file outside of
> distutils." just let me know.

We do things like this in numpy. We've extended distutils to allow functions in
the sources list for Extension.

from numpy.distutils.core import Extension
from distutils.dep_util import newer

def generate_source_c(ext, build_dir):
    target = os.path.join(build_dir, 'source.c')
    if newer(__file__, target):
        # ... parse Python headers
        contents = """
#include "Python.h"
/* ... */
""" % (stuff, more_stuff)
        f = open(target, 'w')
        f.write(contents)
        f.close()
    return target

ext = Extension('my_extension',
                sources=[generate_source_c],
)

setup(#...
    ext_modules=[ext]
)

-- 
Robert Kern
robert.kern at gmail.com

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter




More information about the Python-list mailing list