blob: 72b0cefe42b589fe592c39ea16a54c51b581376f [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
Tarek Ziadé88e2c5d2009-12-21 01:49:00 +00009
Tarek Ziadé8b441d02010-01-29 11:46:31 +000010from distutils.util import get_platform
Greg Ward0f77f952000-03-31 02:55:12 +000011from distutils.core import Command
Tarek Ziadé88e2c5d2009-12-21 01:49:00 +000012from distutils.errors import DistutilsPlatformError, DistutilsOptionError
Greg Ward0f77f952000-03-31 02:55:12 +000013
14
Collin Winter5b7e9d72007-08-30 03:52:21 +000015def show_formats():
Greg Ward34593812000-06-24 01:23:37 +000016 """Print list of available formats (arguments to "--format" option).
17 """
Fred Drake21d45352001-12-06 21:01:19 +000018 from distutils.fancy_getopt import FancyGetopt
Collin Winter5b7e9d72007-08-30 03:52:21 +000019 formats = []
Greg Ward34593812000-06-24 01:23:37 +000020 for format in bdist.format_commands:
21 formats.append(("formats=" + format, None,
22 bdist.format_command[format][1]))
23 pretty_printer = FancyGetopt(formats)
24 pretty_printer.print_help("List of available distribution formats:")
25
26
Collin Winter5b7e9d72007-08-30 03:52:21 +000027class bdist(Command):
Greg Ward0f77f952000-03-31 02:55:12 +000028
29 description = "create a built (binary) distribution"
30
Gregory P. Smithc59d4e02000-05-13 03:08:28 +000031 user_options = [('bdist-base=', 'b',
32 "temporary directory for creating built distributions"),
Greg Ward20283e52000-09-11 00:47:35 +000033 ('plat-name=', 'p',
34 "platform name to embed in generated filenames "
35 "(default: %s)" % get_platform()),
Greg Warde18dd8d2000-06-06 02:51:38 +000036 ('formats=', None,
Greg Ward2ff78872000-06-24 00:23:20 +000037 "formats for distribution (comma-separated list)"),
Greg Ward040dc0b2000-07-05 03:07:18 +000038 ('dist-dir=', 'd',
39 "directory to put final built distributions in "
40 "[default: dist]"),
Martin v. Löwis9668b932002-01-12 11:27:42 +000041 ('skip-build', None,
42 "skip rebuilding everything (for testing/debugging)"),
Tarek Ziadé05b30342009-10-02 23:56:02 +000043 ('owner=', 'u',
44 "Owner name used when creating a tar file"
45 " [default: current user]"),
46 ('group=', 'g',
47 "Group name used when creating a tar file"
48 " [default: current group]"),
Greg Ward0f77f952000-03-31 02:55:12 +000049 ]
50
Martin v. Löwis9668b932002-01-12 11:27:42 +000051 boolean_options = ['skip-build']
52
Greg Ward34593812000-06-24 01:23:37 +000053 help_options = [
54 ('help-formats', None,
55 "lists available distribution formats", show_formats),
Greg Wardfa9ff762000-10-14 04:06:40 +000056 ]
Greg Ward34593812000-06-24 01:23:37 +000057
Gregory P. Smith52e399c2000-05-13 01:49:56 +000058 # The following commands do not take a format option from bdist
Tarek Ziadéf6370502009-04-05 22:57:21 +000059 no_format_option = ('bdist_rpm',)
Gregory P. Smith52e399c2000-05-13 01:49:56 +000060
Greg Ward0f77f952000-03-31 02:55:12 +000061 # This won't do in reality: will need to distinguish RPM-ish Linux,
62 # Debian-ish Linux, Solaris, FreeBSD, ..., Windows, Mac OS.
Tarek Ziadéf6370502009-04-05 22:57:21 +000063 default_format = {'posix': 'gztar',
64 'nt': 'zip',
65 'os2': 'zip'}
Greg Ward0f77f952000-03-31 02:55:12 +000066
Greg Ward6f628bb2000-08-02 01:44:44 +000067 # Establish the preferred order (for the --help-formats option).
68 format_commands = ['rpm', 'gztar', 'bztar', 'ztar', 'tar',
Tarek Ziadéf6370502009-04-05 22:57:21 +000069 'wininst', 'zip', 'msi']
Greg Ward6f628bb2000-08-02 01:44:44 +000070
71 # And the real information.
Tarek Ziadéf6370502009-04-05 22:57:21 +000072 format_command = {'rpm': ('bdist_rpm', "RPM distribution"),
73 'gztar': ('bdist_dumb', "gzip'ed tar file"),
74 'bztar': ('bdist_dumb', "bzip2'ed tar file"),
75 'ztar': ('bdist_dumb', "compressed tar file"),
76 'tar': ('bdist_dumb', "tar file"),
77 'wininst': ('bdist_wininst',
78 "Windows executable installer"),
79 'zip': ('bdist_dumb', "ZIP file"),
80 'msi': ('bdist_msi', "Microsoft Installer")
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
Tarek Ziadé05b30342009-10-02 23:56:02 +000090 self.group = None
91 self.owner = None
Greg Ward0f77f952000-03-31 02:55:12 +000092
Collin Winter5b7e9d72007-08-30 03:52:21 +000093 def finalize_options(self):
Greg Ward20283e52000-09-11 00:47:35 +000094 # have to finalize 'plat_name' before 'bdist_base'
95 if self.plat_name is None:
Christian Heimes5e696852008-04-09 08:37:03 +000096 if self.skip_build:
97 self.plat_name = get_platform()
98 else:
99 self.plat_name = self.get_finalized_command('build').plat_name
Greg Ward20283e52000-09-11 00:47:35 +0000100
Gregory P. Smithc59d4e02000-05-13 03:08:28 +0000101 # 'bdist_base' -- parent of per-built-distribution-format
102 # temporary directories (eg. we'll probably have
103 # "build/bdist.<plat>/dumb", "build/bdist.<plat>/rpm", etc.)
104 if self.bdist_base is None:
Greg Ward4fb29e52000-05-27 17:27:23 +0000105 build_base = self.get_finalized_command('build').build_base
Greg Wardbeb6d722000-09-16 16:04:59 +0000106 self.bdist_base = os.path.join(build_base,
107 'bdist.' + self.plat_name)
Gregory P. Smithc59d4e02000-05-13 03:08:28 +0000108
Greg Warde18dd8d2000-06-06 02:51:38 +0000109 self.ensure_string_list('formats')
110 if self.formats is None:
Greg Ward0f77f952000-03-31 02:55:12 +0000111 try:
Greg Warde18dd8d2000-06-06 02:51:38 +0000112 self.formats = [self.default_format[os.name]]
Greg Ward0f77f952000-03-31 02:55:12 +0000113 except KeyError:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000114 raise DistutilsPlatformError(
115 "don't know how to create built distributions "
116 "on platform %s" % os.name)
Greg Ward040dc0b2000-07-05 03:07:18 +0000117
118 if self.dist_dir is None:
119 self.dist_dir = "dist"
Fred Drake21d45352001-12-06 21:01:19 +0000120
Collin Winter5b7e9d72007-08-30 03:52:21 +0000121 def run(self):
Greg Wardbeb6d722000-09-16 16:04:59 +0000122 # Figure out which sub-commands we need to run.
123 commands = []
Greg Warde18dd8d2000-06-06 02:51:38 +0000124 for format in self.formats:
Greg Warde18dd8d2000-06-06 02:51:38 +0000125 try:
Greg Wardbeb6d722000-09-16 16:04:59 +0000126 commands.append(self.format_command[format][0])
Greg Warde18dd8d2000-06-06 02:51:38 +0000127 except KeyError:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000128 raise DistutilsOptionError("invalid format '%s'" % format)
Greg Warde18dd8d2000-06-06 02:51:38 +0000129
Greg Wardbeb6d722000-09-16 16:04:59 +0000130 # Reinitialize and run each command.
131 for i in range(len(self.formats)):
132 cmd_name = commands[i]
Greg Warde18dd8d2000-06-06 02:51:38 +0000133 sub_cmd = self.reinitialize_command(cmd_name)
134 if cmd_name not in self.no_format_option:
Greg Wardbeb6d722000-09-16 16:04:59 +0000135 sub_cmd.format = self.formats[i]
136
Tarek Ziadé05b30342009-10-02 23:56:02 +0000137 # passing the owner and group names for tar archiving
138 if cmd_name == 'bdist_dumb':
139 sub_cmd.owner = self.owner
140 sub_cmd.group = self.group
141
Greg Wardbeb6d722000-09-16 16:04:59 +0000142 # If we're going to need to run this command again, tell it to
143 # keep its temporary files around so subsequent runs go faster.
144 if cmd_name in commands[i+1:]:
145 sub_cmd.keep_temp = 1
Greg Wardcb1f4c42000-09-30 18:27:54 +0000146 self.run_command(cmd_name)