blob: ba907637b4a1f7674db064f241c1c5ab2fd85a3c [file] [log] [blame]
Fred Drake211a2eb2004-03-22 21:44:43 +00001\documentclass{manual}
Greg Ward16aafcd2000-04-09 04:06:44 +00002\usepackage{distutils}
Greg Wardabc52162000-02-26 00:52:48 +00003
Greg Wardb6528972000-09-07 02:40:37 +00004% $Id$
5
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00006% TODO
7% Document extension.read_setup_file
8% Document build_clib command
9%
10
Greg Ward16aafcd2000-04-09 04:06:44 +000011\title{Distributing Python Modules}
Greg Wardabc52162000-02-26 00:52:48 +000012
Fred Drake20d47382004-01-23 15:23:49 +000013\input{boilerplate}
14
Fred Drake6fca7cc2004-03-23 18:43:03 +000015\author{Greg Ward\\
16 Anthony Baxter}
Fred Drakeb914ef02004-01-02 06:57:50 +000017\authoraddress{
18 \strong{Python Software Foundation}\\
19 Email: \email{distutils-sig@python.org}
20}
Greg Wardabc52162000-02-26 00:52:48 +000021
Greg Warde3cca262000-08-31 16:36:31 +000022\makeindex
Fred Drake6356fff2004-03-23 19:02:38 +000023\makemodindex
Greg Ward16aafcd2000-04-09 04:06:44 +000024
Greg Wardabc52162000-02-26 00:52:48 +000025\begin{document}
26
Greg Wardfacb8db2000-04-09 04:32:40 +000027\maketitle
Georg Brandlf33f5e92005-06-18 20:11:40 +000028
29\input{copyright}
30
Greg Warde3cca262000-08-31 16:36:31 +000031\begin{abstract}
32 \noindent
33 This document describes the Python Distribution Utilities
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000034 (``Distutils'') from the module developer's point of view, describing
Greg Warde3cca262000-08-31 16:36:31 +000035 how to use the Distutils to make Python modules and extensions easily
36 available to a wider audience with very little overhead for
37 build/release/install mechanics.
38\end{abstract}
39
Raymond Hettinger68804312005-01-01 00:28:46 +000040% The ugly "%begin{latexonly}" pseudo-environment suppresses the table
Fred Drakea09262e2001-03-01 18:35:43 +000041% of contents for HTML generation.
42%
43%begin{latexonly}
Greg Wardfacb8db2000-04-09 04:32:40 +000044\tableofcontents
Fred Drakea09262e2001-03-01 18:35:43 +000045%end{latexonly}
46
Greg Ward16aafcd2000-04-09 04:06:44 +000047
Fred Drake211a2eb2004-03-22 21:44:43 +000048\chapter{An Introduction to Distutils}
Greg Warde78298a2000-04-28 17:12:24 +000049\label{intro}
Greg Ward16aafcd2000-04-09 04:06:44 +000050
Andrew M. Kuchling40df7102002-05-08 13:39:03 +000051This document covers using the Distutils to distribute your Python
52modules, concentrating on the role of developer/distributor: if
Fred Drake01df4532000-06-30 03:36:41 +000053you're looking for information on installing Python modules, you
54should refer to the \citetitle[../inst/inst.html]{Installing Python
55Modules} manual.
Greg Ward16aafcd2000-04-09 04:06:44 +000056
57
Greg Wardfacb8db2000-04-09 04:32:40 +000058\section{Concepts \& Terminology}
Greg Warde78298a2000-04-28 17:12:24 +000059\label{concepts}
Greg Ward16aafcd2000-04-09 04:06:44 +000060
61Using the Distutils is quite simple, both for module developers and for
62users/administrators installing third-party modules. As a developer,
Thomas Heller5f52f722001-02-19 17:48:03 +000063your responsibilities (apart from writing solid, well-documented and
Greg Ward16aafcd2000-04-09 04:06:44 +000064well-tested code, of course!) are:
65\begin{itemize}
66\item write a setup script (\file{setup.py} by convention)
67\item (optional) write a setup configuration file
68\item create a source distribution
69\item (optional) create one or more built (binary) distributions
70\end{itemize}
71Each of these tasks is covered in this document.
72
73Not all module developers have access to a multitude of platforms, so
74it's not always feasible to expect them to create a multitude of built
75distributions. It is hoped that a class of intermediaries, called
Greg Ward19c67f82000-06-24 01:33:16 +000076\emph{packagers}, will arise to address this need. Packagers will take
77source distributions released by module developers, build them on one or
78more platforms, and release the resulting built distributions. Thus,
79users on the most popular platforms will be able to install most popular
80Python module distributions in the most natural way for their platform,
81without having to run a single setup script or compile a line of code.
Greg Ward16aafcd2000-04-09 04:06:44 +000082
83
Fred Drake211a2eb2004-03-22 21:44:43 +000084\section{A Simple Example}
Greg Warde78298a2000-04-28 17:12:24 +000085\label{simple-example}
Greg Ward16aafcd2000-04-09 04:06:44 +000086
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000087The setup script is usually quite simple, although since it's written
88in Python, there are no arbitrary limits to what you can do with it,
89though you should be careful about putting arbitrarily expensive
90operations in your setup script. Unlike, say, Autoconf-style configure
91scripts, the setup script may be run multiple times in the course of
Andrew M. Kuchlinge9a54a32003-05-13 15:02:06 +000092building and installing your module distribution.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +000093
94If all you want to do is distribute a module called \module{foo},
95contained in a file \file{foo.py}, then your setup script can be as
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000096simple as this:
Fred Drakea09262e2001-03-01 18:35:43 +000097
Greg Ward16aafcd2000-04-09 04:06:44 +000098\begin{verbatim}
99from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +0000100setup(name='foo',
101 version='1.0',
102 py_modules=['foo'],
103 )
Greg Ward16aafcd2000-04-09 04:06:44 +0000104\end{verbatim}
Greg Ward370248d2000-06-24 01:45:47 +0000105
Greg Ward16aafcd2000-04-09 04:06:44 +0000106Some observations:
107\begin{itemize}
Greg Ward370248d2000-06-24 01:45:47 +0000108\item most information that you supply to the Distutils is supplied as
Greg Wardfacb8db2000-04-09 04:32:40 +0000109 keyword arguments to the \function{setup()} function
Greg Ward16aafcd2000-04-09 04:06:44 +0000110\item those keyword arguments fall into two categories: package
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000111 metadata (name, version number) and information about what's in the
Greg Ward370248d2000-06-24 01:45:47 +0000112 package (a list of pure Python modules, in this case)
Greg Ward16aafcd2000-04-09 04:06:44 +0000113\item modules are specified by module name, not filename (the same will
114 hold true for packages and extensions)
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000115\item it's recommended that you supply a little more metadata, in
Greg Ward16aafcd2000-04-09 04:06:44 +0000116 particular your name, email address and a URL for the project
Greg Ward47f99a62000-09-04 20:07:15 +0000117 (see section~\ref{setup-script} for an example)
Greg Ward16aafcd2000-04-09 04:06:44 +0000118\end{itemize}
119
Greg Ward370248d2000-06-24 01:45:47 +0000120To create a source distribution for this module, you would create a
121setup script, \file{setup.py}, containing the above code, and run:
Fred Drakea09262e2001-03-01 18:35:43 +0000122
Greg Ward16aafcd2000-04-09 04:06:44 +0000123\begin{verbatim}
124python setup.py sdist
125\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000126
Fred Drakeeff9a872000-10-26 16:41:03 +0000127which will create an archive file (e.g., tarball on \UNIX, ZIP file on
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000128Windows) containing your setup script \file{setup.py}, and your module
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000129\file{foo.py}. The archive file will be named \file{foo-1.0.tar.gz} (or
130\file{.zip}), and will unpack into a directory \file{foo-1.0}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000131
132If an end-user wishes to install your \module{foo} module, all she has
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000133to do is download \file{foo-1.0.tar.gz} (or \file{.zip}), unpack it,
134and---from the \file{foo-1.0} directory---run
Fred Drakea09262e2001-03-01 18:35:43 +0000135
Greg Ward16aafcd2000-04-09 04:06:44 +0000136\begin{verbatim}
137python setup.py install
138\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000139
Greg Ward16aafcd2000-04-09 04:06:44 +0000140which will ultimately copy \file{foo.py} to the appropriate directory
141for third-party modules in their Python installation.
142
143This simple example demonstrates some fundamental concepts of the
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000144Distutils. First, both developers and installers have the same basic
Greg Ward16aafcd2000-04-09 04:06:44 +0000145user interface, i.e. the setup script. The difference is which
146Distutils \emph{commands} they use: the \command{sdist} command is
147almost exclusively for module developers, while \command{install} is
148more often for installers (although most developers will want to install
149their own code occasionally).
150
Greg Ward16aafcd2000-04-09 04:06:44 +0000151If you want to make things really easy for your users, you can create
152one or more built distributions for them. For instance, if you are
153running on a Windows machine, and want to make things easy for other
154Windows users, you can create an executable installer (the most
155appropriate type of built distribution for this platform) with the
Greg Ward59d382e2000-05-26 01:04:47 +0000156\command{bdist\_wininst} command. For example:
Fred Drakea09262e2001-03-01 18:35:43 +0000157
Greg Ward16aafcd2000-04-09 04:06:44 +0000158\begin{verbatim}
Greg Ward59d382e2000-05-26 01:04:47 +0000159python setup.py bdist_wininst
Greg Ward16aafcd2000-04-09 04:06:44 +0000160\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000161
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000162will create an executable installer, \file{foo-1.0.win32.exe}, in the
Greg Ward1d8f57a2000-08-05 00:43:11 +0000163current directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000164
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000165Other useful built distribution formats are RPM, implemented by the
166\command{bdist\_rpm} command, Solaris \program{pkgtool}
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000167(\command{bdist\_pkgtool}), and HP-UX \program{swinstall}
168(\command{bdist_sdux}). For example, the following command will
169create an RPM file called \file{foo-1.0.noarch.rpm}:
Fred Drakea09262e2001-03-01 18:35:43 +0000170
Greg Ward1d8f57a2000-08-05 00:43:11 +0000171\begin{verbatim}
172python setup.py bdist_rpm
173\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000174
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000175(The \command{bdist\_rpm} command uses the \command{rpm} executable,
176therefore this has to be run on an RPM-based system such as Red Hat
177Linux, SuSE Linux, or Mandrake Linux.)
Greg Ward1d8f57a2000-08-05 00:43:11 +0000178
179You can find out what distribution formats are available at any time by
180running
Fred Drakea09262e2001-03-01 18:35:43 +0000181
Greg Ward1d8f57a2000-08-05 00:43:11 +0000182\begin{verbatim}
183python setup.py bdist --help-formats
184\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000185
186
Fred Drake211a2eb2004-03-22 21:44:43 +0000187\section{General Python terminology}
Greg Warde78298a2000-04-28 17:12:24 +0000188\label{python-terms}
Greg Ward16aafcd2000-04-09 04:06:44 +0000189
190If you're reading this document, you probably have a good idea of what
191modules, extensions, and so forth are. Nevertheless, just to be sure
192that everyone is operating from a common starting point, we offer the
193following glossary of common Python terms:
194\begin{description}
195\item[module] the basic unit of code reusability in Python: a block of
Greg Ward1d8f57a2000-08-05 00:43:11 +0000196 code imported by some other code. Three types of modules concern us
197 here: pure Python modules, extension modules, and packages.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000198
Greg Ward16aafcd2000-04-09 04:06:44 +0000199\item[pure Python module] a module written in Python and contained in a
200 single \file{.py} file (and possibly associated \file{.pyc} and/or
201 \file{.pyo} files). Sometimes referred to as a ``pure module.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000202
Greg Ward16aafcd2000-04-09 04:06:44 +0000203\item[extension module] a module written in the low-level language of
Fred Drake2884d6d2003-07-02 12:27:43 +0000204 the Python implementation: C/\Cpp{} for Python, Java for Jython.
Greg Ward16aafcd2000-04-09 04:06:44 +0000205 Typically contained in a single dynamically loadable pre-compiled
Fred Drakeeff9a872000-10-26 16:41:03 +0000206 file, e.g. a shared object (\file{.so}) file for Python extensions on
207 \UNIX, a DLL (given the \file{.pyd} extension) for Python extensions
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000208 on Windows, or a Java class file for Jython extensions. (Note that
Fred Drake2884d6d2003-07-02 12:27:43 +0000209 currently, the Distutils only handles C/\Cpp{} extensions for Python.)
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000210
Greg Ward16aafcd2000-04-09 04:06:44 +0000211\item[package] a module that contains other modules; typically contained
212 in a directory in the filesystem and distinguished from other
213 directories by the presence of a file \file{\_\_init\_\_.py}.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000214
Greg Ward6153fa12000-05-26 02:24:28 +0000215\item[root package] the root of the hierarchy of packages. (This isn't
216 really a package, since it doesn't have an \file{\_\_init\_\_.py}
217 file. But we have to call it something.) The vast majority of the
218 standard library is in the root package, as are many small, standalone
219 third-party modules that don't belong to a larger module collection.
220 Unlike regular packages, modules in the root package can be found in
221 many directories: in fact, every directory listed in \code{sys.path}
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000222 contributes modules to the root package.
Greg Ward16aafcd2000-04-09 04:06:44 +0000223\end{description}
224
225
Fred Drake211a2eb2004-03-22 21:44:43 +0000226\section{Distutils-specific terminology}
Greg Warde78298a2000-04-28 17:12:24 +0000227\label{distutils-term}
Greg Ward16aafcd2000-04-09 04:06:44 +0000228
229The following terms apply more specifically to the domain of
230distributing Python modules using the Distutils:
231\begin{description}
232\item[module distribution] a collection of Python modules distributed
233 together as a single downloadable resource and meant to be installed
234 \emph{en masse}. Examples of some well-known module distributions are
235 Numeric Python, PyXML, PIL (the Python Imaging Library), or
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000236 mxBase. (This would be called a \emph{package}, except that term
Greg Ward59d382e2000-05-26 01:04:47 +0000237 is already taken in the Python context: a single module distribution
238 may contain zero, one, or many Python packages.)
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000239
Greg Ward16aafcd2000-04-09 04:06:44 +0000240\item[pure module distribution] a module distribution that contains only
241 pure Python modules and packages. Sometimes referred to as a ``pure
242 distribution.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000243
Greg Ward16aafcd2000-04-09 04:06:44 +0000244\item[non-pure module distribution] a module distribution that contains
245 at least one extension module. Sometimes referred to as a ``non-pure
246 distribution.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000247
Greg Ward16aafcd2000-04-09 04:06:44 +0000248\item[distribution root] the top-level directory of your source tree (or
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000249 source distribution); the directory where \file{setup.py} exists. Generally
250 \file{setup.py} will be run from this directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000251\end{description}
252
253
Fred Drake211a2eb2004-03-22 21:44:43 +0000254\chapter{Writing the Setup Script}
Greg Warde78298a2000-04-28 17:12:24 +0000255\label{setup-script}
Greg Ward16aafcd2000-04-09 04:06:44 +0000256
257The setup script is the centre of all activity in building,
258distributing, and installing modules using the Distutils. The main
259purpose of the setup script is to describe your module distribution to
Greg Wardd5767a52000-04-19 22:48:09 +0000260the Distutils, so that the various commands that operate on your modules
Greg Ward59d382e2000-05-26 01:04:47 +0000261do the right thing. As we saw in section~\ref{simple-example} above,
262the setup script consists mainly of a call to \function{setup()}, and
Greg Ward1bbe3292000-06-25 03:14:13 +0000263most information supplied to the Distutils by the module developer is
264supplied as keyword arguments to \function{setup()}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000265
266Here's a slightly more involved example, which we'll follow for the next
267couple of sections: the Distutils' own setup script. (Keep in mind that
Greg Ward1d8f57a2000-08-05 00:43:11 +0000268although the Distutils are included with Python 1.6 and later, they also
269have an independent existence so that Python 1.5.2 users can use them to
270install other module distributions. The Distutils' own setup script,
271shown here, is used to install the package into Python 1.5.2.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000272
273\begin{verbatim}
274#!/usr/bin/env python
275
276from distutils.core import setup
277
Fred Drake630e5bd2004-03-23 18:54:12 +0000278setup(name='Distutils',
279 version='1.0',
280 description='Python Distribution Utilities',
281 author='Greg Ward',
282 author_email='gward@python.net',
283 url='http://www.python.org/sigs/distutils-sig/',
Fred Drakea09262e2001-03-01 18:35:43 +0000284 packages=['distutils', 'distutils.command'],
285 )
Greg Ward16aafcd2000-04-09 04:06:44 +0000286\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000287
Greg Ward16aafcd2000-04-09 04:06:44 +0000288There are only two differences between this and the trivial one-file
Greg Warde78298a2000-04-28 17:12:24 +0000289distribution presented in section~\ref{simple-example}: more
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000290metadata, and the specification of pure Python modules by package,
Greg Ward16aafcd2000-04-09 04:06:44 +0000291rather than by module. This is important since the Distutils consist of
292a couple of dozen modules split into (so far) two packages; an explicit
293list of every module would be tedious to generate and difficult to
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000294maintain. For more information on the additional meta-data, see
295section~\ref{meta-data}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000296
Greg Ward46b98e32000-04-14 01:53:36 +0000297Note that any pathnames (files or directories) supplied in the setup
Fred Drakeeff9a872000-10-26 16:41:03 +0000298script should be written using the \UNIX{} convention, i.e.
Greg Ward46b98e32000-04-14 01:53:36 +0000299slash-separated. The Distutils will take care of converting this
Greg Ward59d382e2000-05-26 01:04:47 +0000300platform-neutral representation into whatever is appropriate on your
Greg Ward46b98e32000-04-14 01:53:36 +0000301current platform before actually using the pathname. This makes your
302setup script portable across operating systems, which of course is one
303of the major goals of the Distutils. In this spirit, all pathnames in
Brett Cannon7706c2d2005-02-13 22:50:04 +0000304this document are slash-separated. (Mac OS 9 programmers should keep in
Greg Ward59d382e2000-05-26 01:04:47 +0000305mind that the \emph{absence} of a leading slash indicates a relative
Fred Drake781380c2004-02-19 23:17:46 +0000306path, the opposite of the Mac OS convention with colons.)
Greg Ward46b98e32000-04-14 01:53:36 +0000307
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000308This, of course, only applies to pathnames given to Distutils
Fred Drake2a046232003-03-31 16:23:09 +0000309functions. If you, for example, use standard Python functions such as
310\function{glob.glob()} or \function{os.listdir()} to specify files, you
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000311should be careful to write portable code instead of hardcoding path
312separators:
Fred Drakea09262e2001-03-01 18:35:43 +0000313
Thomas Heller5f52f722001-02-19 17:48:03 +0000314\begin{verbatim}
315 glob.glob(os.path.join('mydir', 'subdir', '*.html'))
316 os.listdir(os.path.join('mydir', 'subdir'))
317\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000318
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000319
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000320\section{Listing whole packages}
Greg Ward2afffd42000-08-06 20:37:24 +0000321\label{listing-packages}
Greg Ward16aafcd2000-04-09 04:06:44 +0000322
323The \option{packages} option tells the Distutils to process (build,
324distribute, install, etc.) all pure Python modules found in each package
325mentioned in the \option{packages} list. In order to do this, of
326course, there has to be a correspondence between package names and
327directories in the filesystem. The default correspondence is the most
Greg Ward1ecc2512000-04-19 22:36:24 +0000328obvious one, i.e. package \module{distutils} is found in the directory
Greg Ward16aafcd2000-04-09 04:06:44 +0000329\file{distutils} relative to the distribution root. Thus, when you say
330\code{packages = ['foo']} in your setup script, you are promising that
331the Distutils will find a file \file{foo/\_\_init\_\_.py} (which might
332be spelled differently on your system, but you get the idea) relative to
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000333the directory where your setup script lives. If you break this
334promise, the Distutils will issue a warning but still process the broken
335package anyways.
Greg Ward16aafcd2000-04-09 04:06:44 +0000336
337If you use a different convention to lay out your source directory,
338that's no problem: you just have to supply the \option{package\_dir}
339option to tell the Distutils about your convention. For example, say
Greg Ward1d8f57a2000-08-05 00:43:11 +0000340you keep all Python source under \file{lib}, so that modules in the
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000341``root package'' (i.e., not in any package at all) are in
Greg Ward1d8f57a2000-08-05 00:43:11 +0000342\file{lib}, modules in the \module{foo} package are in \file{lib/foo},
343and so forth. Then you would put
Fred Drakea09262e2001-03-01 18:35:43 +0000344
Greg Ward16aafcd2000-04-09 04:06:44 +0000345\begin{verbatim}
346package_dir = {'': 'lib'}
347\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000348
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000349in your setup script. The keys to this dictionary are package names,
Greg Ward1d8f57a2000-08-05 00:43:11 +0000350and an empty package name stands for the root package. The values are
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000351directory names relative to your distribution root. In this case, when
Greg Ward1d8f57a2000-08-05 00:43:11 +0000352you say \code{packages = ['foo']}, you are promising that the file
Greg Ward16aafcd2000-04-09 04:06:44 +0000353\file{lib/foo/\_\_init\_\_.py} exists.
354
Greg Ward1ecc2512000-04-19 22:36:24 +0000355Another possible convention is to put the \module{foo} package right in
356\file{lib}, the \module{foo.bar} package in \file{lib/bar}, etc. This
Greg Ward16aafcd2000-04-09 04:06:44 +0000357would be written in the setup script as
Fred Drakea09262e2001-03-01 18:35:43 +0000358
Greg Ward16aafcd2000-04-09 04:06:44 +0000359\begin{verbatim}
360package_dir = {'foo': 'lib'}
361\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000362
Greg Ward59d382e2000-05-26 01:04:47 +0000363A \code{\var{package}: \var{dir}} entry in the \option{package\_dir}
364dictionary implicitly applies to all packages below \var{package}, so
365the \module{foo.bar} case is automatically handled here. In this
366example, having \code{packages = ['foo', 'foo.bar']} tells the Distutils
367to look for \file{lib/\_\_init\_\_.py} and
368\file{lib/bar/\_\_init\_\_.py}. (Keep in mind that although
369\option{package\_dir} applies recursively, you must explicitly list all
370packages in \option{packages}: the Distutils will \emph{not} recursively
371scan your source tree looking for any directory with an
372\file{\_\_init\_\_.py} file.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000373
374
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000375\section{Listing individual modules}
Greg Warde78298a2000-04-28 17:12:24 +0000376\label{listing-modules}
Greg Ward16aafcd2000-04-09 04:06:44 +0000377
378For a small module distribution, you might prefer to list all modules
379rather than listing packages---especially the case of a single module
380that goes in the ``root package'' (i.e., no package at all). This
Greg Warde78298a2000-04-28 17:12:24 +0000381simplest case was shown in section~\ref{simple-example}; here is a
Greg Ward16aafcd2000-04-09 04:06:44 +0000382slightly more involved example:
Fred Drakea09262e2001-03-01 18:35:43 +0000383
Greg Ward16aafcd2000-04-09 04:06:44 +0000384\begin{verbatim}
385py_modules = ['mod1', 'pkg.mod2']
386\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000387
Greg Ward16aafcd2000-04-09 04:06:44 +0000388This describes two modules, one of them in the ``root'' package, the
Greg Wardd5767a52000-04-19 22:48:09 +0000389other in the \module{pkg} package. Again, the default package/directory
390layout implies that these two modules can be found in \file{mod1.py} and
391\file{pkg/mod2.py}, and that \file{pkg/\_\_init\_\_.py} exists as well.
Greg Ward2afffd42000-08-06 20:37:24 +0000392And again, you can override the package/directory correspondence using
393the \option{package\_dir} option.
Greg Ward59d382e2000-05-26 01:04:47 +0000394
395
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000396\section{Describing extension modules}
Greg Ward1365a302000-08-31 14:47:05 +0000397\label{describing-extensions}
Greg Ward59d382e2000-05-26 01:04:47 +0000398
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000399% XXX read over this section
Greg Ward2afffd42000-08-06 20:37:24 +0000400Just as writing Python extension modules is a bit more complicated than
401writing pure Python modules, describing them to the Distutils is a bit
402more complicated. Unlike pure modules, it's not enough just to list
403modules or packages and expect the Distutils to go out and find the
404right files; you have to specify the extension name, source file(s), and
405any compile/link requirements (include directories, libraries to link
406with, etc.).
407
408All of this is done through another keyword argument to
Andrew M. Kuchling8f6f08c2005-06-07 18:51:42 +0000409\function{setup()}, the \option{ext_modules} option. \option{ext_modules}
Greg Ward2afffd42000-08-06 20:37:24 +0000410is just a list of \class{Extension} instances, each of which describes a
411single extension module. Suppose your distribution includes a single
412extension, called \module{foo} and implemented by \file{foo.c}. If no
413additional instructions to the compiler/linker are needed, describing
414this extension is quite simple:
Fred Drakea09262e2001-03-01 18:35:43 +0000415
Greg Ward2afffd42000-08-06 20:37:24 +0000416\begin{verbatim}
Fred Drake630e5bd2004-03-23 18:54:12 +0000417Extension('foo', ['foo.c'])
Greg Ward2afffd42000-08-06 20:37:24 +0000418\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000419
Greg Ward2afffd42000-08-06 20:37:24 +0000420The \class{Extension} class can be imported from
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000421\module{distutils.core} along with \function{setup()}. Thus, the setup
Greg Ward2afffd42000-08-06 20:37:24 +0000422script for a module distribution that contains only this one extension
423and nothing else might be:
Fred Drakea09262e2001-03-01 18:35:43 +0000424
Greg Ward2afffd42000-08-06 20:37:24 +0000425\begin{verbatim}
426from distutils.core import setup, Extension
Fred Drake630e5bd2004-03-23 18:54:12 +0000427setup(name='foo',
428 version='1.0',
429 ext_modules=[Extension('foo', ['foo.c'])],
430 )
Greg Ward2afffd42000-08-06 20:37:24 +0000431\end{verbatim}
432
433The \class{Extension} class (actually, the underlying extension-building
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +0000434machinery implemented by the \command{build\_ext} command) supports a
Greg Ward2afffd42000-08-06 20:37:24 +0000435great deal of flexibility in describing Python extensions, which is
436explained in the following sections.
437
438
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000439\subsection{Extension names and packages}
Greg Ward2afffd42000-08-06 20:37:24 +0000440
441The first argument to the \class{Extension} constructor is always the
442name of the extension, including any package names. For example,
Fred Drakea09262e2001-03-01 18:35:43 +0000443
Greg Ward2afffd42000-08-06 20:37:24 +0000444\begin{verbatim}
Fred Drake630e5bd2004-03-23 18:54:12 +0000445Extension('foo', ['src/foo1.c', 'src/foo2.c'])
Greg Ward2afffd42000-08-06 20:37:24 +0000446\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000447
Greg Ward2afffd42000-08-06 20:37:24 +0000448describes an extension that lives in the root package, while
Fred Drakea09262e2001-03-01 18:35:43 +0000449
Greg Ward2afffd42000-08-06 20:37:24 +0000450\begin{verbatim}
Fred Drake630e5bd2004-03-23 18:54:12 +0000451Extension('pkg.foo', ['src/foo1.c', 'src/foo2.c'])
Greg Ward2afffd42000-08-06 20:37:24 +0000452\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000453
Greg Ward2afffd42000-08-06 20:37:24 +0000454describes the same extension in the \module{pkg} package. The source
455files and resulting object code are identical in both cases; the only
456difference is where in the filesystem (and therefore where in Python's
457namespace hierarchy) the resulting extension lives.
458
459If you have a number of extensions all in the same package (or all under
460the same base package), use the \option{ext\_package} keyword argument
461to \function{setup()}. For example,
Fred Drakea09262e2001-03-01 18:35:43 +0000462
Greg Ward2afffd42000-08-06 20:37:24 +0000463\begin{verbatim}
464setup(...
Fred Drake630e5bd2004-03-23 18:54:12 +0000465 ext_package='pkg',
466 ext_modules=[Extension('foo', ['foo.c']),
467 Extension('subpkg.bar', ['bar.c'])],
Greg Ward2afffd42000-08-06 20:37:24 +0000468 )
469\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000470
Greg Ward2afffd42000-08-06 20:37:24 +0000471will compile \file{foo.c} to the extension \module{pkg.foo}, and
472\file{bar.c} to \module{pkg.subpkg.bar}.
473
474
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000475\subsection{Extension source files}
Greg Ward2afffd42000-08-06 20:37:24 +0000476
477The second argument to the \class{Extension} constructor is a list of
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000478source files. Since the Distutils currently only support C, \Cpp, and
479Objective-C extensions, these are normally C/\Cpp/Objective-C source
480files. (Be sure to use appropriate extensions to distinguish \Cpp\
481source files: \file{.cc} and \file{.cpp} seem to be recognized by both
482\UNIX{} and Windows compilers.)
Greg Ward2afffd42000-08-06 20:37:24 +0000483
484However, you can also include SWIG interface (\file{.i}) files in the
485list; the \command{build\_ext} command knows how to deal with SWIG
486extensions: it will run SWIG on the interface file and compile the
Fred Drake2884d6d2003-07-02 12:27:43 +0000487resulting C/\Cpp{} file into your extension.
Greg Ward2afffd42000-08-06 20:37:24 +0000488
489\XXX{SWIG support is rough around the edges and largely untested;
Fred Drake2884d6d2003-07-02 12:27:43 +0000490 especially SWIG support for \Cpp{} extensions! Explain in more detail
Greg Ward2afffd42000-08-06 20:37:24 +0000491 here when the interface firms up.}
492
493On some platforms, you can include non-source files that are processed
494by the compiler and included in your extension. Currently, this just
Thomas Heller5f52f722001-02-19 17:48:03 +0000495means Windows message text (\file{.mc}) files and resource definition
Fred Drake2884d6d2003-07-02 12:27:43 +0000496(\file{.rc}) files for Visual \Cpp. These will be compiled to binary resource
Thomas Heller5f52f722001-02-19 17:48:03 +0000497(\file{.res}) files and linked into the executable.
Greg Ward2afffd42000-08-06 20:37:24 +0000498
499
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000500\subsection{Preprocessor options}
Greg Ward2afffd42000-08-06 20:37:24 +0000501
502Three optional arguments to \class{Extension} will help if you need to
503specify include directories to search or preprocessor macros to
504define/undefine: \code{include\_dirs}, \code{define\_macros}, and
505\code{undef\_macros}.
506
507For example, if your extension requires header files in the
508\file{include} directory under your distribution root, use the
509\code{include\_dirs} option:
Fred Drakea09262e2001-03-01 18:35:43 +0000510
Greg Ward2afffd42000-08-06 20:37:24 +0000511\begin{verbatim}
Fred Drake630e5bd2004-03-23 18:54:12 +0000512Extension('foo', ['foo.c'], include_dirs=['include'])
Greg Ward2afffd42000-08-06 20:37:24 +0000513\end{verbatim}
514
515You can specify absolute directories there; if you know that your
Fred Drakeeff9a872000-10-26 16:41:03 +0000516extension will only be built on \UNIX{} systems with X11R6 installed to
Greg Ward2afffd42000-08-06 20:37:24 +0000517\file{/usr}, you can get away with
Fred Drakea09262e2001-03-01 18:35:43 +0000518
Greg Ward2afffd42000-08-06 20:37:24 +0000519\begin{verbatim}
Fred Drake630e5bd2004-03-23 18:54:12 +0000520Extension('foo', ['foo.c'], include_dirs=['/usr/include/X11'])
Greg Ward2afffd42000-08-06 20:37:24 +0000521\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000522
Greg Ward2afffd42000-08-06 20:37:24 +0000523You should avoid this sort of non-portable usage if you plan to
Greg Ward58437f22002-05-10 14:40:22 +0000524distribute your code: it's probably better to write C code like
525\begin{verbatim}
526#include <X11/Xlib.h>
527\end{verbatim}
Greg Ward2afffd42000-08-06 20:37:24 +0000528
529If you need to include header files from some other Python extension,
Greg Ward58437f22002-05-10 14:40:22 +0000530you can take advantage of the fact that header files are installed in a
531consistent way by the Distutils \command{install\_header} command. For
532example, the Numerical Python header files are installed (on a standard
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000533\UNIX{} installation) to \file{/usr/local/include/python1.5/Numerical}.
Greg Ward58437f22002-05-10 14:40:22 +0000534(The exact location will differ according to your platform and Python
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000535installation.) Since the Python include
Greg Ward58437f22002-05-10 14:40:22 +0000536directory---\file{/usr/local/include/python1.5} in this case---is always
537included in the search path when building Python extensions, the best
538approach is to write C code like
539\begin{verbatim}
540#include <Numerical/arrayobject.h>
541\end{verbatim}
542If you must put the \file{Numerical} include directory right into your
543header search path, though, you can find that directory using the
Fred Drake630e5bd2004-03-23 18:54:12 +0000544Distutils \refmodule{distutils.sysconfig} module:
Fred Drakea09262e2001-03-01 18:35:43 +0000545
Greg Ward2afffd42000-08-06 20:37:24 +0000546\begin{verbatim}
547from distutils.sysconfig import get_python_inc
Fred Drake630e5bd2004-03-23 18:54:12 +0000548incdir = os.path.join(get_python_inc(plat_specific=1), 'Numerical')
Greg Ward2afffd42000-08-06 20:37:24 +0000549setup(...,
Fred Drake630e5bd2004-03-23 18:54:12 +0000550 Extension(..., include_dirs=[incdir]),
551 )
Greg Ward2afffd42000-08-06 20:37:24 +0000552\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000553
Greg Ward2afffd42000-08-06 20:37:24 +0000554Even though this is quite portable---it will work on any Python
555installation, regardless of platform---it's probably easier to just
556write your C code in the sensible way.
557
558You can define and undefine pre-processor macros with the
559\code{define\_macros} and \code{undef\_macros} options.
560\code{define\_macros} takes a list of \code{(name, value)} tuples, where
561\code{name} is the name of the macro to define (a string) and
562\code{value} is its value: either a string or \code{None}. (Defining a
563macro \code{FOO} to \code{None} is the equivalent of a bare
564\code{\#define FOO} in your C source: with most compilers, this sets
565\code{FOO} to the string \code{1}.) \code{undef\_macros} is just
566a list of macros to undefine.
567
568For example:
Fred Drakea09262e2001-03-01 18:35:43 +0000569
Greg Ward2afffd42000-08-06 20:37:24 +0000570\begin{verbatim}
571Extension(...,
Thomas Heller95a97d52003-10-08 12:01:33 +0000572 define_macros=[('NDEBUG', '1'),
573 ('HAVE_STRFTIME', None)],
Greg Ward2afffd42000-08-06 20:37:24 +0000574 undef_macros=['HAVE_FOO', 'HAVE_BAR'])
575\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000576
Greg Ward2afffd42000-08-06 20:37:24 +0000577is the equivalent of having this at the top of every C source file:
Fred Drakea09262e2001-03-01 18:35:43 +0000578
Greg Ward2afffd42000-08-06 20:37:24 +0000579\begin{verbatim}
580#define NDEBUG 1
581#define HAVE_STRFTIME
582#undef HAVE_FOO
583#undef HAVE_BAR
584\end{verbatim}
585
586
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000587\subsection{Library options}
Greg Ward2afffd42000-08-06 20:37:24 +0000588
589You can also specify the libraries to link against when building your
590extension, and the directories to search for those libraries. The
591\code{libraries} option is a list of libraries to link against,
592\code{library\_dirs} is a list of directories to search for libraries at
593link-time, and \code{runtime\_library\_dirs} is a list of directories to
594search for shared (dynamically loaded) libraries at run-time.
595
596For example, if you need to link against libraries known to be in the
597standard library search path on target systems
Fred Drakea09262e2001-03-01 18:35:43 +0000598
Greg Ward2afffd42000-08-06 20:37:24 +0000599\begin{verbatim}
600Extension(...,
Fred Drake630e5bd2004-03-23 18:54:12 +0000601 libraries=['gdbm', 'readline'])
Greg Ward2afffd42000-08-06 20:37:24 +0000602\end{verbatim}
603
604If you need to link with libraries in a non-standard location, you'll
605have to include the location in \code{library\_dirs}:
Fred Drakea09262e2001-03-01 18:35:43 +0000606
Greg Ward2afffd42000-08-06 20:37:24 +0000607\begin{verbatim}
608Extension(...,
Fred Drake630e5bd2004-03-23 18:54:12 +0000609 library_dirs=['/usr/X11R6/lib'],
610 libraries=['X11', 'Xt'])
Greg Ward2afffd42000-08-06 20:37:24 +0000611\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000612
Greg Ward2afffd42000-08-06 20:37:24 +0000613(Again, this sort of non-portable construct should be avoided if you
614intend to distribute your code.)
615
Thomas Heller5f52f722001-02-19 17:48:03 +0000616\XXX{Should mention clib libraries here or somewhere else!}
617
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000618\subsection{Other options}
Thomas Heller5f52f722001-02-19 17:48:03 +0000619
620There are still some other options which can be used to handle special
621cases.
622
623The \option{extra\_objects} option is a list of object files to be passed
624to the linker. These files must not have extensions, as the default
625extension for the compiler is used.
626
627\option{extra\_compile\_args} and \option{extra\_link\_args} can be used
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000628to specify additional command line options for the respective compiler and
629linker command lines.
Thomas Heller5f52f722001-02-19 17:48:03 +0000630
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000631\option{export\_symbols} is only useful on Windows. It can contain a list
Thomas Heller5f52f722001-02-19 17:48:03 +0000632of symbols (functions or variables) to be exported. This option
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000633is not needed when building compiled extensions: Distutils
634will automatically add \code{initmodule}
635to the list of exported symbols.
Thomas Heller5f52f722001-02-19 17:48:03 +0000636
Fred Drakedb7b0022005-03-20 22:19:47 +0000637\section{Relationships between Distributions and Packages}
638
639A distribution may relate to packages in three specific ways:
640
641\begin{enumerate}
642 \item It can require packages or modules.
643
644 \item It can provide packages or modules.
645
646 \item It can obsolete packages or modules.
647\end{enumerate}
648
649These relationships can be specified using keyword arguments to the
650\function{distutils.core.setup()} function.
651
652Dependencies on other Python modules and packages can be specified by
653supplying the \var{requires} keyword argument to \function{setup()}.
654The value must be a list of strings. Each string specifies a package
655that is required, and optionally what versions are sufficient.
656
657To specify that any version of a module or package is required, the
658string should consist entirely of the module or package name.
659Examples include \code{'mymodule'} and \code{'xml.parsers.expat'}.
660
661If specific versions are required, a sequence of qualifiers can be
662supplied in parentheses. Each qualifier may consist of a comparison
663operator and a version number. The accepted comparison operators are:
664
665\begin{verbatim}
666< > ==
667<= >= !=
668\end{verbatim}
669
670These can be combined by using multiple qualifiers separated by commas
671(and optional whitespace). In this case, all of the qualifiers must
672be matched; a logical AND is used to combine the evaluations.
673
674Let's look at a bunch of examples:
675
676\begin{tableii}{l|l}{code}{Requires Expression}{Explanation}
677 \lineii{==1.0} {Only version \code{1.0} is compatible}
678 \lineii{>1.0, !=1.5.1, <2.0} {Any version after \code{1.0} and before
679 \code{2.0} is compatible, except
680 \code{1.5.1}}
681\end{tableii}
682
683Now that we can specify dependencies, we also need to be able to
684specify what we provide that other distributions can require. This is
685done using the \var{provides} keyword argument to \function{setup()}.
686The value for this keyword is a list of strings, each of which names a
687Python module or package, and optionally identifies the version. If
688the version is not specified, it is assumed to match that of the
689distribution.
690
691Some examples:
692
693\begin{tableii}{l|l}{code}{Provides Expression}{Explanation}
694 \lineii{mypkg} {Provide \code{mypkg}, using the distribution version}
695 \lineii{mypkg (1.1} {Provide \code{mypkg} version 1.1, regardless of the
696 distribution version}
697\end{tableii}
698
699A package can declare that it obsoletes other packages using the
700\var{obsoletes} keyword argument. The value for this is similar to
701that of the \var{requires} keyword: a list of strings giving module or
702package specifiers. Each specifier consists of a module or package
703name optionally followed by one or more version qualifiers. Version
704qualifiers are given in parentheses after the module or package name.
705
706The versions identified by the qualifiers are those that are obsoleted
707by the distribution being described. If no qualifiers are given, all
708versions of the named module or package are understood to be
709obsoleted.
710
711
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000712\section{Installing Scripts}
Fred Drakedb7b0022005-03-20 22:19:47 +0000713
Thomas Heller5f52f722001-02-19 17:48:03 +0000714So far we have been dealing with pure and non-pure Python modules,
715which are usually not run by themselves but imported by scripts.
716
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000717Scripts are files containing Python source code, intended to be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000718started from the command line. Scripts don't require Distutils to do
719anything very complicated. The only clever feature is that if the
720first line of the script starts with \code{\#!} and contains the word
721``python'', the Distutils will adjust the first line to refer to the
Martin v. Löwis9f5c0c42004-08-25 11:37:43 +0000722current interpreter location. By default, it is replaced with the
Fred Drakee3a1b482004-08-25 14:01:32 +0000723current interpreter location. The \longprogramopt{executable} (or
724\programopt{-e}) option will allow the interpreter path to be
725explicitly overridden.
Thomas Heller5f52f722001-02-19 17:48:03 +0000726
727The \option{scripts} option simply is a list of files to be handled
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000728in this way. From the PyXML setup script:
729
730\begin{verbatim}
Fred Drake630e5bd2004-03-23 18:54:12 +0000731setup(...
732 scripts=['scripts/xmlproc_parse', 'scripts/xmlproc_val']
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000733 )
734\end{verbatim}
Thomas Heller5f52f722001-02-19 17:48:03 +0000735
736
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000737\section{Installing Package Data}
Fred Drake0eb32a62004-06-11 21:50:33 +0000738
739Often, additional files need to be installed into a package. These
740files are often data that's closely related to the package's
741implementation, or text files containing documentation that might be
742of interest to programmers using the package. These files are called
743\dfn{package data}.
744
745Package data can be added to packages using the \code{package_data}
746keyword argument to the \function{setup()} function. The value must
747be a mapping from package name to a list of relative path names that
748should be copied into the package. The paths are interpreted as
749relative to the directory containing the package (information from the
750\code{package_dir} mapping is used if appropriate); that is, the files
751are expected to be part of the package in the source directories.
752They may contain glob patterns as well.
753
754The path names may contain directory portions; any necessary
755directories will be created in the installation.
756
757For example, if a package should contain a subdirectory with several
758data files, the files can be arranged like this in the source tree:
759
760\begin{verbatim}
761setup.py
762src/
763 mypkg/
764 __init__.py
765 module.py
766 data/
767 tables.dat
768 spoons.dat
769 forks.dat
770\end{verbatim}
771
772The corresponding call to \function{setup()} might be:
773
774\begin{verbatim}
775setup(...,
776 packages=['mypkg'],
777 package_dir={'mypkg': 'src/mypkg'},
Thomas Hellerdd6d2072004-06-18 17:31:23 +0000778 package_data={'mypkg': ['data/*.dat']},
Fred Drake0eb32a62004-06-11 21:50:33 +0000779 )
780\end{verbatim}
781
782
783\versionadded{2.4}
784
785
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000786\section{Installing Additional Files}
Fred Drakea09262e2001-03-01 18:35:43 +0000787
Thomas Heller5f52f722001-02-19 17:48:03 +0000788The \option{data\_files} option can be used to specify additional
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000789files needed by the module distribution: configuration files, message
790catalogs, data files, anything which doesn't fit in the previous
791categories.
Thomas Heller5f52f722001-02-19 17:48:03 +0000792
Fred Drake632bda32002-03-08 22:02:06 +0000793\option{data\_files} specifies a sequence of (\var{directory},
794\var{files}) pairs in the following way:
Fred Drakea09262e2001-03-01 18:35:43 +0000795
Thomas Heller5f52f722001-02-19 17:48:03 +0000796\begin{verbatim}
797setup(...
798 data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000799 ('config', ['cfg/data.cfg']),
800 ('/etc/init.d', ['init-script'])]
801 )
Thomas Heller5f52f722001-02-19 17:48:03 +0000802\end{verbatim}
803
804Note that you can specify the directory names where the data files
805will be installed, but you cannot rename the data files themselves.
806
Fred Drake632bda32002-03-08 22:02:06 +0000807Each (\var{directory}, \var{files}) pair in the sequence specifies the
808installation directory and the files to install there. If
809\var{directory} is a relative path, it is interpreted relative to the
810installation prefix (Python's \code{sys.prefix} for pure-Python
811packages, \code{sys.exec_prefix} for packages that contain extension
812modules). Each file name in \var{files} is interpreted relative to
813the \file{setup.py} script at the top of the package source
814distribution. No directory information from \var{files} is used to
815determine the final location of the installed file; only the name of
816the file is used.
817
Thomas Heller5f52f722001-02-19 17:48:03 +0000818You can specify the \option{data\_files} options as a simple sequence
819of files without specifying a target directory, but this is not recommended,
820and the \command{install} command will print a warning in this case.
821To install data files directly in the target directory, an empty
822string should be given as the directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000823
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000824\section{Additional meta-data}
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000825\label{meta-data}
826
827The setup script may include additional meta-data beyond the name and
828version. This information includes:
829
Fred Drakec440af52003-04-25 16:43:28 +0000830\begin{tableiv}{l|l|l|c}{code}%
831 {Meta-Data}{Description}{Value}{Notes}
832 \lineiv{name}{name of the package}
833 {short string}{(1)}
834 \lineiv{version}{version of this release}
835 {short string}{(1)(2)}
836 \lineiv{author}{package author's name}
837 {short string}{(3)}
838 \lineiv{author_email}{email address of the package author}
839 {email address}{(3)}
840 \lineiv{maintainer}{package maintainer's name}
841 {short string}{(3)}
842 \lineiv{maintainer_email}{email address of the package maintainer}
843 {email address}{(3)}
844 \lineiv{url}{home page for the package}
845 {URL}{(1)}
846 \lineiv{description}{short, summary description of the package}
847 {short string}{}
848 \lineiv{long_description}{longer description of the package}
849 {long string}{}
850 \lineiv{download_url}{location where the package may be downloaded}
851 {URL}{(4)}
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000852 \lineiv{classifiers}{a list of classifiers}
Fred Drakec440af52003-04-25 16:43:28 +0000853 {list of strings}{(4)}
854\end{tableiv}
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000855
856\noindent Notes:
857\begin{description}
Fred Drakec440af52003-04-25 16:43:28 +0000858\item[(1)] These fields are required.
859\item[(2)] It is recommended that versions take the form
860 \emph{major.minor\optional{.patch\optional{.sub}}}.
861\item[(3)] Either the author or the maintainer must be identified.
862\item[(4)] These fields should not be used if your package is to be
863 compatible with Python versions prior to 2.2.3 or 2.3. The list is
864 available from the \ulink{PyPI website}{http://www.python.org/pypi}.
865
Fred Drake630e5bd2004-03-23 18:54:12 +0000866\item['short string'] A single line of text, not more than 200 characters.
867\item['long string'] Multiple lines of plain text in reStructuredText
Fred Drakec440af52003-04-25 16:43:28 +0000868 format (see \url{http://docutils.sf.net/}).
Fred Drake630e5bd2004-03-23 18:54:12 +0000869\item['list of strings'] See below.
Fred Drakec440af52003-04-25 16:43:28 +0000870\end{description}
871
872None of the string values may be Unicode.
873
874Encoding the version information is an art in itself. Python packages
875generally adhere to the version format
876\emph{major.minor\optional{.patch}\optional{sub}}. The major number is
8770 for
878initial, experimental releases of software. It is incremented for
879releases that represent major milestones in a package. The minor
880number is incremented when important new features are added to the
881package. The patch number increments when bug-fix releases are
882made. Additional trailing version information is sometimes used to
883indicate sub-releases. These are "a1,a2,...,aN" (for alpha releases,
884where functionality and API may change), "b1,b2,...,bN" (for beta
885releases, which only fix bugs) and "pr1,pr2,...,prN" (for final
886pre-release release testing). Some examples:
887
888\begin{description}
889\item[0.1.0] the first, experimental release of a package
890\item[1.0.1a2] the second alpha release of the first patch version of 1.0
891\end{description}
892
893\option{classifiers} are specified in a python list:
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000894
895\begin{verbatim}
896setup(...
Fred Drake630e5bd2004-03-23 18:54:12 +0000897 classifiers=[
Fred Drake2a046232003-03-31 16:23:09 +0000898 'Development Status :: 4 - Beta',
899 'Environment :: Console',
900 'Environment :: Web Environment',
901 'Intended Audience :: End Users/Desktop',
902 'Intended Audience :: Developers',
903 'Intended Audience :: System Administrators',
904 'License :: OSI Approved :: Python Software Foundation License',
905 'Operating System :: MacOS :: MacOS X',
906 'Operating System :: Microsoft :: Windows',
907 'Operating System :: POSIX',
908 'Programming Language :: Python',
909 'Topic :: Communications :: Email',
910 'Topic :: Office/Business',
911 'Topic :: Software Development :: Bug Tracking',
912 ],
Fred Drake2a046232003-03-31 16:23:09 +0000913 )
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000914\end{verbatim}
915
Fred Drakec440af52003-04-25 16:43:28 +0000916If you wish to include classifiers in your \file{setup.py} file and also
917wish to remain backwards-compatible with Python releases prior to 2.2.3,
918then you can include the following code fragment in your \file{setup.py}
Fred Drake630e5bd2004-03-23 18:54:12 +0000919before the \function{setup()} call.
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000920
921\begin{verbatim}
Fred Drakec440af52003-04-25 16:43:28 +0000922# patch distutils if it can't cope with the "classifiers" or
923# "download_url" keywords
Brett Cannon076b7322005-11-02 22:58:12 +0000924from sys import version
925if version < '2.2.3':
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000926 from distutils.dist import DistributionMetadata
927 DistributionMetadata.classifiers = None
Fred Drake2a046232003-03-31 16:23:09 +0000928 DistributionMetadata.download_url = None
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000929\end{verbatim}
930
Greg Ward16aafcd2000-04-09 04:06:44 +0000931
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000932\section{Debugging the setup script}
Thomas Heller675580f2003-06-30 19:33:29 +0000933
934Sometimes things go wrong, and the setup script doesn't do what the
935developer wants.
936
937Distutils catches any exceptions when running the setup script, and
938print a simple error message before the script is terminated. The
939motivation for this behaviour is to not confuse administrators who
940don't know much about Python and are trying to install a package. If
941they get a big long traceback from deep inside the guts of Distutils,
942they may think the package or the Python installation is broken
943because they don't read all the way down to the bottom and see that
944it's a permission problem.
945
946On the other hand, this doesn't help the developer to find the cause
947of the failure. For this purpose, the DISTUTILS_DEBUG environment
948variable can be set to anything except an empty string, and distutils
949will now print detailed information what it is doing, and prints the
Martin v. Löwis95cf84a2003-10-19 07:32:24 +0000950full traceback in case an exception occurs.
Thomas Heller675580f2003-06-30 19:33:29 +0000951
Fred Drake211a2eb2004-03-22 21:44:43 +0000952\chapter{Writing the Setup Configuration File}
Greg Warde78298a2000-04-28 17:12:24 +0000953\label{setup-config}
Greg Ward16aafcd2000-04-09 04:06:44 +0000954
Greg Ward16aafcd2000-04-09 04:06:44 +0000955Often, it's not possible to write down everything needed to build a
Greg Ward47f99a62000-09-04 20:07:15 +0000956distribution \emph{a priori}: you may need to get some information from
957the user, or from the user's system, in order to proceed. As long as
958that information is fairly simple---a list of directories to search for
959C header files or libraries, for example---then providing a
960configuration file, \file{setup.cfg}, for users to edit is a cheap and
961easy way to solicit it. Configuration files also let you provide
962default values for any command option, which the installer can then
963override either on the command-line or by editing the config file.
Greg Ward16aafcd2000-04-09 04:06:44 +0000964
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000965% (If you have more advanced needs, such as determining which extensions
966% to build based on what capabilities are present on the target system,
967% then you need the Distutils ``auto-configuration'' facility. This
968% started to appear in Distutils 0.9 but, as of this writing, isn't mature
969% or stable enough yet for real-world use.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000970
Greg Ward47f99a62000-09-04 20:07:15 +0000971The setup configuration file is a useful middle-ground between the setup
972script---which, ideally, would be opaque to installers\footnote{This
973 ideal probably won't be achieved until auto-configuration is fully
974 supported by the Distutils.}---and the command-line to the setup
975script, which is outside of your control and entirely up to the
976installer. In fact, \file{setup.cfg} (and any other Distutils
977configuration files present on the target system) are processed after
978the contents of the setup script, but before the command-line. This has
979several useful consequences:
980\begin{itemize}
981\item installers can override some of what you put in \file{setup.py} by
982 editing \file{setup.cfg}
983\item you can provide non-standard defaults for options that are not
984 easily set in \file{setup.py}
985\item installers can override anything in \file{setup.cfg} using the
986 command-line options to \file{setup.py}
987\end{itemize}
988
989The basic syntax of the configuration file is simple:
Fred Drakea09262e2001-03-01 18:35:43 +0000990
Greg Ward47f99a62000-09-04 20:07:15 +0000991\begin{verbatim}
992[command]
993option=value
994...
995\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000996
Greg Ward47f99a62000-09-04 20:07:15 +0000997where \var{command} is one of the Distutils commands (e.g.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000998\command{build\_py}, \command{install}), and \var{option} is one of
999the options that command supports. Any number of options can be
1000supplied for each command, and any number of command sections can be
1001included in the file. Blank lines are ignored, as are comments, which
1002run from a \character{\#} character until the end of the line. Long
1003option values can be split across multiple lines simply by indenting
1004the continuation lines.
Greg Ward47f99a62000-09-04 20:07:15 +00001005
1006You can find out the list of options supported by a particular command
1007with the universal \longprogramopt{help} option, e.g.
Fred Drakea09262e2001-03-01 18:35:43 +00001008
Greg Ward47f99a62000-09-04 20:07:15 +00001009\begin{verbatim}
1010> python setup.py --help build_ext
1011[...]
1012Options for 'build_ext' command:
1013 --build-lib (-b) directory for compiled extension modules
1014 --build-temp (-t) directory for temporary files (build by-products)
1015 --inplace (-i) ignore build-lib and put compiled extensions into the
1016 source directory alongside your pure Python modules
1017 --include-dirs (-I) list of directories to search for header files
1018 --define (-D) C preprocessor macros to define
1019 --undef (-U) C preprocessor macros to undefine
1020[...]
1021\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001022
Greg Ward47f99a62000-09-04 20:07:15 +00001023Note that an option spelled \longprogramopt{foo-bar} on the command-line
1024is spelled \option{foo\_bar} in configuration files.
1025
1026For example, say you want your extensions to be built
1027``in-place''---that is, you have an extension \module{pkg.ext}, and you
Fred Drakeeff9a872000-10-26 16:41:03 +00001028want the compiled extension file (\file{ext.so} on \UNIX, say) to be put
Greg Ward47f99a62000-09-04 20:07:15 +00001029in the same source directory as your pure Python modules
1030\module{pkg.mod1} and \module{pkg.mod2}. You can always use the
1031\longprogramopt{inplace} option on the command-line to ensure this:
Fred Drakea09262e2001-03-01 18:35:43 +00001032
Greg Ward47f99a62000-09-04 20:07:15 +00001033\begin{verbatim}
1034python setup.py build_ext --inplace
1035\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001036
Greg Ward47f99a62000-09-04 20:07:15 +00001037But this requires that you always specify the \command{build\_ext}
1038command explicitly, and remember to provide \longprogramopt{inplace}.
1039An easier way is to ``set and forget'' this option, by encoding it in
1040\file{setup.cfg}, the configuration file for this distribution:
Fred Drakea09262e2001-03-01 18:35:43 +00001041
Greg Ward47f99a62000-09-04 20:07:15 +00001042\begin{verbatim}
1043[build_ext]
1044inplace=1
1045\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001046
Greg Ward47f99a62000-09-04 20:07:15 +00001047This will affect all builds of this module distribution, whether or not
Raymond Hettinger68804312005-01-01 00:28:46 +00001048you explicitly specify \command{build\_ext}. If you include
Greg Ward47f99a62000-09-04 20:07:15 +00001049\file{setup.cfg} in your source distribution, it will also affect
1050end-user builds---which is probably a bad idea for this option, since
1051always building extensions in-place would break installation of the
1052module distribution. In certain peculiar cases, though, modules are
1053built right in their installation directory, so this is conceivably a
1054useful ability. (Distributing extensions that expect to be built in
1055their installation directory is almost always a bad idea, though.)
1056
1057Another example: certain commands take a lot of options that don't
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001058change from run to run; for example, \command{bdist\_rpm} needs to know
Greg Ward47f99a62000-09-04 20:07:15 +00001059everything required to generate a ``spec'' file for creating an RPM
1060distribution. Some of this information comes from the setup script, and
1061some is automatically generated by the Distutils (such as the list of
1062files installed). But some of it has to be supplied as options to
1063\command{bdist\_rpm}, which would be very tedious to do on the
1064command-line for every run. Hence, here is a snippet from the
1065Distutils' own \file{setup.cfg}:
Fred Drakea09262e2001-03-01 18:35:43 +00001066
Greg Ward47f99a62000-09-04 20:07:15 +00001067\begin{verbatim}
1068[bdist_rpm]
1069release = 1
1070packager = Greg Ward <gward@python.net>
1071doc_files = CHANGES.txt
1072 README.txt
1073 USAGE.txt
1074 doc/
1075 examples/
1076\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001077
Greg Ward47f99a62000-09-04 20:07:15 +00001078Note that the \option{doc\_files} option is simply a
1079whitespace-separated string split across multiple lines for readability.
Greg Ward16aafcd2000-04-09 04:06:44 +00001080
1081
Fred Drakea09262e2001-03-01 18:35:43 +00001082\begin{seealso}
1083 \seetitle[../inst/config-syntax.html]{Installing Python
1084 Modules}{More information on the configuration files is
1085 available in the manual for system administrators.}
1086\end{seealso}
1087
1088
Fred Drake211a2eb2004-03-22 21:44:43 +00001089\chapter{Creating a Source Distribution}
Greg Warde78298a2000-04-28 17:12:24 +00001090\label{source-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +00001091
Greg Warde78298a2000-04-28 17:12:24 +00001092As shown in section~\ref{simple-example}, you use the
Greg Ward16aafcd2000-04-09 04:06:44 +00001093\command{sdist} command to create a source distribution. In the
1094simplest case,
Fred Drakea09262e2001-03-01 18:35:43 +00001095
Greg Ward16aafcd2000-04-09 04:06:44 +00001096\begin{verbatim}
1097python setup.py sdist
1098\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001099
Greg Ward19c67f82000-06-24 01:33:16 +00001100(assuming you haven't specified any \command{sdist} options in the setup
1101script or config file), \command{sdist} creates the archive of the
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001102default format for the current platform. The default format is a gzip'ed
Fred Drakeeff9a872000-10-26 16:41:03 +00001103tar file (\file{.tar.gz}) on \UNIX, and ZIP file on Windows.
Greg Ward54589d42000-09-06 01:37:35 +00001104
Greg Wardd5767a52000-04-19 22:48:09 +00001105You can specify as many formats as you like using the
1106\longprogramopt{formats} option, for example:
Fred Drakea09262e2001-03-01 18:35:43 +00001107
Greg Ward16aafcd2000-04-09 04:06:44 +00001108\begin{verbatim}
1109python setup.py sdist --formats=gztar,zip
1110\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001111
Greg Ward16aafcd2000-04-09 04:06:44 +00001112to create a gzipped tarball and a zip file. The available formats are:
Fred Drake781380c2004-02-19 23:17:46 +00001113
Greg Ward46b98e32000-04-14 01:53:36 +00001114\begin{tableiii}{l|l|c}{code}%
1115 {Format}{Description}{Notes}
Greg Ward54589d42000-09-06 01:37:35 +00001116 \lineiii{zip}{zip file (\file{.zip})}{(1),(3)}
1117 \lineiii{gztar}{gzip'ed tar file (\file{.tar.gz})}{(2),(4)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001118 \lineiii{bztar}{bzip2'ed tar file (\file{.tar.bz2})}{(4)}
Greg Ward47f99a62000-09-04 20:07:15 +00001119 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(4)}
Greg Ward54589d42000-09-06 01:37:35 +00001120 \lineiii{tar}{tar file (\file{.tar})}{(4)}
Greg Ward46b98e32000-04-14 01:53:36 +00001121\end{tableiii}
1122
1123\noindent Notes:
1124\begin{description}
1125\item[(1)] default on Windows
Fred Drakeeff9a872000-10-26 16:41:03 +00001126\item[(2)] default on \UNIX
Greg Wardb6528972000-09-07 02:40:37 +00001127\item[(3)] requires either external \program{zip} utility or
Greg Ward954ce8b2002-05-10 14:42:10 +00001128 \module{zipfile} module (part of the standard Python library since
1129 Python~1.6)
Greg Ward47f99a62000-09-04 20:07:15 +00001130\item[(4)] requires external utilities: \program{tar} and possibly one
1131 of \program{gzip}, \program{bzip2}, or \program{compress}
Greg Ward46b98e32000-04-14 01:53:36 +00001132\end{description}
Greg Ward16aafcd2000-04-09 04:06:44 +00001133
1134
Greg Ward54589d42000-09-06 01:37:35 +00001135
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00001136\section{Specifying the files to distribute}
Greg Warde78298a2000-04-28 17:12:24 +00001137\label{manifest}
Greg Ward16aafcd2000-04-09 04:06:44 +00001138
Greg Ward54589d42000-09-06 01:37:35 +00001139If you don't supply an explicit list of files (or instructions on how to
1140generate one), the \command{sdist} command puts a minimal default set
1141into the source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +00001142\begin{itemize}
Greg Wardfacb8db2000-04-09 04:32:40 +00001143\item all Python source files implied by the \option{py\_modules} and
Greg Ward16aafcd2000-04-09 04:06:44 +00001144 \option{packages} options
Greg Wardfacb8db2000-04-09 04:32:40 +00001145\item all C source files mentioned in the \option{ext\_modules} or
Greg Ward16aafcd2000-04-09 04:06:44 +00001146 \option{libraries} options (\XXX{getting C library sources currently
Fred Drake781380c2004-02-19 23:17:46 +00001147 broken---no \method{get_source_files()} method in \file{build_clib.py}!})
Fred Drake203b10c2004-03-31 01:50:37 +00001148\item scripts identified by the \option{scripts} option
Greg Ward16aafcd2000-04-09 04:06:44 +00001149\item anything that looks like a test script: \file{test/test*.py}
1150 (currently, the Distutils don't do anything with test scripts except
1151 include them in source distributions, but in the future there will be
1152 a standard for testing Python module distributions)
Greg Ward54589d42000-09-06 01:37:35 +00001153\item \file{README.txt} (or \file{README}), \file{setup.py} (or whatever
1154 you called your setup script), and \file{setup.cfg}
Greg Ward16aafcd2000-04-09 04:06:44 +00001155\end{itemize}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001156
Greg Ward16aafcd2000-04-09 04:06:44 +00001157Sometimes this is enough, but usually you will want to specify
1158additional files to distribute. The typical way to do this is to write
1159a \emph{manifest template}, called \file{MANIFEST.in} by default. The
Greg Ward54589d42000-09-06 01:37:35 +00001160manifest template is just a list of instructions for how to generate
1161your manifest file, \file{MANIFEST}, which is the exact list of files to
1162include in your source distribution. The \command{sdist} command
1163processes this template and generates a manifest based on its
1164instructions and what it finds in the filesystem.
1165
1166If you prefer to roll your own manifest file, the format is simple: one
1167filename per line, regular files (or symlinks to them) only. If you do
1168supply your own \file{MANIFEST}, you must specify everything: the
1169default set of files described above does not apply in this case.
Greg Ward16aafcd2000-04-09 04:06:44 +00001170
1171The manifest template has one command per line, where each command
1172specifies a set of files to include or exclude from the source
1173distribution. For an example, again we turn to the Distutils' own
1174manifest template:
Fred Drakea09262e2001-03-01 18:35:43 +00001175
Greg Ward16aafcd2000-04-09 04:06:44 +00001176\begin{verbatim}
1177include *.txt
Greg Ward87da1ea2000-04-21 04:35:25 +00001178recursive-include examples *.txt *.py
Greg Ward16aafcd2000-04-09 04:06:44 +00001179prune examples/sample?/build
1180\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001181
Greg Ward16aafcd2000-04-09 04:06:44 +00001182The meanings should be fairly clear: include all files in the
Fred Drake630e5bd2004-03-23 18:54:12 +00001183distribution root matching \file{*.txt}, all files anywhere under the
1184\file{examples} directory matching \file{*.txt} or \file{*.py}, and
1185exclude all directories matching \file{examples/sample?/build}. All of
Greg Ward54589d42000-09-06 01:37:35 +00001186this is done \emph{after} the standard include set, so you can exclude
1187files from the standard set with explicit instructions in the manifest
1188template. (Or, you can use the \longprogramopt{no-defaults} option to
1189disable the standard set entirely.) There are several other commands
1190available in the manifest template mini-language; see
1191section~\ref{sdist-cmd}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001192
Greg Ward54589d42000-09-06 01:37:35 +00001193The order of commands in the manifest template matters: initially, we
1194have the list of default files as described above, and each command in
1195the template adds to or removes from that list of files. Once we have
1196fully processed the manifest template, we remove files that should not
1197be included in the source distribution:
1198\begin{itemize}
1199\item all files in the Distutils ``build'' tree (default \file{build/})
Tim Peters2f50e902004-05-31 19:27:59 +00001200\item all files in directories named \file{RCS}, \file{CVS} or \file{.svn}
Greg Ward54589d42000-09-06 01:37:35 +00001201\end{itemize}
1202Now we have our complete list of files, which is written to the manifest
1203for future reference, and then used to build the source distribution
1204archive(s).
1205
1206You can disable the default set of included files with the
1207\longprogramopt{no-defaults} option, and you can disable the standard
1208exclude set with \longprogramopt{no-prune}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001209
Greg Ward46b98e32000-04-14 01:53:36 +00001210Following the Distutils' own manifest template, let's trace how the
Greg Ward47f99a62000-09-04 20:07:15 +00001211\command{sdist} command builds the list of files to include in the
Greg Ward46b98e32000-04-14 01:53:36 +00001212Distutils source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +00001213\begin{enumerate}
1214\item include all Python source files in the \file{distutils} and
1215 \file{distutils/command} subdirectories (because packages
1216 corresponding to those two directories were mentioned in the
Greg Ward54589d42000-09-06 01:37:35 +00001217 \option{packages} option in the setup script---see
1218 section~\ref{setup-script})
1219\item include \file{README.txt}, \file{setup.py}, and \file{setup.cfg}
1220 (standard files)
1221\item include \file{test/test*.py} (standard files)
Greg Ward16aafcd2000-04-09 04:06:44 +00001222\item include \file{*.txt} in the distribution root (this will find
1223 \file{README.txt} a second time, but such redundancies are weeded out
1224 later)
Greg Ward54589d42000-09-06 01:37:35 +00001225\item include anything matching \file{*.txt} or \file{*.py} in the
1226 sub-tree under \file{examples},
1227\item exclude all files in the sub-trees starting at directories
1228 matching \file{examples/sample?/build}---this may exclude files
1229 included by the previous two steps, so it's important that the
1230 \code{prune} command in the manifest template comes after the
1231 \code{recursive-include} command
Tim Peters2f50e902004-05-31 19:27:59 +00001232\item exclude the entire \file{build} tree, and any \file{RCS},
1233 \file{CVS} and \file{.svn} directories
Greg Wardfacb8db2000-04-09 04:32:40 +00001234\end{enumerate}
Greg Ward46b98e32000-04-14 01:53:36 +00001235Just like in the setup script, file and directory names in the manifest
1236template should always be slash-separated; the Distutils will take care
1237of converting them to the standard representation on your platform.
1238That way, the manifest template is portable across operating systems.
1239
Greg Ward16aafcd2000-04-09 04:06:44 +00001240
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00001241\section{Manifest-related options}
Greg Warde78298a2000-04-28 17:12:24 +00001242\label{manifest-options}
Greg Ward16aafcd2000-04-09 04:06:44 +00001243
1244The normal course of operations for the \command{sdist} command is as
1245follows:
1246\begin{itemize}
Greg Ward46b98e32000-04-14 01:53:36 +00001247\item if the manifest file, \file{MANIFEST} doesn't exist, read
1248 \file{MANIFEST.in} and create the manifest
Greg Ward54589d42000-09-06 01:37:35 +00001249\item if neither \file{MANIFEST} nor \file{MANIFEST.in} exist, create a
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001250 manifest with just the default file set
Greg Ward1d8f57a2000-08-05 00:43:11 +00001251\item if either \file{MANIFEST.in} or the setup script (\file{setup.py})
1252 are more recent than \file{MANIFEST}, recreate \file{MANIFEST} by
1253 reading \file{MANIFEST.in}
Greg Ward16aafcd2000-04-09 04:06:44 +00001254\item use the list of files now in \file{MANIFEST} (either just
1255 generated or read in) to create the source distribution archive(s)
1256\end{itemize}
Greg Ward54589d42000-09-06 01:37:35 +00001257There are a couple of options that modify this behaviour. First, use
1258the \longprogramopt{no-defaults} and \longprogramopt{no-prune} to
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001259disable the standard ``include'' and ``exclude'' sets.
Greg Ward16aafcd2000-04-09 04:06:44 +00001260
Greg Ward54589d42000-09-06 01:37:35 +00001261Second, you might want to force the manifest to be regenerated---for
Greg Ward16aafcd2000-04-09 04:06:44 +00001262example, if you have added or removed files or directories that match an
1263existing pattern in the manifest template, you should regenerate the
1264manifest:
Fred Drakea09262e2001-03-01 18:35:43 +00001265
Greg Ward16aafcd2000-04-09 04:06:44 +00001266\begin{verbatim}
1267python setup.py sdist --force-manifest
1268\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +00001269
1270Or, you might just want to (re)generate the manifest, but not create a
1271source distribution:
Fred Drakea09262e2001-03-01 18:35:43 +00001272
Greg Ward16aafcd2000-04-09 04:06:44 +00001273\begin{verbatim}
1274python setup.py sdist --manifest-only
1275\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001276
Greg Ward54589d42000-09-06 01:37:35 +00001277\longprogramopt{manifest-only} implies \longprogramopt{force-manifest}.
1278\programopt{-o} is a shortcut for \longprogramopt{manifest-only}, and
1279\programopt{-f} for \longprogramopt{force-manifest}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001280
1281
Fred Drake211a2eb2004-03-22 21:44:43 +00001282\chapter{Creating Built Distributions}
Greg Warde78298a2000-04-28 17:12:24 +00001283\label{built-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +00001284
Greg Ward46b98e32000-04-14 01:53:36 +00001285A ``built distribution'' is what you're probably used to thinking of
1286either as a ``binary package'' or an ``installer'' (depending on your
1287background). It's not necessarily binary, though, because it might
1288contain only Python source code and/or byte-code; and we don't call it a
1289package, because that word is already spoken for in Python. (And
Fred Drake2a1bc502004-02-19 23:03:29 +00001290``installer'' is a term specific to the world of mainstream desktop
1291systems.)
Greg Ward16aafcd2000-04-09 04:06:44 +00001292
Greg Ward46b98e32000-04-14 01:53:36 +00001293A built distribution is how you make life as easy as possible for
1294installers of your module distribution: for users of RPM-based Linux
1295systems, it's a binary RPM; for Windows users, it's an executable
1296installer; for Debian-based Linux users, it's a Debian package; and so
1297forth. Obviously, no one person will be able to create built
Greg Wardb6528972000-09-07 02:40:37 +00001298distributions for every platform under the sun, so the Distutils are
Greg Ward46b98e32000-04-14 01:53:36 +00001299designed to enable module developers to concentrate on their
1300specialty---writing code and creating source distributions---while an
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001301intermediary species called \emph{packagers} springs up to turn source
Greg Ward19c67f82000-06-24 01:33:16 +00001302distributions into built distributions for as many platforms as there
Greg Ward46b98e32000-04-14 01:53:36 +00001303are packagers.
1304
1305Of course, the module developer could be his own packager; or the
1306packager could be a volunteer ``out there'' somewhere who has access to
1307a platform which the original developer does not; or it could be
1308software periodically grabbing new source distributions and turning them
1309into built distributions for as many platforms as the software has
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001310access to. Regardless of who they are, a packager uses the
Greg Ward46b98e32000-04-14 01:53:36 +00001311setup script and the \command{bdist} command family to generate built
1312distributions.
1313
1314As a simple example, if I run the following command in the Distutils
1315source tree:
Fred Drakea09262e2001-03-01 18:35:43 +00001316
Greg Ward46b98e32000-04-14 01:53:36 +00001317\begin{verbatim}
1318python setup.py bdist
1319\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001320
Greg Ward46b98e32000-04-14 01:53:36 +00001321then the Distutils builds my module distribution (the Distutils itself
1322in this case), does a ``fake'' installation (also in the \file{build}
1323directory), and creates the default type of built distribution for my
Greg Wardb6528972000-09-07 02:40:37 +00001324platform. The default format for built distributions is a ``dumb'' tar
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001325file on \UNIX, and a simple executable installer on Windows. (That tar
Greg Wardb6528972000-09-07 02:40:37 +00001326file is considered ``dumb'' because it has to be unpacked in a specific
1327location to work.)
Greg Ward1d8f57a2000-08-05 00:43:11 +00001328
Fred Drakeeff9a872000-10-26 16:41:03 +00001329Thus, the above command on a \UNIX{} system creates
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001330\file{Distutils-1.0.\filevar{plat}.tar.gz}; unpacking this tarball
Greg Wardb6528972000-09-07 02:40:37 +00001331from the right place installs the Distutils just as though you had
1332downloaded the source distribution and run \code{python setup.py
1333 install}. (The ``right place'' is either the root of the filesystem or
1334Python's \filevar{prefix} directory, depending on the options given to
1335the \command{bdist\_dumb} command; the default is to make dumb
1336distributions relative to \filevar{prefix}.)
Greg Ward46b98e32000-04-14 01:53:36 +00001337
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001338Obviously, for pure Python distributions, this isn't any simpler than
1339just running \code{python setup.py install}---but for non-pure
1340distributions, which include extensions that would need to be
1341compiled, it can mean the difference between someone being able to use
1342your extensions or not. And creating ``smart'' built distributions,
1343such as an RPM package or an executable installer for Windows, is far
1344more convenient for users even if your distribution doesn't include
1345any extensions.
Greg Ward46b98e32000-04-14 01:53:36 +00001346
Greg Wardb6528972000-09-07 02:40:37 +00001347The \command{bdist} command has a \longprogramopt{formats} option,
Greg Ward1d8f57a2000-08-05 00:43:11 +00001348similar to the \command{sdist} command, which you can use to select the
1349types of built distribution to generate: for example,
Fred Drakea09262e2001-03-01 18:35:43 +00001350
Greg Ward46b98e32000-04-14 01:53:36 +00001351\begin{verbatim}
1352python setup.py bdist --format=zip
1353\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001354
Fred Drakeeff9a872000-10-26 16:41:03 +00001355would, when run on a \UNIX{} system, create
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001356\file{Distutils-1.0.\filevar{plat}.zip}---again, this archive would be
Greg Ward1d8f57a2000-08-05 00:43:11 +00001357unpacked from the root directory to install the Distutils.
Greg Ward46b98e32000-04-14 01:53:36 +00001358
1359The available formats for built distributions are:
Fred Drake781380c2004-02-19 23:17:46 +00001360
Greg Ward46b98e32000-04-14 01:53:36 +00001361\begin{tableiii}{l|l|c}{code}%
1362 {Format}{Description}{Notes}
Greg Wardb6528972000-09-07 02:40:37 +00001363 \lineiii{gztar}{gzipped tar file (\file{.tar.gz})}{(1),(3)}
1364 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(3)}
1365 \lineiii{tar}{tar file (\file{.tar})}{(3)}
1366 \lineiii{zip}{zip file (\file{.zip})}{(4)}
1367 \lineiii{rpm}{RPM}{(5)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001368 \lineiii{pkgtool}{Solaris \program{pkgtool}}{}
1369 \lineiii{sdux}{HP-UX \program{swinstall}}{}
1370 \lineiii{rpm}{RPM}{(5)}
1371% \lineiii{srpm}{source RPM}{(5) \XXX{to do!}}
Thomas Heller5f52f722001-02-19 17:48:03 +00001372 \lineiii{wininst}{self-extracting ZIP file for Windows}{(2),(4)}
Greg Ward46b98e32000-04-14 01:53:36 +00001373\end{tableiii}
1374
1375\noindent Notes:
1376\begin{description}
Fred Drakeeff9a872000-10-26 16:41:03 +00001377\item[(1)] default on \UNIX
Greg Ward1d8f57a2000-08-05 00:43:11 +00001378\item[(2)] default on Windows \XXX{to-do!}
Greg Wardb6528972000-09-07 02:40:37 +00001379\item[(3)] requires external utilities: \program{tar} and possibly one
1380 of \program{gzip}, \program{bzip2}, or \program{compress}
1381\item[(4)] requires either external \program{zip} utility or
Greg Ward954ce8b2002-05-10 14:42:10 +00001382 \module{zipfile} module (part of the standard Python library since
1383 Python~1.6)
Greg Wardb6528972000-09-07 02:40:37 +00001384\item[(5)] requires external \program{rpm} utility, version 3.0.4 or
1385 better (use \code{rpm --version} to find out which version you have)
Greg Ward46b98e32000-04-14 01:53:36 +00001386\end{description}
1387
1388You don't have to use the \command{bdist} command with the
Greg Wardd5767a52000-04-19 22:48:09 +00001389\longprogramopt{formats} option; you can also use the command that
Greg Ward1d8f57a2000-08-05 00:43:11 +00001390directly implements the format you're interested in. Some of these
Greg Ward46b98e32000-04-14 01:53:36 +00001391\command{bdist} ``sub-commands'' actually generate several similar
1392formats; for instance, the \command{bdist\_dumb} command generates all
1393the ``dumb'' archive formats (\code{tar}, \code{ztar}, \code{gztar}, and
1394\code{zip}), and \command{bdist\_rpm} generates both binary and source
1395RPMs. The \command{bdist} sub-commands, and the formats generated by
1396each, are:
Fred Drake781380c2004-02-19 23:17:46 +00001397
Greg Ward46b98e32000-04-14 01:53:36 +00001398\begin{tableii}{l|l}{command}%
1399 {Command}{Formats}
1400 \lineii{bdist\_dumb}{tar, ztar, gztar, zip}
1401 \lineii{bdist\_rpm}{rpm, srpm}
Greg Ward1d8f57a2000-08-05 00:43:11 +00001402 \lineii{bdist\_wininst}{wininst}
Greg Ward46b98e32000-04-14 01:53:36 +00001403\end{tableii}
Greg Ward16aafcd2000-04-09 04:06:44 +00001404
Greg Wardb6528972000-09-07 02:40:37 +00001405The following sections give details on the individual \command{bdist\_*}
1406commands.
1407
1408
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00001409\section{Creating dumb built distributions}
Greg Wardb6528972000-09-07 02:40:37 +00001410\label{creating-dumb}
1411
1412\XXX{Need to document absolute vs. prefix-relative packages here, but
1413 first I have to implement it!}
1414
1415
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00001416\section{Creating RPM packages}
Greg Wardb6528972000-09-07 02:40:37 +00001417\label{creating-rpms}
1418
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001419The RPM format is used by many popular Linux distributions, including
Greg Wardb6528972000-09-07 02:40:37 +00001420Red Hat, SuSE, and Mandrake. If one of these (or any of the other
1421RPM-based Linux distributions) is your usual environment, creating RPM
1422packages for other users of that same distribution is trivial.
1423Depending on the complexity of your module distribution and differences
1424between Linux distributions, you may also be able to create RPMs that
1425work on different RPM-based distributions.
1426
1427The usual way to create an RPM of your module distribution is to run the
1428\command{bdist\_rpm} command:
Fred Drakea09262e2001-03-01 18:35:43 +00001429
Greg Wardb6528972000-09-07 02:40:37 +00001430\begin{verbatim}
1431python setup.py bdist_rpm
1432\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001433
Greg Wardb6528972000-09-07 02:40:37 +00001434or the \command{bdist} command with the \longprogramopt{format} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001435
Greg Wardb6528972000-09-07 02:40:37 +00001436\begin{verbatim}
1437python setup.py bdist --formats=rpm
1438\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001439
Greg Wardb6528972000-09-07 02:40:37 +00001440The former allows you to specify RPM-specific options; the latter allows
1441you to easily specify multiple formats in one run. If you need to do
1442both, you can explicitly specify multiple \command{bdist\_*} commands
1443and their options:
Fred Drakea09262e2001-03-01 18:35:43 +00001444
Greg Wardb6528972000-09-07 02:40:37 +00001445\begin{verbatim}
Fred Drake630e5bd2004-03-23 18:54:12 +00001446python setup.py bdist_rpm --packager="John Doe <jdoe@example.org>" \
Greg Wardb6528972000-09-07 02:40:37 +00001447 bdist_wininst --target_version="2.0"
1448\end{verbatim}
1449
1450Creating RPM packages is driven by a \file{.spec} file, much as using
1451the Distutils is driven by the setup script. To make your life easier,
1452the \command{bdist\_rpm} command normally creates a \file{.spec} file
1453based on the information you supply in the setup script, on the command
1454line, and in any Distutils configuration files. Various options and
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001455sections in the \file{.spec} file are derived from options in the setup
Greg Wardb6528972000-09-07 02:40:37 +00001456script as follows:
Fred Drake781380c2004-02-19 23:17:46 +00001457
Greg Wardb6528972000-09-07 02:40:37 +00001458\begin{tableii}{l|l}{textrm}%
1459 {RPM \file{.spec} file option or section}{Distutils setup script option}
1460 \lineii{Name}{\option{name}}
1461 \lineii{Summary (in preamble)}{\option{description}}
1462 \lineii{Version}{\option{version}}
1463 \lineii{Vendor}{\option{author} and \option{author\_email}, or \\&
1464 \option{maintainer} and \option{maintainer\_email}}
1465 \lineii{Copyright}{\option{licence}}
1466 \lineii{Url}{\option{url}}
1467 \lineii{\%description (section)}{\option{long\_description}}
1468\end{tableii}
1469
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001470Additionally, there are many options in \file{.spec} files that don't have
Greg Wardb6528972000-09-07 02:40:37 +00001471corresponding options in the setup script. Most of these are handled
1472through options to the \command{bdist\_rpm} command as follows:
Fred Drake781380c2004-02-19 23:17:46 +00001473
Greg Wardb6528972000-09-07 02:40:37 +00001474\begin{tableiii}{l|l|l}{textrm}%
1475 {RPM \file{.spec} file option or section}%
1476 {\command{bdist\_rpm} option}%
1477 {default value}
1478 \lineiii{Release}{\option{release}}{``1''}
1479 \lineiii{Group}{\option{group}}{``Development/Libraries''}
1480 \lineiii{Vendor}{\option{vendor}}{(see above)}
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001481 \lineiii{Packager}{\option{packager}}{(none)}
1482 \lineiii{Provides}{\option{provides}}{(none)}
1483 \lineiii{Requires}{\option{requires}}{(none)}
1484 \lineiii{Conflicts}{\option{conflicts}}{(none)}
1485 \lineiii{Obsoletes}{\option{obsoletes}}{(none)}
Greg Wardb6528972000-09-07 02:40:37 +00001486 \lineiii{Distribution}{\option{distribution\_name}}{(none)}
1487 \lineiii{BuildRequires}{\option{build\_requires}}{(none)}
1488 \lineiii{Icon}{\option{icon}}{(none)}
1489\end{tableiii}
Fred Drake781380c2004-02-19 23:17:46 +00001490
Greg Wardb6528972000-09-07 02:40:37 +00001491Obviously, supplying even a few of these options on the command-line
1492would be tedious and error-prone, so it's usually best to put them in
1493the setup configuration file, \file{setup.cfg}---see
1494section~\ref{setup-config}. If you distribute or package many Python
1495module distributions, you might want to put options that apply to all of
1496them in your personal Distutils configuration file
1497(\file{\textasciitilde/.pydistutils.cfg}).
1498
1499There are three steps to building a binary RPM package, all of which are
1500handled automatically by the Distutils:
Fred Drake781380c2004-02-19 23:17:46 +00001501
Greg Wardb6528972000-09-07 02:40:37 +00001502\begin{enumerate}
1503\item create a \file{.spec} file, which describes the package (analogous
1504 to the Distutils setup script; in fact, much of the information in the
1505 setup script winds up in the \file{.spec} file)
1506\item create the source RPM
1507\item create the ``binary'' RPM (which may or may not contain binary
1508 code, depending on whether your module distribution contains Python
1509 extensions)
1510\end{enumerate}
Fred Drake781380c2004-02-19 23:17:46 +00001511
Greg Wardb6528972000-09-07 02:40:37 +00001512Normally, RPM bundles the last two steps together; when you use the
1513Distutils, all three steps are typically bundled together.
1514
1515If you wish, you can separate these three steps. You can use the
Fred Drake781380c2004-02-19 23:17:46 +00001516\longprogramopt{spec-only} option to make \command{bdist_rpm} just
Greg Wardb6528972000-09-07 02:40:37 +00001517create the \file{.spec} file and exit; in this case, the \file{.spec}
1518file will be written to the ``distribution directory''---normally
1519\file{dist/}, but customizable with the \longprogramopt{dist-dir}
1520option. (Normally, the \file{.spec} file winds up deep in the ``build
Fred Drake781380c2004-02-19 23:17:46 +00001521tree,'' in a temporary directory created by \command{bdist_rpm}.)
Greg Wardb6528972000-09-07 02:40:37 +00001522
Fred Drake781380c2004-02-19 23:17:46 +00001523% \XXX{this isn't implemented yet---is it needed?!}
1524% You can also specify a custom \file{.spec} file with the
1525% \longprogramopt{spec-file} option; used in conjunction with
1526% \longprogramopt{spec-only}, this gives you an opportunity to customize
1527% the \file{.spec} file manually:
1528%
Matthias Klose4c8fa422004-08-04 23:18:49 +00001529% \ begin{verbatim}
Fred Drake781380c2004-02-19 23:17:46 +00001530% > python setup.py bdist_rpm --spec-only
1531% # ...edit dist/FooBar-1.0.spec
1532% > python setup.py bdist_rpm --spec-file=dist/FooBar-1.0.spec
Matthias Klose4c8fa422004-08-04 23:18:49 +00001533% \ end{verbatim}
Fred Drake781380c2004-02-19 23:17:46 +00001534%
1535% (Although a better way to do this is probably to override the standard
1536% \command{bdist\_rpm} command with one that writes whatever else you want
1537% to the \file{.spec} file.)
Greg Wardb6528972000-09-07 02:40:37 +00001538
1539
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00001540\section{Creating Windows Installers}
Greg Wardb6528972000-09-07 02:40:37 +00001541\label{creating-wininst}
1542
Thomas Hellere61f3652002-11-15 20:13:26 +00001543Executable installers are the natural format for binary distributions
1544on Windows. They display a nice graphical user interface, display
1545some information about the module distribution to be installed taken
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +00001546from the metadata in the setup script, let the user select a few
Thomas Hellere61f3652002-11-15 20:13:26 +00001547options, and start or cancel the installation.
Greg Wardb6528972000-09-07 02:40:37 +00001548
Thomas Hellere61f3652002-11-15 20:13:26 +00001549Since the metadata is taken from the setup script, creating Windows
1550installers is usually as easy as running:
Fred Drakea09262e2001-03-01 18:35:43 +00001551
Thomas Heller5f52f722001-02-19 17:48:03 +00001552\begin{verbatim}
1553python setup.py bdist_wininst
1554\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001555
Thomas Heller36343f62002-11-15 19:20:56 +00001556or the \command{bdist} command with the \longprogramopt{formats} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001557
Thomas Heller5f52f722001-02-19 17:48:03 +00001558\begin{verbatim}
1559python setup.py bdist --formats=wininst
1560\end{verbatim}
1561
Thomas Hellere61f3652002-11-15 20:13:26 +00001562If you have a pure module distribution (only containing pure Python
1563modules and packages), the resulting installer will be version
1564independent and have a name like \file{foo-1.0.win32.exe}. These
Fred Drakec54d9252004-02-19 22:16:05 +00001565installers can even be created on \UNIX{} or Mac OS platforms.
Thomas Heller5f52f722001-02-19 17:48:03 +00001566
1567If you have a non-pure distribution, the extensions can only be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001568created on a Windows platform, and will be Python version dependent.
Thomas Heller5f52f722001-02-19 17:48:03 +00001569The installer filename will reflect this and now has the form
Thomas Hellere61f3652002-11-15 20:13:26 +00001570\file{foo-1.0.win32-py2.0.exe}. You have to create a separate installer
Thomas Heller5f52f722001-02-19 17:48:03 +00001571for every Python version you want to support.
1572
1573The installer will try to compile pure modules into bytecode after
Thomas Hellere61f3652002-11-15 20:13:26 +00001574installation on the target system in normal and optimizing mode. If
1575you don't want this to happen for some reason, you can run the
Fred Drake0e9bfa32002-11-15 20:34:52 +00001576\command{bdist_wininst} command with the
1577\longprogramopt{no-target-compile} and/or the
1578\longprogramopt{no-target-optimize} option.
Thomas Hellere61f3652002-11-15 20:13:26 +00001579
Fred Drake0e9bfa32002-11-15 20:34:52 +00001580By default the installer will display the cool ``Python Powered'' logo
Thomas Hellere61f3652002-11-15 20:13:26 +00001581when it is run, but you can also supply your own bitmap which must be
Fred Drake0e9bfa32002-11-15 20:34:52 +00001582a Windows \file{.bmp} file with the \longprogramopt{bitmap} option.
Thomas Hellere61f3652002-11-15 20:13:26 +00001583
1584The installer will also display a large title on the desktop
1585background window when it is run, which is constructed from the name
1586of your distribution and the version number. This can be changed to
1587another text by using the \longprogramopt{title} option.
1588
1589The installer file will be written to the ``distribution directory''
1590--- normally \file{dist/}, but customizable with the
1591\longprogramopt{dist-dir} option.
1592
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00001593\subsection{The Postinstallation script}
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001594\label{postinstallation-script}
1595
1596Starting with Python 2.3, a postinstallation script can be specified
1597which the \longprogramopt{install-script} option. The basename of the
1598script must be specified, and the script filename must also be listed
1599in the scripts argument to the setup function.
1600
1601This script will be run at installation time on the target system
Fred Drakec54d9252004-02-19 22:16:05 +00001602after all the files have been copied, with \code{argv[1]} set to
1603\programopt{-install}, and again at uninstallation time before the
1604files are removed with \code{argv[1]} set to \programopt{-remove}.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001605
1606The installation script runs embedded in the windows installer, every
Fred Drakec54d9252004-02-19 22:16:05 +00001607output (\code{sys.stdout}, \code{sys.stderr}) is redirected into a
1608buffer and will be displayed in the GUI after the script has finished.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001609
Fred Drakea9ee0da2004-02-19 22:28:15 +00001610Some functions especially useful in this context are available as
1611additional built-in functions in the installation script.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001612
Fred Drakea9ee0da2004-02-19 22:28:15 +00001613\begin{funcdesc}{directory_created}{path}
1614\funcline{file_created}{path}
1615 These functions should be called when a directory or file is created
1616 by the postinstall script at installation time. It will register
1617 \var{path} with the uninstaller, so that it will be removed when the
1618 distribution is uninstalled. To be safe, directories are only removed
1619 if they are empty.
1620\end{funcdesc}
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001621
Fred Drakea9ee0da2004-02-19 22:28:15 +00001622\begin{funcdesc}{get_special_folder_path}{csidl_string}
1623 This function can be used to retrieve special folder locations on
1624 Windows like the Start Menu or the Desktop. It returns the full
1625 path to the folder. \var{csidl_string} must be one of the following
1626 strings:
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001627
1628\begin{verbatim}
1629"CSIDL_APPDATA"
1630
1631"CSIDL_COMMON_STARTMENU"
1632"CSIDL_STARTMENU"
1633
1634"CSIDL_COMMON_DESKTOPDIRECTORY"
1635"CSIDL_DESKTOPDIRECTORY"
1636
1637"CSIDL_COMMON_STARTUP"
1638"CSIDL_STARTUP"
1639
1640"CSIDL_COMMON_PROGRAMS"
1641"CSIDL_PROGRAMS"
1642
1643"CSIDL_FONTS"
1644\end{verbatim}
1645
Fred Drakea9ee0da2004-02-19 22:28:15 +00001646 If the folder cannot be retrieved, \exception{OSError} is raised.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001647
Fred Drakea9ee0da2004-02-19 22:28:15 +00001648 Which folders are available depends on the exact Windows version,
1649 and probably also the configuration. For details refer to
1650 Microsoft's documentation of the
1651 \cfunction{SHGetSpecialFolderPath()} function.
1652\end{funcdesc}
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001653
Fred Drakea9ee0da2004-02-19 22:28:15 +00001654\begin{funcdesc}{create_shortcut}{target, description,
1655 filename\optional{,
1656 arguments\optional{,
1657 workdir\optional{,
1658 iconpath\optional{, iconindex}}}}}
1659 This function creates a shortcut.
1660 \var{target} is the path to the program to be started by the shortcut.
Neal Norwitz523c9f02005-08-30 03:34:46 +00001661 \var{description} is the description of the shortcut.
Fred Drakea9ee0da2004-02-19 22:28:15 +00001662 \var{filename} is the title of the shortcut that the user will see.
1663 \var{arguments} specifies the command line arguments, if any.
1664 \var{workdir} is the working directory for the program.
1665 \var{iconpath} is the file containing the icon for the shortcut,
1666 and \var{iconindex} is the index of the icon in the file
1667 \var{iconpath}. Again, for details consult the Microsoft
1668 documentation for the \class{IShellLink} interface.
1669\end{funcdesc}
Greg Wardb6528972000-09-07 02:40:37 +00001670
Fred Drake211a2eb2004-03-22 21:44:43 +00001671\chapter{Registering with the Package Index}
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +00001672\label{package-index}
1673
1674The Python Package Index (PyPI) holds meta-data describing distributions
1675packaged with distutils. The distutils command \command{register} is
1676used to submit your distribution's meta-data to the index. It is invoked
1677as follows:
1678
1679\begin{verbatim}
1680python setup.py register
1681\end{verbatim}
1682
1683Distutils will respond with the following prompt:
1684
1685\begin{verbatim}
1686running register
1687We need to know who you are, so please choose either:
1688 1. use your existing login,
1689 2. register as a new user,
1690 3. have the server generate a new password for you (and email it to you), or
1691 4. quit
1692Your selection [default 1]:
1693\end{verbatim}
1694
1695\noindent Note: if your username and password are saved locally, you will
1696not see this menu.
1697
1698If you have not registered with PyPI, then you will need to do so now. You
1699should choose option 2, and enter your details as required. Soon after
1700submitting your details, you will receive an email which will be used to
1701confirm your registration.
1702
1703Once you are registered, you may choose option 1 from the menu. You will
1704be prompted for your PyPI username and password, and \command{register}
1705will then submit your meta-data to the index.
1706
1707You may submit any number of versions of your distribution to the index. If
1708you alter the meta-data for a particular version, you may submit it again
1709and the index will be updated.
1710
1711PyPI holds a record for each (name, version) combination submitted. The
1712first user to submit information for a given name is designated the Owner
1713of that name. They may submit changes through the \command{register}
1714command or through the web interface. They may also designate other users
1715as Owners or Maintainers. Maintainers may edit the package information, but
1716not designate other Owners or Maintainers.
1717
1718By default PyPI will list all versions of a given package. To hide certain
1719versions, the Hidden property should be set to yes. This must be edited
1720through the web interface.
1721
Martin v. Löwis55f1bb82005-03-21 20:56:35 +00001722\section{The .pypirc file}
1723\label{pypirc}
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +00001724
Martin v. Löwis55f1bb82005-03-21 20:56:35 +00001725The format of the \file{.pypirc} file is formated as follows:
1726
1727\begin{verbatim}
1728[server-login]
1729repository: <repository-url>
1730username: <username>
1731password: <password>
1732\end{verbatim}
1733
1734\var{repository} can be ommitted and defaults to
1735\code{http://www.python.org/pypi}.
1736
1737\chapter{Uploading Packages to the Package Index}
1738\label{package-upload}
1739
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001740\versionadded{2.5}
1741
Martin v. Löwis55f1bb82005-03-21 20:56:35 +00001742The Python Package Index (PyPI) not only stores the package info, but also
1743the package data if the author of the package wishes to. The distutils
1744command \command{upload} pushes the distribution files to PyPI.
1745
Walter Dörwaldc8734a72005-03-23 10:38:59 +00001746The command is invoked immediately after building one or more distribution
Fred Drakeae22bbe2005-03-22 04:09:37 +00001747files. For example, the command
Martin v. Löwis55f1bb82005-03-21 20:56:35 +00001748
1749\begin{verbatim}
1750python setup.py sdist bdist_wininst upload
1751\end{verbatim}
1752
Fred Drakeae22bbe2005-03-22 04:09:37 +00001753will cause the source distribution and the Windows installer to be
1754uploaded to PyPI. Note that these will be uploaded even if they are
1755built using an earlier invocation of \file{setup.py}, but that only
1756distributions named on the command line for the invocation including
1757the \command{upload} command are uploaded.
1758
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001759The \command{upload} command uses the username, password, and repository
1760URL from the \file{\$HOME/.pypirc} file (see section~\ref{pypirc} for
1761more on this file).
1762
Thomas Wouters477c8d52006-05-27 19:21:47 +00001763You can use the \longprogramopt{sign} option to tell \command{upload} to
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001764sign each uploaded file using GPG (GNU Privacy Guard). The
1765\program{gpg} program must be available for execution on the system
1766\envvar{PATH}. You can also specify which key to use for signing
Thomas Wouters477c8d52006-05-27 19:21:47 +00001767using the \longprogramopt{identity=\var{name}} option.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001768
1769Other \command{upload} options include
Thomas Wouters477c8d52006-05-27 19:21:47 +00001770\longprogramopt{repository=\var{url}} (which lets you override the
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001771repository setting from \file{\$HOME/.pypirc}), and
Thomas Wouters477c8d52006-05-27 19:21:47 +00001772\longprogramopt{show-response} (which displays the full response text
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001773from the PyPI server for help in debugging upload problems).
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +00001774
Fred Drake211a2eb2004-03-22 21:44:43 +00001775\chapter{Examples}
Greg Ward007c04a2002-05-10 14:45:59 +00001776\label{examples}
Fred Drake40333ce2004-06-14 22:07:50 +00001777
1778This chapter provides a number of basic examples to help get started
1779with distutils. Additional information about using distutils can be
1780found in the Distutils Cookbook.
1781
1782\begin{seealso}
1783 \seelink{http://www.python.org/cgi-bin/moinmoin/DistutilsCookbook}
1784 {Distutils Cookbook}
1785 {Collection of recipes showing how to achieve more control
1786 over distutils.}
1787\end{seealso}
1788
1789
Fred Drake211a2eb2004-03-22 21:44:43 +00001790\section{Pure Python distribution (by module)}
Greg Ward007c04a2002-05-10 14:45:59 +00001791\label{pure-mod}
1792
1793If you're just distributing a couple of modules, especially if they
1794don't live in a particular package, you can specify them individually
1795using the \option{py\_modules} option in the setup script.
1796
1797In the simplest case, you'll have two files to worry about: a setup
1798script and the single module you're distributing, \file{foo.py} in this
1799example:
1800\begin{verbatim}
1801<root>/
1802 setup.py
1803 foo.py
1804\end{verbatim}
1805(In all diagrams in this section, \verb|<root>| will refer to the
1806distribution root directory.) A minimal setup script to describe this
1807situation would be:
1808\begin{verbatim}
1809from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001810setup(name='foo',
1811 version='1.0',
1812 py_modules=['foo'],
1813 )
Greg Ward007c04a2002-05-10 14:45:59 +00001814\end{verbatim}
1815Note that the name of the distribution is specified independently with
1816the \option{name} option, and there's no rule that says it has to be the
1817same as the name of the sole module in the distribution (although that's
1818probably a good convention to follow). However, the distribution name
1819is used to generate filenames, so you should stick to letters, digits,
1820underscores, and hyphens.
1821
1822Since \option{py\_modules} is a list, you can of course specify multiple
1823modules, eg. if you're distributing modules \module{foo} and
1824\module{bar}, your setup might look like this:
1825\begin{verbatim}
1826<root>/
1827 setup.py
1828 foo.py
1829 bar.py
1830\end{verbatim}
1831and the setup script might be
1832\begin{verbatim}
1833from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001834setup(name='foobar',
1835 version='1.0',
1836 py_modules=['foo', 'bar'],
1837 )
Greg Ward007c04a2002-05-10 14:45:59 +00001838\end{verbatim}
1839
1840You can put module source files into another directory, but if you have
1841enough modules to do that, it's probably easier to specify modules by
1842package rather than listing them individually.
Greg Ward16aafcd2000-04-09 04:06:44 +00001843
1844
Fred Drake211a2eb2004-03-22 21:44:43 +00001845\section{Pure Python distribution (by package)}
Greg Ward007c04a2002-05-10 14:45:59 +00001846\label{pure-pkg}
1847
1848If you have more than a couple of modules to distribute, especially if
1849they are in multiple packages, it's probably easier to specify whole
1850packages rather than individual modules. This works even if your
1851modules are not in a package; you can just tell the Distutils to process
1852modules from the root package, and that works the same as any other
1853package (except that you don't have to have an \file{\_\_init\_\_.py}
1854file).
1855
1856The setup script from the last example could also be written as
1857\begin{verbatim}
1858from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001859setup(name='foobar',
1860 version='1.0',
1861 packages=[''],
1862 )
Greg Ward007c04a2002-05-10 14:45:59 +00001863\end{verbatim}
1864(The empty string stands for the root package.)
1865
1866If those two files are moved into a subdirectory, but remain in the root
1867package, e.g.:
1868\begin{verbatim}
1869<root>/
1870 setup.py
1871 src/ foo.py
1872 bar.py
1873\end{verbatim}
1874then you would still specify the root package, but you have to tell the
1875Distutils where source files in the root package live:
1876\begin{verbatim}
1877from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001878setup(name='foobar',
1879 version='1.0',
1880 package_dir={'': 'src'},
1881 packages=[''],
1882 )
Greg Ward007c04a2002-05-10 14:45:59 +00001883\end{verbatim}
1884
1885More typically, though, you will want to distribute multiple modules in
1886the same package (or in sub-packages). For example, if the \module{foo}
1887and \module{bar} modules belong in package \module{foobar}, one way to
1888layout your source tree is
1889\begin{verbatim}
1890<root>/
1891 setup.py
1892 foobar/
1893 __init__.py
1894 foo.py
1895 bar.py
1896\end{verbatim}
1897This is in fact the default layout expected by the Distutils, and the
1898one that requires the least work to describe in your setup script:
1899\begin{verbatim}
1900from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001901setup(name='foobar',
1902 version='1.0',
1903 packages=['foobar'],
1904 )
Greg Ward007c04a2002-05-10 14:45:59 +00001905\end{verbatim}
1906
1907If you want to put modules in directories not named for their package,
1908then you need to use the \option{package\_dir} option again. For
1909example, if the \file{src} directory holds modules in the
1910\module{foobar} package:
1911\begin{verbatim}
1912<root>/
1913 setup.py
1914 src/
1915 __init__.py
1916 foo.py
1917 bar.py
1918\end{verbatim}
1919an appropriate setup script would be
1920\begin{verbatim}
1921from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001922setup(name='foobar',
1923 version='1.0',
1924 package_dir={'foobar': 'src'},
1925 packages=['foobar'],
1926 )
Greg Ward007c04a2002-05-10 14:45:59 +00001927\end{verbatim}
1928
1929Or, you might put modules from your main package right in the
1930distribution root:
1931\begin{verbatim}
1932<root>/
1933 setup.py
1934 __init__.py
1935 foo.py
1936 bar.py
1937\end{verbatim}
1938in which case your setup script would be
1939\begin{verbatim}
1940from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001941setup(name='foobar',
1942 version='1.0',
1943 package_dir={'foobar': ''},
1944 packages=['foobar'],
1945 )
Greg Ward007c04a2002-05-10 14:45:59 +00001946\end{verbatim}
1947(The empty string also stands for the current directory.)
1948
1949If you have sub-packages, they must be explicitly listed in
1950\option{packages}, but any entries in \option{package\_dir}
1951automatically extend to sub-packages. (In other words, the Distutils
1952does \emph{not} scan your source tree, trying to figure out which
1953directories correspond to Python packages by looking for
1954\file{\_\_init\_\_.py} files.) Thus, if the default layout grows a
1955sub-package:
1956\begin{verbatim}
1957<root>/
1958 setup.py
1959 foobar/
1960 __init__.py
1961 foo.py
1962 bar.py
1963 subfoo/
1964 __init__.py
1965 blah.py
1966\end{verbatim}
1967then the corresponding setup script would be
1968\begin{verbatim}
1969from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001970setup(name='foobar',
1971 version='1.0',
1972 packages=['foobar', 'foobar.subfoo'],
1973 )
Greg Ward007c04a2002-05-10 14:45:59 +00001974\end{verbatim}
1975(Again, the empty string in \option{package\_dir} stands for the current
1976directory.)
Greg Ward16aafcd2000-04-09 04:06:44 +00001977
1978
Fred Drake211a2eb2004-03-22 21:44:43 +00001979\section{Single extension module}
Greg Ward007c04a2002-05-10 14:45:59 +00001980\label{single-ext}
1981
1982Extension modules are specified using the \option{ext\_modules} option.
1983\option{package\_dir} has no effect on where extension source files are
1984found; it only affects the source for pure Python modules. The simplest
1985case, a single extension module in a single C source file, is:
1986\begin{verbatim}
1987<root>/
1988 setup.py
1989 foo.c
1990\end{verbatim}
1991If the \module{foo} extension belongs in the root package, the setup
1992script for this could be
1993\begin{verbatim}
1994from distutils.core import setup
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001995from distutils.extension import Extension
Fred Drake630e5bd2004-03-23 18:54:12 +00001996setup(name='foobar',
1997 version='1.0',
1998 ext_modules=[Extension('foo', ['foo.c'])],
1999 )
Greg Ward007c04a2002-05-10 14:45:59 +00002000\end{verbatim}
2001
2002If the extension actually belongs in a package, say \module{foopkg},
2003then
2004
2005With exactly the same source tree layout, this extension can be put in
2006the \module{foopkg} package simply by changing the name of the
2007extension:
2008\begin{verbatim}
2009from distutils.core import setup
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002010from distutils.extension import Extension
Fred Drake630e5bd2004-03-23 18:54:12 +00002011setup(name='foobar',
2012 version='1.0',
2013 ext_modules=[Extension('foopkg.foo', ['foo.c'])],
2014 )
Greg Ward007c04a2002-05-10 14:45:59 +00002015\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +00002016
2017
Fred Drake211a2eb2004-03-22 21:44:43 +00002018%\section{Multiple extension modules}
Fred Drakea09262e2001-03-01 18:35:43 +00002019%\label{multiple-ext}
Greg Ward16aafcd2000-04-09 04:06:44 +00002020
2021
Fred Drake211a2eb2004-03-22 21:44:43 +00002022%\section{Putting it all together}
Greg Ward16aafcd2000-04-09 04:06:44 +00002023
2024
Fred Drake0c84c7f2004-08-02 21:39:11 +00002025\chapter{Extending Distutils \label{extending}}
2026
2027Distutils can be extended in various ways. Most extensions take the
2028form of new commands or replacements for existing commands. New
2029commands may be written to support new types of platform-specific
2030packaging, for example, while replacements for existing commands may
2031be made to modify details of how the command operates on a package.
2032
2033Most extensions of the distutils are made within \file{setup.py}
2034scripts that want to modify existing commands; many simply add a few
2035file extensions that should be copied into packages in addition to
2036\file{.py} files as a convenience.
2037
2038Most distutils command implementations are subclasses of the
2039\class{Command} class from \refmodule{distutils.cmd}. New commands
2040may directly inherit from \class{Command}, while replacements often
2041derive from \class{Command} indirectly, directly subclassing the
Fred Drakebec69f62004-08-02 23:05:25 +00002042command they are replacing. Commands are required to derive from
2043\class{Command}.
Greg Ward4a9e7222000-04-25 02:57:36 +00002044
2045
Fred Drake211a2eb2004-03-22 21:44:43 +00002046%\section{Extending existing commands}
Fred Drakea09262e2001-03-01 18:35:43 +00002047%\label{extend-existing}
Greg Ward4a9e7222000-04-25 02:57:36 +00002048
2049
Fred Drake211a2eb2004-03-22 21:44:43 +00002050%\section{Writing new commands}
Fred Drakea09262e2001-03-01 18:35:43 +00002051%\label{new-commands}
Greg Ward4a9e7222000-04-25 02:57:36 +00002052
Fred Drakea09262e2001-03-01 18:35:43 +00002053%\XXX{Would an uninstall command be a good example here?}
Thomas Heller5f52f722001-02-19 17:48:03 +00002054
Fred Drake0c84c7f2004-08-02 21:39:11 +00002055\section{Integrating new commands}
2056
2057There are different ways to integrate new command implementations into
2058distutils. The most difficult is to lobby for the inclusion of the
2059new features in distutils itself, and wait for (and require) a version
2060of Python that provides that support. This is really hard for many
2061reasons.
2062
2063The most common, and possibly the most reasonable for most needs, is
2064to include the new implementations with your \file{setup.py} script,
2065and cause the \function{distutils.core.setup()} function use them:
2066
2067\begin{verbatim}
2068from distutils.command.build_py import build_py as _build_py
2069from distutils.core import setup
2070
2071class build_py(_build_py):
2072 """Specialized Python source builder."""
2073
2074 # implement whatever needs to be different...
2075
2076setup(cmdclass={'build_py': build_py},
2077 ...)
2078\end{verbatim}
2079
2080This approach is most valuable if the new implementations must be used
2081to use a particular package, as everyone interested in the package
2082will need to have the new command implementation.
Greg Ward4a9e7222000-04-25 02:57:36 +00002083
Fred Draked04573f2004-08-03 16:37:40 +00002084Beginning with Python 2.4, a third option is available, intended to
2085allow new commands to be added which can support existing
2086\file{setup.py} scripts without requiring modifications to the Python
2087installation. This is expected to allow third-party extensions to
2088provide support for additional packaging systems, but the commands can
2089be used for anything distutils commands can be used for. A new
2090configuration option, \option{command\_packages} (command-line option
2091\longprogramopt{command-packages}), can be used to specify additional
2092packages to be searched for modules implementing commands. Like all
2093distutils options, this can be specified on the command line or in a
2094configuration file. This option can only be set in the
2095\code{[global]} section of a configuration file, or before any
2096commands on the command line. If set in a configuration file, it can
2097be overridden from the command line; setting it to an empty string on
2098the command line causes the default to be used. This should never be
2099set in a configuration file provided with a package.
2100
2101This new option can be used to add any number of packages to the list
2102of packages searched for command implementations; multiple package
2103names should be separated by commas. When not specified, the search
2104is only performed in the \module{distutils.command} package. When
2105\file{setup.py} is run with the option
2106\longprogramopt{command-packages} \programopt{distcmds,buildcmds},
2107however, the packages \module{distutils.command}, \module{distcmds},
2108and \module{buildcmds} will be searched in that order. New commands
2109are expected to be implemented in modules of the same name as the
2110command by classes sharing the same name. Given the example command
2111line option above, the command \command{bdist\_openpkg} could be
2112implemented by the class \class{distcmds.bdist_openpkg.bdist_openpkg}
2113or \class{buildcmds.bdist_openpkg.bdist_openpkg}.
2114
Martin v. Löwis55f1bb82005-03-21 20:56:35 +00002115\section{Adding new distribution types}
2116
Fred Drakeae22bbe2005-03-22 04:09:37 +00002117Commands that create distributions (files in the \file{dist/}
2118directory) need to add \code{(\var{command}, \var{filename})} pairs to
2119\code{self.distribution.dist_files} so that \command{upload} can
2120upload it to PyPI. The \var{filename} in the pair contains no path
2121information, only the name of the file itself. In dry-run mode, pairs
2122should still be added to represent what would have been created.
Greg Ward4a9e7222000-04-25 02:57:36 +00002123
Fred Drake211a2eb2004-03-22 21:44:43 +00002124\chapter{Command Reference}
Greg Ward47f99a62000-09-04 20:07:15 +00002125\label{reference}
Greg Ward16aafcd2000-04-09 04:06:44 +00002126
2127
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00002128%\section{Building modules: the \protect\command{build} command family}
Fred Drakea09262e2001-03-01 18:35:43 +00002129%\label{build-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00002130
Fred Drakea09262e2001-03-01 18:35:43 +00002131%\subsubsection{\protect\command{build}}
2132%\label{build-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00002133
Fred Drakea09262e2001-03-01 18:35:43 +00002134%\subsubsection{\protect\command{build\_py}}
2135%\label{build-py-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00002136
Fred Drakea09262e2001-03-01 18:35:43 +00002137%\subsubsection{\protect\command{build\_ext}}
2138%\label{build-ext-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00002139
Fred Drakea09262e2001-03-01 18:35:43 +00002140%\subsubsection{\protect\command{build\_clib}}
2141%\label{build-clib-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00002142
2143
Fred Drake211a2eb2004-03-22 21:44:43 +00002144\section{Installing modules: the \protect\command{install} command family}
Greg Warde78298a2000-04-28 17:12:24 +00002145\label{install-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00002146
Gregory P. Smith147e5f32000-05-12 00:58:18 +00002147The install command ensures that the build commands have been run and then
2148runs the subcommands \command{install\_lib},
2149\command{install\_data} and
2150\command{install\_scripts}.
2151
Fred Drakea09262e2001-03-01 18:35:43 +00002152%\subsubsection{\protect\command{install\_lib}}
2153%\label{install-lib-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00002154
Fred Drake211a2eb2004-03-22 21:44:43 +00002155\subsection{\protect\command{install\_data}}
Greg Ward1365a302000-08-31 14:47:05 +00002156\label{install-data-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00002157This command installs all data files provided with the distribution.
2158
Fred Drake211a2eb2004-03-22 21:44:43 +00002159\subsection{\protect\command{install\_scripts}}
Greg Ward1365a302000-08-31 14:47:05 +00002160\label{install-scripts-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00002161This command installs all (Python) scripts in the distribution.
2162
Greg Ward16aafcd2000-04-09 04:06:44 +00002163
Fred Drakea09262e2001-03-01 18:35:43 +00002164%\subsection{Cleaning up: the \protect\command{clean} command}
2165%\label{clean-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00002166
2167
Fred Drake211a2eb2004-03-22 21:44:43 +00002168\section{Creating a source distribution: the
Fred Drakeeff9a872000-10-26 16:41:03 +00002169 \protect\command{sdist} command}
Greg Warde78298a2000-04-28 17:12:24 +00002170\label{sdist-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00002171
2172
2173\XXX{fragment moved down from above: needs context!}
Greg Wardb6528972000-09-07 02:40:37 +00002174
Greg Ward16aafcd2000-04-09 04:06:44 +00002175The manifest template commands are:
Fred Drake781380c2004-02-19 23:17:46 +00002176
Greg Ward16aafcd2000-04-09 04:06:44 +00002177\begin{tableii}{ll}{command}{Command}{Description}
Greg Ward87da1ea2000-04-21 04:35:25 +00002178 \lineii{include \var{pat1} \var{pat2} ... }
2179 {include all files matching any of the listed patterns}
2180 \lineii{exclude \var{pat1} \var{pat2} ... }
2181 {exclude all files matching any of the listed patterns}
2182 \lineii{recursive-include \var{dir} \var{pat1} \var{pat2} ... }
2183 {include all files under \var{dir} matching any of the listed patterns}
2184 \lineii{recursive-exclude \var{dir} \var{pat1} \var{pat2} ...}
2185 {exclude all files under \var{dir} matching any of the listed patterns}
2186 \lineii{global-include \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00002187 {include all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00002188 any of the listed patterns}
2189 \lineii{global-exclude \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00002190 {exclude all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00002191 any of the listed patterns}
Greg Ward16aafcd2000-04-09 04:06:44 +00002192 \lineii{prune \var{dir}}{exclude all files under \var{dir}}
2193 \lineii{graft \var{dir}}{include all files under \var{dir}}
2194\end{tableii}
Fred Drake781380c2004-02-19 23:17:46 +00002195
Fred Drakeeff9a872000-10-26 16:41:03 +00002196The patterns here are \UNIX-style ``glob'' patterns: \code{*} matches any
Greg Ward16aafcd2000-04-09 04:06:44 +00002197sequence of regular filename characters, \code{?} matches any single
2198regular filename character, and \code{[\var{range}]} matches any of the
2199characters in \var{range} (e.g., \code{a-z}, \code{a-zA-Z},
Greg Wardfacb8db2000-04-09 04:32:40 +00002200\code{a-f0-9\_.}). The definition of ``regular filename character'' is
Fred Drakeeff9a872000-10-26 16:41:03 +00002201platform-specific: on \UNIX{} it is anything except slash; on Windows
Brett Cannon7706c2d2005-02-13 22:50:04 +00002202anything except backslash or colon; on Mac OS 9 anything except colon.
Greg Wardb6528972000-09-07 02:40:37 +00002203
Brett Cannon7706c2d2005-02-13 22:50:04 +00002204\XXX{Windows support not there yet}
Greg Ward16aafcd2000-04-09 04:06:44 +00002205
2206
Fred Drake211a2eb2004-03-22 21:44:43 +00002207%\section{Creating a built distribution: the
Fred Drakea09262e2001-03-01 18:35:43 +00002208% \protect\command{bdist} command family}
2209%\label{bdist-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00002210
2211
Fred Drake211a2eb2004-03-22 21:44:43 +00002212%\subsection{\protect\command{bdist}}
Greg Ward16aafcd2000-04-09 04:06:44 +00002213
Fred Drake211a2eb2004-03-22 21:44:43 +00002214%\subsection{\protect\command{bdist\_dumb}}
Greg Ward16aafcd2000-04-09 04:06:44 +00002215
Fred Drake211a2eb2004-03-22 21:44:43 +00002216%\subsection{\protect\command{bdist\_rpm}}
Greg Ward16aafcd2000-04-09 04:06:44 +00002217
Fred Drake211a2eb2004-03-22 21:44:43 +00002218%\subsection{\protect\command{bdist\_wininst}}
Fred Drakeab70b382001-08-02 15:13:15 +00002219
2220
Fred Drake6fca7cc2004-03-23 18:43:03 +00002221\chapter{API Reference \label{api-reference}}
2222
2223\section{\module{distutils.core} --- Core Distutils functionality}
2224
2225\declaremodule{standard}{distutils.core}
2226\modulesynopsis{The core Distutils functionality}
2227
2228The \module{distutils.core} module is the only module that needs to be
2229installed to use the Distutils. It provides the \function{setup()} (which
2230is called from the setup script). Indirectly provides the
2231\class{distutils.dist.Distribution} and \class{distutils.cmd.Command} class.
2232
2233\begin{funcdesc}{setup}{arguments}
2234The basic do-everything function that does most everything you could ever
2235ask for from a Distutils method. See XXXXX
2236
2237The setup function takes a large number of arguments. These
2238are laid out in the following table.
2239
2240\begin{tableiii}{c|l|l}{argument name}{argument name}{value}{type}
2241\lineiii{name}{The name of the package}{a string}
2242\lineiii{version}{The version number of the package}{See \refmodule{distutils.version}}
2243\lineiii{description}{A single line describing the package}{a string}
2244\lineiii{long_description}{Longer description of the package}{a string}
2245\lineiii{author}{The name of the package author}{a string}
2246\lineiii{author_email}{The email address of the package author}{a string}
2247\lineiii{maintainer}{The name of the current maintainer, if different from the author}{a string}
2248\lineiii{maintainer_email}{The email address of the current maintainer, if different from the author}{}
2249\lineiii{url}{A URL for the package (homepage)}{a URL}
2250\lineiii{download_url}{A URL to download the package}{a URL}
2251\lineiii{packages}{A list of Python packages that distutils will manipulate}{a list of strings}
2252\lineiii{py_modules}{A list of Python modules that distutils will manipulate}{a list of strings}
2253\lineiii{scripts}{A list of standalone script files to be built and installed}{a list of strings}
2254\lineiii{ext_modules}{A list of Python extensions to be built}{A list of
2255instances of \class{distutils.core.Extension}}
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002256\lineiii{classifiers}{A list of categories for the package}{The list of available categorizations is at \url{http://cheeseshop.python.org/pypi?:action=list_classifiers}.}
Fred Drake6fca7cc2004-03-23 18:43:03 +00002257\lineiii{distclass}{the \class{Distribution} class to use}{A subclass of \class{distutils.core.Distribution}}
2258% What on earth is the use case for script_name?
2259\lineiii{script_name}{The name of the setup.py script - defaults to \code{sys.argv[0]}}{a string}
2260\lineiii{script_args}{Arguments to supply to the setup script}{a list of strings}
2261\lineiii{options}{default options for the setup script}{a string}
2262\lineiii{license}{The license for the package}{}
2263\lineiii{keywords}{Descriptive meta-data. See \pep{314}}{}
2264\lineiii{platforms}{}{}
2265\lineiii{cmdclass}{A mapping of command names to \class{Command} subclasses}{a dictionary}
2266\end{tableiii}
2267
2268\end{funcdesc}
2269
2270\begin{funcdesc}{run_setup}{script_name\optional{, script_args=\code{None}, stop_after=\code{'run'}}}
2271Run a setup script in a somewhat controlled environment, and return
2272the \class{distutils.dist.Distribution} instance that drives things.
2273This is useful if you need to find out the distribution meta-data
2274(passed as keyword args from \var{script} to \function{setup()}), or
2275the contents of the config files or command-line.
2276
2277\var{script_name} is a file that will be run with \function{execfile()}
Fred Drake9687b4d2005-03-10 03:48:14 +00002278\code{sys.argv[0]} will be replaced with \var{script} for the duration of the
Fred Drake6fca7cc2004-03-23 18:43:03 +00002279call. \var{script_args} is a list of strings; if supplied,
Fred Drake9687b4d2005-03-10 03:48:14 +00002280\code{sys.argv[1:]} will be replaced by \var{script_args} for the duration
Fred Drake6fca7cc2004-03-23 18:43:03 +00002281of the call.
2282
2283\var{stop_after} tells \function{setup()} when to stop processing; possible
2284values:
2285
2286\begin{tableii}{c|l}{value}{value}{description}
2287\lineii{init}{Stop after the \class{Distribution} instance has been created
2288and populated with the keyword arguments to \function{setup()}}
2289\lineii{config}{Stop after config files have been parsed (and their data
2290stored in the \class{Distribution} instance)}
2291\lineii{commandline}{Stop after the command-line (\code{sys.argv[1:]} or
2292\var{script_args}) have been parsed (and the data stored in the
2293\class{Distribution} instance.)}
2294\lineii{run}{Stop after all commands have been run (the same as
2295if \function{setup()} had been called in the usual way). This is the default
2296value.}
2297\end{tableii}
2298\end{funcdesc}
2299
2300In addition, the \module{distutils.core} module exposed a number of
2301classes that live elsewhere.
2302
2303\begin{itemize}
2304\item \class{Extension} from \refmodule{distutils.extension}
2305\item \class{Command} from \refmodule{distutils.cmd}
2306\item \class{Distribution} from \refmodule{distutils.dist}
2307\end{itemize}
2308
2309A short description of each of these follows, but see the relevant
2310module for the full reference.
2311
2312\begin{classdesc*}{Extension}
2313
2314The Extension class describes a single C or \Cpp extension module in a
Georg Brandl7eb4b7d2005-07-22 21:49:32 +00002315setup script. It accepts the following keyword arguments in its
Fred Drake6fca7cc2004-03-23 18:43:03 +00002316constructor
2317
2318\begin{tableiii}{c|l|l}{argument name}{argument name}{value}{type}
2319\lineiii{name}{the full name of the extension, including any packages
2320--- ie. \emph{not} a filename or pathname, but Python dotted name}{string}
2321\lineiii{sources}{list of source filenames, relative to the distribution
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002322root (where the setup script lives), in \UNIX{} form (slash-separated) for
Fred Drake6fca7cc2004-03-23 18:43:03 +00002323portability. Source files may be C, \Cpp, SWIG (.i), platform-specific
2324resource files, or whatever else is recognized by the \command{build_ext}
2325command as source for a Python extension.}{string}
2326\lineiii{include_dirs}{list of directories to search for C/\Cpp{} header
2327files (in \UNIX{} form for portability)}{string}
2328\lineiii{define_macros}{list of macros to define; each macro is defined
2329using a 2-tuple, where 'value' is either the string to define it to or
2330\code{None} to define it without a particular value (equivalent of
2331\code{\#define FOO} in source or \programopt{-DFOO} on \UNIX{} C
2332compiler command line) }{ (string,string)
2333tuple or (name,\code{None}) }
2334\lineiii{undef_macros}{list of macros to undefine explicitly}{string}
2335\lineiii{library_dirs}{list of directories to search for C/\Cpp{} libraries
2336at link time }{string}
2337\lineiii{libraries}{list of library names (not filenames or paths) to
2338link against }{string}
2339\lineiii{runtime_library_dirs}{list of directories to search for C/\Cpp{}
2340libraries at run time (for shared extensions, this is when the extension
2341is loaded)}{string}
2342\lineiii{extra_objects}{list of extra files to link with (eg. object
2343files not implied by 'sources', static library that must be explicitly
2344specified, binary resource files, etc.)}{string}
2345\lineiii{extra_compile_args}{any extra platform- and compiler-specific
2346information to use when compiling the source files in 'sources'. For
2347platforms and compilers where a command line makes sense, this is
2348typically a list of command-line arguments, but for other platforms it
2349could be anything.}{string}
2350\lineiii{extra_link_args}{any extra platform- and compiler-specific
2351information to use when linking object files together to create the
2352extension (or to create a new static Python interpreter). Similar
2353interpretation as for 'extra_compile_args'.}{string}
2354\lineiii{export_symbols}{list of symbols to be exported from a shared
2355extension. Not used on all platforms, and not generally necessary for
2356Python extensions, which typically export exactly one symbol: \code{init} +
2357extension_name. }{string}
2358\lineiii{depends}{list of files that the extension depends on }{string}
2359\lineiii{language}{extension language (i.e. \code{'c'}, \code{'c++'},
2360\code{'objc'}). Will be detected from the source extensions if not provided.
2361}{string}
2362\end{tableiii}
2363\end{classdesc*}
2364
2365\begin{classdesc*}{Distribution}
2366A \class{Distribution} describes how to build, install and package up a
2367Python software package.
2368
2369See the \function{setup()} function for a list of keyword arguments accepted
2370by the Distribution constructor. \function{setup()} creates a Distribution
2371instance.
2372\end{classdesc*}
2373
2374\begin{classdesc*}{Command}
Georg Brandl7eb4b7d2005-07-22 21:49:32 +00002375A \class{Command} class (or rather, an instance of one of its subclasses)
Fred Drake6fca7cc2004-03-23 18:43:03 +00002376implement a single distutils command.
2377\end{classdesc*}
2378
2379
2380\section{\module{distutils.ccompiler} --- CCompiler base class}
2381\declaremodule{standard}{distutils.ccompiler}
2382\modulesynopsis{Abstract CCompiler class}
2383
2384This module provides the abstract base class for the \class{CCompiler}
2385classes. A \class{CCompiler} instance can be used for all the compile
2386and link steps needed to build a single project. Methods are provided to
2387set options for the compiler --- macro definitions, include directories,
2388link path, libraries and the like.
2389
2390This module provides the following functions.
2391
2392\begin{funcdesc}{gen_lib_options}{compiler, library_dirs, runtime_library_dirs, libraries}
2393Generate linker options for searching library directories and
2394linking with specific libraries. \var{libraries} and \var{library_dirs} are,
2395respectively, lists of library names (not filenames!) and search
2396directories. Returns a list of command-line options suitable for use
2397with some compiler (depending on the two format strings passed in).
2398\end{funcdesc}
2399
2400\begin{funcdesc}{gen_preprocess_options}{macros, include_dirs}
Fred Drake9687b4d2005-03-10 03:48:14 +00002401Generate C pre-processor options (\programopt{-D}, \programopt{-U},
2402\programopt{-I}) as used by at least
Fred Drake6fca7cc2004-03-23 18:43:03 +00002403two types of compilers: the typical \UNIX{} compiler and Visual \Cpp.
Fred Drake9687b4d2005-03-10 03:48:14 +00002404\var{macros} is the usual thing, a list of 1- or 2-tuples, where
2405\code{(\var{name},)} means undefine (\programopt{-U}) macro \var{name},
2406and \code{(\var{name}, \var{value})} means define (\programopt{-D})
2407macro \var{name} to \var{value}. \var{include_dirs} is just a list of
2408directory names to be added to the header file search path (\programopt{-I}).
2409Returns a list of command-line options suitable for either \UNIX{} compilers
2410or Visual \Cpp.
Fred Drake6fca7cc2004-03-23 18:43:03 +00002411\end{funcdesc}
2412
2413\begin{funcdesc}{get_default_compiler}{osname, platform}
2414Determine the default compiler to use for the given platform.
2415
Fred Drake9687b4d2005-03-10 03:48:14 +00002416\var{osname} should be one of the standard Python OS names (i.e.\ the
2417ones returned by \code{os.name}) and \var{platform} the common value
2418returned by \code{sys.platform} for the platform in question.
Fred Drake6fca7cc2004-03-23 18:43:03 +00002419
2420The default values are \code{os.name} and \code{sys.platform} in case the
2421parameters are not given.
2422\end{funcdesc}
2423
2424\begin{funcdesc}{new_compiler}{plat=\code{None}, compiler=\code{None}, verbose=\code{0}, dry_run=\code{0}, force=\code{0}}
2425Factory function to generate an instance of some CCompiler subclass
2426for the supplied platform/compiler combination. \var{plat} defaults
2427to \code{os.name} (eg. \code{'posix'}, \code{'nt'}), and \var{compiler}
2428defaults to the default compiler for that platform. Currently only
2429\code{'posix'} and \code{'nt'} are supported, and the default
2430compilers are ``traditional \UNIX{} interface'' (\class{UnixCCompiler}
2431class) and Visual \Cpp (\class{MSVCCompiler} class). Note that it's
2432perfectly possible to ask for a \UNIX{} compiler object under Windows,
2433and a Microsoft compiler object under \UNIX---if you supply a value
2434for \var{compiler}, \var{plat} is ignored.
2435% Is the posix/nt only thing still true? Mac OS X seems to work, and
2436% returns a UnixCCompiler instance. How to document this... hmm.
2437\end{funcdesc}
2438
2439\begin{funcdesc}{show_compilers}{}
2440Print list of available compilers (used by the
2441\longprogramopt{help-compiler} options to \command{build},
2442\command{build_ext}, \command{build_clib}).
2443\end{funcdesc}
2444
2445\begin{classdesc}{CCompiler}{\optional{verbose=\code{0}, dry_run=\code{0}, force=\code{0}}}
2446
2447The abstract base class \class{CCompiler} defines the interface that
2448must be implemented by real compiler classes. The class also has
2449some utility methods used by several compiler classes.
2450
2451The basic idea behind a compiler abstraction class is that each
2452instance can be used for all the compile/link steps in building a
2453single project. Thus, attributes common to all of those compile and
2454link steps --- include directories, macros to define, libraries to link
2455against, etc. --- are attributes of the compiler instance. To allow for
2456variability in how individual files are treated, most of those
2457attributes may be varied on a per-compilation or per-link basis.
2458
2459The constructor for each subclass creates an instance of the Compiler
2460object. Flags are \var{verbose} (show verbose output), \var{dry_run}
2461(don't actually execute the steps) and \var{force} (rebuild
2462everything, regardless of dependencies). All of these flags default to
2463\code{0} (off). Note that you probably don't want to instantiate
Georg Brandl7eb4b7d2005-07-22 21:49:32 +00002464\class{CCompiler} or one of its subclasses directly - use the
Fred Drake6fca7cc2004-03-23 18:43:03 +00002465\function{distutils.CCompiler.new_compiler()} factory function
2466instead.
2467
2468The following methods allow you to manually alter compiler options for
2469the instance of the Compiler class.
2470
2471\begin{methoddesc}{add_include_dir}{dir}
2472Add \var{dir} to the list of directories that will be searched for
2473header files. The compiler is instructed to search directories in
2474the order in which they are supplied by successive calls to
2475\method{add_include_dir()}.
2476\end{methoddesc}
2477
2478\begin{methoddesc}{set_include_dirs}{dirs}
2479Set the list of directories that will be searched to \var{dirs} (a
2480list of strings). Overrides any preceding calls to
2481\method{add_include_dir()}; subsequent calls to
2482\method{add_include_dir()} add to the list passed to
2483\method{set_include_dirs()}. This does not affect any list of
2484standard include directories that the compiler may search by default.
2485\end{methoddesc}
2486
2487\begin{methoddesc}{add_library}{libname}
2488
2489Add \var{libname} to the list of libraries that will be included in
2490all links driven by this compiler object. Note that \var{libname}
2491should *not* be the name of a file containing a library, but the
2492name of the library itself: the actual filename will be inferred by
2493the linker, the compiler, or the compiler class (depending on the
2494platform).
2495
2496The linker will be instructed to link against libraries in the
2497order they were supplied to \method{add_library()} and/or
2498\method{set_libraries()}. It is perfectly valid to duplicate library
2499names; the linker will be instructed to link against libraries as
2500many times as they are mentioned.
2501\end{methoddesc}
2502
2503\begin{methoddesc}{set_libraries}{libnames}
2504Set the list of libraries to be included in all links driven by
2505this compiler object to \var{libnames} (a list of strings). This does
2506not affect any standard system libraries that the linker may
2507include by default.
2508\end{methoddesc}
2509
2510\begin{methoddesc}{add_library_dir}{dir}
2511Add \var{dir} to the list of directories that will be searched for
2512libraries specified to \method{add_library()} and
2513\method{set_libraries()}. The linker will be instructed to search for
2514libraries in the order they are supplied to \method{add_library_dir()}
2515and/or \method{set_library_dirs()}.
2516\end{methoddesc}
2517
2518\begin{methoddesc}{set_library_dirs}{dirs}
2519Set the list of library search directories to \var{dirs} (a list of
2520strings). This does not affect any standard library search path
2521that the linker may search by default.
2522\end{methoddesc}
2523
2524\begin{methoddesc}{add_runtime_library_dir}{dir}
2525Add \var{dir} to the list of directories that will be searched for
2526shared libraries at runtime.
2527\end{methoddesc}
2528
2529\begin{methoddesc}{set_runtime_library_dirs}{dirs}
2530Set the list of directories to search for shared libraries at
2531runtime to \var{dirs} (a list of strings). This does not affect any
2532standard search path that the runtime linker may search by
2533default.
2534\end{methoddesc}
2535
2536\begin{methoddesc}{define_macro}{name\optional{, value=\code{None}}}
2537Define a preprocessor macro for all compilations driven by this
2538compiler object. The optional parameter \var{value} should be a
2539string; if it is not supplied, then the macro will be defined
2540without an explicit value and the exact outcome depends on the
2541compiler used (XXX true? does ANSI say anything about this?)
2542\end{methoddesc}
2543
2544\begin{methoddesc}{undefine_macro}{name}
2545Undefine a preprocessor macro for all compilations driven by
2546this compiler object. If the same macro is defined by
2547\method{define_macro()} and undefined by \method{undefine_macro()}
2548the last call takes precedence (including multiple redefinitions or
2549undefinitions). If the macro is redefined/undefined on a
2550per-compilation basis (ie. in the call to \method{compile()}), then that
2551takes precedence.
2552\end{methoddesc}
2553
2554\begin{methoddesc}{add_link_object}{object}
2555Add \var{object} to the list of object files (or analogues, such as
2556explicitly named library files or the output of ``resource
2557compilers'') to be included in every link driven by this compiler
2558object.
2559\end{methoddesc}
2560
2561\begin{methoddesc}{set_link_objects}{objects}
2562Set the list of object files (or analogues) to be included in
2563every link to \var{objects}. This does not affect any standard object
2564files that the linker may include by default (such as system
2565libraries).
2566\end{methoddesc}
2567
2568The following methods implement methods for autodetection of compiler
2569options, providing some functionality similar to GNU \program{autoconf}.
2570
2571\begin{methoddesc}{detect_language}{sources}
2572Detect the language of a given file, or list of files. Uses the
2573instance attributes \member{language_map} (a dictionary), and
2574\member{language_order} (a list) to do the job.
2575\end{methoddesc}
2576
2577\begin{methoddesc}{find_library_file}{dirs, lib\optional{, debug=\code{0}}}
2578Search the specified list of directories for a static or shared
2579library file \var{lib} and return the full path to that file. If
2580\var{debug} is true, look for a debugging version (if that makes sense on
2581the current platform). Return \code{None} if \var{lib} wasn't found in any of
2582the specified directories.
2583\end{methoddesc}
2584
2585\begin{methoddesc}{has_function}{funcname \optional{, includes=\code{None}, include_dirs=\code{None}, libraries=\code{None}, library_dirs=\code{None}}}
2586Return a boolean indicating whether \var{funcname} is supported on
2587the current platform. The optional arguments can be used to
2588augment the compilation environment by providing additional include
2589files and paths and libraries and paths.
2590\end{methoddesc}
2591
2592\begin{methoddesc}{library_dir_option}{dir}
2593Return the compiler option to add \var{dir} to the list of
2594directories searched for libraries.
2595\end{methoddesc}
2596
2597\begin{methoddesc}{library_option}{lib}
2598Return the compiler option to add \var{dir} to the list of libraries
2599linked into the shared library or executable.
2600\end{methoddesc}
2601
2602\begin{methoddesc}{runtime_library_dir_option}{dir}
2603Return the compiler option to add \var{dir} to the list of
2604directories searched for runtime libraries.
2605\end{methoddesc}
2606
2607\begin{methoddesc}{set_executables}{**args}
2608Define the executables (and options for them) that will be run
2609to perform the various stages of compilation. The exact set of
2610executables that may be specified here depends on the compiler
2611class (via the 'executables' class attribute), but most will have:
2612
2613\begin{tableii}{l|l}{attribute}{attribute}{description}
2614\lineii{compiler}{the C/\Cpp{} compiler}
2615\lineii{linker_so}{linker used to create shared objects and libraries}
2616\lineii{linker_exe}{linker used to create binary executables}
2617\lineii{archiver}{static library creator}
2618\end{tableii}
2619
2620On platforms with a command-line (\UNIX, DOS/Windows), each of these
2621is a string that will be split into executable name and (optional)
2622list of arguments. (Splitting the string is done similarly to how
2623\UNIX{} shells operate: words are delimited by spaces, but quotes and
2624backslashes can override this. See
2625\function{distutils.util.split_quoted()}.)
2626\end{methoddesc}
2627
2628The following methods invoke stages in the build process.
2629
2630\begin{methoddesc}{compile}{sources\optional{, output_dir=\code{None}, macros=\code{None}, include_dirs=\code{None}, debug=\code{0}, extra_preargs=\code{None}, extra_postargs=\code{None}, depends=\code{None}}}
2631Compile one or more source files. Generates object files (e.g.
2632transforms a \file{.c} file to a \file{.o} file.)
2633
2634\var{sources} must be a list of filenames, most likely C/\Cpp
2635files, but in reality anything that can be handled by a
2636particular compiler and compiler class (eg. \class{MSVCCompiler} can
2637handle resource files in \var{sources}). Return a list of object
2638filenames, one per source filename in \var{sources}. Depending on
2639the implementation, not all source files will necessarily be
2640compiled, but all corresponding object filenames will be
2641returned.
2642
2643If \var{output_dir} is given, object files will be put under it, while
2644retaining their original path component. That is, \file{foo/bar.c}
2645normally compiles to \file{foo/bar.o} (for a \UNIX{} implementation); if
2646\var{output_dir} is \var{build}, then it would compile to
2647\file{build/foo/bar.o}.
2648
2649\var{macros}, if given, must be a list of macro definitions. A macro
Fred Drake9687b4d2005-03-10 03:48:14 +00002650definition is either a \code{(\var{name}, \var{value})} 2-tuple or a
2651\code{(\var{name},)} 1-tuple.
Fred Drake6fca7cc2004-03-23 18:43:03 +00002652The former defines a macro; if the value is \code{None}, the macro is
2653defined without an explicit value. The 1-tuple case undefines a
2654macro. Later definitions/redefinitions/undefinitions take
2655precedence.
2656
2657\var{include_dirs}, if given, must be a list of strings, the
2658directories to add to the default include file search path for this
2659compilation only.
2660
2661\var{debug} is a boolean; if true, the compiler will be instructed to
2662output debug symbols in (or alongside) the object file(s).
2663
Fred Drake9687b4d2005-03-10 03:48:14 +00002664\var{extra_preargs} and \var{extra_postargs} are implementation-dependent.
Fred Drake6fca7cc2004-03-23 18:43:03 +00002665On platforms that have the notion of a command-line (e.g. \UNIX,
2666DOS/Windows), they are most likely lists of strings: extra
Raymond Hettinger68804312005-01-01 00:28:46 +00002667command-line arguments to prepend/append to the compiler command
Fred Drake6fca7cc2004-03-23 18:43:03 +00002668line. On other platforms, consult the implementation class
2669documentation. In any event, they are intended as an escape hatch
2670for those occasions when the abstract compiler framework doesn't
2671cut the mustard.
2672
2673\var{depends}, if given, is a list of filenames that all targets
2674depend on. If a source file is older than any file in
2675depends, then the source file will be recompiled. This
2676supports dependency tracking, but only at a coarse
2677granularity.
2678
2679Raises \exception{CompileError} on failure.
2680\end{methoddesc}
2681
2682\begin{methoddesc}{create_static_lib}{objects, output_libname\optional{, output_dir=\code{None}, debug=\code{0}, target_lang=\code{None}}}
2683Link a bunch of stuff together to create a static library file.
2684The ``bunch of stuff'' consists of the list of object files supplied
2685as \var{objects}, the extra object files supplied to
2686\method{add_link_object()} and/or \method{set_link_objects()}, the libraries
2687supplied to \method{add_library()} and/or \method{set_libraries()}, and the
2688libraries supplied as \var{libraries} (if any).
2689
2690\var{output_libname} should be a library name, not a filename; the
2691filename will be inferred from the library name. \var{output_dir} is
2692the directory where the library file will be put. XXX defaults to what?
2693
2694\var{debug} is a boolean; if true, debugging information will be
2695included in the library (note that on most platforms, it is the
2696compile step where this matters: the \var{debug} flag is included here
2697just for consistency).
2698
2699\var{target_lang} is the target language for which the given objects
2700are being compiled. This allows specific linkage time treatment of
2701certain languages.
2702
2703Raises \exception{LibError} on failure.
2704\end{methoddesc}
2705
2706\begin{methoddesc}{link}{target_desc, objects, output_filename\optional{, output_dir=\code{None}, libraries=\code{None}, library_dirs=\code{None}, runtime_library_dirs=\code{None}, export_symbols=\code{None}, debug=\code{0}, extra_preargs=\code{None}, extra_postargs=\code{None}, build_temp=\code{None}, target_lang=\code{None}}}
2707Link a bunch of stuff together to create an executable or
2708shared library file.
2709
2710The ``bunch of stuff'' consists of the list of object files supplied
2711as \var{objects}. \var{output_filename} should be a filename. If
2712\var{output_dir} is supplied, \var{output_filename} is relative to it
2713(i.e. \var{output_filename} can provide directory components if
2714needed).
2715
2716\var{libraries} is a list of libraries to link against. These are
2717library names, not filenames, since they're translated into
2718filenames in a platform-specific way (eg. \var{foo} becomes \file{libfoo.a}
2719on \UNIX{} and \file{foo.lib} on DOS/Windows). However, they can include a
2720directory component, which means the linker will look in that
2721specific directory rather than searching all the normal locations.
2722
2723\var{library_dirs}, if supplied, should be a list of directories to
2724search for libraries that were specified as bare library names
2725(ie. no directory component). These are on top of the system
2726default and those supplied to \method{add_library_dir()} and/or
2727\method{set_library_dirs()}. \var{runtime_library_dirs} is a list of
2728directories that will be embedded into the shared library and used
2729to search for other shared libraries that *it* depends on at
2730run-time. (This may only be relevant on \UNIX.)
2731
2732\var{export_symbols} is a list of symbols that the shared library will
2733export. (This appears to be relevant only on Windows.)
2734
2735\var{debug} is as for \method{compile()} and \method{create_static_lib()},
2736with the slight distinction that it actually matters on most platforms (as
2737opposed to \method{create_static_lib()}, which includes a \var{debug} flag
2738mostly for form's sake).
2739
2740\var{extra_preargs} and \var{extra_postargs} are as for \method{compile()}
2741(except of course that they supply command-line arguments for the
2742particular linker being used).
2743
2744\var{target_lang} is the target language for which the given objects
2745are being compiled. This allows specific linkage time treatment of
2746certain languages.
2747
2748Raises \exception{LinkError} on failure.
2749\end{methoddesc}
2750
2751\begin{methoddesc}{link_executable}{objects, output_progname\optional{, output_dir=\code{None}, libraries=\code{None}, library_dirs=\code{None}, runtime_library_dirs=\code{None}, debug=\code{0}, extra_preargs=\code{None}, extra_postargs=\code{None}, target_lang=\code{None}}}
2752Link an executable.
2753\var{output_progname} is the name of the file executable,
2754while \var{objects} are a list of object filenames to link in. Other arguments
2755are as for the \method{link} method.
2756\end{methoddesc}
2757
2758\begin{methoddesc}{link_shared_lib}{objects, output_libname\optional{, output_dir=\code{None}, libraries=\code{None}, library_dirs=\code{None}, runtime_library_dirs=\code{None}, export_symbols=\code{None}, debug=\code{0}, extra_preargs=\code{None}, extra_postargs=\code{None}, build_temp=\code{None}, target_lang=\code{None}}}
2759Link a shared library. \var{output_libname} is the name of the output
2760library, while \var{objects} is a list of object filenames to link in.
2761Other arguments are as for the \method{link} method.
2762\end{methoddesc}
2763
2764\begin{methoddesc}{link_shared_object}{objects, output_filename\optional{, output_dir=\code{None}, libraries=\code{None}, library_dirs=\code{None}, runtime_library_dirs=\code{None}, export_symbols=\code{None}, debug=\code{0}, extra_preargs=\code{None}, extra_postargs=\code{None}, build_temp=\code{None}, target_lang=\code{None}}}
2765Link a shared object. \var{output_filename} is the name of the shared object
2766that will be created, while \var{objects} is a list of object filenames
2767to link in. Other arguments are as for the \method{link} method.
2768\end{methoddesc}
2769
2770\begin{methoddesc}{preprocess}{source\optional{, output_file=\code{None}, macros=\code{None}, include_dirs=\code{None}, extra_preargs=\code{None}, extra_postargs=\code{None}}}
2771Preprocess a single C/\Cpp{} source file, named in \var{source}.
2772Output will be written to file named \var{output_file}, or \var{stdout} if
2773\var{output_file} not supplied. \var{macros} is a list of macro
2774definitions as for \method{compile()}, which will augment the macros set
2775with \method{define_macro()} and \method{undefine_macro()}.
2776\var{include_dirs} is a list of directory names that will be added to the
2777default list, in the same way as \method{add_include_dir()}.
2778
2779Raises \exception{PreprocessError} on failure.
2780\end{methoddesc}
2781
2782The following utility methods are defined by the \class{CCompiler} class,
2783for use by the various concrete subclasses.
2784
2785\begin{methoddesc}{executable_filename}{basename\optional{, strip_dir=\code{0}, output_dir=\code{''}}}
2786Returns the filename of the executable for the given \var{basename}.
2787Typically for non-Windows platforms this is the same as the basename,
2788while Windows will get a \file{.exe} added.
2789\end{methoddesc}
2790
2791\begin{methoddesc}{library_filename}{libname\optional{, lib_type=\code{'static'}, strip_dir=\code{0}, output_dir=\code{''}}}
2792Returns the filename for the given library name on the current platform.
2793On \UNIX{} a library with \var{lib_type} of \code{'static'} will typically
2794be of the form \file{liblibname.a}, while a \var{lib_type} of \code{'dynamic'}
2795will be of the form \file{liblibname.so}.
2796\end{methoddesc}
2797
2798\begin{methoddesc}{object_filenames}{source_filenames\optional{, strip_dir=\code{0}, output_dir=\code{''}}}
2799Returns the name of the object files for the given source files.
2800\var{source_filenames} should be a list of filenames.
2801\end{methoddesc}
2802
2803\begin{methoddesc}{shared_object_filename}{basename\optional{, strip_dir=\code{0}, output_dir=\code{''}}}
2804Returns the name of a shared object file for the given file name \var{basename}.
2805\end{methoddesc}
2806
2807\begin{methoddesc}{execute}{func, args\optional{, msg=\code{None}, level=\code{1}}}
2808Invokes \function{distutils.util.execute()} This method invokes a
2809Python function \var{func} with the given arguments \var{args}, after
2810logging and taking into account the \var{dry_run} flag. XXX see also.
2811\end{methoddesc}
2812
2813\begin{methoddesc}{spawn}{cmd}
2814Invokes \function{distutils.util.spawn()}. This invokes an external
2815process to run the given command. XXX see also.
2816\end{methoddesc}
2817
2818\begin{methoddesc}{mkpath}{name\optional{, mode=\code{511}}}
2819
2820Invokes \function{distutils.dir_util.mkpath()}. This creates a directory
2821and any missing ancestor directories. XXX see also.
2822\end{methoddesc}
2823
2824\begin{methoddesc}{move_file}{src, dst}
2825Invokes \method{distutils.file_util.move_file()}. Renames \var{src} to
2826\var{dst}. XXX see also.
2827\end{methoddesc}
2828
2829\begin{methoddesc}{announce}{msg\optional{, level=\code{1}}}
2830Write a message using \function{distutils.log.debug()}. XXX see also.
2831\end{methoddesc}
2832
2833\begin{methoddesc}{warn}{msg}
2834Write a warning message \var{msg} to standard error.
2835\end{methoddesc}
2836
2837\begin{methoddesc}{debug_print}{msg}
2838If the \var{debug} flag is set on this \class{CCompiler} instance, print
2839\var{msg} to standard output, otherwise do nothing.
2840\end{methoddesc}
2841
2842\end{classdesc}
2843
2844%\subsection{Compiler-specific modules}
2845%
2846%The following modules implement concrete subclasses of the abstract
2847%\class{CCompiler} class. They should not be instantiated directly, but should
2848%be created using \function{distutils.ccompiler.new_compiler()} factory
2849%function.
2850
2851\section{\module{distutils.unixccompiler} --- Unix C Compiler}
2852\declaremodule{standard}{distutils.unixccompiler}
2853\modulesynopsis{UNIX C Compiler}
2854
2855This module provides the \class{UnixCCompiler} class, a subclass of
2856\class{CCompiler} that handles the typical \UNIX-style command-line
2857C compiler:
2858
2859\begin{itemize}
2860\item macros defined with \programopt{-D\var{name}\optional{=value}}
2861\item macros undefined with \programopt{-U\var{name}}
2862\item include search directories specified with
2863 \programopt{-I\var{dir}}
2864\item libraries specified with \programopt{-l\var{lib}}
2865\item library search directories specified with \programopt{-L\var{dir}}
2866\item compile handled by \program{cc} (or similar) executable with
2867 \programopt{-c} option: compiles \file{.c} to \file{.o}
2868\item link static library handled by \program{ar} command (possibly
2869 with \program{ranlib})
2870\item link shared library handled by \program{cc} \programopt{-shared}
2871\end{itemize}
2872
2873\section{\module{distutils.msvccompiler} --- Microsoft Compiler}
2874\declaremodule{standard}{distutils.msvccompiler}
2875\modulesynopsis{Microsoft Compiler}
2876
2877This module provides \class{MSVCCompiler}, an implementation of the abstract
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002878\class{CCompiler} class for Microsoft Visual Studio. Typically, extension
2879modules need to be compiled with the same compiler that was used to compile
2880Python. For Python 2.3 and earlier, the compiler was Visual Studio 6. For
2881Python 2.4 and 2.5, the compiler is Visual Studio .NET 2003. The AMD64
2882and Itanium binaries are created using the Platform SDK.
2883
2884\class{MSVCCompiler} will normally choose the right compiler, linker etc.
2885on its own. To override this choice, the environment variables
2886\var{DISTUTILS\_USE\_SDK} and \var{MSSdk} must be both set. \var{MSSdk}
2887indicates that the current environment has been setup by the SDK's
2888\code{SetEnv.Cmd} script, or that the environment variables had been
2889registered when the SDK was installed; \var{DISTUTILS\_USE\_SDK} indicates
2890that the distutils user has made an explicit choice to override the
2891compiler selection by \class{MSVCCompiler}.
Fred Drake6fca7cc2004-03-23 18:43:03 +00002892
2893\section{\module{distutils.bcppcompiler} --- Borland Compiler}
2894\declaremodule{standard}{distutils.bcppcompiler}
2895This module provides \class{BorlandCCompiler}, an subclass of the abstract \class{CCompiler} class for the Borland \Cpp{} compiler.
2896
2897\section{\module{distutils.cygwincompiler} --- Cygwin Compiler}
2898\declaremodule{standard}{distutils.cygwinccompiler}
2899
2900This module provides the \class{CygwinCCompiler} class, a subclass of \class{UnixCCompiler} that
2901handles the Cygwin port of the GNU C compiler to Windows. It also contains
2902the Mingw32CCompiler class which handles the mingw32 port of GCC (same as
2903cygwin in no-cygwin mode).
2904
2905\section{\module{distutils.emxccompiler} --- OS/2 EMX Compiler}
2906\declaremodule{standard}{distutils.emxccompiler}
2907\modulesynopsis{OS/2 EMX Compiler support}
2908
2909This module provides the EMXCCompiler class, a subclass of \class{UnixCCompiler} that handles the EMX port of the GNU C compiler to OS/2.
2910
2911\section{\module{distutils.mwerkscompiler} --- Metrowerks CodeWarrior support}
2912\declaremodule{standard}{distutils.mwerkscompiler}
2913\modulesynopsis{Metrowerks CodeWarrior support}
2914
2915Contains \class{MWerksCompiler}, an implementation of the abstract
Brett Cannon7706c2d2005-02-13 22:50:04 +00002916\class{CCompiler} class for MetroWerks CodeWarrior on the pre-Mac OS X Macintosh.
2917Needs work to support CW on Windows or Mac OS X.
Fred Drake6fca7cc2004-03-23 18:43:03 +00002918
2919
2920%\subsection{Utility modules}
2921%
2922%The following modules all provide general utility functions. They haven't
2923%all been documented yet.
2924
2925\section{\module{distutils.archive_util} ---
2926 Archiving utilities}
2927\declaremodule[distutils.archiveutil]{standard}{distutils.archive_util}
2928\modulesynopsis{Utility functions for creating archive files (tarballs, zip files, ...)}
2929
2930This module provides a few functions for creating archive files, such as
2931tarballs or zipfiles.
2932
2933\begin{funcdesc}{make_archive}{base_name, format\optional{, root_dir=\code{None}, base_dir=\code{None}, verbose=\code{0}, dry_run=\code{0}}}
2934Create an archive file (eg. \code{zip} or \code{tar}). \var{base_name}
2935is the name of the file to create, minus any format-specific extension;
2936\var{format} is the archive format: one of \code{zip}, \code{tar},
2937\code{ztar}, or \code{gztar}.
2938\var{root_dir} is a directory that will be the root directory of the
2939archive; ie. we typically \code{chdir} into \var{root_dir} before
2940creating the archive. \var{base_dir} is the directory where we start
2941archiving from; ie. \var{base_dir} will be the common prefix of all files and
2942directories in the archive. \var{root_dir} and \var{base_dir} both default
2943to the current directory. Returns the name of the archive file.
2944
2945\warning{This should be changed to support bz2 files}
2946\end{funcdesc}
2947
2948\begin{funcdesc}{make_tarball}{base_name, base_dir\optional{, compress=\code{'gzip'}, verbose=\code{0}, dry_run=\code{0}}}'Create an (optional compressed) archive as a tar file from all files in and under \var{base_dir}. \var{compress} must be \code{'gzip'} (the default),
Fred Drake9687b4d2005-03-10 03:48:14 +00002949\code{'compress'}, \code{'bzip2'}, or \code{None}. Both \program{tar}
2950and the compression utility named by \var{compress} must be on the
Fred Drake6fca7cc2004-03-23 18:43:03 +00002951default program search path, so this is probably \UNIX-specific. The
2952output tar file will be named \file{\var{base_dir}.tar}, possibly plus
2953the appropriate compression extension (\file{.gz}, \file{.bz2} or
2954\file{.Z}). Return the output filename.
2955
2956\warning{This should be replaced with calls to the \module{tarfile} module.}
2957\end{funcdesc}
2958
2959\begin{funcdesc}{make_zipfile}{base_name, base_dir\optional{, verbose=\code{0}, dry_run=\code{0}}}
2960Create a zip file from all files in and under \var{base_dir}. The output
2961zip file will be named \var{base_dir} + \file{.zip}. Uses either the
2962\module{zipfile} Python module (if available) or the InfoZIP \file{zip}
2963utility (if installed and found on the default search path). If neither
2964tool is available, raises \exception{DistutilsExecError}.
2965Returns the name of the output zip file.
2966\end{funcdesc}
2967
2968\section{\module{distutils.dep_util} --- Dependency checking}
2969\declaremodule[distutils.deputil]{standard}{distutils.dep_util}
2970\modulesynopsis{Utility functions for simple dependency checking}
2971
2972This module provides functions for performing simple, timestamp-based
2973dependency of files and groups of files; also, functions based entirely
2974on such timestamp dependency analysis.
2975
2976\begin{funcdesc}{newer}{source, target}
2977Return true if \var{source} exists and is more recently modified than
2978\var{target}, or if \var{source} exists and \var{target} doesn't.
2979Return false if both exist and \var{target} is the same age or newer
2980than \var{source}.
2981Raise \exception{DistutilsFileError} if \var{source} does not exist.
2982\end{funcdesc}
2983
2984\begin{funcdesc}{newer_pairwise}{sources, targets}
2985Walk two filename lists in parallel, testing if each source is newer
2986than its corresponding target. Return a pair of lists (\var{sources},
2987\var{targets}) where source is newer than target, according to the semantics
2988of \function{newer()}
2989%% equivalent to a listcomp...
2990\end{funcdesc}
2991
2992\begin{funcdesc}{newer_group}{sources, target\optional{, missing=\code{'error'}}}
2993Return true if \var{target} is out-of-date with respect to any file
2994listed in \var{sources} In other words, if \var{target} exists and is newer
2995than every file in \var{sources}, return false; otherwise return true.
2996\var{missing} controls what we do when a source file is missing; the
2997default (\code{'error'}) is to blow up with an \exception{OSError} from
2998inside \function{os.stat()};
2999if it is \code{'ignore'}, we silently drop any missing source files; if it is
3000\code{'newer'}, any missing source files make us assume that \var{target} is
3001out-of-date (this is handy in ``dry-run'' mode: it'll make you pretend to
3002carry out commands that wouldn't work because inputs are missing, but
3003that doesn't matter because you're not actually going to run the
3004commands).
3005\end{funcdesc}
3006
3007\section{\module{distutils.dir_util} --- Directory tree operations}
3008\declaremodule[distutils.dirutil]{standard}{distutils.dir_util}
3009\modulesynopsis{Utility functions for operating on directories and directory trees}
3010
3011This module provides functions for operating on directories and trees
3012of directories.
3013
3014\begin{funcdesc}{mkpath}{name\optional{, mode=\code{0777}, verbose=\code{0}, dry_run=\code{0}}}
3015Create a directory and any missing ancestor directories. If the
3016directory already exists (or if \var{name} is the empty string, which
3017means the current directory, which of course exists), then do
3018nothing. Raise \exception{DistutilsFileError} if unable to create some
3019directory along the way (eg. some sub-path exists, but is a file
3020rather than a directory). If \var{verbose} is true, print a one-line
3021summary of each mkdir to stdout. Return the list of directories
3022actually created.
3023\end{funcdesc}
3024
3025\begin{funcdesc}{create_tree}{base_dir, files\optional{, mode=\code{0777}, verbose=\code{0}, dry_run=\code{0}}}
3026Create all the empty directories under \var{base_dir} needed to
3027put \var{files} there. \var{base_dir} is just the a name of a directory
3028which doesn't necessarily exist yet; \var{files} is a list of filenames
3029to be interpreted relative to \var{base_dir}. \var{base_dir} + the
3030directory portion of every file in \var{files} will be created if it
3031doesn't already exist. \var{mode}, \var{verbose} and \var{dry_run} flags
3032are as for \function{mkpath()}.
3033\end{funcdesc}
3034
3035\begin{funcdesc}{copy_tree}{src, dst\optional{preserve_mode=\code{1}, preserve_times=\code{1}, preserve_symlinks=\code{0}, update=\code{0}, verbose=\code{0}, dry_run=\code{0}}}
3036Copy an entire directory tree \var{src} to a new location \var{dst}. Both
3037\var{src} and \var{dst} must be directory names. If \var{src} is not a
3038directory, raise \exception{DistutilsFileError}. If \var{dst} does
Fred Drake9687b4d2005-03-10 03:48:14 +00003039not exist, it is created with \function{mkpath()}. The end result of the
Fred Drake6fca7cc2004-03-23 18:43:03 +00003040copy is that every file in \var{src} is copied to \var{dst}, and
3041directories under \var{src} are recursively copied to \var{dst}.
3042Return the list of files that were copied or might have been copied,
3043using their output name. The return value is unaffected by \var{update}
3044or \var{dry_run}: it is simply the list of all files under \var{src},
3045with the names changed to be under \var{dst}.
3046
3047\var{preserve_mode} and \var{preserve_times} are the same as for
3048\function{copy_file} in \refmodule[distutils.fileutil]{distutils.file_util};
3049note that they only apply to regular files, not to directories. If
3050\var{preserve_symlinks} is true, symlinks will be copied as symlinks
3051(on platforms that support them!); otherwise (the default), the
3052destination of the symlink will be copied. \var{update} and
3053\var{verbose} are the same as for
3054\function{copy_file()}.
3055\end{funcdesc}
3056
3057\begin{funcdesc}{remove_tree}{directory\optional{verbose=\code{0}, dry_run=\code{0}}}
3058Recursively remove \var{directory} and all files and directories underneath
Fred Drake9687b4d2005-03-10 03:48:14 +00003059it. Any errors are ignored (apart from being reported to \code{sys.stdout} if
Fred Drake6fca7cc2004-03-23 18:43:03 +00003060\var{verbose} is true).
3061\end{funcdesc}
3062
3063\XXX{Some of this could be replaced with the shutil module?}
3064
3065\section{\module{distutils.file_util} --- Single file operations}
3066\declaremodule[distutils.fileutil]{standard}{distutils.file_util}
3067\modulesynopsis{Utility functions for operating on single files}
3068
3069This module contains some utility functions for operating on individual files.
3070
3071\begin{funcdesc}{copy_file}{src, dst\optional{preserve_mode=\code{1}, preserve_times=\code{1}, update=\code{0}, link=\code{None}, verbose=\code{0}, dry_run=\code{0}}}
3072Copy file \var{src} to \var{dst}. If \var{dst} is a directory, then
3073\var{src} is copied there with the same name; otherwise, it must be a
3074filename. (If the file exists, it will be ruthlessly clobbered.) If
3075\var{preserve_mode} is true (the default), the file's mode (type and
3076permission bits, or whatever is analogous on the current platform) is
3077copied. If \var{preserve_times} is true (the default), the last-modified
3078and last-access times are copied as well. If \var{update} is true,
3079\var{src} will only be copied if \var{dst} does not exist, or if
3080\var{dst} does exist but is older than \var{src}.
3081
3082\var{link} allows you to make hard links (using \function{os.link}) or
3083symbolic links (using \function{os.symlink}) instead of copying: set it
3084to \code{'hard'} or \code{'sym'}; if it is \code{None} (the default),
3085files are copied. Don't set \var{link} on systems that don't support
3086it: \function{copy_file()} doesn't check if hard or symbolic linking is
Fred Drake9687b4d2005-03-10 03:48:14 +00003087available. It uses \function{_copy_file_contents()} to copy file contents.
Fred Drake6fca7cc2004-03-23 18:43:03 +00003088
3089Return a tuple \samp{(dest_name, copied)}: \var{dest_name} is the actual
3090name of the output file, and \var{copied} is true if the file was copied
3091(or would have been copied, if \var{dry_run} true).
3092% XXX if the destination file already exists, we clobber it if
3093% copying, but blow up if linking. Hmmm. And I don't know what
3094% macostools.copyfile() does. Should definitely be consistent, and
3095% should probably blow up if destination exists and we would be
3096% changing it (ie. it's not already a hard/soft link to src OR
3097% (not update) and (src newer than dst)).
3098\end{funcdesc}
3099
3100\begin{funcdesc}{move_file}{src, dst\optional{verbose, dry_run}}
3101Move file \var{src} to \var{dst}. If \var{dst} is a directory, the file will
3102be moved into it with the same name; otherwise, \var{src} is just renamed
3103to \var{dst}. Returns the new full name of the file.
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003104\warning{Handles cross-device moves on \UNIX{} using \function{copy_file()}.
Fred Drake6fca7cc2004-03-23 18:43:03 +00003105What about other systems???}
3106\end{funcdesc}
3107
3108\begin{funcdesc}{write_file}{filename, contents}
3109Create a file called \var{filename} and write \var{contents} (a
3110sequence of strings without line terminators) to it.
3111\end{funcdesc}
3112
Thomas Heller949f6612004-06-18 06:55:28 +00003113\section{\module{distutils.util} --- Miscellaneous other utility functions}
Fred Drake6fca7cc2004-03-23 18:43:03 +00003114\declaremodule{standard}{distutils.util}
3115\modulesynopsis{Miscellaneous other utility functions}
3116
3117This module contains other assorted bits and pieces that don't fit into
3118any other utility module.
3119
3120\begin{funcdesc}{get_platform}{}
3121Return a string that identifies the current platform. This is used
3122mainly to distinguish platform-specific build directories and
3123platform-specific built distributions. Typically includes the OS name
3124and version and the architecture (as supplied by 'os.uname()'),
3125although the exact information included depends on the OS; eg. for IRIX
3126the architecture isn't particularly important (IRIX only runs on SGI
3127hardware), but for Linux the kernel version isn't particularly
3128important.
3129
3130Examples of returned values:
3131\begin{itemize}
3132\item \code{linux-i586}
3133\item \code{linux-alpha}
3134\item \code{solaris-2.6-sun4u}
3135\item \code{irix-5.3}
3136\item \code{irix64-6.2}
3137\end{itemize}
3138
3139For non-\POSIX{} platforms, currently just returns \code{sys.platform}.
3140% XXX isn't this also provided by some other non-distutils module?
3141\end{funcdesc}
3142
3143\begin{funcdesc}{convert_path}{pathname}
3144Return 'pathname' as a name that will work on the native filesystem,
3145i.e. split it on '/' and put it back together again using the current
3146directory separator. Needed because filenames in the setup script are
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003147always supplied in \UNIX{} style, and have to be converted to the local
Fred Drake6fca7cc2004-03-23 18:43:03 +00003148convention before we can actually use them in the filesystem. Raises
3149\exception{ValueError} on non-\UNIX-ish systems if \var{pathname} either
3150starts or ends with a slash.
3151\end{funcdesc}
3152
3153\begin{funcdesc}{change_root}{new_root, pathname}
3154Return \var{pathname} with \var{new_root} prepended. If \var{pathname} is
3155relative, this is equivalent to \samp{os.path.join(new_root,pathname)}
3156Otherwise, it requires making \var{pathname} relative and then joining the
Brett Cannon7706c2d2005-02-13 22:50:04 +00003157two, which is tricky on DOS/Windows.
Fred Drake6fca7cc2004-03-23 18:43:03 +00003158\end{funcdesc}
3159
3160\begin{funcdesc}{check_environ}{}
3161Ensure that 'os.environ' has all the environment variables we
3162guarantee that users can use in config files, command-line options,
3163etc. Currently this includes:
3164\begin{itemize}
3165\item \envvar{HOME} - user's home directory (\UNIX{} only)
3166\item \envvar{PLAT} - description of the current platform, including
3167 hardware and OS (see \function{get_platform()})
3168\end{itemize}
3169\end{funcdesc}
3170
3171\begin{funcdesc}{subst_vars}{s, local_vars}
3172Perform shell/Perl-style variable substitution on \var{s}. Every
3173occurrence of \code{\$} followed by a name is considered a variable, and
3174variable is substituted by the value found in the \var{local_vars}
3175dictionary, or in \code{os.environ} if it's not in \var{local_vars}.
3176\var{os.environ} is first checked/augmented to guarantee that it contains
3177certain values: see \function{check_environ()}. Raise \exception{ValueError}
3178for any variables not found in either \var{local_vars} or \code{os.environ}.
3179
3180Note that this is not a fully-fledged string interpolation function. A
3181valid \code{\$variable} can consist only of upper and lower case letters,
3182numbers and an underscore. No \{ \} or \( \) style quoting is available.
3183\end{funcdesc}
3184
3185\begin{funcdesc}{grok_environment_error}{exc\optional{, prefix=\samp{'error: '}}}
3186Generate a useful error message from an \exception{EnvironmentError}
3187(\exception{IOError} or \exception{OSError}) exception object.
3188Handles Python 1.5.1 and later styles, and does what it can to deal with
3189exception objects that don't have a filename (which happens when the error
3190is due to a two-file operation, such as \function{rename()} or
3191\function{link()}). Returns the error message as a string prefixed
3192with \var{prefix}.
3193\end{funcdesc}
3194
3195\begin{funcdesc}{split_quoted}{s}
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003196Split a string up according to \UNIX{} shell-like rules for quotes and
Fred Drake6fca7cc2004-03-23 18:43:03 +00003197backslashes. In short: words are delimited by spaces, as long as those
3198spaces are not escaped by a backslash, or inside a quoted string.
3199Single and double quotes are equivalent, and the quote characters can
3200be backslash-escaped. The backslash is stripped from any two-character
3201escape sequence, leaving only the escaped character. The quote
3202characters are stripped from any quoted string. Returns a list of
3203words.
3204% Should probably be moved into the standard library.
3205\end{funcdesc}
3206
3207\begin{funcdesc}{execute}{func, args\optional{, msg=\code{None}, verbose=\code{0}, dry_run=\code{0}}}
3208Perform some action that affects the outside world (for instance,
3209writing to the filesystem). Such actions are special because they
3210are disabled by the \var{dry_run} flag. This method takes
3211care of all that bureaucracy for you; all you have to do is supply the
3212function to call and an argument tuple for it (to embody the
3213``external action'' being performed), and an optional message to
3214print.
3215\end{funcdesc}
3216
3217\begin{funcdesc}{strtobool}{val}
3218Convert a string representation of truth to true (1) or false (0).
3219
3220True values are \code{y}, \code{yes}, \code{t}, \code{true}, \code{on}
3221and \code{1}; false values are \code{n}, \code{no}, \code{f}, \code{false},
3222\code{off} and \code{0}. Raises \exception{ValueError} if \var{val}
3223is anything else.
3224\end{funcdesc}
3225
3226\begin{funcdesc}{byte_compile}{py_files\optional{,
3227 optimize=\code{0}, force=\code{0},
3228 prefix=\code{None}, base_dir=\code{None},
3229 verbose=\code{1}, dry_run=\code{0},
3230 direct=\code{None}}}
3231Byte-compile a collection of Python source files to either \file{.pyc}
3232or \file{.pyo} files in the same directory. \var{py_files} is a list of files
3233to compile; any files that don't end in \file{.py} are silently skipped.
3234\var{optimize} must be one of the following:
3235\begin{itemize}
3236\item \code{0} - don't optimize (generate \file{.pyc})
3237\item \code{1} - normal optimization (like \samp{python -O})
3238\item \code{2} - extra optimization (like \samp{python -OO})
3239\end{itemize}
3240
3241If \var{force} is true, all files are recompiled regardless of
3242timestamps.
3243
3244The source filename encoded in each bytecode file defaults to the
3245filenames listed in \var{py_files}; you can modify these with \var{prefix} and
3246\var{basedir}. \var{prefix} is a string that will be stripped off of each
3247source filename, and \var{base_dir} is a directory name that will be
3248prepended (after \var{prefix} is stripped). You can supply either or both
3249(or neither) of \var{prefix} and \var{base_dir}, as you wish.
3250
3251If \var{dry_run} is true, doesn't actually do anything that would
3252affect the filesystem.
3253
3254Byte-compilation is either done directly in this interpreter process
3255with the standard \module{py_compile} module, or indirectly by writing a
3256temporary script and executing it. Normally, you should let
3257\function{byte_compile()} figure out to use direct compilation or not (see
3258the source for details). The \var{direct} flag is used by the script
3259generated in indirect mode; unless you know what you're doing, leave
3260it set to \code{None}.
3261\end{funcdesc}
3262
3263\begin{funcdesc}{rfc822_escape}{header}
3264Return a version of \var{header} escaped for inclusion in an
3265\rfc{822} header, by ensuring there are 8 spaces space after each newline.
3266Note that it does no other modification of the string.
3267% this _can_ be replaced
3268\end{funcdesc}
3269
3270%\subsection{Distutils objects}
3271
3272\section{\module{distutils.dist} --- The Distribution class}
3273\declaremodule{standard}{distutils.dist}
3274\modulesynopsis{Provides the Distribution class, which represents the
3275 module distribution being built/installed/distributed}
3276
3277This module provides the \class{Distribution} class, which represents
3278the module distribution being built/installed/distributed.
3279
3280
3281\section{\module{distutils.extension} --- The Extension class}
3282\declaremodule{standard}{distutils.extension}
3283\modulesynopsis{Provides the Extension class, used to describe
3284 C/\Cpp{} extension modules in setup scripts}
3285
3286This module provides the \class{Extension} class, used to describe
3287C/\Cpp{} extension modules in setup scripts.
3288
3289%\subsection{Ungrouped modules}
3290%The following haven't been moved into a more appropriate section yet.
3291
3292\section{\module{distutils.debug} --- Distutils debug mode}
3293\declaremodule{standard}{distutils.debug}
3294\modulesynopsis{Provides the debug flag for distutils}
3295
3296This module provides the DEBUG flag.
3297
3298\section{\module{distutils.errors} --- Distutils exceptions}
3299\declaremodule{standard}{distutils.errors}
3300\modulesynopsis{Provides standard distutils exceptions}
3301
3302Provides exceptions used by the Distutils modules. Note that Distutils
3303modules may raise standard exceptions; in particular, SystemExit is
3304usually raised for errors that are obviously the end-user's fault
3305(eg. bad command-line arguments).
3306
3307This module is safe to use in \samp{from ... import *} mode; it only exports
3308symbols whose names start with \code{Distutils} and end with \code{Error}.
3309
3310\section{\module{distutils.fancy_getopt}
3311 --- Wrapper around the standard getopt module}
3312\declaremodule[distutils.fancygetopt]{standard}{distutils.fancy_getopt}
3313\modulesynopsis{Additional \module{getopt} functionality}
3314
3315This module provides a wrapper around the standard \module{getopt}
3316module that provides the following additional features:
3317
3318\begin{itemize}
3319\item short and long options are tied together
3320\item options have help strings, so \function{fancy_getopt} could potentially
3321create a complete usage summary
3322\item options set attributes of a passed-in object
3323\item boolean options can have ``negative aliases'' --- eg. if
3324\longprogramopt{quiet} is the ``negative alias'' of
3325\longprogramopt{verbose}, then \longprogramopt{quiet} on the command
3326line sets \var{verbose} to false.
3327
3328\end{itemize}
3329
3330\XXX{Should be replaced with \module{optik} (which is also now
3331known as \module{optparse} in Python 2.3 and later).}
3332
3333\begin{funcdesc}{fancy_getopt}{options, negative_opt, object, args}
3334Wrapper function. \var{options} is a list of
3335\samp{(long_option, short_option, help_string)} 3-tuples as described in the
3336constructor for \class{FancyGetopt}. \var{negative_opt} should be a dictionary
3337mapping option names to option names, both the key and value should be in the
3338\var{options} list. \var{object} is an object which will be used to store
3339values (see the \method{getopt()} method of the \class{FancyGetopt} class).
3340\var{args} is the argument list. Will use \code{sys.argv[1:]} if you
3341pass \code{None} as \var{args}.
3342\end{funcdesc}
3343
3344\begin{funcdesc}{wrap_text}{text, width}
3345Wraps \var{text} to less than \var{width} wide.
3346
3347\warning{Should be replaced with \module{textwrap} (which is available
3348in Python 2.3 and later).}
3349\end{funcdesc}
3350
3351\begin{classdesc}{FancyGetopt}{\optional{option_table=\code{None}}}
3352The option_table is a list of 3-tuples: \samp{(long_option,
3353short_option, help_string)}
3354
Georg Brandl7eb4b7d2005-07-22 21:49:32 +00003355If an option takes an argument, its \var{long_option} should have \code{'='}
Fred Drake6fca7cc2004-03-23 18:43:03 +00003356appended; \var{short_option} should just be a single character, no \code{':'}
3357in any case. \var{short_option} should be \code{None} if a \var{long_option}
3358doesn't have a corresponding \var{short_option}. All option tuples must have
3359long options.
3360\end{classdesc}
3361
3362The \class{FancyGetopt} class provides the following methods:
3363
3364\begin{methoddesc}{getopt}{\optional{args=\code{None}, object=\code{None}}}
3365Parse command-line options in args. Store as attributes on \var{object}.
3366
3367If \var{args} is \code{None} or not supplied, uses \code{sys.argv[1:]}. If
3368\var{object} is \code{None} or not supplied, creates a new \class{OptionDummy}
3369instance, stores option values there, and returns a tuple \samp{(args,
3370object)}. If \var{object} is supplied, it is modified in place and
3371\function{getopt()} just returns \var{args}; in both cases, the returned
3372\var{args} is a modified copy of the passed-in \var{args} list, which
3373is left untouched.
3374% and args returned are?
3375\end{methoddesc}
3376
3377\begin{methoddesc}{get_option_order}{}
3378Returns the list of \samp{(option, value)} tuples processed by the
3379previous run of \method{getopt()} Raises \exception{RuntimeError} if
3380\method{getopt()} hasn't been called yet.
3381\end{methoddesc}
3382
3383\begin{methoddesc}{generate_help}{\optional{header=\code{None}}}
3384Generate help text (a list of strings, one per suggested line of
3385output) from the option table for this \class{FancyGetopt} object.
3386
3387If supplied, prints the supplied \var{header} at the top of the help.
3388\end{methoddesc}
3389
3390\section{\module{distutils.filelist} --- The FileList class}
3391\declaremodule{standard}{distutils.filelist}
3392\modulesynopsis{The \class{FileList} class, used for poking about the
3393 file system and building lists of files.}
3394
3395This module provides the \class{FileList} class, used for poking about
3396the filesystem and building lists of files.
3397
3398
3399\section{\module{distutils.log} --- Simple PEP 282-style logging}
3400\declaremodule{standard}{distutils.log}
3401\modulesynopsis{A simple logging mechanism, \pep{282}-style}
3402
3403\warning{Should be replaced with standard \module{logging} module.}
3404
3405%\subsubsection{\module{} --- }
3406%\declaremodule{standard}{distutils.magic}
3407%\modulesynopsis{ }
3408
3409
3410\section{\module{distutils.spawn} --- Spawn a sub-process}
3411\declaremodule{standard}{distutils.spawn}
3412\modulesynopsis{Provides the spawn() function}
3413
3414This module provides the \function{spawn()} function, a front-end to
3415various platform-specific functions for launching another program in a
3416sub-process.
3417Also provides \function{find_executable()} to search the path for a given
3418executable name.
3419
3420
Fred Drakeab70b382001-08-02 15:13:15 +00003421\input{sysconfig}
Greg Ward16aafcd2000-04-09 04:06:44 +00003422
3423
Fred Drake6fca7cc2004-03-23 18:43:03 +00003424\section{\module{distutils.text_file} --- The TextFile class}
3425\declaremodule[distutils.textfile]{standard}{distutils.text_file}
3426\modulesynopsis{provides the TextFile class, a simple interface to text files}
3427
3428This module provides the \class{TextFile} class, which gives an interface
3429to text files that (optionally) takes care of stripping comments, ignoring
3430blank lines, and joining lines with backslashes.
3431
3432\begin{classdesc}{TextFile}{\optional{filename=\code{None}, file=\code{None}, **options}}
3433This class provides a file-like object that takes care of all
3434the things you commonly want to do when processing a text file
3435that has some line-by-line syntax: strip comments (as long as \code{\#}
3436is your comment character), skip blank lines, join adjacent lines by
3437escaping the newline (ie. backslash at end of line), strip
3438leading and/or trailing whitespace. All of these are optional
3439and independently controllable.
3440
3441The class provides a \method{warn()} method so you can generate
3442warning messages that report physical line number, even if the
3443logical line in question spans multiple physical lines. Also
3444provides \method{unreadline()} for implementing line-at-a-time lookahead.
3445
3446\class{TextFile} instances are create with either \var{filename}, \var{file},
3447or both. \exception{RuntimeError} is raised if both are \code{None}.
3448\var{filename} should be a string, and \var{file} a file object (or
3449something that provides \method{readline()} and \method{close()}
3450methods). It is recommended that you supply at least \var{filename},
3451so that \class{TextFile} can include it in warning messages. If
Fred Drake9687b4d2005-03-10 03:48:14 +00003452\var{file} is not supplied, \class{TextFile} creates its own using the
3453\function{open()} built-in function.
Fred Drake6fca7cc2004-03-23 18:43:03 +00003454
3455The options are all boolean, and affect the values returned by
Fred Drake9687b4d2005-03-10 03:48:14 +00003456\method{readline()}
Fred Drake6fca7cc2004-03-23 18:43:03 +00003457
3458\begin{tableiii}{c|l|l}{option name}{option name}{description}{default}
3459\lineiii{strip_comments}{
3460strip from \character{\#} to end-of-line, as well as any whitespace
3461leading up to the \character{\#}---unless it is escaped by a backslash}
3462{true}
3463\lineiii{lstrip_ws}{
3464strip leading whitespace from each line before returning it}
3465{false}
3466\lineiii{rstrip_ws}{
3467strip trailing whitespace (including line terminator!) from
3468each line before returning it.}
3469{true}
3470\lineiii{skip_blanks}{
3471skip lines that are empty *after* stripping comments and
3472whitespace. (If both lstrip_ws and rstrip_ws are false,
3473then some lines may consist of solely whitespace: these will
3474*not* be skipped, even if \var{skip_blanks} is true.)}
3475{true}
3476\lineiii{join_lines}{
3477if a backslash is the last non-newline character on a line
3478after stripping comments and whitespace, join the following line
3479to it to form one logical line; if N consecutive lines end
3480with a backslash, then N+1 physical lines will be joined to
3481form one logical line.}
3482{false}
3483\lineiii{collapse_join}{
3484strip leading whitespace from lines that are joined to their
3485predecessor; only matters if \samp{(join_lines and not lstrip_ws)}}
3486{false}
3487\end{tableiii}
3488
3489Note that since \var{rstrip_ws} can strip the trailing newline, the
3490semantics of \method{readline()} must differ from those of the builtin file
3491object's \method{readline()} method! In particular, \method{readline()}
3492returns \code{None} for end-of-file: an empty string might just be a
3493blank line (or an all-whitespace line), if \var{rstrip_ws} is true
3494but \var{skip_blanks} is not.
3495
3496\begin{methoddesc}{open}{filename}
3497Open a new file \var{filename}. This overrides any \var{file} or
3498\var{filename} constructor arguments.
3499\end{methoddesc}
3500
3501\begin{methoddesc}{close}{}
3502Close the current file and forget everything we know about it (including
3503the filename and the current line number).
3504\end{methoddesc}
3505
3506\begin{methoddesc}{warn}{msg\optional{,line=\code{None}}}
3507Print (to stderr) a warning message tied to the current logical
3508line in the current file. If the current logical line in the
3509file spans multiple physical lines, the warning refers to the
3510whole range, such as \samp{"lines 3-5"}. If \var{line} is supplied,
3511it overrides the current line number; it may be a list or tuple
3512to indicate a range of physical lines, or an integer for a
3513single physical line.
3514\end{methoddesc}
3515
3516\begin{methoddesc}{readline}{}
3517Read and return a single logical line from the current file (or
3518from an internal buffer if lines have previously been ``unread''
3519with \method{unreadline()}). If the \var{join_lines} option
3520is true, this may involve reading multiple physical lines
3521concatenated into a single string. Updates the current line number,
3522so calling \method{warn()} after \method{readline()} emits a warning
3523about the physical line(s) just read. Returns \code{None} on end-of-file,
3524since the empty string can occur if \var{rstrip_ws} is true but
3525\var{strip_blanks} is not.
3526\end{methoddesc}
3527\begin{methoddesc}{readlines}{}
3528Read and return the list of all logical lines remaining in the current file.
3529This updates the current line number to the last line of the file.
3530\end{methoddesc}
3531\begin{methoddesc}{unreadline}{line}
3532Push \var{line} (a string) onto an internal buffer that will be
3533checked by future \method{readline()} calls. Handy for implementing
3534a parser with line-at-a-time lookahead. Note that lines that are ``unread''
3535with \method{unreadline} are not subsequently re-cleansed (whitespace
3536stripped, or whatever) when read with \method{readline}. If multiple
3537calls are made to \method{unreadline} before a call to \method{readline},
3538the lines will be returned most in most recent first order.
3539\end{methoddesc}
3540
3541\end{classdesc}
3542
3543
3544\section{\module{distutils.version} --- Version number classes}
3545\declaremodule{standard}{distutils.version}
3546\modulesynopsis{implements classes that represent module version numbers. }
3547
3548% todo
3549
3550%\section{Distutils Commands}
3551%
3552%This part of Distutils implements the various Distutils commands, such
3553%as \code{build}, \code{install} \&c. Each command is implemented as a
3554%separate module, with the command name as the name of the module.
3555
3556\section{\module{distutils.cmd} --- Abstract base class for Distutils commands}
3557\declaremodule{standard}{distutils.cmd}
3558\modulesynopsis{This module provides the abstract base class Command. This
3559class is subclassed by the modules in the \refmodule{distutils.command}
3560subpackage. }
3561
3562This module supplies the abstract base class \class{Command}.
3563
3564\begin{classdesc}{Command}{dist}
3565Abstract base class for defining command classes, the ``worker bees''
3566of the Distutils. A useful analogy for command classes is to think of
3567them as subroutines with local variables called \var{options}. The
3568options are declared in \method{initialize_options()} and defined
3569(given their final values) in \method{finalize_options()}, both of
3570which must be defined by every command class. The distinction between
3571the two is necessary because option values might come from the outside
3572world (command line, config file, ...), and any options dependent on
3573other options must be computed after these outside influences have
3574been processed --- hence \method{finalize_options()}. The body of the
3575subroutine, where it does all its work based on the values of its
3576options, is the \method{run()} method, which must also be implemented
3577by every command class.
3578
3579The class constructor takes a single argument \var{dist}, a
3580\class{Distribution} instance.
3581\end{classdesc}
3582
3583
3584\section{\module{distutils.command} --- Individual Distutils commands}
3585\declaremodule{standard}{distutils.command}
3586\modulesynopsis{This subpackage contains one module for each standard Distutils command.}
3587
3588%\subsubsection{Individual Distutils commands}
3589
3590% todo
3591
3592\section{\module{distutils.command.bdist} --- Build a binary installer}
3593\declaremodule{standard}{distutils.command.bdist}
3594\modulesynopsis{Build a binary installer for a package}
3595
3596% todo
3597
3598\section{\module{distutils.command.bdist_packager} --- Abstract base class for packagers}
3599\declaremodule[distutils.command.bdistpackager]{standard}{distutils.command.bdist_packager}
3600\modulesynopsis{Abstract base class for packagers}
3601
3602% todo
3603
3604\section{\module{distutils.command.bdist_dumb} --- Build a ``dumb'' installer}
3605\declaremodule[distutils.command.bdistdumb]{standard}{distutils.command.bdist_dumb}
3606\modulesynopsis{Build a ``dumb'' installer - a simple archive of files}
3607
3608% todo
3609
Thomas Wouters89f507f2006-12-13 04:49:30 +00003610\section{\module{distutils.command.bdist_msi} --- Build a Microsoft Installer binary package}
3611\declaremodule[distutils.command.bdistmsi]{standard}{distutils.command.bdist_msi}
3612\modulesynopsis{Build a binary distribution as a Windows MSI file}
3613
3614% todo
Fred Drake6fca7cc2004-03-23 18:43:03 +00003615
3616\section{\module{distutils.command.bdist_rpm} --- Build a binary distribution as a Redhat RPM and SRPM}
3617\declaremodule[distutils.command.bdistrpm]{standard}{distutils.command.bdist_rpm}
3618\modulesynopsis{Build a binary distribution as a Redhat RPM and SRPM}
3619
3620% todo
3621
3622\section{\module{distutils.command.bdist_wininst} --- Build a Windows installer}
3623\declaremodule[distutils.command.bdistwininst]{standard}{distutils.command.bdist_wininst}
3624\modulesynopsis{Build a Windows installer}
3625
3626% todo
3627
3628\section{\module{distutils.command.sdist} --- Build a source distribution}
3629\declaremodule{standard}{distutils.command.sdist}
3630\modulesynopsis{Build a source distribution}
3631
3632% todo
3633
3634\section{\module{distutils.command.build} --- Build all files of a package}
3635\declaremodule{standard}{distutils.command.build}
3636\modulesynopsis{Build all files of a package}
3637
3638% todo
3639
3640\section{\module{distutils.command.build_clib} --- Build any C libraries in a package}
3641\declaremodule[distutils.command.buildclib]{standard}{distutils.command.build_clib}
3642\modulesynopsis{Build any C libraries in a package}
3643
3644% todo
3645
3646\section{\module{distutils.command.build_ext} --- Build any extensions in a package}
3647\declaremodule[distutils.command.buildext]{standard}{distutils.command.build_ext}
3648\modulesynopsis{Build any extensions in a package}
3649
3650% todo
3651
3652\section{\module{distutils.command.build_py} --- Build the .py/.pyc files of a package}
3653\declaremodule[distutils.command.buildpy]{standard}{distutils.command.build_py}
3654\modulesynopsis{Build the .py/.pyc files of a package}
3655
3656% todo
3657
3658\section{\module{distutils.command.build_scripts} --- Build the scripts of a package}
3659\declaremodule[distutils.command.buildscripts]{standard}{distutils.command.build_scripts}
3660\modulesynopsis{Build the scripts of a package}
3661
3662% todo
3663
3664\section{\module{distutils.command.clean} --- Clean a package build area}
3665\declaremodule{standard}{distutils.command.clean}
3666\modulesynopsis{Clean a package build area}
3667
3668% todo
3669
3670\section{\module{distutils.command.config} --- Perform package configuration}
3671\declaremodule{standard}{distutils.command.config}
3672\modulesynopsis{Perform package configuration}
3673
3674% todo
3675
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00003676\section{\module{distutils.command.install} --- Install a package}
Fred Drake6fca7cc2004-03-23 18:43:03 +00003677\declaremodule{standard}{distutils.command.install}
3678\modulesynopsis{Install a package}
3679
3680% todo
3681
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00003682\section{\module{distutils.command.install_data}
Fred Drake6fca7cc2004-03-23 18:43:03 +00003683 --- Install data files from a package}
3684\declaremodule[distutils.command.installdata]{standard}{distutils.command.install_data}
3685\modulesynopsis{Install data files from a package}
3686
3687% todo
3688
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00003689\section{\module{distutils.command.install_headers}
Fred Drake6fca7cc2004-03-23 18:43:03 +00003690 --- Install C/\Cpp{} header files from a package}
3691\declaremodule[distutils.command.installheaders]{standard}{distutils.command.install_headers}
3692\modulesynopsis{Install C/\Cpp{} header files from a package}
3693
3694% todo
3695
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00003696\section{\module{distutils.command.install_lib}
Fred Drake6fca7cc2004-03-23 18:43:03 +00003697 --- Install library files from a package}
3698\declaremodule[distutils.command.installlib]{standard}{distutils.command.install_lib}
3699\modulesynopsis{Install library files from a package}
3700
3701% todo
3702
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00003703\section{\module{distutils.command.install_scripts}
Fred Drake6fca7cc2004-03-23 18:43:03 +00003704 --- Install script files from a package}
3705\declaremodule[distutils.command.installscripts]{standard}{distutils.command.install_scripts}
3706\modulesynopsis{Install script files from a package}
3707
3708% todo
3709
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00003710\section{\module{distutils.command.register}
Fred Drake6fca7cc2004-03-23 18:43:03 +00003711 --- Register a module with the Python Package Index}
3712\declaremodule{standard}{distutils.command.register}
3713\modulesynopsis{Register a module with the Python Package Index}
3714
3715The \code{register} command registers the package with the Python Package
3716Index. This is described in more detail in \pep{301}.
3717% todo
3718
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00003719\section{Creating a new Distutils command}
Fred Drake6fca7cc2004-03-23 18:43:03 +00003720
3721This section outlines the steps to create a new Distutils command.
3722
3723A new command lives in a module in the \module{distutils.command}
3724package. There is a sample template in that directory called
3725\file{command_template}. Copy this file to a new module with the
3726same name as the new command you're implementing. This module should
3727implement a class with the same name as the module (and the command).
3728So, for instance, to create the command \code{peel_banana} (so that users
3729can run \samp{setup.py peel_banana}), you'd copy \file{command_template}
3730to \file{distutils/command/peel_banana.py}, then edit it so that it's
3731implementing the class \class{peel_banana}, a subclass of
3732\class{distutils.cmd.Command}.
3733
3734Subclasses of \class{Command} must define the following methods.
3735
3736\begin{methoddesc}{initialize_options()}
3737Set default values for all the options that this command
3738supports. Note that these defaults may be overridden by other
3739commands, by the setup script, by config files, or by the
3740command-line. Thus, this is not the place to code dependencies
3741between options; generally, \method{initialize_options()} implementations
3742are just a bunch of \samp{self.foo = None} assignments.
3743\end{methoddesc}
3744
3745\begin{methoddesc}{finalize_options}{}
3746Set final values for all the options that this command supports.
3747This is always called as late as possible, ie. after any option
3748assignments from the command-line or from other commands have been
3749done. Thus, this is the place to to code option dependencies: if
3750\var{foo} depends on \var{bar}, then it is safe to set \var{foo} from
3751\var{bar} as long as \var{foo} still has the same value it was assigned in
3752\method{initialize_options()}.
3753\end{methoddesc}
3754\begin{methoddesc}{run}{}
3755A command's raison d'etre: carry out the action it exists to
3756perform, controlled by the options initialized in
3757\method{initialize_options()}, customized by other commands, the setup
3758script, the command-line, and config files, and finalized in
3759\method{finalize_options()}. All terminal output and filesystem
3760interaction should be done by \method{run()}.
3761\end{methoddesc}
3762
3763\var{sub_commands} formalizes the notion of a ``family'' of commands,
3764eg. \code{install} as the parent with sub-commands \code{install_lib},
3765\code{install_headers}, etc. The parent of a family of commands
3766defines \var{sub_commands} as a class attribute; it's a list of
37672-tuples \samp{(command_name, predicate)}, with \var{command_name} a string
3768and \var{predicate} an unbound method, a string or None.
3769\var{predicate} is a method of the parent command that
3770determines whether the corresponding command is applicable in the
3771current situation. (Eg. we \code{install_headers} is only applicable if
3772we have any C header files to install.) If \var{predicate} is None,
3773that command is always applicable.
3774
3775\var{sub_commands} is usually defined at the *end* of a class, because
3776predicates can be unbound methods, so they must already have been
3777defined. The canonical example is the \command{install} command.
3778
Fred Drake6356fff2004-03-23 19:02:38 +00003779%
3780% The ugly "%begin{latexonly}" pseudo-environments are really just to
3781% keep LaTeX2HTML quiet during the \renewcommand{} macros; they're
3782% not really valuable.
3783%
3784
3785%begin{latexonly}
3786\renewcommand{\indexname}{Module Index}
3787%end{latexonly}
Fred Drakead622022004-03-25 16:35:10 +00003788\input{moddist.ind} % Module Index
Fred Drake6356fff2004-03-23 19:02:38 +00003789
3790%begin{latexonly}
3791\renewcommand{\indexname}{Index}
3792%end{latexonly}
Fred Drakead622022004-03-25 16:35:10 +00003793\input{dist.ind} % Index
Fred Drake6fca7cc2004-03-23 18:43:03 +00003794
Greg Wardabc52162000-02-26 00:52:48 +00003795\end{document}