[Image-SIG] Install script for PIL

Bernhard Herzog herzog@online.de
13 Mar 1999 20:08:56 +0100

I asked for it, so I wrote it: an install script for PIL.

I've only tested this on Linux, but I expect it to work on other
Unix(-like) systems as well.

See the usage message in the script for what it actually does and how to
invoke it.

#! /usr/bin/env python

# Install script for the Python Imaging Library

import sys, os
import glob
import compileall
import shutil

from string import split, join

class DefaultOptions:

    # Directories

    # The directory prefix
    prefix = sys.prefix

    # The python library directory.
    libdir = None

    # The site-packages directory
    sitepackagedir = None

    # The include file directory for python's header files
    includedir = None
    # The include file directory for extension header files
    extensionincludedir = None

    # Other options

    # if true, just print messages about what the script would be doing
    # but don't actually do anything.
    noop = 0 

    # Print lots of messages
    verbose = 1

    help = 0

    # Some methods

    def print_messages(self):
        return self.verbose or self.noop

    def fix_directories(self):
        pyverdir = 'python' + sys.version[:3]
        if self.libdir is None:
            self.libdir = os.path.join(self.prefix, 'lib', pyverdir)
        if self.sitepackagedir is None:
            self.sitepackagedir = os.path.join(self.libdir, 'site-packages')
        if self.includedir is None:
            self.includedir = os.path.join(self.prefix, 'include', pyverdir)
        if self.extensionincludedir is None:
            self.extensionincludedir = os.path.join(self.includedir,

options = DefaultOptions()

def create_directory(dir):
    '''Create the directory dir and its parent directories if necessary'''
    if os.path.isdir(dir):
    parent, base = os.path.split(dir)
        if options.print_messages():
            print 'create directory %s' % dir
        if not options.noop:
            os.mkdir(dir, 0777)
    except os.error, exc:
        sys.stderr.write("can't create directory %s:%s\n" % (dir, exc))

def install_file(srcfile, dest, **flags):
    '''Copy the file srcfile, into the directory dest.

       If srcfile is a relative filename and the optional keyword
       argument recursive is true, preserve the leading directory
       components of the source filename and create the appropriate
       subdirectories in dest.
    srcdir, basename = os.path.split(srcfile)
    if flags.get('recursive') and not os.path.isabs(srcdir):
        destdir = os.path.join(dest, srcdir)
        destdir = dest
    destfile = os.path.join(destdir, basename)
    if options.print_messages():
        print 'copying %s to %s' % (srcfile, destfile)
    if not options.noop:
        shutil.copy(srcfile, destfile)
def bytecompile(dir):
    '''Bytecompile all python source files in dir recursively'''
    if options.noop:
        print 'Bytecompile all python source files under %s' % dir
        compileall.compile_dir(os.path.join(os.getcwd(), dir))

def install(pattern, dest, **flags):
    '''Install the files described by pattern pattern into the directory dest.
       The shell pattern is expanded by the glob module.

       An optional keyword argument recursive, if true, means to
       preserve the leading directory components of the source filenames
       if they are relative filenames and create the appropriate
       subdirectories in dest.
    files = glob.glob(pattern)
    if not files:
        sys.stderr.write('No files matching %s\n' % pattern)
    for file in files:
        apply(install_file, (file, dest), flags)

def check_option(option, value):
    if value is None:
        sys.stderr.write('Value required for option %s\n' % option)

def parse_cmd_line():
    argv = sys.argv[1:]
    for arg in argv:
        if '=' in arg:
            arg, value = split(arg, '=', 1)
            value = None
        if arg in ('-h', '--help'):
            options.help = 1
        elif arg == '--prefix':
            options.prefix = os.path.expanduser(value)
        elif arg == '--install-dir':
            check_option('--prefix', value)
            options.sitepackagedir = os.path.expanduser(value)
        elif arg == '--include-dir':
            check_option('--include-dir', value)
            options.extensionincludedir = os.path.expanduser(value)
        elif arg == '--noop':
            options.noop = 1
            sys.stderr.write('Unknown option %s\n' % arg)

def print_help():
    opt = DefaultOptions()
    dict = opt.__class__.__dict__.copy()
    dict['pyversion'] = sys.version[:3]
    print help_message % dict
help_message = """\
install.py [options...]

install.py install the Python Imaging Library and its headerfiles.
Normally the PIL is installed in the site-packages directory and the
headerfiles in a subdirectory under the directory containing Python's
headerfiles. You can change these directories with the options listed


        -h, --help

                Print this help message. Do nothing else


                Do nothing. Just print messages about what would be


                Install PIL files in DIR/lib/python-%(pyversion)s/site-packages/PIL
                Default is %(prefix)s


                Install PIL in DIR. Default is %(sitepackagedir)s


                Install the PIL header file in DIR.
                Default is %(extensionincludedir)s

def main():
    if options.help:
        pildir = os.path.join(options.sitepackagedir, 'PIL')
        install('PIL/*.py', options.sitepackagedir, recursive = 1)
        install('_imaging.so', pildir, recursive = 1)
        install('PIL.pth', options.sitepackagedir, recursive = 1)
        for file in ('Imaging.h', 'ImConfig.h', 'ImPlatform.h'):
            install(os.path.join('libImaging', file),

if __name__ == '__main__':