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