• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""distutils.command.bdist
2
3Implements the Distutils 'bdist' command (create a built [binary]
4distribution)."""
5
6__revision__ = "$Id$"
7
8import os
9
10from distutils.util import get_platform
11from distutils.core import Command
12from distutils.errors import DistutilsPlatformError, DistutilsOptionError
13
14
15def show_formats():
16    """Print list of available formats (arguments to "--format" option).
17    """
18    from distutils.fancy_getopt import FancyGetopt
19    formats = []
20    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
27class bdist(Command):
28
29    description = "create a built (binary) distribution"
30
31    user_options = [('bdist-base=', 'b',
32                     "temporary directory for creating built distributions"),
33                    ('plat-name=', 'p',
34                     "platform name to embed in generated filenames "
35                     "(default: %s)" % get_platform()),
36                    ('formats=', None,
37                     "formats for distribution (comma-separated list)"),
38                    ('dist-dir=', 'd',
39                     "directory to put final built distributions in "
40                     "[default: dist]"),
41                    ('skip-build', None,
42                     "skip rebuilding everything (for testing/debugging)"),
43                    ('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]"),
49                   ]
50
51    boolean_options = ['skip-build']
52
53    help_options = [
54        ('help-formats', None,
55         "lists available distribution formats", show_formats),
56        ]
57
58    # The following commands do not take a format option from bdist
59    no_format_option = ('bdist_rpm',)
60
61    # This won't do in reality: will need to distinguish RPM-ish Linux,
62    # Debian-ish Linux, Solaris, FreeBSD, ..., Windows, Mac OS.
63    default_format = {'posix': 'gztar',
64                      'nt': 'zip',
65                      'os2': 'zip'}
66
67    # Establish the preferred order (for the --help-formats option).
68    format_commands = ['rpm', 'gztar', 'bztar', 'ztar', 'tar',
69                       'wininst', 'zip', 'msi']
70
71    # And the real information.
72    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")
81                      }
82
83
84    def initialize_options(self):
85        self.bdist_base = None
86        self.plat_name = None
87        self.formats = None
88        self.dist_dir = None
89        self.skip_build = 0
90        self.group = None
91        self.owner = None
92
93    def finalize_options(self):
94        # have to finalize 'plat_name' before 'bdist_base'
95        if self.plat_name is None:
96            if self.skip_build:
97                self.plat_name = get_platform()
98            else:
99                self.plat_name = self.get_finalized_command('build').plat_name
100
101        # '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:
105            build_base = self.get_finalized_command('build').build_base
106            self.bdist_base = os.path.join(build_base,
107                                           'bdist.' + self.plat_name)
108
109        self.ensure_string_list('formats')
110        if self.formats is None:
111            try:
112                self.formats = [self.default_format[os.name]]
113            except KeyError:
114                raise DistutilsPlatformError, \
115                      "don't know how to create built distributions " + \
116                      "on platform %s" % os.name
117
118        if self.dist_dir is None:
119            self.dist_dir = "dist"
120
121    def run(self):
122        # Figure out which sub-commands we need to run.
123        commands = []
124        for format in self.formats:
125            try:
126                commands.append(self.format_command[format][0])
127            except KeyError:
128                raise DistutilsOptionError, "invalid format '%s'" % format
129
130        # Reinitialize and run each command.
131        for i in range(len(self.formats)):
132            cmd_name = commands[i]
133            sub_cmd = self.reinitialize_command(cmd_name)
134            if cmd_name not in self.no_format_option:
135                sub_cmd.format = self.formats[i]
136
137            # 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
142            # 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
146            self.run_command(cmd_name)
147