bdist.py 5.4 KB

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