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