blob: 4f086c64fbd1cfd817053f8ab89fb6b9c0cc5e5e [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001.. _built-dist:
2
3****************************
4Creating Built Distributions
5****************************
6
7A "built distribution" is what you're probably used to thinking of either as a
8"binary package" or an "installer" (depending on your background). It's not
9necessarily binary, though, because it might contain only Python source code
10and/or byte-code; and we don't call it a package, because that word is already
11spoken for in Python. (And "installer" is a term specific to the world of
12mainstream desktop systems.)
13
14A built distribution is how you make life as easy as possible for installers of
15your module distribution: for users of RPM-based Linux systems, it's a binary
16RPM; for Windows users, it's an executable installer; for Debian-based Linux
17users, it's a Debian package; and so forth. Obviously, no one person will be
18able to create built distributions for every platform under the sun, so the
19Distutils are designed to enable module developers to concentrate on their
20specialty---writing code and creating source distributions---while an
21intermediary species called *packagers* springs up to turn source distributions
22into built distributions for as many platforms as there are packagers.
23
24Of course, the module developer could be his own packager; or the packager could
25be a volunteer "out there" somewhere who has access to a platform which the
26original developer does not; or it could be software periodically grabbing new
27source distributions and turning them into built distributions for as many
28platforms as the software has access to. Regardless of who they are, a packager
29uses the setup script and the :command:`bdist` command family to generate built
30distributions.
31
32As a simple example, if I run the following command in the Distutils source
33tree::
34
35 python setup.py bdist
36
37then the Distutils builds my module distribution (the Distutils itself in this
38case), does a "fake" installation (also in the :file:`build` directory), and
39creates the default type of built distribution for my platform. The default
40format for built distributions is a "dumb" tar file on Unix, and a simple
41executable installer on Windows. (That tar file is considered "dumb" because it
42has to be unpacked in a specific location to work.)
43
44Thus, the above command on a Unix system creates
45:file:`Distutils-1.0.{plat}.tar.gz`; unpacking this tarball from the right place
46installs the Distutils just as though you had downloaded the source distribution
47and run ``python setup.py install``. (The "right place" is either the root of
48the filesystem or Python's :file:`{prefix}` directory, depending on the options
49given to the :command:`bdist_dumb` command; the default is to make dumb
50distributions relative to :file:`{prefix}`.)
51
52Obviously, for pure Python distributions, this isn't any simpler than just
53running ``python setup.py install``\ ---but for non-pure distributions, which
54include extensions that would need to be compiled, it can mean the difference
55between someone being able to use your extensions or not. And creating "smart"
56built distributions, such as an RPM package or an executable installer for
57Windows, is far more convenient for users even if your distribution doesn't
58include any extensions.
59
60The :command:`bdist` command has a :option:`--formats` option, similar to the
61:command:`sdist` command, which you can use to select the types of built
62distribution to generate: for example, ::
63
64 python setup.py bdist --format=zip
65
66would, when run on a Unix system, create :file:`Distutils-1.0.{plat}.zip`\
67---again, this archive would be unpacked from the root directory to install the
68Distutils.
69
70The available formats for built distributions are:
71
72+-------------+------------------------------+---------+
73| Format | Description | Notes |
74+=============+==============================+=========+
75| ``gztar`` | gzipped tar file | (1),(3) |
76| | (:file:`.tar.gz`) | |
77+-------------+------------------------------+---------+
78| ``ztar`` | compressed tar file | \(3) |
79| | (:file:`.tar.Z`) | |
80+-------------+------------------------------+---------+
81| ``tar`` | tar file (:file:`.tar`) | \(3) |
82+-------------+------------------------------+---------+
Tarek Ziadéf6370502009-04-05 22:57:21 +000083| ``zip`` | zip file (:file:`.zip`) | (2),(4) |
Georg Brandl116aa622007-08-15 14:28:22 +000084+-------------+------------------------------+---------+
85| ``rpm`` | RPM | \(5) |
86+-------------+------------------------------+---------+
87| ``pkgtool`` | Solaris :program:`pkgtool` | |
88+-------------+------------------------------+---------+
89| ``sdux`` | HP-UX :program:`swinstall` | |
90+-------------+------------------------------+---------+
91| ``rpm`` | RPM | \(5) |
92+-------------+------------------------------+---------+
Tarek Ziadéf6370502009-04-05 22:57:21 +000093| ``wininst`` | self-extracting ZIP file for | \(4) |
Georg Brandl116aa622007-08-15 14:28:22 +000094| | Windows | |
95+-------------+------------------------------+---------+
Tarek Ziadéf6370502009-04-05 22:57:21 +000096| ``msi`` | Microsoft Installer. | |
97+-------------+------------------------------+---------+
98
Georg Brandl116aa622007-08-15 14:28:22 +000099
100Notes:
101
102(1)
103 default on Unix
104
105(2)
106 default on Windows
107
Georg Brandl116aa622007-08-15 14:28:22 +0000108(3)
109 requires external utilities: :program:`tar` and possibly one of :program:`gzip`,
110 :program:`bzip2`, or :program:`compress`
111
112(4)
113 requires either external :program:`zip` utility or :mod:`zipfile` module (part
114 of the standard Python library since Python 1.6)
115
116(5)
117 requires external :program:`rpm` utility, version 3.0.4 or better (use ``rpm
118 --version`` to find out which version you have)
119
120You don't have to use the :command:`bdist` command with the :option:`--formats`
121option; you can also use the command that directly implements the format you're
122interested in. Some of these :command:`bdist` "sub-commands" actually generate
123several similar formats; for instance, the :command:`bdist_dumb` command
124generates all the "dumb" archive formats (``tar``, ``ztar``, ``gztar``, and
125``zip``), and :command:`bdist_rpm` generates both binary and source RPMs. The
126:command:`bdist` sub-commands, and the formats generated by each, are:
127
128+--------------------------+-----------------------+
129| Command | Formats |
130+==========================+=======================+
131| :command:`bdist_dumb` | tar, ztar, gztar, zip |
132+--------------------------+-----------------------+
133| :command:`bdist_rpm` | rpm, srpm |
134+--------------------------+-----------------------+
135| :command:`bdist_wininst` | wininst |
136+--------------------------+-----------------------+
Tarek Ziadéf6370502009-04-05 22:57:21 +0000137| :command:`bdist_msi` | msi |
138+--------------------------+-----------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000139
140The following sections give details on the individual :command:`bdist_\*`
141commands.
142
143
144.. _creating-dumb:
145
146Creating dumb built distributions
147=================================
148
Georg Brandl914a2182010-10-06 08:13:26 +0000149.. XXX Need to document absolute vs. prefix-relative packages here, but first
150 I have to implement it!
Georg Brandl116aa622007-08-15 14:28:22 +0000151
152
153.. _creating-rpms:
154
155Creating RPM packages
156=====================
157
158The RPM format is used by many popular Linux distributions, including Red Hat,
159SuSE, and Mandrake. If one of these (or any of the other RPM-based Linux
160distributions) is your usual environment, creating RPM packages for other users
161of that same distribution is trivial. Depending on the complexity of your module
162distribution and differences between Linux distributions, you may also be able
163to create RPMs that work on different RPM-based distributions.
164
165The usual way to create an RPM of your module distribution is to run the
166:command:`bdist_rpm` command::
167
168 python setup.py bdist_rpm
169
170or the :command:`bdist` command with the :option:`--format` option::
171
172 python setup.py bdist --formats=rpm
173
174The former allows you to specify RPM-specific options; the latter allows you to
175easily specify multiple formats in one run. If you need to do both, you can
176explicitly specify multiple :command:`bdist_\*` commands and their options::
177
178 python setup.py bdist_rpm --packager="John Doe <jdoe@example.org>" \
179 bdist_wininst --target_version="2.0"
180
181Creating RPM packages is driven by a :file:`.spec` file, much as using the
182Distutils is driven by the setup script. To make your life easier, the
183:command:`bdist_rpm` command normally creates a :file:`.spec` file based on the
184information you supply in the setup script, on the command line, and in any
185Distutils configuration files. Various options and sections in the
186:file:`.spec` file are derived from options in the setup script as follows:
187
188+------------------------------------------+----------------------------------------------+
189| RPM :file:`.spec` file option or section | Distutils setup script option |
190+==========================================+==============================================+
191| Name | :option:`name` |
192+------------------------------------------+----------------------------------------------+
193| Summary (in preamble) | :option:`description` |
194+------------------------------------------+----------------------------------------------+
195| Version | :option:`version` |
196+------------------------------------------+----------------------------------------------+
197| Vendor | :option:`author` and :option:`author_email`, |
198| | or --- & :option:`maintainer` and |
199| | :option:`maintainer_email` |
200+------------------------------------------+----------------------------------------------+
Christian Heimesc3f30c42008-02-22 16:37:40 +0000201| Copyright | :option:`license` |
Georg Brandl116aa622007-08-15 14:28:22 +0000202+------------------------------------------+----------------------------------------------+
203| Url | :option:`url` |
204+------------------------------------------+----------------------------------------------+
205| %description (section) | :option:`long_description` |
206+------------------------------------------+----------------------------------------------+
207
208Additionally, there are many options in :file:`.spec` files that don't have
209corresponding options in the setup script. Most of these are handled through
210options to the :command:`bdist_rpm` command as follows:
211
212+-------------------------------+-----------------------------+-------------------------+
213| RPM :file:`.spec` file option | :command:`bdist_rpm` option | default value |
214| or section | | |
215+===============================+=============================+=========================+
216| Release | :option:`release` | "1" |
217+-------------------------------+-----------------------------+-------------------------+
218| Group | :option:`group` | "Development/Libraries" |
219+-------------------------------+-----------------------------+-------------------------+
220| Vendor | :option:`vendor` | (see above) |
221+-------------------------------+-----------------------------+-------------------------+
222| Packager | :option:`packager` | (none) |
223+-------------------------------+-----------------------------+-------------------------+
224| Provides | :option:`provides` | (none) |
225+-------------------------------+-----------------------------+-------------------------+
226| Requires | :option:`requires` | (none) |
227+-------------------------------+-----------------------------+-------------------------+
228| Conflicts | :option:`conflicts` | (none) |
229+-------------------------------+-----------------------------+-------------------------+
230| Obsoletes | :option:`obsoletes` | (none) |
231+-------------------------------+-----------------------------+-------------------------+
232| Distribution | :option:`distribution_name` | (none) |
233+-------------------------------+-----------------------------+-------------------------+
234| BuildRequires | :option:`build_requires` | (none) |
235+-------------------------------+-----------------------------+-------------------------+
236| Icon | :option:`icon` | (none) |
237+-------------------------------+-----------------------------+-------------------------+
238
239Obviously, supplying even a few of these options on the command-line would be
240tedious and error-prone, so it's usually best to put them in the setup
241configuration file, :file:`setup.cfg`\ ---see section :ref:`setup-config`. If
242you distribute or package many Python module distributions, you might want to
243put options that apply to all of them in your personal Distutils configuration
244file (:file:`~/.pydistutils.cfg`).
245
246There are three steps to building a binary RPM package, all of which are
247handled automatically by the Distutils:
248
249#. create a :file:`.spec` file, which describes the package (analogous to the
250 Distutils setup script; in fact, much of the information in the setup script
251 winds up in the :file:`.spec` file)
252
253#. create the source RPM
254
255#. create the "binary" RPM (which may or may not contain binary code, depending
256 on whether your module distribution contains Python extensions)
257
258Normally, RPM bundles the last two steps together; when you use the Distutils,
259all three steps are typically bundled together.
260
261If you wish, you can separate these three steps. You can use the
262:option:`--spec-only` option to make :command:`bdist_rpm` just create the
263:file:`.spec` file and exit; in this case, the :file:`.spec` file will be
264written to the "distribution directory"---normally :file:`dist/`, but
265customizable with the :option:`--dist-dir` option. (Normally, the :file:`.spec`
266file winds up deep in the "build tree," in a temporary directory created by
267:command:`bdist_rpm`.)
268
269.. % \XXX{this isn't implemented yet---is it needed?!}
270.. % You can also specify a custom \file{.spec} file with the
271.. % \longprogramopt{spec-file} option; used in conjunction with
272.. % \longprogramopt{spec-only}, this gives you an opportunity to customize
273.. % the \file{.spec} file manually:
Georg Brandl48310cd2009-01-03 21:18:54 +0000274.. %
Georg Brandl116aa622007-08-15 14:28:22 +0000275.. % \ begin{verbatim}
276.. % > python setup.py bdist_rpm --spec-only
277.. % # ...edit dist/FooBar-1.0.spec
278.. % > python setup.py bdist_rpm --spec-file=dist/FooBar-1.0.spec
279.. % \ end{verbatim}
Georg Brandl48310cd2009-01-03 21:18:54 +0000280.. %
Georg Brandl116aa622007-08-15 14:28:22 +0000281.. % (Although a better way to do this is probably to override the standard
282.. % \command{bdist\_rpm} command with one that writes whatever else you want
283.. % to the \file{.spec} file.)
284
285
286.. _creating-wininst:
287
288Creating Windows Installers
289===========================
290
291Executable installers are the natural format for binary distributions on
292Windows. They display a nice graphical user interface, display some information
293about the module distribution to be installed taken from the metadata in the
294setup script, let the user select a few options, and start or cancel the
295installation.
296
297Since the metadata is taken from the setup script, creating Windows installers
298is usually as easy as running::
299
300 python setup.py bdist_wininst
301
302or the :command:`bdist` command with the :option:`--formats` option::
303
304 python setup.py bdist --formats=wininst
305
306If you have a pure module distribution (only containing pure Python modules and
307packages), the resulting installer will be version independent and have a name
Georg Brandlc575c902008-09-13 17:46:05 +0000308like :file:`foo-1.0.win32.exe`. These installers can even be created on Unix
309platforms or Mac OS X.
Georg Brandl116aa622007-08-15 14:28:22 +0000310
311If you have a non-pure distribution, the extensions can only be created on a
312Windows platform, and will be Python version dependent. The installer filename
313will reflect this and now has the form :file:`foo-1.0.win32-py2.0.exe`. You
314have to create a separate installer for every Python version you want to
315support.
316
Georg Brandl9afde1c2007-11-01 20:32:30 +0000317The installer will try to compile pure modules into :term:`bytecode` after installation
Georg Brandl116aa622007-08-15 14:28:22 +0000318on the target system in normal and optimizing mode. If you don't want this to
319happen for some reason, you can run the :command:`bdist_wininst` command with
320the :option:`--no-target-compile` and/or the :option:`--no-target-optimize`
321option.
322
323By default the installer will display the cool "Python Powered" logo when it is
Georg Brandlc62efa82010-07-11 10:41:07 +0000324run, but you can also supply your own 152x261 bitmap which must be a Windows
Georg Brandl116aa622007-08-15 14:28:22 +0000325:file:`.bmp` file with the :option:`--bitmap` option.
326
327The installer will also display a large title on the desktop background window
328when it is run, which is constructed from the name of your distribution and the
329version number. This can be changed to another text by using the
330:option:`--title` option.
331
332The installer file will be written to the "distribution directory" --- normally
333:file:`dist/`, but customizable with the :option:`--dist-dir` option.
334
Christian Heimes5e696852008-04-09 08:37:03 +0000335.. _cross-compile-windows:
336
337Cross-compiling on Windows
338==========================
339
Georg Brandl48310cd2009-01-03 21:18:54 +0000340Starting with Python 2.6, distutils is capable of cross-compiling between
341Windows platforms. In practice, this means that with the correct tools
Christian Heimes5e696852008-04-09 08:37:03 +0000342installed, you can use a 32bit version of Windows to create 64bit extensions
343and vice-versa.
344
Georg Brandl48310cd2009-01-03 21:18:54 +0000345To build for an alternate platform, specify the :option:`--plat-name` option
346to the build command. Valid values are currently 'win32', 'win-amd64' and
Christian Heimes5e696852008-04-09 08:37:03 +0000347'win-ia64'. For example, on a 32bit version of Windows, you could execute::
348
349 python setup.py build --plat-name=win-amd64
350
Georg Brandl48310cd2009-01-03 21:18:54 +0000351to build a 64bit version of your extension. The Windows Installers also
Christian Heimes5e696852008-04-09 08:37:03 +0000352support this option, so the command::
353
354 python setup.py build --plat-name=win-amd64 bdist_wininst
355
356would create a 64bit installation executable on your 32bit version of Windows.
357
Georg Brandl48310cd2009-01-03 21:18:54 +0000358To cross-compile, you must download the Python source code and cross-compile
Christian Heimes5e696852008-04-09 08:37:03 +0000359Python itself for the platform you are targetting - it is not possible from a
360binary installtion of Python (as the .lib etc file for other platforms are
Georg Brandl48310cd2009-01-03 21:18:54 +0000361not included.) In practice, this means the user of a 32 bit operating
362system will need to use Visual Studio 2008 to open the
363:file:`PCBuild/PCbuild.sln` solution in the Python source tree and build the
364"x64" configuration of the 'pythoncore' project before cross-compiling
Christian Heimes5e696852008-04-09 08:37:03 +0000365extensions is possible.
366
367Note that by default, Visual Studio 2008 does not install 64bit compilers or
368tools. You may need to reexecute the Visual Studio setup process and select
369these tools (using Control Panel->[Add/Remove] Programs is a convenient way to
370check or modify your existing install.)
Georg Brandl116aa622007-08-15 14:28:22 +0000371
372.. _postinstallation-script:
373
374The Postinstallation script
375---------------------------
376
Georg Brandlc62efa82010-07-11 10:41:07 +0000377Starting with Python 2.3, a postinstallation script can be specified with the
Georg Brandl116aa622007-08-15 14:28:22 +0000378:option:`--install-script` option. The basename of the script must be
379specified, and the script filename must also be listed in the scripts argument
380to the setup function.
381
382This script will be run at installation time on the target system after all the
383files have been copied, with ``argv[1]`` set to :option:`-install`, and again at
384uninstallation time before the files are removed with ``argv[1]`` set to
385:option:`-remove`.
386
387The installation script runs embedded in the windows installer, every output
388(``sys.stdout``, ``sys.stderr``) is redirected into a buffer and will be
389displayed in the GUI after the script has finished.
390
391Some functions especially useful in this context are available as additional
392built-in functions in the installation script.
393
394
395.. function:: directory_created(path)
396 file_created(path)
397
398 These functions should be called when a directory or file is created by the
399 postinstall script at installation time. It will register *path* with the
400 uninstaller, so that it will be removed when the distribution is uninstalled.
401 To be safe, directories are only removed if they are empty.
402
403
404.. function:: get_special_folder_path(csidl_string)
405
406 This function can be used to retrieve special folder locations on Windows like
407 the Start Menu or the Desktop. It returns the full path to the folder.
408 *csidl_string* must be one of the following strings::
409
410 "CSIDL_APPDATA"
411
412 "CSIDL_COMMON_STARTMENU"
413 "CSIDL_STARTMENU"
414
415 "CSIDL_COMMON_DESKTOPDIRECTORY"
416 "CSIDL_DESKTOPDIRECTORY"
417
418 "CSIDL_COMMON_STARTUP"
419 "CSIDL_STARTUP"
420
421 "CSIDL_COMMON_PROGRAMS"
422 "CSIDL_PROGRAMS"
423
424 "CSIDL_FONTS"
425
426 If the folder cannot be retrieved, :exc:`OSError` is raised.
427
428 Which folders are available depends on the exact Windows version, and probably
429 also the configuration. For details refer to Microsoft's documentation of the
430 :cfunc:`SHGetSpecialFolderPath` function.
431
432
433.. function:: create_shortcut(target, description, filename[, arguments[, workdir[, iconpath[, iconindex]]]])
434
435 This function creates a shortcut. *target* is the path to the program to be
436 started by the shortcut. *description* is the description of the shortcut.
437 *filename* is the title of the shortcut that the user will see. *arguments*
438 specifies the command line arguments, if any. *workdir* is the working directory
439 for the program. *iconpath* is the file containing the icon for the shortcut,
440 and *iconindex* is the index of the icon in the file *iconpath*. Again, for
441 details consult the Microsoft documentation for the :class:`IShellLink`
442 interface.
Benjamin Petersona8332062009-09-11 22:36:27 +0000443
444
445Vista User Access Control (UAC)
446===============================
447
448Starting with Python 2.6, bdist_wininst supports a :option:`--user-access-control`
449option. The default is 'none' (meaning no UAC handling is done), and other
450valid values are 'auto' (meaning prompt for UAC elevation if Python was
451installed for all users) and 'force' (meaning always prompt for elevation).