summaryrefslogtreecommitdiff
path: root/distutils2/command/install_data.py
blob: 3179ebba127c45bb7c6a9c32196d18abec646594 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""distutils.command.install_data

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

# contributed by Bastian Kleineidam


import os
from distutils2.command.cmd import Command
from distutils2.util import change_root, convert_path
from distutils2._backport.sysconfig import get_paths, format_value
from distutils2._backport.shutil import Error

class install_data(Command):

    description = "install data files"

    user_options = [
        ('install-dir=', 'd',
         "base directory for installing data files "
         "(default: installation base dir)"),
        ('root=', None,
         "install everything relative to this alternate root directory"),
        ('force', 'f', "force installation (overwrite existing files)"),
        ]

    boolean_options = ['force']

    def initialize_options(self):
        self.install_dir = None
        self.outfiles = []
        self.data_files_out = []
        self.root = None
        self.force = 0
        self.data_files = self.distribution.data_files
        self.warn_dir = 1

    def finalize_options(self):
        self.set_undefined_options('install_dist',
                                   ('install_data', 'install_dir'),
                                   'root', 'force')

    def run(self):
        self.mkpath(self.install_dir)
        for file in self.data_files.items():
            destination = convert_path(self.expand_categories(file[1]))
            dir_dest = os.path.abspath(os.path.dirname(destination))
            
            self.mkpath(dir_dest)
            try:
                (out, _) = self.copy_file(file[0], dir_dest)
            except Error, e:
                self.warn(e)
                out = destination

            self.outfiles.append(out)
            self.data_files_out.append((file[0], destination))

    def expand_categories(self, path_with_categories):
        local_vars = get_paths()
        local_vars['distribution.name'] = self.distribution.metadata['Name']
        expanded_path = format_value(path_with_categories, local_vars)
        expanded_path = format_value(expanded_path, local_vars)
        if '{' in expanded_path and '}' in expanded_path:
            self.warn("Unable to expand %s, some categories may missing." %
                path_with_categories)
        return expanded_path

    def get_source_files(self):
        return self.data_files.keys()

    def get_inputs(self):
        return self.data_files.keys()

    def get_outputs(self):
        return self.outfiles

    def get_resources_out(self):
        return self.data_files_out