1"""distutils.command.install_data 2 3Implements the Distutils 'install_data' command, for installing 4platform-independent data files.""" 5 6# contributed by Bastian Kleineidam 7 8import os 9from distutils.core import Command 10from distutils.util import change_root, convert_path 11 12class install_data(Command): 13 14 description = "install data files" 15 16 user_options = [ 17 ('install-dir=', 'd', 18 "base directory for installing data files " 19 "(default: installation base dir)"), 20 ('root=', None, 21 "install everything relative to this alternate root directory"), 22 ('force', 'f', "force installation (overwrite existing files)"), 23 ] 24 25 boolean_options = ['force'] 26 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', 37 ('install_data', 'install_dir'), 38 ('root', 'root'), 39 ('force', 'force'), 40 ) 41 42 def run(self): 43 self.mkpath(self.install_dir) 44 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 if self.warn_dir: 49 self.warn("setup script did not provide a directory for " 50 "'%s' -- installing right in '%s'" % 51 (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 if not os.path.isabs(dir): 58 dir = os.path.join(self.install_dir, dir) 59 elif self.root: 60 dir = change_root(self.root, dir) 61 self.mkpath(dir) 62 63 if f[1] == []: 64 # If there are no files listed, the user must be 65 # trying to create an empty directory, so add the 66 # directory to the list of output files. 67 self.outfiles.append(dir) 68 else: 69 # Copy files, adding them to the list of output files. 70 for data in f[1]: 71 data = convert_path(data) 72 (out, _) = self.copy_file(data, dir) 73 self.outfiles.append(out) 74 75 def get_inputs(self): 76 return self.data_files or [] 77 78 def get_outputs(self): 79 return self.outfiles 80