python (3.11.7)
       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  import os
       9  from distutils.core import Command
      10  from distutils.util import change_root, convert_path
      11  
      12  
      13  class ESC[4;38;5;81minstall_data(ESC[4;38;5;149mCommand):
      14  
      15      description = "install data files"
      16  
      17      user_options = [
      18          (
      19              'install-dir=',
      20              '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      boolean_options = ['force']
      29  
      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(
      40              'install',
      41              ('install_data', 'install_dir'),
      42              ('root', 'root'),
      43              ('force', 'force'),
      44          )
      45  
      46      def run(self):
      47          self.mkpath(self.install_dir)
      48          for f in self.data_files:
      49              if isinstance(f, str):
      50                  # it's a simple file, so copy it
      51                  f = convert_path(f)
      52                  if self.warn_dir:
      53                      self.warn(
      54                          "setup script did not provide a directory for "
      55                          "'%s' -- installing right in '%s'" % (f, self.install_dir)
      56                      )
      57                  (out, _) = self.copy_file(f, self.install_dir)
      58                  self.outfiles.append(out)
      59              else:
      60                  # it's a tuple with path to install to and a list of files
      61                  dir = convert_path(f[0])
      62                  if not os.path.isabs(dir):
      63                      dir = os.path.join(self.install_dir, dir)
      64                  elif self.root:
      65                      dir = change_root(self.root, dir)
      66                  self.mkpath(dir)
      67  
      68                  if f[1] == []:
      69                      # If there are no files listed, the user must be
      70                      # trying to create an empty directory, so add the
      71                      # directory to the list of output files.
      72                      self.outfiles.append(dir)
      73                  else:
      74                      # Copy files, adding them to the list of output files.
      75                      for data in f[1]:
      76                          data = convert_path(data)
      77                          (out, _) = self.copy_file(data, dir)
      78                          self.outfiles.append(out)
      79  
      80      def get_inputs(self):
      81          return self.data_files or []
      82  
      83      def get_outputs(self):
      84          return self.outfiles