blob: 014871d280edb57971aa1eb0fbe26862ce43bf53 [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
Neal Norwitz9d72bb42007-04-17 08:48:32 +00006import os
Greg Ward0f77f952000-03-31 02:55:12 +00007from distutils.core import Command
Tarek Ziadé36797272010-07-22 12:50:05 +00008from distutils.errors import *
9from distutils.util import get_platform
Greg Ward0f77f952000-03-31 02:55:12 +000010
11
Collin Winter5b7e9d72007-08-30 03:52:21 +000012def show_formats():
Greg Ward34593812000-06-24 01:23:37 +000013 """Print list of available formats (arguments to "--format" option).
14 """
Fred Drake21d45352001-12-06 21:01:19 +000015 from distutils.fancy_getopt import FancyGetopt
Collin Winter5b7e9d72007-08-30 03:52:21 +000016 formats = []
Greg Ward34593812000-06-24 01:23:37 +000017 for format in bdist.format_commands:
18 formats.append(("formats=" + format, None,
19 bdist.format_command[format][1]))
20 pretty_printer = FancyGetopt(formats)
21 pretty_printer.print_help("List of available distribution formats:")
22
23
Collin Winter5b7e9d72007-08-30 03:52:21 +000024class bdist(Command):
Greg Ward0f77f952000-03-31 02:55:12 +000025
26 description = "create a built (binary) distribution"
27
Gregory P. Smithc59d4e02000-05-13 03:08:28 +000028 user_options = [('bdist-base=', 'b',
29 "temporary directory for creating built distributions"),
Greg Ward20283e52000-09-11 00:47:35 +000030 ('plat-name=', 'p',
31 "platform name to embed in generated filenames "
32 "(default: %s)" % get_platform()),
Greg Warde18dd8d2000-06-06 02:51:38 +000033 ('formats=', None,
Greg Ward2ff78872000-06-24 00:23:20 +000034 "formats for distribution (comma-separated list)"),
Greg Ward040dc0b2000-07-05 03:07:18 +000035 ('dist-dir=', 'd',
36 "directory to put final built distributions in "
37 "[default: dist]"),
Martin v. Löwis9668b932002-01-12 11:27:42 +000038 ('skip-build', None,
39 "skip rebuilding everything (for testing/debugging)"),
Andrew Kuchling5e2d4562013-11-15 13:01:52 -050040 ('owner=', 'u',
41 "Owner name used when creating a tar file"
42 " [default: current user]"),
43 ('group=', 'g',
44 "Group name used when creating a tar file"
45 " [default: current group]"),
Greg Ward0f77f952000-03-31 02:55:12 +000046 ]
47
Martin v. Löwis9668b932002-01-12 11:27:42 +000048 boolean_options = ['skip-build']
49
Greg Ward34593812000-06-24 01:23:37 +000050 help_options = [
51 ('help-formats', None,
52 "lists available distribution formats", show_formats),
Greg Wardfa9ff762000-10-14 04:06:40 +000053 ]
Greg Ward34593812000-06-24 01:23:37 +000054
Gregory P. Smith52e399c2000-05-13 01:49:56 +000055 # The following commands do not take a format option from bdist
Tarek Ziadéf6370502009-04-05 22:57:21 +000056 no_format_option = ('bdist_rpm',)
Gregory P. Smith52e399c2000-05-13 01:49:56 +000057
Greg Ward0f77f952000-03-31 02:55:12 +000058 # This won't do in reality: will need to distinguish RPM-ish Linux,
59 # Debian-ish Linux, Solaris, FreeBSD, ..., Windows, Mac OS.
Tarek Ziadéf6370502009-04-05 22:57:21 +000060 default_format = {'posix': 'gztar',
Jesus Cead17833d2012-10-11 01:20:12 +020061 'nt': 'zip'}
Greg Ward0f77f952000-03-31 02:55:12 +000062
Greg Ward6f628bb2000-08-02 01:44:44 +000063 # Establish the preferred order (for the --help-formats option).
Serhiy Storchakab9cec6a2015-05-16 22:13:27 +030064 format_commands = ['rpm', 'gztar', 'bztar', 'xztar', 'ztar', 'tar',
Tarek Ziadéf6370502009-04-05 22:57:21 +000065 'wininst', 'zip', 'msi']
Greg Ward6f628bb2000-08-02 01:44:44 +000066
67 # And the real information.
Tarek Ziadéf6370502009-04-05 22:57:21 +000068 format_command = {'rpm': ('bdist_rpm', "RPM distribution"),
69 'gztar': ('bdist_dumb', "gzip'ed tar file"),
70 'bztar': ('bdist_dumb', "bzip2'ed tar file"),
Serhiy Storchakab9cec6a2015-05-16 22:13:27 +030071 'xztar': ('bdist_dumb', "xz'ed tar file"),
Tarek Ziadéf6370502009-04-05 22:57:21 +000072 'ztar': ('bdist_dumb', "compressed tar file"),
73 'tar': ('bdist_dumb', "tar file"),
74 'wininst': ('bdist_wininst',
75 "Windows executable installer"),
76 'zip': ('bdist_dumb', "ZIP file"),
77 'msi': ('bdist_msi', "Microsoft Installer")
Marc-André Lemburg0538f1f2002-04-17 20:30:10 +000078 }
Greg Ward0f77f952000-03-31 02:55:12 +000079
80
Collin Winter5b7e9d72007-08-30 03:52:21 +000081 def initialize_options(self):
Gregory P. Smithc59d4e02000-05-13 03:08:28 +000082 self.bdist_base = None
Greg Ward20283e52000-09-11 00:47:35 +000083 self.plat_name = None
Greg Warde18dd8d2000-06-06 02:51:38 +000084 self.formats = None
Greg Ward040dc0b2000-07-05 03:07:18 +000085 self.dist_dir = None
Martin v. Löwis9668b932002-01-12 11:27:42 +000086 self.skip_build = 0
Andrew Kuchling5e2d4562013-11-15 13:01:52 -050087 self.group = None
88 self.owner = None
Greg Ward0f77f952000-03-31 02:55:12 +000089
Collin Winter5b7e9d72007-08-30 03:52:21 +000090 def finalize_options(self):
Greg Ward20283e52000-09-11 00:47:35 +000091 # have to finalize 'plat_name' before 'bdist_base'
92 if self.plat_name is None:
Christian Heimes5e696852008-04-09 08:37:03 +000093 if self.skip_build:
94 self.plat_name = get_platform()
95 else:
96 self.plat_name = self.get_finalized_command('build').plat_name
Greg Ward20283e52000-09-11 00:47:35 +000097
Gregory P. Smithc59d4e02000-05-13 03:08:28 +000098 # 'bdist_base' -- parent of per-built-distribution-format
99 # temporary directories (eg. we'll probably have
100 # "build/bdist.<plat>/dumb", "build/bdist.<plat>/rpm", etc.)
101 if self.bdist_base is None:
Greg Ward4fb29e52000-05-27 17:27:23 +0000102 build_base = self.get_finalized_command('build').build_base
Greg Wardbeb6d722000-09-16 16:04:59 +0000103 self.bdist_base = os.path.join(build_base,
104 'bdist.' + self.plat_name)
Gregory P. Smithc59d4e02000-05-13 03:08:28 +0000105
Greg Warde18dd8d2000-06-06 02:51:38 +0000106 self.ensure_string_list('formats')
107 if self.formats is None:
Greg Ward0f77f952000-03-31 02:55:12 +0000108 try:
Greg Warde18dd8d2000-06-06 02:51:38 +0000109 self.formats = [self.default_format[os.name]]
Greg Ward0f77f952000-03-31 02:55:12 +0000110 except KeyError:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000111 raise DistutilsPlatformError(
112 "don't know how to create built distributions "
113 "on platform %s" % os.name)
Greg Ward040dc0b2000-07-05 03:07:18 +0000114
115 if self.dist_dir is None:
116 self.dist_dir = "dist"
Fred Drake21d45352001-12-06 21:01:19 +0000117
Collin Winter5b7e9d72007-08-30 03:52:21 +0000118 def run(self):
Greg Wardbeb6d722000-09-16 16:04:59 +0000119 # Figure out which sub-commands we need to run.
120 commands = []
Greg Warde18dd8d2000-06-06 02:51:38 +0000121 for format in self.formats:
Greg Warde18dd8d2000-06-06 02:51:38 +0000122 try:
Greg Wardbeb6d722000-09-16 16:04:59 +0000123 commands.append(self.format_command[format][0])
Greg Warde18dd8d2000-06-06 02:51:38 +0000124 except KeyError:
Collin Winter5b7e9d72007-08-30 03:52:21 +0000125 raise DistutilsOptionError("invalid format '%s'" % format)
Greg Warde18dd8d2000-06-06 02:51:38 +0000126
Greg Wardbeb6d722000-09-16 16:04:59 +0000127 # Reinitialize and run each command.
128 for i in range(len(self.formats)):
129 cmd_name = commands[i]
Greg Warde18dd8d2000-06-06 02:51:38 +0000130 sub_cmd = self.reinitialize_command(cmd_name)
131 if cmd_name not in self.no_format_option:
Greg Wardbeb6d722000-09-16 16:04:59 +0000132 sub_cmd.format = self.formats[i]
133
Andrew Kuchling5e2d4562013-11-15 13:01:52 -0500134 # passing the owner and group names for tar archiving
135 if cmd_name == 'bdist_dumb':
136 sub_cmd.owner = self.owner
137 sub_cmd.group = self.group
138
Greg Wardbeb6d722000-09-16 16:04:59 +0000139 # If we're going to need to run this command again, tell it to
140 # keep its temporary files around so subsequent runs go faster.
141 if cmd_name in commands[i+1:]:
142 sub_cmd.keep_temp = 1
Greg Wardcb1f4c42000-09-30 18:27:54 +0000143 self.run_command(cmd_name)