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