| 1 | """distutils.command.install_data |
| 2 | |
| 3 | Implements the Distutils 'install_data' command, for installing |
| 4 | platform-independent data files.""" |
| 5 | |
| 6 | # contributed by Bastian Kleineidam |
| 7 | |
| 8 | # This module should be kept compatible with Python 1.5.2. |
| 9 | |
| 10 | __revision__ = "$Id$" |
| 11 | |
| 12 | import os |
| 13 | from types import StringType |
| 14 | from distutils.core import Command |
| 15 | from distutils.util import change_root, convert_path |
| 16 | |
| 17 | class install_data (Command): |
| 18 | |
| 19 | description = "install data files" |
| 20 | |
| 21 | user_options = [ |
| 22 | ('install-dir=', 'd', |
| 23 | "base directory for installing data files " |
| 24 | "(default: installation base dir)"), |
| 25 | ('root=', None, |
| 26 | "install everything relative to this alternate root directory"), |
| 27 | ('force', 'f', "force installation (overwrite existing files)"), |
| 28 | ] |
| 29 | |
| 30 | boolean_options = ['force'] |
| 31 | |
| 32 | def initialize_options (self): |
| 33 | self.install_dir = None |
| 34 | self.outfiles = [] |
| 35 | self.root = None |
| 36 | self.force = 0 |
| 37 | |
| 38 | self.data_files = self.distribution.data_files |
| 39 | self.warn_dir = 1 |
| 40 | |
| 41 | def finalize_options (self): |
| 42 | self.set_undefined_options('install', |
| 43 | ('install_data', 'install_dir'), |
| 44 | ('root', 'root'), |
| 45 | ('force', 'force'), |
| 46 | ) |
| 47 | |
| 48 | def run (self): |
| 49 | self.mkpath(self.install_dir) |
| 50 | for f in self.data_files: |
| 51 | if type(f) is StringType: |
| 52 | # it's a simple file, so copy it |
| 53 | f = convert_path(f) |
| 54 | if self.warn_dir: |
| 55 | self.warn("setup script did not provide a directory for " |
| 56 | "'%s' -- installing right in '%s'" % |
| 57 | (f, self.install_dir)) |
| 58 | (out, _) = self.copy_file(f, self.install_dir) |
| 59 | self.outfiles.append(out) |
| 60 | else: |
| 61 | # it's a tuple with path to install to and a list of files |
| 62 | dir = convert_path(f[0]) |
| 63 | if not os.path.isabs(dir): |
| 64 | dir = os.path.join(self.install_dir, dir) |
| 65 | elif self.root: |
| 66 | dir = change_root(self.root, dir) |
| 67 | self.mkpath(dir) |
| 68 | |
| 69 | if f[1] == []: |
| 70 | # If there are no files listed, the user must be |
| 71 | # trying to create an empty directory, so add the |
| 72 | # directory to the list of output files. |
| 73 | self.outfiles.append(dir) |
| 74 | else: |
| 75 | # Copy files, adding them to the list of output files. |
| 76 | for data in f[1]: |
| 77 | data = convert_path(data) |
| 78 | (out, _) = self.copy_file(data, dir) |
| 79 | self.outfiles.append(out) |
| 80 | |
| 81 | def get_inputs (self): |
| 82 | return self.data_files or [] |
| 83 | |
| 84 | def get_outputs (self): |
| 85 | return self.outfiles |