• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""distutils.command.bdist
2
3Implements the Distutils 'bdist' command (create a built [binary]
4distribution)."""
5
6import os
7from distutils.core import Command
8from distutils.errors import *
9from distutils.util import get_platform
10
11
12def show_formats():
13    """Print list of available formats (arguments to "--format" option).
14    """
15    from distutils.fancy_getopt import FancyGetopt
16    formats = []
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
24class bdist(Command):
25
26    description = "create a built (binary) distribution"
27
28    user_options = [('bdist-base=', 'b',
29                     "temporary directory for creating built distributions"),
30                    ('plat-name=', 'p',
31                     "platform name to embed in generated filenames "
32                     "(default: %s)" % get_platform()),
33                    ('formats=', None,
34                     "formats for distribution (comma-separated list)"),
35                    ('dist-dir=', 'd',
36                     "directory to put final built distributions in "
37                     "[default: dist]"),
38                    ('skip-build', None,
39                     "skip rebuilding everything (for testing/debugging)"),
40                    ('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]"),
46                   ]
47
48    boolean_options = ['skip-build']
49
50    help_options = [
51        ('help-formats', None,
52         "lists available distribution formats", show_formats),
53        ]
54
55    # The following commands do not take a format option from bdist
56    no_format_option = ('bdist_rpm',)
57
58    # This won't do in reality: will need to distinguish RPM-ish Linux,
59    # 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    format_command = {'rpm':   ('bdist_rpm',  "RPM distribution"),
69                      'gztar': ('bdist_dumb', "gzip'ed tar file"),
70                      'bztar': ('bdist_dumb', "bzip2'ed tar file"),
71                      'xztar': ('bdist_dumb', "xz'ed tar file"),
72                      '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")
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        self.group = None
88        self.owner = None
89
90    def finalize_options(self):
91        # have to finalize 'plat_name' before 'bdist_base'
92        if self.plat_name is None:
93            if self.skip_build:
94                self.plat_name = get_platform()
95            else:
96                self.plat_name = self.get_finalized_command('build').plat_name
97
98        # '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:
102            build_base = self.get_finalized_command('build').build_base
103            self.bdist_base = os.path.join(build_base,
104                                           'bdist.' + self.plat_name)
105
106        self.ensure_string_list('formats')
107        if self.formats is None:
108            try:
109                self.formats = [self.default_format[os.name]]
110            except KeyError:
111                raise DistutilsPlatformError(
112                      "don't know how to create built distributions "
113                      "on platform %s" % os.name)
114
115        if self.dist_dir is None:
116            self.dist_dir = "dist"
117
118    def run(self):
119        # Figure out which sub-commands we need to run.
120        commands = []
121        for format in self.formats:
122            try:
123                commands.append(self.format_command[format][0])
124            except KeyError:
125                raise DistutilsOptionError("invalid format '%s'" % format)
126
127        # Reinitialize and run each command.
128        for i in range(len(self.formats)):
129            cmd_name = commands[i]
130            sub_cmd = self.reinitialize_command(cmd_name)
131            if cmd_name not in self.no_format_option:
132                sub_cmd.format = self.formats[i]
133
134            # 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
139            # 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
143            self.run_command(cmd_name)
144