[Distutils] Installing non-program files into the lib directory

Pete Shinners pete@visionart.com
Wed May 16 14:18:01 2001


> I have a package which includes some non-program files (in fact
> scripts, but which are run by exec_file) that have to be installed in
> a specific subpackage folder. Is there any simple way to do this with
> distutils, i.e. one which doesn't require me to reimplement several
> methods? Library installation ignores everything which is not .py,
> whereas data file installation doesn't let me access the lib
> directory.

yeah, i have figured this out. i have the exact same setup, a couple
resources that need to be in the same directory as the .PY files that
use them.

i have a "lib" directory with all the files i want in my final 
directory. here's the relevant snips from my setup.py file to get
them all installed nicely

note that it took me quite awhile to get everything to this stage,
but now it is working beautifully.

you'll see the main secret is creating my own "install_data"
command that actually puts files where the installation is
going, instead of pretty much random locations on different
platforms.

hope this helps!

-----------------------------------------------------------------



#note, this is cut and pasted together. just the relevent parts
#of my setup.py file.


DESCRIPTION = """Pygame is a Python wrapper module for the
SDL multimedia library. It contains python functions and classes
that will allow you to use SDL's support for playing cdroms,
audio and video output, and keyboard, mouse and joystick input.
Pygame also includes support for the Numerical Python extension."""

METADATA = {
    "name":             "pygame",
    "version":          "1.0.1",
    "license":          "LGPL",
    "url":              "http://pygame.seul.org",
    "author":           "Pete Shinners",
    "author_email":     "pygame@seul.org",
    "description":      "Python Game Development Package",
    "long_description": DESCRIPTION,
}



import os, glob, sys
import distutils.sysconfig 
from distutils.command.install_data import install_data


#get us to the correct directory
path = os.path.split(os.path.abspath(sys.argv[0]))[0]
os.chdir(path)


#add non .py files in lib directory
data_files = []
for f in glob.glob(os.path.join('lib', '*')):
    if not f.endswith('.py') and os.path.isfile(f):
        data_files.append(f)


#data installer with improved intelligence over distutils
#data files are copied into the project directory instead
#of willy-nilly
class smart_install_data(install_data):   
    def run(self):
        #need to change self.install_dir to the library dir
        install_cmd = self.get_finalized_command('install')
        self.install_dir = getattr(install_cmd, 'install_lib')
        return install_data.run(self)


#finally, 
#call distutils with all needed info
PACKAGEDATA = {
       "cmdclass":    {'install_data': smart_install_data},
       "packages":    ['pygame'],
       "package_dir": {'pygame': 'lib'},
       "data_files":  [['pygame', data_files]],
}
PACKAGEDATA.update(METADATA)
apply(setup, [], PACKAGEDATA)