bdist.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. """distutils.command.bdist
  2. Implements the Distutils 'bdist' command (create a built [binary]
  3. distribution)."""
  4. import os
  5. import warnings
  6. from ..core import Command
  7. from ..errors import DistutilsPlatformError, DistutilsOptionError
  8. from ..util import get_platform
  9. def show_formats():
  10. """Print list of available formats (arguments to "--format" option)."""
  11. from ..fancy_getopt import FancyGetopt
  12. formats = []
  13. for format in bdist.format_commands:
  14. formats.append(("formats=" + format, None, bdist.format_commands[format][1]))
  15. pretty_printer = FancyGetopt(formats)
  16. pretty_printer.print_help("List of available distribution formats:")
  17. class ListCompat(dict):
  18. # adapter to allow for Setuptools compatibility in format_commands
  19. def append(self, item):
  20. warnings.warn(
  21. """format_commands is now a dict. append is deprecated.""",
  22. DeprecationWarning,
  23. stacklevel=2,
  24. )
  25. class bdist(Command):
  26. description = "create a built (binary) distribution"
  27. user_options = [
  28. ('bdist-base=', 'b', "temporary directory for creating built distributions"),
  29. (
  30. 'plat-name=',
  31. 'p',
  32. "platform name to embed in generated filenames "
  33. "(default: %s)" % get_platform(),
  34. ),
  35. ('formats=', None, "formats for distribution (comma-separated list)"),
  36. (
  37. 'dist-dir=',
  38. 'd',
  39. "directory to put final built distributions in " "[default: dist]",
  40. ),
  41. ('skip-build', None, "skip rebuilding everything (for testing/debugging)"),
  42. (
  43. 'owner=',
  44. 'u',
  45. "Owner name used when creating a tar file" " [default: current user]",
  46. ),
  47. (
  48. 'group=',
  49. 'g',
  50. "Group name used when creating a tar file" " [default: current group]",
  51. ),
  52. ]
  53. boolean_options = ['skip-build']
  54. help_options = [
  55. ('help-formats', None, "lists available distribution formats", show_formats),
  56. ]
  57. # The following commands do not take a format option from bdist
  58. no_format_option = ('bdist_rpm',)
  59. # This won't do in reality: will need to distinguish RPM-ish Linux,
  60. # Debian-ish Linux, Solaris, FreeBSD, ..., Windows, Mac OS.
  61. default_format = {'posix': 'gztar', 'nt': 'zip'}
  62. # Define commands in preferred order for the --help-formats option
  63. format_commands = ListCompat(
  64. {
  65. 'rpm': ('bdist_rpm', "RPM distribution"),
  66. 'gztar': ('bdist_dumb', "gzip'ed tar file"),
  67. 'bztar': ('bdist_dumb', "bzip2'ed tar file"),
  68. 'xztar': ('bdist_dumb', "xz'ed tar file"),
  69. 'ztar': ('bdist_dumb', "compressed tar file"),
  70. 'tar': ('bdist_dumb', "tar file"),
  71. 'zip': ('bdist_dumb', "ZIP file"),
  72. }
  73. )
  74. # for compatibility until consumers only reference format_commands
  75. format_command = format_commands
  76. def initialize_options(self):
  77. self.bdist_base = None
  78. self.plat_name = None
  79. self.formats = None
  80. self.dist_dir = None
  81. self.skip_build = 0
  82. self.group = None
  83. self.owner = None
  84. def finalize_options(self):
  85. # have to finalize 'plat_name' before 'bdist_base'
  86. if self.plat_name is None:
  87. if self.skip_build:
  88. self.plat_name = get_platform()
  89. else:
  90. self.plat_name = self.get_finalized_command('build').plat_name
  91. # 'bdist_base' -- parent of per-built-distribution-format
  92. # temporary directories (eg. we'll probably have
  93. # "build/bdist.<plat>/dumb", "build/bdist.<plat>/rpm", etc.)
  94. if self.bdist_base is None:
  95. build_base = self.get_finalized_command('build').build_base
  96. self.bdist_base = os.path.join(build_base, 'bdist.' + self.plat_name)
  97. self.ensure_string_list('formats')
  98. if self.formats is None:
  99. try:
  100. self.formats = [self.default_format[os.name]]
  101. except KeyError:
  102. raise DistutilsPlatformError(
  103. "don't know how to create built distributions "
  104. "on platform %s" % os.name
  105. )
  106. if self.dist_dir is None:
  107. self.dist_dir = "dist"
  108. def run(self):
  109. # Figure out which sub-commands we need to run.
  110. commands = []
  111. for format in self.formats:
  112. try:
  113. commands.append(self.format_commands[format][0])
  114. except KeyError:
  115. raise DistutilsOptionError("invalid format '%s'" % format)
  116. # Reinitialize and run each command.
  117. for i in range(len(self.formats)):
  118. cmd_name = commands[i]
  119. sub_cmd = self.reinitialize_command(cmd_name)
  120. if cmd_name not in self.no_format_option:
  121. sub_cmd.format = self.formats[i]
  122. # passing the owner and group names for tar archiving
  123. if cmd_name == 'bdist_dumb':
  124. sub_cmd.owner = self.owner
  125. sub_cmd.group = self.group
  126. # If we're going to need to run this command again, tell it to
  127. # keep its temporary files around so subsequent runs go faster.
  128. if cmd_name in commands[i + 1 :]:
  129. sub_cmd.keep_temp = 1
  130. self.run_command(cmd_name)