install_data.py 2.7 KB

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