ยปCore Development>Code coverage>Lib/packaging/command/bdist.py

Python code coverage for Lib/packaging/command/bdist.py

#countcontent
1n/a"""Create a built (binary) distribution.
2n/a
3n/aIf a --formats option was given on the command line, this command will
4n/acall the corresponding bdist_* commands; if the option was absent, a
5n/abdist_* command depending on the current platform will be called.
6n/a"""
7n/a
8n/aimport os
9n/a
10n/afrom packaging import util
11n/afrom packaging.command.cmd import Command
12n/afrom packaging.errors import PackagingPlatformError, PackagingOptionError
13n/a
14n/a
15n/adef show_formats():
16n/a """Print list of available formats (arguments to "--format" option).
17n/a """
18n/a from packaging.fancy_getopt import FancyGetopt
19n/a formats = []
20n/a for format in bdist.format_commands:
21n/a formats.append(("formats=" + format, None,
22n/a bdist.format_command[format][1]))
23n/a pretty_printer = FancyGetopt(formats)
24n/a pretty_printer.print_help("List of available distribution formats:")
25n/a
26n/a
27n/aclass bdist(Command):
28n/a
29n/a description = "create a built (binary) distribution"
30n/a
31n/a user_options = [('bdist-base=', 'b',
32n/a "temporary directory for creating built distributions"),
33n/a ('plat-name=', 'p',
34n/a "platform name to embed in generated filenames "
35n/a "(default: %s)" % util.get_platform()),
36n/a ('formats=', None,
37n/a "formats for distribution (comma-separated list)"),
38n/a ('dist-dir=', 'd',
39n/a "directory to put final built distributions in "
40n/a "[default: dist]"),
41n/a ('skip-build', None,
42n/a "skip rebuilding everything (for testing/debugging)"),
43n/a ('owner=', 'u',
44n/a "Owner name used when creating a tar file"
45n/a " [default: current user]"),
46n/a ('group=', 'g',
47n/a "Group name used when creating a tar file"
48n/a " [default: current group]"),
49n/a ]
50n/a
51n/a boolean_options = ['skip-build']
52n/a
53n/a help_options = [
54n/a ('help-formats', None,
55n/a "lists available distribution formats", show_formats),
56n/a ]
57n/a
58n/a # This is of course very simplistic. The various UNIX family operating
59n/a # systems have their specific formats, but they are out of scope for us;
60n/a # bdist_dumb is, well, dumb; it's more a building block for other
61n/a # packaging tools than a real end-user binary format.
62n/a default_format = {'posix': 'gztar',
63n/a 'nt': 'zip',
64n/a 'os2': 'zip'}
65n/a
66n/a # Establish the preferred order (for the --help-formats option).
67n/a format_commands = ['gztar', 'bztar', 'tar',
68n/a 'wininst', 'zip', 'msi']
69n/a
70n/a # And the real information.
71n/a format_command = {'gztar': ('bdist_dumb', "gzip'ed tar file"),
72n/a 'bztar': ('bdist_dumb', "bzip2'ed tar file"),
73n/a 'tar': ('bdist_dumb', "tar file"),
74n/a 'wininst': ('bdist_wininst',
75n/a "Windows executable installer"),
76n/a 'zip': ('bdist_dumb', "ZIP file"),
77n/a 'msi': ('bdist_msi', "Microsoft Installer"),
78n/a }
79n/a
80n/a def initialize_options(self):
81n/a self.bdist_base = None
82n/a self.plat_name = None
83n/a self.formats = None
84n/a self.dist_dir = None
85n/a self.skip_build = False
86n/a self.group = None
87n/a self.owner = None
88n/a
89n/a def finalize_options(self):
90n/a # have to finalize 'plat_name' before 'bdist_base'
91n/a if self.plat_name is None:
92n/a if self.skip_build:
93n/a self.plat_name = util.get_platform()
94n/a else:
95n/a self.plat_name = self.get_finalized_command('build').plat_name
96n/a
97n/a # 'bdist_base' -- parent of per-built-distribution-format
98n/a # temporary directories (eg. we'll probably have
99n/a # "build/bdist.<plat>/dumb", etc.)
100n/a if self.bdist_base is None:
101n/a build_base = self.get_finalized_command('build').build_base
102n/a self.bdist_base = os.path.join(build_base,
103n/a 'bdist.' + self.plat_name)
104n/a
105n/a self.ensure_string_list('formats')
106n/a if self.formats is None:
107n/a try:
108n/a self.formats = [self.default_format[os.name]]
109n/a except KeyError:
110n/a raise PackagingPlatformError(
111n/a "don't know how to create built distributions "
112n/a "on platform %s" % os.name)
113n/a
114n/a if self.dist_dir is None:
115n/a self.dist_dir = "dist"
116n/a
117n/a def run(self):
118n/a # Figure out which sub-commands we need to run.
119n/a commands = []
120n/a for format in self.formats:
121n/a try:
122n/a commands.append(self.format_command[format][0])
123n/a except KeyError:
124n/a raise PackagingOptionError("invalid format '%s'" % format)
125n/a
126n/a # Reinitialize and run each command.
127n/a for i in range(len(self.formats)):
128n/a cmd_name = commands[i]
129n/a sub_cmd = self.reinitialize_command(cmd_name)
130n/a sub_cmd.format = self.formats[i]
131n/a
132n/a # passing the owner and group names for tar archiving
133n/a if cmd_name == 'bdist_dumb':
134n/a sub_cmd.owner = self.owner
135n/a sub_cmd.group = self.group
136n/a
137n/a # If we're going to need to run this command again, tell it to
138n/a # keep its temporary files around so subsequent runs go faster.
139n/a if cmd_name in commands[i+1:]:
140n/a sub_cmd.keep_temp = True
141n/a self.run_command(cmd_name)