Differences between revisions 2 and 3
Revision 2 as of 2004-04-08 03:31:54
Size: 1963
Editor: CPE0004e21253a5-CM012059940518
Comment:
Revision 3 as of 2008-11-15 14:00:39
Size: 1965
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 53: Line 53:
You can see a real-world usage example in the [http://cvs.sourceforge.net/viewcvs.py/pytable/table/setup.py?view=markup PyTable setup script] You can see a real-world usage example in the [[http://cvs.sourceforge.net/viewcvs.py/pytable/table/setup.py?view=markup|PyTable setup script]]

Problem

Distutils requires that you manually specify each directory and data-file to be included in the distribution. For packages with large and deep sub-package hierarchies it can be a pain to keep this list in sync with the code, particularly as forgetting an entry is not noticable until a user happens to report that a resource is missing.

Solution

Use an automatic scanning mechanism to generate the data_files parameter for setup:

import os
def npFilesFor( dirname ):
    """Return all non-python-file filenames in dir"""
    result = []
    allResults = []
    for name in os.listdir(dirname):
        path = os.path.join( dirname, name )
        if (
            os.path.isfile( path) and
            os.path.splitext( name )[1] not in
                ('.py','.pyc','.pyo')
        ):
            result.append( path )
        elif os.path.isdir( path ) and name.lower() !='cvs':
            allResults.extend( npFilesFor(path))
    if result:
        allResults.append( (dirname, result))
    return allResults

Then call it for the directories which contain data-files you want to include.

dataFiles = (
    npFilesFor( 'pytable') +
    npFilesFor( os.path.join('pytable','doc'))
)

and pass the result to setup:

setup (
    name = "pytable",
    version = "0.7.7a",
    data_files = dataFiles,
    cmdclass = {'install_data':smart_install_data},
    **extraArguments
)

(See DistutilsInstallDataScattered for the smart_install_data command.)

You can see a real-world usage example in the PyTable setup script

Discussion

Again, there should be some way to do this with a distutils template processing call or something, but this direct processing approach works well enough. This approach is very similar to DistutilsAutoPackageDiscovery.


CategoryDistutilsCookbook

Distutils/Cookbook/AutoDataDiscovery (last edited 2009-04-22 03:53:19 by eth595)

Unable to edit the page? See the FrontPage for instructions.