/Lib/distutils/command/bdist.py

http://unladen-swallow.googlecode.com/ · Python · 152 lines · 114 code · 17 blank · 21 comment · 14 complexity · 462e55daa0a2c590e122032785c63612 MD5 · raw file

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