1.. _built-dist: 2 3**************************** 4Creating Built Distributions 5**************************** 6 7.. include:: ./_setuptools_disclaimer.rst 8 9A "built distribution" is what you're probably used to thinking of either as a 10"binary package" or an "installer" (depending on your background). It's not 11necessarily binary, though, because it might contain only Python source code 12and/or byte-code; and we don't call it a package, because that word is already 13spoken for in Python. (And "installer" is a term specific to the world of 14mainstream desktop systems.) 15 16A built distribution is how you make life as easy as possible for installers of 17your module distribution: for users of RPM-based Linux systems, it's a binary 18RPM; for Windows users, it's an executable installer; for Debian-based Linux 19users, it's a Debian package; and so forth. Obviously, no one person will be 20able to create built distributions for every platform under the sun, so the 21Distutils are designed to enable module developers to concentrate on their 22specialty---writing code and creating source distributions---while an 23intermediary species called *packagers* springs up to turn source distributions 24into built distributions for as many platforms as there are packagers. 25 26Of course, the module developer could be their own packager; or the packager could 27be a volunteer "out there" somewhere who has access to a platform which the 28original developer does not; or it could be software periodically grabbing new 29source distributions and turning them into built distributions for as many 30platforms as the software has access to. Regardless of who they are, a packager 31uses the setup script and the :command:`bdist` command family to generate built 32distributions. 33 34As a simple example, if I run the following command in the Distutils source 35tree:: 36 37 python setup.py bdist 38 39then the Distutils builds my module distribution (the Distutils itself in this 40case), does a "fake" installation (also in the :file:`build` directory), and 41creates the default type of built distribution for my platform. The default 42format for built distributions is a "dumb" tar file on Unix, and a simple 43executable installer on Windows. (That tar file is considered "dumb" because it 44has to be unpacked in a specific location to work.) 45 46Thus, the above command on a Unix system creates 47:file:`Distutils-1.0.{plat}.tar.gz`; unpacking this tarball from the right place 48installs the Distutils just as though you had downloaded the source distribution 49and run ``python setup.py install``. (The "right place" is either the root of 50the filesystem or Python's :file:`{prefix}` directory, depending on the options 51given to the :command:`bdist_dumb` command; the default is to make dumb 52distributions relative to :file:`{prefix}`.) 53 54Obviously, for pure Python distributions, this isn't any simpler than just 55running ``python setup.py install``\ ---but for non-pure distributions, which 56include extensions that would need to be compiled, it can mean the difference 57between someone being able to use your extensions or not. And creating "smart" 58built distributions, such as an RPM package or an executable installer for 59Windows, is far more convenient for users even if your distribution doesn't 60include any extensions. 61 62The :command:`bdist` command has a :option:`!--formats` option, similar to the 63:command:`sdist` command, which you can use to select the types of built 64distribution to generate: for example, :: 65 66 python setup.py bdist --format=zip 67 68would, when run on a Unix system, create 69:file:`Distutils-1.0.{plat}.zip`\ ---again, this archive would be unpacked 70from the root directory to install the Distutils. 71 72The available formats for built distributions are: 73 74+-------------+------------------------------+---------+ 75| Format | Description | Notes | 76+=============+==============================+=========+ 77| ``gztar`` | gzipped tar file | \(1) | 78| | (:file:`.tar.gz`) | | 79+-------------+------------------------------+---------+ 80| ``bztar`` | bzipped tar file | | 81| | (:file:`.tar.bz2`) | | 82+-------------+------------------------------+---------+ 83| ``xztar`` | xzipped tar file | | 84| | (:file:`.tar.xz`) | | 85+-------------+------------------------------+---------+ 86| ``ztar`` | compressed tar file | \(3) | 87| | (:file:`.tar.Z`) | | 88+-------------+------------------------------+---------+ 89| ``tar`` | tar file (:file:`.tar`) | | 90+-------------+------------------------------+---------+ 91| ``zip`` | zip file (:file:`.zip`) | (2),(4) | 92+-------------+------------------------------+---------+ 93| ``rpm`` | RPM | \(5) | 94+-------------+------------------------------+---------+ 95| ``pkgtool`` | Solaris :program:`pkgtool` | | 96+-------------+------------------------------+---------+ 97| ``sdux`` | HP-UX :program:`swinstall` | | 98+-------------+------------------------------+---------+ 99| ``msi`` | Microsoft Installer. | | 100+-------------+------------------------------+---------+ 101 102.. versionchanged:: 3.5 103 Added support for the ``xztar`` format. 104 105 106Notes: 107 108(1) 109 default on Unix 110 111(2) 112 default on Windows 113 114(3) 115 requires external :program:`compress` utility. 116 117(4) 118 requires either external :program:`zip` utility or :mod:`zipfile` module (part 119 of the standard Python library since Python 1.6) 120 121(5) 122 requires external :program:`rpm` utility, version 3.0.4 or better (use ``rpm 123 --version`` to find out which version you have) 124 125You don't have to use the :command:`bdist` command with the :option:`!--formats` 126option; you can also use the command that directly implements the format you're 127interested in. Some of these :command:`bdist` "sub-commands" actually generate 128several similar formats; for instance, the :command:`bdist_dumb` command 129generates all the "dumb" archive formats (``tar``, ``gztar``, ``bztar``, 130``xztar``, ``ztar``, and ``zip``), and :command:`bdist_rpm` generates both 131binary and source RPMs. The :command:`bdist` sub-commands, and the formats 132generated by each, are: 133 134+--------------------------+-------------------------------------+ 135| Command | Formats | 136+==========================+=====================================+ 137| :command:`bdist_dumb` | tar, gztar, bztar, xztar, ztar, zip | 138+--------------------------+-------------------------------------+ 139| :command:`bdist_rpm` | rpm, srpm | 140+--------------------------+-------------------------------------+ 141 142The following sections give details on the individual :command:`bdist_\*` 143commands. 144 145 146.. .. _creating-dumb: 147 148.. Creating dumb built distributions 149.. ================================= 150 151.. XXX Need to document absolute vs. prefix-relative packages here, but first 152 I have to implement it! 153 154 155.. _creating-rpms: 156 157Creating RPM packages 158===================== 159 160The RPM format is used by many popular Linux distributions, including Red Hat, 161SuSE, and Mandrake. If one of these (or any of the other RPM-based Linux 162distributions) is your usual environment, creating RPM packages for other users 163of that same distribution is trivial. Depending on the complexity of your module 164distribution and differences between Linux distributions, you may also be able 165to create RPMs that work on different RPM-based distributions. 166 167The usual way to create an RPM of your module distribution is to run the 168:command:`bdist_rpm` command:: 169 170 python setup.py bdist_rpm 171 172or the :command:`bdist` command with the :option:`!--format` option:: 173 174 python setup.py bdist --formats=rpm 175 176The former allows you to specify RPM-specific options; the latter allows you to 177easily specify multiple formats in one run. If you need to do both, you can 178explicitly specify multiple :command:`bdist_\*` commands and their options:: 179 180 python setup.py bdist_rpm --packager="John Doe <[email protected]>" 181 182Creating RPM packages is driven by a :file:`.spec` file, much as using the 183Distutils is driven by the setup script. To make your life easier, the 184:command:`bdist_rpm` command normally creates a :file:`.spec` file based on the 185information you supply in the setup script, on the command line, and in any 186Distutils configuration files. Various options and sections in the 187:file:`.spec` file are derived from options in the setup script as follows: 188 189+------------------------------------------+----------------------------------------------+ 190| RPM :file:`.spec` file option or section | Distutils setup script option | 191+==========================================+==============================================+ 192| Name | ``name`` | 193+------------------------------------------+----------------------------------------------+ 194| Summary (in preamble) | ``description`` | 195+------------------------------------------+----------------------------------------------+ 196| Version | ``version`` | 197+------------------------------------------+----------------------------------------------+ 198| Vendor | ``author`` and ``author_email``, | 199| | or --- & ``maintainer`` and | 200| | ``maintainer_email`` | 201+------------------------------------------+----------------------------------------------+ 202| Copyright | ``license`` | 203+------------------------------------------+----------------------------------------------+ 204| Url | ``url`` | 205+------------------------------------------+----------------------------------------------+ 206| %description (section) | ``long_description`` | 207+------------------------------------------+----------------------------------------------+ 208 209Additionally, there are many options in :file:`.spec` files that don't have 210corresponding options in the setup script. Most of these are handled through 211options to the :command:`bdist_rpm` command as follows: 212 213+-------------------------------+-----------------------------+-------------------------+ 214| RPM :file:`.spec` file option | :command:`bdist_rpm` option | default value | 215| or section | | | 216+===============================+=============================+=========================+ 217| Release | ``release`` | "1" | 218+-------------------------------+-----------------------------+-------------------------+ 219| Group | ``group`` | "Development/Libraries" | 220+-------------------------------+-----------------------------+-------------------------+ 221| Vendor | ``vendor`` | (see above) | 222+-------------------------------+-----------------------------+-------------------------+ 223| Packager | ``packager`` | (none) | 224+-------------------------------+-----------------------------+-------------------------+ 225| Provides | ``provides`` | (none) | 226+-------------------------------+-----------------------------+-------------------------+ 227| Requires | ``requires`` | (none) | 228+-------------------------------+-----------------------------+-------------------------+ 229| Conflicts | ``conflicts`` | (none) | 230+-------------------------------+-----------------------------+-------------------------+ 231| Obsoletes | ``obsoletes`` | (none) | 232+-------------------------------+-----------------------------+-------------------------+ 233| Distribution | ``distribution_name`` | (none) | 234+-------------------------------+-----------------------------+-------------------------+ 235| BuildRequires | ``build_requires`` | (none) | 236+-------------------------------+-----------------------------+-------------------------+ 237| Icon | ``icon`` | (none) | 238+-------------------------------+-----------------------------+-------------------------+ 239 240Obviously, supplying even a few of these options on the command-line would be 241tedious and error-prone, so it's usually best to put them in the setup 242configuration file, :file:`setup.cfg`\ ---see section :ref:`setup-config`. If 243you distribute or package many Python module distributions, you might want to 244put options that apply to all of them in your personal Distutils configuration 245file (:file:`~/.pydistutils.cfg`). If you want to temporarily disable 246this file, you can pass the :option:`!--no-user-cfg` option to :file:`setup.py`. 247 248There are three steps to building a binary RPM package, all of which are 249handled automatically by the Distutils: 250 251#. create a :file:`.spec` file, which describes the package (analogous to the 252 Distutils setup script; in fact, much of the information in the setup script 253 winds up in the :file:`.spec` file) 254 255#. create the source RPM 256 257#. create the "binary" RPM (which may or may not contain binary code, depending 258 on whether your module distribution contains Python extensions) 259 260Normally, RPM bundles the last two steps together; when you use the Distutils, 261all three steps are typically bundled together. 262 263If you wish, you can separate these three steps. You can use the 264:option:`!--spec-only` option to make :command:`bdist_rpm` just create the 265:file:`.spec` file and exit; in this case, the :file:`.spec` file will be 266written to the "distribution directory"---normally :file:`dist/`, but 267customizable with the :option:`!--dist-dir` option. (Normally, the :file:`.spec` 268file winds up deep in the "build tree," in a temporary directory created by 269:command:`bdist_rpm`.) 270 271.. % \XXX{this isn't implemented yet---is it needed?!} 272.. % You can also specify a custom \file{.spec} file with the 273.. % \longprogramopt{spec-file} option; used in conjunction with 274.. % \longprogramopt{spec-only}, this gives you an opportunity to customize 275.. % the \file{.spec} file manually: 276.. % 277.. % \ begin{verbatim} 278.. % > python setup.py bdist_rpm --spec-only 279.. % # ...edit dist/FooBar-1.0.spec 280.. % > python setup.py bdist_rpm --spec-file=dist/FooBar-1.0.spec 281.. % \ end{verbatim} 282.. % 283.. % (Although a better way to do this is probably to override the standard 284.. % \command{bdist\_rpm} command with one that writes whatever else you want 285.. % to the \file{.spec} file.) 286 287 288.. _cross-compile-windows: 289 290Cross-compiling on Windows 291========================== 292 293Starting with Python 2.6, distutils is capable of cross-compiling between 294Windows platforms. In practice, this means that with the correct tools 295installed, you can use a 32bit version of Windows to create 64bit extensions 296and vice-versa. 297 298To build for an alternate platform, specify the :option:`!--plat-name` option 299to the build command. Valid values are currently 'win32', and 'win-amd64'. 300For example, on a 32bit version of Windows, you could execute:: 301 302 python setup.py build --plat-name=win-amd64 303 304to build a 64bit version of your extension. 305 306would create a 64bit installation executable on your 32bit version of Windows. 307 308To cross-compile, you must download the Python source code and cross-compile 309Python itself for the platform you are targeting - it is not possible from a 310binary installation of Python (as the .lib etc file for other platforms are 311not included.) In practice, this means the user of a 32 bit operating 312system will need to use Visual Studio 2008 to open the 313:file:`PCbuild/PCbuild.sln` solution in the Python source tree and build the 314"x64" configuration of the 'pythoncore' project before cross-compiling 315extensions is possible. 316 317Note that by default, Visual Studio 2008 does not install 64bit compilers or 318tools. You may need to reexecute the Visual Studio setup process and select 319these tools (using Control Panel->[Add/Remove] Programs is a convenient way to 320check or modify your existing install.) 321 322.. _postinstallation-script: 323 324The Postinstallation script 325--------------------------- 326 327Starting with Python 2.3, a postinstallation script can be specified with the 328:option:`!--install-script` option. The basename of the script must be 329specified, and the script filename must also be listed in the scripts argument 330to the setup function. 331 332This script will be run at installation time on the target system after all the 333files have been copied, with ``argv[1]`` set to :option:`!-install`, and again at 334uninstallation time before the files are removed with ``argv[1]`` set to 335:option:`!-remove`. 336 337The installation script runs embedded in the windows installer, every output 338(``sys.stdout``, ``sys.stderr``) is redirected into a buffer and will be 339displayed in the GUI after the script has finished. 340 341Some functions especially useful in this context are available as additional 342built-in functions in the installation script. 343 344 345.. function:: directory_created(path) 346 file_created(path) 347 348 These functions should be called when a directory or file is created by the 349 postinstall script at installation time. It will register *path* with the 350 uninstaller, so that it will be removed when the distribution is uninstalled. 351 To be safe, directories are only removed if they are empty. 352 353 354.. function:: get_special_folder_path(csidl_string) 355 356 This function can be used to retrieve special folder locations on Windows like 357 the Start Menu or the Desktop. It returns the full path to the folder. 358 *csidl_string* must be one of the following strings:: 359 360 "CSIDL_APPDATA" 361 362 "CSIDL_COMMON_STARTMENU" 363 "CSIDL_STARTMENU" 364 365 "CSIDL_COMMON_DESKTOPDIRECTORY" 366 "CSIDL_DESKTOPDIRECTORY" 367 368 "CSIDL_COMMON_STARTUP" 369 "CSIDL_STARTUP" 370 371 "CSIDL_COMMON_PROGRAMS" 372 "CSIDL_PROGRAMS" 373 374 "CSIDL_FONTS" 375 376 If the folder cannot be retrieved, :exc:`OSError` is raised. 377 378 Which folders are available depends on the exact Windows version, and probably 379 also the configuration. For details refer to Microsoft's documentation of the 380 :c:func:`SHGetSpecialFolderPath` function. 381 382 383.. function:: create_shortcut(target, description, filename[, arguments[, workdir[, iconpath[, iconindex]]]]) 384 385 This function creates a shortcut. *target* is the path to the program to be 386 started by the shortcut. *description* is the description of the shortcut. 387 *filename* is the title of the shortcut that the user will see. *arguments* 388 specifies the command line arguments, if any. *workdir* is the working directory 389 for the program. *iconpath* is the file containing the icon for the shortcut, 390 and *iconindex* is the index of the icon in the file *iconpath*. Again, for 391 details consult the Microsoft documentation for the :class:`IShellLink` 392 interface. 393