bdist.py 5.1 KB

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