install_data.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. from __future__ import annotations
  6. import functools
  7. import os
  8. from collections.abc import Iterable
  9. from typing import ClassVar
  10. from ..core import Command
  11. from ..util import change_root, convert_path
  12. class install_data(Command):
  13. description = "install data files"
  14. user_options = [
  15. (
  16. 'install-dir=',
  17. 'd',
  18. "base directory for installing data files [default: installation base dir]",
  19. ),
  20. ('root=', None, "install everything relative to this alternate root directory"),
  21. ('force', 'f', "force installation (overwrite existing files)"),
  22. ]
  23. boolean_options: ClassVar[list[str]] = ['force']
  24. def initialize_options(self):
  25. self.install_dir = None
  26. self.outfiles = []
  27. self.root = None
  28. self.force = False
  29. self.data_files = self.distribution.data_files
  30. self.warn_dir = True
  31. def finalize_options(self) -> None:
  32. self.set_undefined_options(
  33. 'install',
  34. ('install_data', 'install_dir'),
  35. ('root', 'root'),
  36. ('force', 'force'),
  37. )
  38. def run(self) -> None:
  39. self.mkpath(self.install_dir)
  40. for f in self.data_files:
  41. self._copy(f)
  42. @functools.singledispatchmethod
  43. def _copy(self, f: tuple[str | os.PathLike, Iterable[str | os.PathLike]]):
  44. # it's a tuple with path to install to and a list of files
  45. dir = convert_path(f[0])
  46. if not os.path.isabs(dir):
  47. dir = os.path.join(self.install_dir, dir)
  48. elif self.root:
  49. dir = change_root(self.root, dir)
  50. self.mkpath(dir)
  51. if f[1] == []:
  52. # If there are no files listed, the user must be
  53. # trying to create an empty directory, so add the
  54. # directory to the list of output files.
  55. self.outfiles.append(dir)
  56. else:
  57. # Copy files, adding them to the list of output files.
  58. for data in f[1]:
  59. data = convert_path(data)
  60. (out, _) = self.copy_file(data, dir)
  61. self.outfiles.append(out)
  62. @_copy.register(str)
  63. @_copy.register(os.PathLike)
  64. def _(self, f: str | os.PathLike):
  65. # it's a simple file, so copy it
  66. f = convert_path(f)
  67. if self.warn_dir:
  68. self.warn(
  69. "setup script did not provide a directory for "
  70. f"'{f}' -- installing right in '{self.install_dir}'"
  71. )
  72. (out, _) = self.copy_file(f, self.install_dir)
  73. self.outfiles.append(out)
  74. def get_inputs(self):
  75. return self.data_files or []
  76. def get_outputs(self):
  77. return self.outfiles