blob: e3b047c66fc6f69a1c0d30fbe73a1e4f11b6b2ec [file] [log] [blame]
Greg Ward0f77f952000-03-31 02:55:12 +00001"""distutils.command.bdist
2
3Implements the Distutils 'bdist' command (create a built [binary]
4distribution)."""
5
Greg Ward0f77f952000-03-31 02:55:12 +00006__revision__ = "$Id$"
7
Neal Norwitz9d72bb42007-04-17 08:48:32 +00008import os
Greg Ward0f77f952000-03-31 02:55:12 +00009from distutils.core import Command
Greg Ward02296ce2000-03-31 05:08:50 +000010from distutils.errors import *
Gregory P. Smithc59d4e02000-05-13 03:08:28 +000011from distutils.util import get_platform
Greg Ward0f77f952000-03-31 02:55:12 +000012
13
Collin Winter5b7e9d72007-08-30 03:52:21 +000014def show_formats():
Greg Ward34593812000-06-24 01:23:37 +000015 """Print list of available formats (arguments to "--format" option).
16 """
Fred Drake21d45352001-12-06 21:01:19 +000017 from distutils.fancy_getopt import FancyGetopt
Collin Winter5b7e9d72007-08-30 03:52:21 +000018 formats = []
Greg Ward34593812000-06-24 01:23:37 +000019 for format in bdist.format_commands:
20 formats.append(("formats=" + format, None,
21 bdist.format_command[format][1]))
22 pretty_printer = FancyGetopt(formats)
23 pretty_printer.print_help("List of available distribution formats:")
24
25
Collin Winter5b7e9d72007-08-30 03:52:21 +000026class bdist(Command):
Greg Ward0f77f952000-03-31 02:55:12 +000027
28 description = "create a built (binary) distribution"
29
Gregory P. Smithc59d4e02000-05-13 03:08:28 +000030 user_options = [('bdist-base=', 'b',
31 "temporary directory for creating built distributions"),
Greg Ward20283e52000-09-11 00:47:35 +000032 ('plat-name=', 'p',
33 "platform name to embed in generated filenames "
34 "(default: %s)" % get_platform()),
Greg Warde18dd8d2000-06-06 02:51:38 +000035 ('formats=', None,
Greg Ward2ff78872000-06-24 00:23:20 +000036 "formats for distribution (comma-separated list)"),
Greg Ward040dc0b2000-07-05 03:07:18 +000037 ('dist-dir=', 'd',
38 "directory to put final built distributions in "
39 "[default: dist]"),
Martin v. Löwis9668b932002-01-12 11:27:42 +000040 ('skip-build', None,
41 "skip rebuilding everything (for testing/debugging)"),
Greg Ward0f77f952000-03-31 02:55:12 +000042 ]
43
Martin v. Löwis9668b932002-01-12 11:27:42 +000044 boolean_options = ['skip-build']
45
Greg Ward34593812000-06-24 01:23:37 +000046 help_options = [
47 ('help-formats', None,
48 "lists available distribution formats", show_formats),
Greg Wardfa9ff762000-10-14 04:06:40 +000049 ]
Greg Ward34593812000-06-24 01:23:37 +000050
Gregory P. Smith52e399c2000-05-13 01:49:56 +000051 # The following commands do not take a format option from bdist
Marc-André Lemburgc7cdd712002-10-04 09:30:06 +000052 no_format_option = ('bdist_rpm',
53 #'bdist_sdux', 'bdist_pkgtool'
54 )
Gregory P. Smith52e399c2000-05-13 01:49:56 +000055
Greg Ward0f77f952000-03-31 02:55:12 +000056 # This won't do in reality: will need to distinguish RPM-ish Linux,
57 # Debian-ish Linux, Solaris, FreeBSD, ..., Windows, Mac OS.
58 default_format = { 'posix': 'gztar',
Marc-André Lemburg2544f512002-01-31 18:56:00 +000059 'nt': 'zip',
60 'os2': 'zip', }
Greg Ward0f77f952000-03-31 02:55:12 +000061
Greg Ward6f628bb2000-08-02 01:44:44 +000062 # Establish the preferred order (for the --help-formats option).
63 format_commands = ['rpm', 'gztar', 'bztar', 'ztar', 'tar',
Marc-André Lemburgc7cdd712002-10-04 09:30:06 +000064 'wininst', 'zip',
65 #'pkgtool', 'sdux'
66 ]
Greg Ward6f628bb2000-08-02 01:44:44 +000067
68 # And the real information.
Greg Ward2ff78872000-06-24 00:23:20 +000069 format_command = { 'rpm': ('bdist_rpm', "RPM distribution"),
Marc-André Lemburgc7cdd712002-10-04 09:30:06 +000070 'zip': ('bdist_dumb', "ZIP file"),
71 'gztar': ('bdist_dumb', "gzip'ed tar file"),
Greg Ward2ff78872000-06-24 00:23:20 +000072 'bztar': ('bdist_dumb', "bzip2'ed tar file"),
73 'ztar': ('bdist_dumb', "compressed tar file"),
74 'tar': ('bdist_dumb', "tar file"),
Greg Ward1f9b73b2000-06-27 01:24:07 +000075 'wininst': ('bdist_wininst',
76 "Windows executable installer"),
Greg Ward6f628bb2000-08-02 01:44:44 +000077 'zip': ('bdist_dumb', "ZIP file"),
Tim Peters182b5ac2004-07-18 06:16:08 +000078 #'pkgtool': ('bdist_pkgtool',
Marc-André Lemburgc7cdd712002-10-04 09:30:06 +000079 # "Solaris pkgtool distribution"),
80 #'sdux': ('bdist_sdux', "HP-UX swinstall depot"),
Marc-André Lemburg0538f1f2002-04-17 20:30:10 +000081 }
Greg Ward0f77f952000-03-31 02:55:12 +000082
83
Collin Winter5b7e9d72007-08-30 03:52:21 +000084 def initialize_options(self):
Gregory P. Smithc59d4e02000-05-13 03:08:28 +000085 self.bdist_base = None
Greg Ward20283e52000-09-11 00:47:35 +000086 self.plat_name = None
Greg Warde18dd8d2000-06-06 02:51:38 +000087 self.formats = None
Greg Ward040dc0b2000-07-05 03:07:18 +000088 self.dist_dir = None
Martin v. Löwis9668b932002-01-12 11:27:42 +000089 self.skip_build = 0
Greg Ward0f77f952000-03-31 02:55:12 +000090
Collin Winter5b7e9d72007-08-30 03:52:21 +000091 def finalize_options(self):
Greg Ward20283e52000-09-11 00:47:35 +000092 # have to finalize 'plat_name' before 'bdist_base'
93 if self.plat_name is None:
Christian Heimes5e696852008-04-09 08:37:03 +000094 if self.skip_build:
95 self.plat_name = get_platform()
96 else:
97 self.plat_name = self.get_finalized_command('build').plat_name
Greg Ward20283e52000-09-11 00:47:35 +000098
Gregory P. Smithc59d4e02000-05-13 03:08:28 +000099 # 'bdist_base' -- parent of per-built-distribution-format
100 # temporary directories (eg. we'll probably have
101 # "build/bdist.<plat>/dumb", "build/bdist.<plat>/rpm", etc.)
102 if self.bdist_base is None:
Greg Ward4fb29e52000-05-27 17:27:23 +0000103 build_base = self.get_finalized_command('build').build_base
Greg Wardbeb6d722000-09-16 16:04:59 +0000104 self.bdist_base = os.path.join(build_base,
105 'bdist.' + self.plat_name)
Gregory P. Smithc59d4e02000-05-13 03:08:28 +0000106
Greg Warde18dd8d2000-06-06 02:51:38 +0000107 self.ensure_string_list('formats')
108 if self.formats is None:
Greg Ward0f77f952000-03-31 02:55:12 +0000109 try:
Greg Warde18dd8d2000-06-06 02:51:38 +0000110 self.formats = [self.default_format[os.name]]
Greg Ward0f77f952000-03-31 02:55:12 +0000111 except KeyError:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000112 raise DistutilsPlatformError(
113 "don't know how to create built distributions "
114 "on platform %s" % os.name)
Greg Ward040dc0b2000-07-05 03:07:18 +0000115
116 if self.dist_dir is None:
117 self.dist_dir = "dist"
Fred Drake21d45352001-12-06 21:01:19 +0000118
Collin Winter5b7e9d72007-08-30 03:52:21 +0000119 def run(self):
Greg Wardbeb6d722000-09-16 16:04:59 +0000120 # Figure out which sub-commands we need to run.
121 commands = []
Greg Warde18dd8d2000-06-06 02:51:38 +0000122 for format in self.formats:
Greg Warde18dd8d2000-06-06 02:51:38 +0000123 try:
Greg Wardbeb6d722000-09-16 16:04:59 +0000124 commands.append(self.format_command[format][0])
Greg Warde18dd8d2000-06-06 02:51:38 +0000125 except KeyError:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000126 raise DistutilsOptionError("invalid format '%s'" % format)
Greg Warde18dd8d2000-06-06 02:51:38 +0000127
Greg Wardbeb6d722000-09-16 16:04:59 +0000128 # Reinitialize and run each command.
129 for i in range(len(self.formats)):
130 cmd_name = commands[i]
Greg Warde18dd8d2000-06-06 02:51:38 +0000131 sub_cmd = self.reinitialize_command(cmd_name)
132 if cmd_name not in self.no_format_option:
Greg Wardbeb6d722000-09-16 16:04:59 +0000133 sub_cmd.format = self.formats[i]
134
Greg Wardbeb6d722000-09-16 16:04:59 +0000135 # If we're going to need to run this command again, tell it to
136 # keep its temporary files around so subsequent runs go faster.
137 if cmd_name in commands[i+1:]:
138 sub_cmd.keep_temp = 1
Greg Wardcb1f4c42000-09-30 18:27:54 +0000139 self.run_command(cmd_name)