bdist.py 5.43 KB
Newer Older
1 2 3 4 5
"""distutils.command.bdist

Implements the Distutils 'bdist' command (create a built [binary]
distribution)."""

6
import os
7
from distutils.core import Command
8 9
from distutils.errors import *
from distutils.util import get_platform
10 11


12
def show_formats():
13 14
    """Print list of available formats (arguments to "--format" option).
    """
Fred Drake's avatar
Fred Drake committed
15
    from distutils.fancy_getopt import FancyGetopt
16
    formats = []
17 18 19 20 21 22 23
    for format in bdist.format_commands:
        formats.append(("formats=" + format, None,
                        bdist.format_command[format][1]))
    pretty_printer = FancyGetopt(formats)
    pretty_printer.print_help("List of available distribution formats:")


24
class bdist(Command):
25 26 27

    description = "create a built (binary) distribution"

28 29
    user_options = [('bdist-base=', 'b',
                     "temporary directory for creating built distributions"),
30 31 32
                    ('plat-name=', 'p',
                     "platform name to embed in generated filenames "
                     "(default: %s)" % get_platform()),
33
                    ('formats=', None,
34
                     "formats for distribution (comma-separated list)"),
35 36 37
                    ('dist-dir=', 'd',
                     "directory to put final built distributions in "
                     "[default: dist]"),
38 39
                    ('skip-build', None,
                     "skip rebuilding everything (for testing/debugging)"),
40 41 42 43 44 45
                    ('owner=', 'u',
                     "Owner name used when creating a tar file"
                     " [default: current user]"),
                    ('group=', 'g',
                     "Group name used when creating a tar file"
                     " [default: current group]"),
46 47
                   ]

48 49
    boolean_options = ['skip-build']

50 51 52
    help_options = [
        ('help-formats', None,
         "lists available distribution formats", show_formats),
Greg Ward's avatar
Greg Ward committed
53
        ]
54

55
    # The following commands do not take a format option from bdist
56
    no_format_option = ('bdist_rpm',)
57

58 59
    # This won't do in reality: will need to distinguish RPM-ish Linux,
    # Debian-ish Linux, Solaris, FreeBSD, ..., Windows, Mac OS.
60
    default_format = {'posix': 'gztar',
61
                      'nt': 'zip'}
62

63
    # Establish the preferred order (for the --help-formats option).
64
    format_commands = ['rpm', 'gztar', 'bztar', 'xztar', 'ztar', 'tar',
65
                       'wininst', 'zip', 'msi']
66 67

    # And the real information.
68 69 70
    format_command = {'rpm':   ('bdist_rpm',  "RPM distribution"),
                      'gztar': ('bdist_dumb', "gzip'ed tar file"),
                      'bztar': ('bdist_dumb', "bzip2'ed tar file"),
71
                      'xztar': ('bdist_dumb', "xz'ed tar file"),
72 73 74 75 76 77
                      'ztar':  ('bdist_dumb', "compressed tar file"),
                      'tar':   ('bdist_dumb', "tar file"),
                      'wininst': ('bdist_wininst',
                                  "Windows executable installer"),
                      'zip':   ('bdist_dumb', "ZIP file"),
                      'msi':   ('bdist_msi',  "Microsoft Installer")
78
                      }
79 80


81
    def initialize_options(self):
82
        self.bdist_base = None
83
        self.plat_name = None
84
        self.formats = None
85
        self.dist_dir = None
86
        self.skip_build = 0
87 88
        self.group = None
        self.owner = None
89

90
    def finalize_options(self):
91 92
        # have to finalize 'plat_name' before 'bdist_base'
        if self.plat_name is None:
Christian Heimes's avatar
Christian Heimes committed
93 94 95 96
            if self.skip_build:
                self.plat_name = get_platform()
            else:
                self.plat_name = self.get_finalized_command('build').plat_name
97

98 99 100 101
        # 'bdist_base' -- parent of per-built-distribution-format
        # temporary directories (eg. we'll probably have
        # "build/bdist.<plat>/dumb", "build/bdist.<plat>/rpm", etc.)
        if self.bdist_base is None:
102
            build_base = self.get_finalized_command('build').build_base
103 104
            self.bdist_base = os.path.join(build_base,
                                           'bdist.' + self.plat_name)
105

106 107
        self.ensure_string_list('formats')
        if self.formats is None:
108
            try:
109
                self.formats = [self.default_format[os.name]]
110
            except KeyError:
111 112 113
                raise DistutilsPlatformError(
                      "don't know how to create built distributions "
                      "on platform %s" % os.name)
114 115 116

        if self.dist_dir is None:
            self.dist_dir = "dist"
Fred Drake's avatar
Fred Drake committed
117

118
    def run(self):
119 120
        # Figure out which sub-commands we need to run.
        commands = []
121 122
        for format in self.formats:
            try:
123
                commands.append(self.format_command[format][0])
124
            except KeyError:
125
                raise DistutilsOptionError("invalid format '%s'" % format)
126

127 128 129
        # Reinitialize and run each command.
        for i in range(len(self.formats)):
            cmd_name = commands[i]
130 131
            sub_cmd = self.reinitialize_command(cmd_name)
            if cmd_name not in self.no_format_option:
132 133
                sub_cmd.format = self.formats[i]

134 135 136 137 138
            # passing the owner and group names for tar archiving
            if cmd_name == 'bdist_dumb':
                sub_cmd.owner = self.owner
                sub_cmd.group = self.group

139 140 141 142
            # If we're going to need to run this command again, tell it to
            # keep its temporary files around so subsequent runs go faster.
            if cmd_name in commands[i+1:]:
                sub_cmd.keep_temp = 1
143
            self.run_command(cmd_name)