]>
Commit | Line | Data |
---|---|---|
e0df8241 JR |
1 | """distutils.command.bdist |
2 | ||
3 | Implements the Distutils 'bdist' command (create a built [binary] | |
4 | distribution).""" | |
5 | ||
6 | import os | |
7 | import warnings | |
8 | ||
9 | from ..core import Command | |
10 | from ..errors import DistutilsPlatformError, DistutilsOptionError | |
11 | from ..util import get_platform | |
12 | ||
13 | ||
14 | def show_formats(): | |
15 | """Print list of available formats (arguments to "--format" option).""" | |
16 | from ..fancy_getopt import FancyGetopt | |
17 | ||
18 | formats = [] | |
19 | for format in bdist.format_commands: | |
20 | formats.append(("formats=" + format, None, bdist.format_commands[format][1])) | |
21 | pretty_printer = FancyGetopt(formats) | |
22 | pretty_printer.print_help("List of available distribution formats:") | |
23 | ||
24 | ||
25 | class ListCompat(dict): | |
26 | # adapter to allow for Setuptools compatibility in format_commands | |
27 | def append(self, item): | |
28 | warnings.warn( | |
29 | """format_commands is now a dict. append is deprecated.""", | |
30 | DeprecationWarning, | |
31 | stacklevel=2, | |
32 | ) | |
33 | ||
34 | ||
35 | class bdist(Command): | |
36 | description = "create a built (binary) distribution" | |
37 | ||
38 | user_options = [ | |
39 | ('bdist-base=', 'b', "temporary directory for creating built distributions"), | |
40 | ( | |
41 | 'plat-name=', | |
42 | 'p', | |
43 | "platform name to embed in generated filenames " | |
44 | "(default: %s)" % get_platform(), | |
45 | ), | |
46 | ('formats=', None, "formats for distribution (comma-separated list)"), | |
47 | ( | |
48 | 'dist-dir=', | |
49 | 'd', | |
50 | "directory to put final built distributions in " "[default: dist]", | |
51 | ), | |
52 | ('skip-build', None, "skip rebuilding everything (for testing/debugging)"), | |
53 | ( | |
54 | 'owner=', | |
55 | 'u', | |
56 | "Owner name used when creating a tar file" " [default: current user]", | |
57 | ), | |
58 | ( | |
59 | 'group=', | |
60 | 'g', | |
61 | "Group name used when creating a tar file" " [default: current group]", | |
62 | ), | |
63 | ] | |
64 | ||
65 | boolean_options = ['skip-build'] | |
66 | ||
67 | help_options = [ | |
68 | ('help-formats', None, "lists available distribution formats", show_formats), | |
69 | ] | |
70 | ||
71 | # The following commands do not take a format option from bdist | |
72 | no_format_option = ('bdist_rpm',) | |
73 | ||
74 | # This won't do in reality: will need to distinguish RPM-ish Linux, | |
75 | # Debian-ish Linux, Solaris, FreeBSD, ..., Windows, Mac OS. | |
76 | default_format = {'posix': 'gztar', 'nt': 'zip'} | |
77 | ||
78 | # Define commands in preferred order for the --help-formats option | |
79 | format_commands = ListCompat( | |
80 | { | |
81 | 'rpm': ('bdist_rpm', "RPM distribution"), | |
82 | 'gztar': ('bdist_dumb', "gzip'ed tar file"), | |
83 | 'bztar': ('bdist_dumb', "bzip2'ed tar file"), | |
84 | 'xztar': ('bdist_dumb', "xz'ed tar file"), | |
85 | 'ztar': ('bdist_dumb', "compressed tar file"), | |
86 | 'tar': ('bdist_dumb', "tar file"), | |
87 | 'zip': ('bdist_dumb', "ZIP file"), | |
88 | } | |
89 | ) | |
90 | ||
91 | # for compatibility until consumers only reference format_commands | |
92 | format_command = format_commands | |
93 | ||
94 | def initialize_options(self): | |
95 | self.bdist_base = None | |
96 | self.plat_name = None | |
97 | self.formats = None | |
98 | self.dist_dir = None | |
99 | self.skip_build = 0 | |
100 | self.group = None | |
101 | self.owner = None | |
102 | ||
103 | def finalize_options(self): | |
104 | # have to finalize 'plat_name' before 'bdist_base' | |
105 | if self.plat_name is None: | |
106 | if self.skip_build: | |
107 | self.plat_name = get_platform() | |
108 | else: | |
109 | self.plat_name = self.get_finalized_command('build').plat_name | |
110 | ||
111 | # 'bdist_base' -- parent of per-built-distribution-format | |
112 | # temporary directories (eg. we'll probably have | |
113 | # "build/bdist.<plat>/dumb", "build/bdist.<plat>/rpm", etc.) | |
114 | if self.bdist_base is None: | |
115 | build_base = self.get_finalized_command('build').build_base | |
116 | self.bdist_base = os.path.join(build_base, 'bdist.' + self.plat_name) | |
117 | ||
118 | self.ensure_string_list('formats') | |
119 | if self.formats is None: | |
120 | try: | |
121 | self.formats = [self.default_format[os.name]] | |
122 | except KeyError: | |
123 | raise DistutilsPlatformError( | |
124 | "don't know how to create built distributions " | |
125 | "on platform %s" % os.name | |
126 | ) | |
127 | ||
128 | if self.dist_dir is None: | |
129 | self.dist_dir = "dist" | |
130 | ||
131 | def run(self): | |
132 | # Figure out which sub-commands we need to run. | |
133 | commands = [] | |
134 | for format in self.formats: | |
135 | try: | |
136 | commands.append(self.format_commands[format][0]) | |
137 | except KeyError: | |
138 | raise DistutilsOptionError("invalid format '%s'" % format) | |
139 | ||
140 | # Reinitialize and run each command. | |
141 | for i in range(len(self.formats)): | |
142 | cmd_name = commands[i] | |
143 | sub_cmd = self.reinitialize_command(cmd_name) | |
144 | if cmd_name not in self.no_format_option: | |
145 | sub_cmd.format = self.formats[i] | |
146 | ||
147 | # passing the owner and group names for tar archiving | |
148 | if cmd_name == 'bdist_dumb': | |
149 | sub_cmd.owner = self.owner | |
150 | sub_cmd.group = self.group | |
151 | ||
152 | # If we're going to need to run this command again, tell it to | |
153 | # keep its temporary files around so subsequent runs go faster. | |
154 | if cmd_name in commands[i + 1 :]: | |
155 | sub_cmd.keep_temp = 1 | |
156 | self.run_command(cmd_name) |