bdist.py 5.5 KB

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