install_data.py 2.81 KB
Newer Older
1 2 3 4 5 6 7 8 9
"""distutils.command.install_data

Implements the Distutils 'install_data' command, for installing
platform-independent data files."""

# contributed by Bastian Kleineidam

__revision__ = "$Id$"

10 11 12
import os
from types import StringType
from distutils.core import Command
13
from distutils.util import change_root, convert_path
14

15
class install_data(Command):
16 17 18

    description = "install data files"

19 20
    user_options = [
        ('install-dir=', 'd',
21
         "base directory for installing data files "
22
         "(default: installation base dir)"),
23 24
        ('root=', None,
         "install everything relative to this alternate root directory"),
25
        ('force', 'f', "force installation (overwrite existing files)"),
26 27
        ]

28 29
    boolean_options = ['force']

30
    def initialize_options(self):
31
        self.install_dir = None
32
        self.outfiles = []
33
        self.root = None
34
        self.force = 0
35
        self.data_files = self.distribution.data_files
36
        self.warn_dir = 1
37

38
    def finalize_options (self):
39
        self.set_undefined_options('install',
Greg Ward's avatar
Greg Ward committed
40 41
                                   ('install_data', 'install_dir'),
                                   ('root', 'root'),
42
                                   ('force', 'force'),
Greg Ward's avatar
Greg Ward committed
43
                                  )
44

45
    def run(self):
46 47
        self.mkpath(self.install_dir)
        for f in self.data_files:
48
            if type(f) is StringType:
49
                # it's a simple file, so copy it
50
                f = convert_path(f)
51 52 53 54
                if self.warn_dir:
                    self.warn("setup script did not provide a directory for "
                              "'%s' -- installing right in '%s'" %
                              (f, self.install_dir))
55
                (out, _) = self.copy_file(f, self.install_dir)
56
                self.outfiles.append(out)
57
            else:
58
                # it's a tuple with path to install to and a list of files
59
                dir = convert_path(f[0])
60 61 62
                if not os.path.isabs(dir):
                    dir = os.path.join(self.install_dir, dir)
                elif self.root:
63
                    dir = change_root(self.root, dir)
64
                self.mkpath(dir)
65 66 67 68 69 70 71 72 73 74 75 76

                if f[1] == []:
                    # If there are no files listed, the user must be
                    # trying to create an empty directory, so add the
                    # directory to the list of output files.
                    self.outfiles.append(dir)
                else:
                    # Copy files, adding them to the list of output files.
                    for data in f[1]:
                        data = convert_path(data)
                        (out, _) = self.copy_file(data, dir)
                        self.outfiles.append(out)
77

78
    def get_inputs(self):
79 80
        return self.data_files or []

81
    def get_outputs(self):
82
        return self.outfiles