/Doc/distutils/builtdist.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 446 lines · 348 code · 98 blank · 0 comment · 0 complexity · 8b9e551d0bb4bce68ed89518fc50b975 MD5 · raw file

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