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

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

6
# This module should be kept compatible with Python 2.1.
7

8 9
__revision__ = "$Id$"

10
import os
Greg Ward's avatar
Greg Ward committed
11
from types import *
12
from distutils.core import Command
13
from distutils.errors import *
14
from distutils.util import get_platform
15 16


17 18 19
def show_formats ():
    """Print list of available formats (arguments to "--format" option).
    """
Fred Drake's avatar
Fred Drake committed
20
    from distutils.fancy_getopt import FancyGetopt
21 22 23 24 25 26 27 28
    formats=[]
    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:")


29 30 31 32
class bdist (Command):

    description = "create a built (binary) distribution"

33 34
    user_options = [('bdist-base=', 'b',
                     "temporary directory for creating built distributions"),
35 36 37
                    ('plat-name=', 'p',
                     "platform name to embed in generated filenames "
                     "(default: %s)" % get_platform()),
38
                    ('formats=', None,
39
                     "formats for distribution (comma-separated list)"),
40 41 42
                    ('dist-dir=', 'd',
                     "directory to put final built distributions in "
                     "[default: dist]"),
43 44
                    ('skip-build', None,
                     "skip rebuilding everything (for testing/debugging)"),
45 46
                   ]

47 48
    boolean_options = ['skip-build']

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

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

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

65 66
    # Establish the preferred order (for the --help-formats option).
    format_commands = ['rpm', 'gztar', 'bztar', 'ztar', 'tar',
67 68 69
                       'wininst', 'zip',
                       #'pkgtool', 'sdux'
                       ]
70 71

    # And the real information.
72
    format_command = { 'rpm':   ('bdist_rpm',  "RPM distribution"),
73 74
                       'zip':   ('bdist_dumb', "ZIP file"),
                       'gztar': ('bdist_dumb', "gzip'ed tar file"),
75 76 77
                       'bztar': ('bdist_dumb', "bzip2'ed tar file"),
                       'ztar':  ('bdist_dumb', "compressed tar file"),
                       'tar':   ('bdist_dumb', "tar file"),
78 79
                       'wininst': ('bdist_wininst',
                                   "Windows executable installer"),
80
                       'zip':   ('bdist_dumb', "ZIP file"),
81
                       #'pkgtool': ('bdist_pkgtool',
82 83
                       #            "Solaris pkgtool distribution"),
                       #'sdux':  ('bdist_sdux', "HP-UX swinstall depot"),
84
                      }
85 86 87


    def initialize_options (self):
88
        self.bdist_base = None
89
        self.plat_name = None
90
        self.formats = None
91
        self.dist_dir = None
92
        self.skip_build = 0
93 94 95 96 97

    # initialize_options()


    def finalize_options (self):
98 99
        # have to finalize 'plat_name' before 'bdist_base'
        if self.plat_name is None:
100 101 102 103
            if self.skip_build:
                self.plat_name = get_platform()
            else:
                self.plat_name = self.get_finalized_command('build').plat_name
104

105 106 107 108
        # '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:
109
            build_base = self.get_finalized_command('build').build_base
110 111
            self.bdist_base = os.path.join(build_base,
                                           'bdist.' + self.plat_name)
112

113 114
        self.ensure_string_list('formats')
        if self.formats is None:
115
            try:
116
                self.formats = [self.default_format[os.name]]
117 118 119 120
            except KeyError:
                raise DistutilsPlatformError, \
                      "don't know how to create built distributions " + \
                      "on platform %s" % os.name
121 122 123

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

125 126 127 128
    # finalize_options()

    def run (self):

129 130
        # Figure out which sub-commands we need to run.
        commands = []
131 132
        for format in self.formats:
            try:
133
                commands.append(self.format_command[format][0])
134
            except KeyError:
135
                raise DistutilsOptionError, "invalid format '%s'" % format
136

137 138 139
        # Reinitialize and run each command.
        for i in range(len(self.formats)):
            cmd_name = commands[i]
140 141
            sub_cmd = self.reinitialize_command(cmd_name)
            if cmd_name not in self.no_format_option:
142 143 144 145 146 147
                sub_cmd.format = self.formats[i]

            # 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
148
            self.run_command(cmd_name)
149 150 151 152

    # run()

# class bdist