Greg Ward | 0f77f95 | 2000-03-31 02:55:12 +0000 | [diff] [blame] | 1 | """distutils.command.bdist |
| 2 | |
| 3 | Implements the Distutils 'bdist' command (create a built [binary] |
| 4 | distribution).""" |
| 5 | |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 6 | import os |
Greg Ward | 0f77f95 | 2000-03-31 02:55:12 +0000 | [diff] [blame] | 7 | from distutils.core import Command |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 8 | from distutils.errors import * |
| 9 | from distutils.util import get_platform |
Greg Ward | 0f77f95 | 2000-03-31 02:55:12 +0000 | [diff] [blame] | 10 | |
| 11 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 12 | def show_formats(): |
Greg Ward | 3459381 | 2000-06-24 01:23:37 +0000 | [diff] [blame] | 13 | """Print list of available formats (arguments to "--format" option). |
| 14 | """ |
Fred Drake | 21d4535 | 2001-12-06 21:01:19 +0000 | [diff] [blame] | 15 | from distutils.fancy_getopt import FancyGetopt |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 16 | formats = [] |
Greg Ward | 3459381 | 2000-06-24 01:23:37 +0000 | [diff] [blame] | 17 | 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 Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 24 | class bdist(Command): |
Greg Ward | 0f77f95 | 2000-03-31 02:55:12 +0000 | [diff] [blame] | 25 | |
| 26 | description = "create a built (binary) distribution" |
| 27 | |
Gregory P. Smith | c59d4e0 | 2000-05-13 03:08:28 +0000 | [diff] [blame] | 28 | user_options = [('bdist-base=', 'b', |
| 29 | "temporary directory for creating built distributions"), |
Greg Ward | 20283e5 | 2000-09-11 00:47:35 +0000 | [diff] [blame] | 30 | ('plat-name=', 'p', |
| 31 | "platform name to embed in generated filenames " |
| 32 | "(default: %s)" % get_platform()), |
Greg Ward | e18dd8d | 2000-06-06 02:51:38 +0000 | [diff] [blame] | 33 | ('formats=', None, |
Greg Ward | 2ff7887 | 2000-06-24 00:23:20 +0000 | [diff] [blame] | 34 | "formats for distribution (comma-separated list)"), |
Greg Ward | 040dc0b | 2000-07-05 03:07:18 +0000 | [diff] [blame] | 35 | ('dist-dir=', 'd', |
| 36 | "directory to put final built distributions in " |
| 37 | "[default: dist]"), |
Martin v. Löwis | 9668b93 | 2002-01-12 11:27:42 +0000 | [diff] [blame] | 38 | ('skip-build', None, |
| 39 | "skip rebuilding everything (for testing/debugging)"), |
Greg Ward | 0f77f95 | 2000-03-31 02:55:12 +0000 | [diff] [blame] | 40 | ] |
| 41 | |
Martin v. Löwis | 9668b93 | 2002-01-12 11:27:42 +0000 | [diff] [blame] | 42 | boolean_options = ['skip-build'] |
| 43 | |
Greg Ward | 3459381 | 2000-06-24 01:23:37 +0000 | [diff] [blame] | 44 | help_options = [ |
| 45 | ('help-formats', None, |
| 46 | "lists available distribution formats", show_formats), |
Greg Ward | fa9ff76 | 2000-10-14 04:06:40 +0000 | [diff] [blame] | 47 | ] |
Greg Ward | 3459381 | 2000-06-24 01:23:37 +0000 | [diff] [blame] | 48 | |
Gregory P. Smith | 52e399c | 2000-05-13 01:49:56 +0000 | [diff] [blame] | 49 | # The following commands do not take a format option from bdist |
Tarek Ziadé | f637050 | 2009-04-05 22:57:21 +0000 | [diff] [blame] | 50 | no_format_option = ('bdist_rpm',) |
Gregory P. Smith | 52e399c | 2000-05-13 01:49:56 +0000 | [diff] [blame] | 51 | |
Greg Ward | 0f77f95 | 2000-03-31 02:55:12 +0000 | [diff] [blame] | 52 | # This won't do in reality: will need to distinguish RPM-ish Linux, |
| 53 | # Debian-ish Linux, Solaris, FreeBSD, ..., Windows, Mac OS. |
Tarek Ziadé | f637050 | 2009-04-05 22:57:21 +0000 | [diff] [blame] | 54 | default_format = {'posix': 'gztar', |
Jesus Cea | d17833d | 2012-10-11 01:20:12 +0200 | [diff] [blame] | 55 | 'nt': 'zip'} |
Greg Ward | 0f77f95 | 2000-03-31 02:55:12 +0000 | [diff] [blame] | 56 | |
Greg Ward | 6f628bb | 2000-08-02 01:44:44 +0000 | [diff] [blame] | 57 | # Establish the preferred order (for the --help-formats option). |
| 58 | format_commands = ['rpm', 'gztar', 'bztar', 'ztar', 'tar', |
Tarek Ziadé | f637050 | 2009-04-05 22:57:21 +0000 | [diff] [blame] | 59 | 'wininst', 'zip', 'msi'] |
Greg Ward | 6f628bb | 2000-08-02 01:44:44 +0000 | [diff] [blame] | 60 | |
| 61 | # And the real information. |
Tarek Ziadé | f637050 | 2009-04-05 22:57:21 +0000 | [diff] [blame] | 62 | format_command = {'rpm': ('bdist_rpm', "RPM distribution"), |
| 63 | 'gztar': ('bdist_dumb', "gzip'ed tar file"), |
| 64 | 'bztar': ('bdist_dumb', "bzip2'ed tar file"), |
| 65 | 'ztar': ('bdist_dumb', "compressed tar file"), |
| 66 | 'tar': ('bdist_dumb', "tar file"), |
| 67 | 'wininst': ('bdist_wininst', |
| 68 | "Windows executable installer"), |
| 69 | 'zip': ('bdist_dumb', "ZIP file"), |
| 70 | 'msi': ('bdist_msi', "Microsoft Installer") |
Marc-André Lemburg | 0538f1f | 2002-04-17 20:30:10 +0000 | [diff] [blame] | 71 | } |
Greg Ward | 0f77f95 | 2000-03-31 02:55:12 +0000 | [diff] [blame] | 72 | |
| 73 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 74 | def initialize_options(self): |
Gregory P. Smith | c59d4e0 | 2000-05-13 03:08:28 +0000 | [diff] [blame] | 75 | self.bdist_base = None |
Greg Ward | 20283e5 | 2000-09-11 00:47:35 +0000 | [diff] [blame] | 76 | self.plat_name = None |
Greg Ward | e18dd8d | 2000-06-06 02:51:38 +0000 | [diff] [blame] | 77 | self.formats = None |
Greg Ward | 040dc0b | 2000-07-05 03:07:18 +0000 | [diff] [blame] | 78 | self.dist_dir = None |
Martin v. Löwis | 9668b93 | 2002-01-12 11:27:42 +0000 | [diff] [blame] | 79 | self.skip_build = 0 |
Greg Ward | 0f77f95 | 2000-03-31 02:55:12 +0000 | [diff] [blame] | 80 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 81 | def finalize_options(self): |
Greg Ward | 20283e5 | 2000-09-11 00:47:35 +0000 | [diff] [blame] | 82 | # have to finalize 'plat_name' before 'bdist_base' |
| 83 | if self.plat_name is None: |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 84 | if self.skip_build: |
| 85 | self.plat_name = get_platform() |
| 86 | else: |
| 87 | self.plat_name = self.get_finalized_command('build').plat_name |
Greg Ward | 20283e5 | 2000-09-11 00:47:35 +0000 | [diff] [blame] | 88 | |
Gregory P. Smith | c59d4e0 | 2000-05-13 03:08:28 +0000 | [diff] [blame] | 89 | # 'bdist_base' -- parent of per-built-distribution-format |
| 90 | # temporary directories (eg. we'll probably have |
| 91 | # "build/bdist.<plat>/dumb", "build/bdist.<plat>/rpm", etc.) |
| 92 | if self.bdist_base is None: |
Greg Ward | 4fb29e5 | 2000-05-27 17:27:23 +0000 | [diff] [blame] | 93 | build_base = self.get_finalized_command('build').build_base |
Greg Ward | beb6d72 | 2000-09-16 16:04:59 +0000 | [diff] [blame] | 94 | self.bdist_base = os.path.join(build_base, |
| 95 | 'bdist.' + self.plat_name) |
Gregory P. Smith | c59d4e0 | 2000-05-13 03:08:28 +0000 | [diff] [blame] | 96 | |
Greg Ward | e18dd8d | 2000-06-06 02:51:38 +0000 | [diff] [blame] | 97 | self.ensure_string_list('formats') |
| 98 | if self.formats is None: |
Greg Ward | 0f77f95 | 2000-03-31 02:55:12 +0000 | [diff] [blame] | 99 | try: |
Greg Ward | e18dd8d | 2000-06-06 02:51:38 +0000 | [diff] [blame] | 100 | self.formats = [self.default_format[os.name]] |
Greg Ward | 0f77f95 | 2000-03-31 02:55:12 +0000 | [diff] [blame] | 101 | except KeyError: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 102 | raise DistutilsPlatformError( |
| 103 | "don't know how to create built distributions " |
| 104 | "on platform %s" % os.name) |
Greg Ward | 040dc0b | 2000-07-05 03:07:18 +0000 | [diff] [blame] | 105 | |
| 106 | if self.dist_dir is None: |
| 107 | self.dist_dir = "dist" |
Fred Drake | 21d4535 | 2001-12-06 21:01:19 +0000 | [diff] [blame] | 108 | |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 109 | def run(self): |
Greg Ward | beb6d72 | 2000-09-16 16:04:59 +0000 | [diff] [blame] | 110 | # Figure out which sub-commands we need to run. |
| 111 | commands = [] |
Greg Ward | e18dd8d | 2000-06-06 02:51:38 +0000 | [diff] [blame] | 112 | for format in self.formats: |
Greg Ward | e18dd8d | 2000-06-06 02:51:38 +0000 | [diff] [blame] | 113 | try: |
Greg Ward | beb6d72 | 2000-09-16 16:04:59 +0000 | [diff] [blame] | 114 | commands.append(self.format_command[format][0]) |
Greg Ward | e18dd8d | 2000-06-06 02:51:38 +0000 | [diff] [blame] | 115 | except KeyError: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 116 | raise DistutilsOptionError("invalid format '%s'" % format) |
Greg Ward | e18dd8d | 2000-06-06 02:51:38 +0000 | [diff] [blame] | 117 | |
Greg Ward | beb6d72 | 2000-09-16 16:04:59 +0000 | [diff] [blame] | 118 | # Reinitialize and run each command. |
| 119 | for i in range(len(self.formats)): |
| 120 | cmd_name = commands[i] |
Greg Ward | e18dd8d | 2000-06-06 02:51:38 +0000 | [diff] [blame] | 121 | sub_cmd = self.reinitialize_command(cmd_name) |
| 122 | if cmd_name not in self.no_format_option: |
Greg Ward | beb6d72 | 2000-09-16 16:04:59 +0000 | [diff] [blame] | 123 | sub_cmd.format = self.formats[i] |
| 124 | |
Greg Ward | beb6d72 | 2000-09-16 16:04:59 +0000 | [diff] [blame] | 125 | # If we're going to need to run this command again, tell it to |
| 126 | # keep its temporary files around so subsequent runs go faster. |
| 127 | if cmd_name in commands[i+1:]: |
| 128 | sub_cmd.keep_temp = 1 |
Greg Ward | cb1f4c4 | 2000-09-30 18:27:54 +0000 | [diff] [blame] | 129 | self.run_command(cmd_name) |