bdist_dumb.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. """distutils.command.bdist_dumb
  2. Implements the Distutils 'bdist_dumb' command (create a "dumb" built
  3. distribution -- i.e., just an archive to be unpacked under $prefix or
  4. $exec_prefix)."""
  5. import os
  6. from distutils._log import log
  7. from typing import ClassVar
  8. from ..core import Command
  9. from ..dir_util import ensure_relative, remove_tree
  10. from ..errors import DistutilsPlatformError
  11. from ..sysconfig import get_python_version
  12. from ..util import get_platform
  13. class bdist_dumb(Command):
  14. description = "create a \"dumb\" built distribution"
  15. user_options = [
  16. ('bdist-dir=', 'd', "temporary directory for creating the distribution"),
  17. (
  18. 'plat-name=',
  19. 'p',
  20. "platform name to embed in generated filenames "
  21. f"[default: {get_platform()}]",
  22. ),
  23. (
  24. 'format=',
  25. 'f',
  26. "archive format to create (tar, gztar, bztar, xztar, ztar, zip)",
  27. ),
  28. (
  29. 'keep-temp',
  30. 'k',
  31. "keep the pseudo-installation tree around after creating the distribution archive",
  32. ),
  33. ('dist-dir=', 'd', "directory to put final built distributions in"),
  34. ('skip-build', None, "skip rebuilding everything (for testing/debugging)"),
  35. (
  36. 'relative',
  37. None,
  38. "build the archive using relative paths [default: false]",
  39. ),
  40. (
  41. 'owner=',
  42. 'u',
  43. "Owner name used when creating a tar file [default: current user]",
  44. ),
  45. (
  46. 'group=',
  47. 'g',
  48. "Group name used when creating a tar file [default: current group]",
  49. ),
  50. ]
  51. boolean_options: ClassVar[list[str]] = ['keep-temp', 'skip-build', 'relative']
  52. default_format = {'posix': 'gztar', 'nt': 'zip'}
  53. def initialize_options(self):
  54. self.bdist_dir = None
  55. self.plat_name = None
  56. self.format = None
  57. self.keep_temp = False
  58. self.dist_dir = None
  59. self.skip_build = None
  60. self.relative = False
  61. self.owner = None
  62. self.group = None
  63. def finalize_options(self):
  64. if self.bdist_dir is None:
  65. bdist_base = self.get_finalized_command('bdist').bdist_base
  66. self.bdist_dir = os.path.join(bdist_base, 'dumb')
  67. if self.format is None:
  68. try:
  69. self.format = self.default_format[os.name]
  70. except KeyError:
  71. raise DistutilsPlatformError(
  72. "don't know how to create dumb built distributions "
  73. f"on platform {os.name}"
  74. )
  75. self.set_undefined_options(
  76. 'bdist',
  77. ('dist_dir', 'dist_dir'),
  78. ('plat_name', 'plat_name'),
  79. ('skip_build', 'skip_build'),
  80. )
  81. def run(self):
  82. if not self.skip_build:
  83. self.run_command('build')
  84. install = self.reinitialize_command('install', reinit_subcommands=True)
  85. install.root = self.bdist_dir
  86. install.skip_build = self.skip_build
  87. install.warn_dir = False
  88. log.info("installing to %s", self.bdist_dir)
  89. self.run_command('install')
  90. # And make an archive relative to the root of the
  91. # pseudo-installation tree.
  92. archive_basename = f"{self.distribution.get_fullname()}.{self.plat_name}"
  93. pseudoinstall_root = os.path.join(self.dist_dir, archive_basename)
  94. if not self.relative:
  95. archive_root = self.bdist_dir
  96. else:
  97. if self.distribution.has_ext_modules() and (
  98. install.install_base != install.install_platbase
  99. ):
  100. raise DistutilsPlatformError(
  101. "can't make a dumb built distribution where "
  102. f"base and platbase are different ({install.install_base!r}, {install.install_platbase!r})"
  103. )
  104. else:
  105. archive_root = os.path.join(
  106. self.bdist_dir, ensure_relative(install.install_base)
  107. )
  108. # Make the archive
  109. filename = self.make_archive(
  110. pseudoinstall_root,
  111. self.format,
  112. root_dir=archive_root,
  113. owner=self.owner,
  114. group=self.group,
  115. )
  116. if self.distribution.has_ext_modules():
  117. pyversion = get_python_version()
  118. else:
  119. pyversion = 'any'
  120. self.distribution.dist_files.append(('bdist_dumb', pyversion, filename))
  121. if not self.keep_temp:
  122. remove_tree(self.bdist_dir)