• 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                       '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                      'zip':   ('bdist_dumb', "ZIP file"),
75                      'msi':   ('bdist_msi',  "Microsoft Installer")
76                      }
77
78
79    def initialize_options(self):
80        self.bdist_base = None
81        self.plat_name = None
82        self.formats = None
83        self.dist_dir = None
84        self.skip_build = 0
85        self.group = None
86        self.owner = None
87
88    def finalize_options(self):
89        # have to finalize 'plat_name' before 'bdist_base'
90        if self.plat_name is None:
91            if self.skip_build:
92                self.plat_name = get_platform()
93            else:
94                self.plat_name = self.get_finalized_command('build').plat_name
95
96        # 'bdist_base' -- parent of per-built-distribution-format
97        # temporary directories (eg. we'll probably have
98        # "build/bdist.<plat>/dumb", "build/bdist.<plat>/rpm", etc.)
99        if self.bdist_base is None:
100            build_base = self.get_finalized_command('build').build_base
101            self.bdist_base = os.path.join(build_base,
102                                           'bdist.' + self.plat_name)
103
104        self.ensure_string_list('formats')
105        if self.formats is None:
106            try:
107                self.formats = [self.default_format[os.name]]
108            except KeyError:
109                raise DistutilsPlatformError(
110                      "don't know how to create built distributions "
111                      "on platform %s" % os.name)
112
113        if self.dist_dir is None:
114            self.dist_dir = "dist"
115
116    def run(self):
117        # Figure out which sub-commands we need to run.
118        commands = []
119        for format in self.formats:
120            try:
121                commands.append(self.format_command[format][0])
122            except KeyError:
123                raise DistutilsOptionError("invalid format '%s'" % format)
124
125        # Reinitialize and run each command.
126        for i in range(len(self.formats)):
127            cmd_name = commands[i]
128            sub_cmd = self.reinitialize_command(cmd_name)
129            if cmd_name not in self.no_format_option:
130                sub_cmd.format = self.formats[i]
131
132            # passing the owner and group names for tar archiving
133            if cmd_name == 'bdist_dumb':
134                sub_cmd.owner = self.owner
135                sub_cmd.group = self.group
136
137            # If we're going to need to run this command again, tell it to
138            # keep its temporary files around so subsequent runs go faster.
139            if cmd_name in commands[i+1:]:
140                sub_cmd.keep_temp = 1
141            self.run_command(cmd_name)
142