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

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

# contributed by Bastian Kleineidam

8 9
import os
from distutils.core import Command
10
from distutils.util import change_root, convert_path
11

12
class install_data(Command):
13 14 15

    description = "install data files"

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

25 26
    boolean_options = ['force']

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

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

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

                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)
74

75
    def get_inputs(self):
76 77
        return self.data_files or []

78
    def get_outputs(self):
79
        return self.outfiles