blob: b5f973302aa4540ec6ea4264a8a9b15d5fb2fa0b [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
Greg Ward16aafcd2000-04-09 04:06:44 +000023
Greg Wardabc52162000-02-26 00:52:48 +000024\begin{document}
25
Greg Wardfacb8db2000-04-09 04:32:40 +000026\maketitle
Greg Warde3cca262000-08-31 16:36:31 +000027\begin{abstract}
28 \noindent
29 This document describes the Python Distribution Utilities
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000030 (``Distutils'') from the module developer's point of view, describing
Greg Warde3cca262000-08-31 16:36:31 +000031 how to use the Distutils to make Python modules and extensions easily
32 available to a wider audience with very little overhead for
33 build/release/install mechanics.
34\end{abstract}
35
Fred Drakea09262e2001-03-01 18:35:43 +000036% The ugly "%begin{latexonly}" pseudo-environment supresses the table
37% of contents for HTML generation.
38%
39%begin{latexonly}
Greg Wardfacb8db2000-04-09 04:32:40 +000040\tableofcontents
Fred Drakea09262e2001-03-01 18:35:43 +000041%end{latexonly}
42
Greg Ward16aafcd2000-04-09 04:06:44 +000043
Fred Drake211a2eb2004-03-22 21:44:43 +000044\chapter{An Introduction to Distutils}
Greg Warde78298a2000-04-28 17:12:24 +000045\label{intro}
Greg Ward16aafcd2000-04-09 04:06:44 +000046
Andrew M. Kuchling40df7102002-05-08 13:39:03 +000047This document covers using the Distutils to distribute your Python
48modules, concentrating on the role of developer/distributor: if
Fred Drake01df4532000-06-30 03:36:41 +000049you're looking for information on installing Python modules, you
50should refer to the \citetitle[../inst/inst.html]{Installing Python
51Modules} manual.
Greg Ward16aafcd2000-04-09 04:06:44 +000052
53
Greg Wardfacb8db2000-04-09 04:32:40 +000054\section{Concepts \& Terminology}
Greg Warde78298a2000-04-28 17:12:24 +000055\label{concepts}
Greg Ward16aafcd2000-04-09 04:06:44 +000056
57Using the Distutils is quite simple, both for module developers and for
58users/administrators installing third-party modules. As a developer,
Thomas Heller5f52f722001-02-19 17:48:03 +000059your responsibilities (apart from writing solid, well-documented and
Greg Ward16aafcd2000-04-09 04:06:44 +000060well-tested code, of course!) are:
61\begin{itemize}
62\item write a setup script (\file{setup.py} by convention)
63\item (optional) write a setup configuration file
64\item create a source distribution
65\item (optional) create one or more built (binary) distributions
66\end{itemize}
67Each of these tasks is covered in this document.
68
69Not all module developers have access to a multitude of platforms, so
70it's not always feasible to expect them to create a multitude of built
71distributions. It is hoped that a class of intermediaries, called
Greg Ward19c67f82000-06-24 01:33:16 +000072\emph{packagers}, will arise to address this need. Packagers will take
73source distributions released by module developers, build them on one or
74more platforms, and release the resulting built distributions. Thus,
75users on the most popular platforms will be able to install most popular
76Python module distributions in the most natural way for their platform,
77without having to run a single setup script or compile a line of code.
Greg Ward16aafcd2000-04-09 04:06:44 +000078
79
Fred Drake211a2eb2004-03-22 21:44:43 +000080\section{A Simple Example}
Greg Warde78298a2000-04-28 17:12:24 +000081\label{simple-example}
Greg Ward16aafcd2000-04-09 04:06:44 +000082
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000083The setup script is usually quite simple, although since it's written
84in Python, there are no arbitrary limits to what you can do with it,
85though you should be careful about putting arbitrarily expensive
86operations in your setup script. Unlike, say, Autoconf-style configure
87scripts, the setup script may be run multiple times in the course of
Andrew M. Kuchlinge9a54a32003-05-13 15:02:06 +000088building and installing your module distribution.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +000089
90If all you want to do is distribute a module called \module{foo},
91contained in a file \file{foo.py}, then your setup script can be as
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000092simple as this:
Fred Drakea09262e2001-03-01 18:35:43 +000093
Greg Ward16aafcd2000-04-09 04:06:44 +000094\begin{verbatim}
95from distutils.core import setup
Fred Drakea09262e2001-03-01 18:35:43 +000096setup(name="foo",
97 version="1.0",
98 py_modules=["foo"])
Greg Ward16aafcd2000-04-09 04:06:44 +000099\end{verbatim}
Greg Ward370248d2000-06-24 01:45:47 +0000100
Greg Ward16aafcd2000-04-09 04:06:44 +0000101Some observations:
102\begin{itemize}
Greg Ward370248d2000-06-24 01:45:47 +0000103\item most information that you supply to the Distutils is supplied as
Greg Wardfacb8db2000-04-09 04:32:40 +0000104 keyword arguments to the \function{setup()} function
Greg Ward16aafcd2000-04-09 04:06:44 +0000105\item those keyword arguments fall into two categories: package
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000106 metadata (name, version number) and information about what's in the
Greg Ward370248d2000-06-24 01:45:47 +0000107 package (a list of pure Python modules, in this case)
Greg Ward16aafcd2000-04-09 04:06:44 +0000108\item modules are specified by module name, not filename (the same will
109 hold true for packages and extensions)
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000110\item it's recommended that you supply a little more metadata, in
Greg Ward16aafcd2000-04-09 04:06:44 +0000111 particular your name, email address and a URL for the project
Greg Ward47f99a62000-09-04 20:07:15 +0000112 (see section~\ref{setup-script} for an example)
Greg Ward16aafcd2000-04-09 04:06:44 +0000113\end{itemize}
114
Greg Ward370248d2000-06-24 01:45:47 +0000115To create a source distribution for this module, you would create a
116setup script, \file{setup.py}, containing the above code, and run:
Fred Drakea09262e2001-03-01 18:35:43 +0000117
Greg Ward16aafcd2000-04-09 04:06:44 +0000118\begin{verbatim}
119python setup.py sdist
120\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000121
Fred Drakeeff9a872000-10-26 16:41:03 +0000122which will create an archive file (e.g., tarball on \UNIX, ZIP file on
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000123Windows) containing your setup script \file{setup.py}, and your module
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000124\file{foo.py}. The archive file will be named \file{foo-1.0.tar.gz} (or
125\file{.zip}), and will unpack into a directory \file{foo-1.0}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000126
127If an end-user wishes to install your \module{foo} module, all she has
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000128to do is download \file{foo-1.0.tar.gz} (or \file{.zip}), unpack it,
129and---from the \file{foo-1.0} directory---run
Fred Drakea09262e2001-03-01 18:35:43 +0000130
Greg Ward16aafcd2000-04-09 04:06:44 +0000131\begin{verbatim}
132python setup.py install
133\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000134
Greg Ward16aafcd2000-04-09 04:06:44 +0000135which will ultimately copy \file{foo.py} to the appropriate directory
136for third-party modules in their Python installation.
137
138This simple example demonstrates some fundamental concepts of the
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000139Distutils. First, both developers and installers have the same basic
Greg Ward16aafcd2000-04-09 04:06:44 +0000140user interface, i.e. the setup script. The difference is which
141Distutils \emph{commands} they use: the \command{sdist} command is
142almost exclusively for module developers, while \command{install} is
143more often for installers (although most developers will want to install
144their own code occasionally).
145
Greg Ward16aafcd2000-04-09 04:06:44 +0000146If you want to make things really easy for your users, you can create
147one or more built distributions for them. For instance, if you are
148running on a Windows machine, and want to make things easy for other
149Windows users, you can create an executable installer (the most
150appropriate type of built distribution for this platform) with the
Greg Ward59d382e2000-05-26 01:04:47 +0000151\command{bdist\_wininst} command. For example:
Fred Drakea09262e2001-03-01 18:35:43 +0000152
Greg Ward16aafcd2000-04-09 04:06:44 +0000153\begin{verbatim}
Greg Ward59d382e2000-05-26 01:04:47 +0000154python setup.py bdist_wininst
Greg Ward16aafcd2000-04-09 04:06:44 +0000155\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000156
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000157will create an executable installer, \file{foo-1.0.win32.exe}, in the
Greg Ward1d8f57a2000-08-05 00:43:11 +0000158current directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000159
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000160Other useful built distribution formats are RPM, implemented by the
161\command{bdist\_rpm} command, Solaris \program{pkgtool}
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000162(\command{bdist\_pkgtool}), and HP-UX \program{swinstall}
163(\command{bdist_sdux}). For example, the following command will
164create an RPM file called \file{foo-1.0.noarch.rpm}:
Fred Drakea09262e2001-03-01 18:35:43 +0000165
Greg Ward1d8f57a2000-08-05 00:43:11 +0000166\begin{verbatim}
167python setup.py bdist_rpm
168\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000169
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000170(The \command{bdist\_rpm} command uses the \command{rpm} executable,
171therefore this has to be run on an RPM-based system such as Red Hat
172Linux, SuSE Linux, or Mandrake Linux.)
Greg Ward1d8f57a2000-08-05 00:43:11 +0000173
174You can find out what distribution formats are available at any time by
175running
Fred Drakea09262e2001-03-01 18:35:43 +0000176
Greg Ward1d8f57a2000-08-05 00:43:11 +0000177\begin{verbatim}
178python setup.py bdist --help-formats
179\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000180
181
Fred Drake211a2eb2004-03-22 21:44:43 +0000182\section{General Python terminology}
Greg Warde78298a2000-04-28 17:12:24 +0000183\label{python-terms}
Greg Ward16aafcd2000-04-09 04:06:44 +0000184
185If you're reading this document, you probably have a good idea of what
186modules, extensions, and so forth are. Nevertheless, just to be sure
187that everyone is operating from a common starting point, we offer the
188following glossary of common Python terms:
189\begin{description}
190\item[module] the basic unit of code reusability in Python: a block of
Greg Ward1d8f57a2000-08-05 00:43:11 +0000191 code imported by some other code. Three types of modules concern us
192 here: pure Python modules, extension modules, and packages.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000193
Greg Ward16aafcd2000-04-09 04:06:44 +0000194\item[pure Python module] a module written in Python and contained in a
195 single \file{.py} file (and possibly associated \file{.pyc} and/or
196 \file{.pyo} files). Sometimes referred to as a ``pure module.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000197
Greg Ward16aafcd2000-04-09 04:06:44 +0000198\item[extension module] a module written in the low-level language of
Fred Drake2884d6d2003-07-02 12:27:43 +0000199 the Python implementation: C/\Cpp{} for Python, Java for Jython.
Greg Ward16aafcd2000-04-09 04:06:44 +0000200 Typically contained in a single dynamically loadable pre-compiled
Fred Drakeeff9a872000-10-26 16:41:03 +0000201 file, e.g. a shared object (\file{.so}) file for Python extensions on
202 \UNIX, a DLL (given the \file{.pyd} extension) for Python extensions
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000203 on Windows, or a Java class file for Jython extensions. (Note that
Fred Drake2884d6d2003-07-02 12:27:43 +0000204 currently, the Distutils only handles C/\Cpp{} extensions for Python.)
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000205
Greg Ward16aafcd2000-04-09 04:06:44 +0000206\item[package] a module that contains other modules; typically contained
207 in a directory in the filesystem and distinguished from other
208 directories by the presence of a file \file{\_\_init\_\_.py}.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000209
Greg Ward6153fa12000-05-26 02:24:28 +0000210\item[root package] the root of the hierarchy of packages. (This isn't
211 really a package, since it doesn't have an \file{\_\_init\_\_.py}
212 file. But we have to call it something.) The vast majority of the
213 standard library is in the root package, as are many small, standalone
214 third-party modules that don't belong to a larger module collection.
215 Unlike regular packages, modules in the root package can be found in
216 many directories: in fact, every directory listed in \code{sys.path}
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000217 contributes modules to the root package.
Greg Ward16aafcd2000-04-09 04:06:44 +0000218\end{description}
219
220
Fred Drake211a2eb2004-03-22 21:44:43 +0000221\section{Distutils-specific terminology}
Greg Warde78298a2000-04-28 17:12:24 +0000222\label{distutils-term}
Greg Ward16aafcd2000-04-09 04:06:44 +0000223
224The following terms apply more specifically to the domain of
225distributing Python modules using the Distutils:
226\begin{description}
227\item[module distribution] a collection of Python modules distributed
228 together as a single downloadable resource and meant to be installed
229 \emph{en masse}. Examples of some well-known module distributions are
230 Numeric Python, PyXML, PIL (the Python Imaging Library), or
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000231 mxBase. (This would be called a \emph{package}, except that term
Greg Ward59d382e2000-05-26 01:04:47 +0000232 is already taken in the Python context: a single module distribution
233 may contain zero, one, or many Python packages.)
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000234
Greg Ward16aafcd2000-04-09 04:06:44 +0000235\item[pure module distribution] a module distribution that contains only
236 pure Python modules and packages. Sometimes referred to as a ``pure
237 distribution.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000238
Greg Ward16aafcd2000-04-09 04:06:44 +0000239\item[non-pure module distribution] a module distribution that contains
240 at least one extension module. Sometimes referred to as a ``non-pure
241 distribution.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000242
Greg Ward16aafcd2000-04-09 04:06:44 +0000243\item[distribution root] the top-level directory of your source tree (or
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000244 source distribution); the directory where \file{setup.py} exists. Generally
245 \file{setup.py} will be run from this directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000246\end{description}
247
248
Fred Drake211a2eb2004-03-22 21:44:43 +0000249\chapter{Writing the Setup Script}
Greg Warde78298a2000-04-28 17:12:24 +0000250\label{setup-script}
Greg Ward16aafcd2000-04-09 04:06:44 +0000251
252The setup script is the centre of all activity in building,
253distributing, and installing modules using the Distutils. The main
254purpose of the setup script is to describe your module distribution to
Greg Wardd5767a52000-04-19 22:48:09 +0000255the Distutils, so that the various commands that operate on your modules
Greg Ward59d382e2000-05-26 01:04:47 +0000256do the right thing. As we saw in section~\ref{simple-example} above,
257the setup script consists mainly of a call to \function{setup()}, and
Greg Ward1bbe3292000-06-25 03:14:13 +0000258most information supplied to the Distutils by the module developer is
259supplied as keyword arguments to \function{setup()}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000260
261Here's a slightly more involved example, which we'll follow for the next
262couple of sections: the Distutils' own setup script. (Keep in mind that
Greg Ward1d8f57a2000-08-05 00:43:11 +0000263although the Distutils are included with Python 1.6 and later, they also
264have an independent existence so that Python 1.5.2 users can use them to
265install other module distributions. The Distutils' own setup script,
266shown here, is used to install the package into Python 1.5.2.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000267
268\begin{verbatim}
269#!/usr/bin/env python
270
271from distutils.core import setup
272
Fred Drakea09262e2001-03-01 18:35:43 +0000273setup(name="Distutils",
274 version="1.0",
275 description="Python Distribution Utilities",
276 author="Greg Ward",
277 author_email="gward@python.net",
278 url="http://www.python.org/sigs/distutils-sig/",
279 packages=['distutils', 'distutils.command'],
280 )
Greg Ward16aafcd2000-04-09 04:06:44 +0000281\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000282
Greg Ward16aafcd2000-04-09 04:06:44 +0000283There are only two differences between this and the trivial one-file
Greg Warde78298a2000-04-28 17:12:24 +0000284distribution presented in section~\ref{simple-example}: more
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000285metadata, and the specification of pure Python modules by package,
Greg Ward16aafcd2000-04-09 04:06:44 +0000286rather than by module. This is important since the Distutils consist of
287a couple of dozen modules split into (so far) two packages; an explicit
288list of every module would be tedious to generate and difficult to
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000289maintain. For more information on the additional meta-data, see
290section~\ref{meta-data}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000291
Greg Ward46b98e32000-04-14 01:53:36 +0000292Note that any pathnames (files or directories) supplied in the setup
Fred Drakeeff9a872000-10-26 16:41:03 +0000293script should be written using the \UNIX{} convention, i.e.
Greg Ward46b98e32000-04-14 01:53:36 +0000294slash-separated. The Distutils will take care of converting this
Greg Ward59d382e2000-05-26 01:04:47 +0000295platform-neutral representation into whatever is appropriate on your
Greg Ward46b98e32000-04-14 01:53:36 +0000296current platform before actually using the pathname. This makes your
297setup script portable across operating systems, which of course is one
298of the major goals of the Distutils. In this spirit, all pathnames in
Fred Drake781380c2004-02-19 23:17:46 +0000299this document are slash-separated. (Mac OS programmers should keep in
Greg Ward59d382e2000-05-26 01:04:47 +0000300mind that the \emph{absence} of a leading slash indicates a relative
Fred Drake781380c2004-02-19 23:17:46 +0000301path, the opposite of the Mac OS convention with colons.)
Greg Ward46b98e32000-04-14 01:53:36 +0000302
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000303This, of course, only applies to pathnames given to Distutils
Fred Drake2a046232003-03-31 16:23:09 +0000304functions. If you, for example, use standard Python functions such as
305\function{glob.glob()} or \function{os.listdir()} to specify files, you
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000306should be careful to write portable code instead of hardcoding path
307separators:
Fred Drakea09262e2001-03-01 18:35:43 +0000308
Thomas Heller5f52f722001-02-19 17:48:03 +0000309\begin{verbatim}
310 glob.glob(os.path.join('mydir', 'subdir', '*.html'))
311 os.listdir(os.path.join('mydir', 'subdir'))
312\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000313
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000314
Greg Ward2afffd42000-08-06 20:37:24 +0000315\subsection{Listing whole packages}
316\label{listing-packages}
Greg Ward16aafcd2000-04-09 04:06:44 +0000317
318The \option{packages} option tells the Distutils to process (build,
319distribute, install, etc.) all pure Python modules found in each package
320mentioned in the \option{packages} list. In order to do this, of
321course, there has to be a correspondence between package names and
322directories in the filesystem. The default correspondence is the most
Greg Ward1ecc2512000-04-19 22:36:24 +0000323obvious one, i.e. package \module{distutils} is found in the directory
Greg Ward16aafcd2000-04-09 04:06:44 +0000324\file{distutils} relative to the distribution root. Thus, when you say
325\code{packages = ['foo']} in your setup script, you are promising that
326the Distutils will find a file \file{foo/\_\_init\_\_.py} (which might
327be spelled differently on your system, but you get the idea) relative to
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000328the directory where your setup script lives. If you break this
329promise, the Distutils will issue a warning but still process the broken
330package anyways.
Greg Ward16aafcd2000-04-09 04:06:44 +0000331
332If you use a different convention to lay out your source directory,
333that's no problem: you just have to supply the \option{package\_dir}
334option to tell the Distutils about your convention. For example, say
Greg Ward1d8f57a2000-08-05 00:43:11 +0000335you keep all Python source under \file{lib}, so that modules in the
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000336``root package'' (i.e., not in any package at all) are in
Greg Ward1d8f57a2000-08-05 00:43:11 +0000337\file{lib}, modules in the \module{foo} package are in \file{lib/foo},
338and so forth. Then you would put
Fred Drakea09262e2001-03-01 18:35:43 +0000339
Greg Ward16aafcd2000-04-09 04:06:44 +0000340\begin{verbatim}
341package_dir = {'': 'lib'}
342\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000343
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000344in your setup script. The keys to this dictionary are package names,
Greg Ward1d8f57a2000-08-05 00:43:11 +0000345and an empty package name stands for the root package. The values are
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000346directory names relative to your distribution root. In this case, when
Greg Ward1d8f57a2000-08-05 00:43:11 +0000347you say \code{packages = ['foo']}, you are promising that the file
Greg Ward16aafcd2000-04-09 04:06:44 +0000348\file{lib/foo/\_\_init\_\_.py} exists.
349
Greg Ward1ecc2512000-04-19 22:36:24 +0000350Another possible convention is to put the \module{foo} package right in
351\file{lib}, the \module{foo.bar} package in \file{lib/bar}, etc. This
Greg Ward16aafcd2000-04-09 04:06:44 +0000352would be written in the setup script as
Fred Drakea09262e2001-03-01 18:35:43 +0000353
Greg Ward16aafcd2000-04-09 04:06:44 +0000354\begin{verbatim}
355package_dir = {'foo': 'lib'}
356\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000357
Greg Ward59d382e2000-05-26 01:04:47 +0000358A \code{\var{package}: \var{dir}} entry in the \option{package\_dir}
359dictionary implicitly applies to all packages below \var{package}, so
360the \module{foo.bar} case is automatically handled here. In this
361example, having \code{packages = ['foo', 'foo.bar']} tells the Distutils
362to look for \file{lib/\_\_init\_\_.py} and
363\file{lib/bar/\_\_init\_\_.py}. (Keep in mind that although
364\option{package\_dir} applies recursively, you must explicitly list all
365packages in \option{packages}: the Distutils will \emph{not} recursively
366scan your source tree looking for any directory with an
367\file{\_\_init\_\_.py} file.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000368
369
370\subsection{Listing individual modules}
Greg Warde78298a2000-04-28 17:12:24 +0000371\label{listing-modules}
Greg Ward16aafcd2000-04-09 04:06:44 +0000372
373For a small module distribution, you might prefer to list all modules
374rather than listing packages---especially the case of a single module
375that goes in the ``root package'' (i.e., no package at all). This
Greg Warde78298a2000-04-28 17:12:24 +0000376simplest case was shown in section~\ref{simple-example}; here is a
Greg Ward16aafcd2000-04-09 04:06:44 +0000377slightly more involved example:
Fred Drakea09262e2001-03-01 18:35:43 +0000378
Greg Ward16aafcd2000-04-09 04:06:44 +0000379\begin{verbatim}
380py_modules = ['mod1', 'pkg.mod2']
381\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000382
Greg Ward16aafcd2000-04-09 04:06:44 +0000383This describes two modules, one of them in the ``root'' package, the
Greg Wardd5767a52000-04-19 22:48:09 +0000384other in the \module{pkg} package. Again, the default package/directory
385layout implies that these two modules can be found in \file{mod1.py} and
386\file{pkg/mod2.py}, and that \file{pkg/\_\_init\_\_.py} exists as well.
Greg Ward2afffd42000-08-06 20:37:24 +0000387And again, you can override the package/directory correspondence using
388the \option{package\_dir} option.
Greg Ward59d382e2000-05-26 01:04:47 +0000389
390
391\subsection{Describing extension modules}
Greg Ward1365a302000-08-31 14:47:05 +0000392\label{describing-extensions}
Greg Ward59d382e2000-05-26 01:04:47 +0000393
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000394% XXX read over this section
Greg Ward2afffd42000-08-06 20:37:24 +0000395Just as writing Python extension modules is a bit more complicated than
396writing pure Python modules, describing them to the Distutils is a bit
397more complicated. Unlike pure modules, it's not enough just to list
398modules or packages and expect the Distutils to go out and find the
399right files; you have to specify the extension name, source file(s), and
400any compile/link requirements (include directories, libraries to link
401with, etc.).
402
403All of this is done through another keyword argument to
404\function{setup()}, the \option{extensions} option. \option{extensions}
405is just a list of \class{Extension} instances, each of which describes a
406single extension module. Suppose your distribution includes a single
407extension, called \module{foo} and implemented by \file{foo.c}. If no
408additional instructions to the compiler/linker are needed, describing
409this extension is quite simple:
Fred Drakea09262e2001-03-01 18:35:43 +0000410
Greg Ward2afffd42000-08-06 20:37:24 +0000411\begin{verbatim}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000412uExtension("foo", ["foo.c"])
Greg Ward2afffd42000-08-06 20:37:24 +0000413\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000414
Greg Ward2afffd42000-08-06 20:37:24 +0000415The \class{Extension} class can be imported from
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000416\module{distutils.core} along with \function{setup()}. Thus, the setup
Greg Ward2afffd42000-08-06 20:37:24 +0000417script for a module distribution that contains only this one extension
418and nothing else might be:
Fred Drakea09262e2001-03-01 18:35:43 +0000419
Greg Ward2afffd42000-08-06 20:37:24 +0000420\begin{verbatim}
421from distutils.core import setup, Extension
Fred Drakea09262e2001-03-01 18:35:43 +0000422setup(name="foo", version="1.0",
423 ext_modules=[Extension("foo", ["foo.c"])])
Greg Ward2afffd42000-08-06 20:37:24 +0000424\end{verbatim}
425
426The \class{Extension} class (actually, the underlying extension-building
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +0000427machinery implemented by the \command{build\_ext} command) supports a
Greg Ward2afffd42000-08-06 20:37:24 +0000428great deal of flexibility in describing Python extensions, which is
429explained in the following sections.
430
431
432\subsubsection{Extension names and packages}
433
434The first argument to the \class{Extension} constructor is always the
435name of the extension, including any package names. For example,
Fred Drakea09262e2001-03-01 18:35:43 +0000436
Greg Ward2afffd42000-08-06 20:37:24 +0000437\begin{verbatim}
438Extension("foo", ["src/foo1.c", "src/foo2.c"])
439\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000440
Greg Ward2afffd42000-08-06 20:37:24 +0000441describes an extension that lives in the root package, while
Fred Drakea09262e2001-03-01 18:35:43 +0000442
Greg Ward2afffd42000-08-06 20:37:24 +0000443\begin{verbatim}
444Extension("pkg.foo", ["src/foo1.c", "src/foo2.c"])
445\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000446
Greg Ward2afffd42000-08-06 20:37:24 +0000447describes the same extension in the \module{pkg} package. The source
448files and resulting object code are identical in both cases; the only
449difference is where in the filesystem (and therefore where in Python's
450namespace hierarchy) the resulting extension lives.
451
452If you have a number of extensions all in the same package (or all under
453the same base package), use the \option{ext\_package} keyword argument
454to \function{setup()}. For example,
Fred Drakea09262e2001-03-01 18:35:43 +0000455
Greg Ward2afffd42000-08-06 20:37:24 +0000456\begin{verbatim}
457setup(...
Fred Drakea09262e2001-03-01 18:35:43 +0000458 ext_package="pkg",
459 ext_modules=[Extension("foo", ["foo.c"]),
460 Extension("subpkg.bar", ["bar.c"])]
Greg Ward2afffd42000-08-06 20:37:24 +0000461 )
462\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000463
Greg Ward2afffd42000-08-06 20:37:24 +0000464will compile \file{foo.c} to the extension \module{pkg.foo}, and
465\file{bar.c} to \module{pkg.subpkg.bar}.
466
467
468\subsubsection{Extension source files}
469
470The second argument to the \class{Extension} constructor is a list of
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000471source files. Since the Distutils currently only support C, \Cpp, and
472Objective-C extensions, these are normally C/\Cpp/Objective-C source
473files. (Be sure to use appropriate extensions to distinguish \Cpp\
474source files: \file{.cc} and \file{.cpp} seem to be recognized by both
475\UNIX{} and Windows compilers.)
Greg Ward2afffd42000-08-06 20:37:24 +0000476
477However, you can also include SWIG interface (\file{.i}) files in the
478list; the \command{build\_ext} command knows how to deal with SWIG
479extensions: it will run SWIG on the interface file and compile the
Fred Drake2884d6d2003-07-02 12:27:43 +0000480resulting C/\Cpp{} file into your extension.
Greg Ward2afffd42000-08-06 20:37:24 +0000481
482\XXX{SWIG support is rough around the edges and largely untested;
Fred Drake2884d6d2003-07-02 12:27:43 +0000483 especially SWIG support for \Cpp{} extensions! Explain in more detail
Greg Ward2afffd42000-08-06 20:37:24 +0000484 here when the interface firms up.}
485
486On some platforms, you can include non-source files that are processed
487by the compiler and included in your extension. Currently, this just
Thomas Heller5f52f722001-02-19 17:48:03 +0000488means Windows message text (\file{.mc}) files and resource definition
Fred Drake2884d6d2003-07-02 12:27:43 +0000489(\file{.rc}) files for Visual \Cpp. These will be compiled to binary resource
Thomas Heller5f52f722001-02-19 17:48:03 +0000490(\file{.res}) files and linked into the executable.
Greg Ward2afffd42000-08-06 20:37:24 +0000491
492
493\subsubsection{Preprocessor options}
494
495Three optional arguments to \class{Extension} will help if you need to
496specify include directories to search or preprocessor macros to
497define/undefine: \code{include\_dirs}, \code{define\_macros}, and
498\code{undef\_macros}.
499
500For example, if your extension requires header files in the
501\file{include} directory under your distribution root, use the
502\code{include\_dirs} option:
Fred Drakea09262e2001-03-01 18:35:43 +0000503
Greg Ward2afffd42000-08-06 20:37:24 +0000504\begin{verbatim}
505Extension("foo", ["foo.c"], include_dirs=["include"])
506\end{verbatim}
507
508You can specify absolute directories there; if you know that your
Fred Drakeeff9a872000-10-26 16:41:03 +0000509extension will only be built on \UNIX{} systems with X11R6 installed to
Greg Ward2afffd42000-08-06 20:37:24 +0000510\file{/usr}, you can get away with
Fred Drakea09262e2001-03-01 18:35:43 +0000511
Greg Ward2afffd42000-08-06 20:37:24 +0000512\begin{verbatim}
513Extension("foo", ["foo.c"], include_dirs=["/usr/include/X11"])
514\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000515
Greg Ward2afffd42000-08-06 20:37:24 +0000516You should avoid this sort of non-portable usage if you plan to
Greg Ward58437f22002-05-10 14:40:22 +0000517distribute your code: it's probably better to write C code like
518\begin{verbatim}
519#include <X11/Xlib.h>
520\end{verbatim}
Greg Ward2afffd42000-08-06 20:37:24 +0000521
522If you need to include header files from some other Python extension,
Greg Ward58437f22002-05-10 14:40:22 +0000523you can take advantage of the fact that header files are installed in a
524consistent way by the Distutils \command{install\_header} command. For
525example, the Numerical Python header files are installed (on a standard
526Unix installation) to \file{/usr/local/include/python1.5/Numerical}.
527(The exact location will differ according to your platform and Python
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000528installation.) Since the Python include
Greg Ward58437f22002-05-10 14:40:22 +0000529directory---\file{/usr/local/include/python1.5} in this case---is always
530included in the search path when building Python extensions, the best
531approach is to write C code like
532\begin{verbatim}
533#include <Numerical/arrayobject.h>
534\end{verbatim}
535If you must put the \file{Numerical} include directory right into your
536header search path, though, you can find that directory using the
537Distutils \module{sysconfig} module:
Fred Drakea09262e2001-03-01 18:35:43 +0000538
Greg Ward2afffd42000-08-06 20:37:24 +0000539\begin{verbatim}
540from distutils.sysconfig import get_python_inc
541incdir = os.path.join(get_python_inc(plat_specific=1), "Numerical")
542setup(...,
543 Extension(..., include_dirs=[incdir]))
544\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000545
Greg Ward2afffd42000-08-06 20:37:24 +0000546Even though this is quite portable---it will work on any Python
547installation, regardless of platform---it's probably easier to just
548write your C code in the sensible way.
549
550You can define and undefine pre-processor macros with the
551\code{define\_macros} and \code{undef\_macros} options.
552\code{define\_macros} takes a list of \code{(name, value)} tuples, where
553\code{name} is the name of the macro to define (a string) and
554\code{value} is its value: either a string or \code{None}. (Defining a
555macro \code{FOO} to \code{None} is the equivalent of a bare
556\code{\#define FOO} in your C source: with most compilers, this sets
557\code{FOO} to the string \code{1}.) \code{undef\_macros} is just
558a list of macros to undefine.
559
560For example:
Fred Drakea09262e2001-03-01 18:35:43 +0000561
Greg Ward2afffd42000-08-06 20:37:24 +0000562\begin{verbatim}
563Extension(...,
Thomas Heller95a97d52003-10-08 12:01:33 +0000564 define_macros=[('NDEBUG', '1'),
565 ('HAVE_STRFTIME', None)],
Greg Ward2afffd42000-08-06 20:37:24 +0000566 undef_macros=['HAVE_FOO', 'HAVE_BAR'])
567\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000568
Greg Ward2afffd42000-08-06 20:37:24 +0000569is the equivalent of having this at the top of every C source file:
Fred Drakea09262e2001-03-01 18:35:43 +0000570
Greg Ward2afffd42000-08-06 20:37:24 +0000571\begin{verbatim}
572#define NDEBUG 1
573#define HAVE_STRFTIME
574#undef HAVE_FOO
575#undef HAVE_BAR
576\end{verbatim}
577
578
579\subsubsection{Library options}
580
581You can also specify the libraries to link against when building your
582extension, and the directories to search for those libraries. The
583\code{libraries} option is a list of libraries to link against,
584\code{library\_dirs} is a list of directories to search for libraries at
585link-time, and \code{runtime\_library\_dirs} is a list of directories to
586search for shared (dynamically loaded) libraries at run-time.
587
588For example, if you need to link against libraries known to be in the
589standard library search path on target systems
Fred Drakea09262e2001-03-01 18:35:43 +0000590
Greg Ward2afffd42000-08-06 20:37:24 +0000591\begin{verbatim}
592Extension(...,
593 libraries=["gdbm", "readline"])
594\end{verbatim}
595
596If you need to link with libraries in a non-standard location, you'll
597have to include the location in \code{library\_dirs}:
Fred Drakea09262e2001-03-01 18:35:43 +0000598
Greg Ward2afffd42000-08-06 20:37:24 +0000599\begin{verbatim}
600Extension(...,
601 library_dirs=["/usr/X11R6/lib"],
602 libraries=["X11", "Xt"])
603\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000604
Greg Ward2afffd42000-08-06 20:37:24 +0000605(Again, this sort of non-portable construct should be avoided if you
606intend to distribute your code.)
607
Thomas Heller5f52f722001-02-19 17:48:03 +0000608\XXX{Should mention clib libraries here or somewhere else!}
609
610\subsubsection{Other options}
611
612There are still some other options which can be used to handle special
613cases.
614
615The \option{extra\_objects} option is a list of object files to be passed
616to the linker. These files must not have extensions, as the default
617extension for the compiler is used.
618
619\option{extra\_compile\_args} and \option{extra\_link\_args} can be used
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000620to specify additional command line options for the respective compiler and
621linker command lines.
Thomas Heller5f52f722001-02-19 17:48:03 +0000622
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000623\option{export\_symbols} is only useful on Windows. It can contain a list
Thomas Heller5f52f722001-02-19 17:48:03 +0000624of symbols (functions or variables) to be exported. This option
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000625is not needed when building compiled extensions: Distutils
626will automatically add \code{initmodule}
627to the list of exported symbols.
Thomas Heller5f52f722001-02-19 17:48:03 +0000628
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000629\subsection{Installing Scripts}
Thomas Heller5f52f722001-02-19 17:48:03 +0000630So far we have been dealing with pure and non-pure Python modules,
631which are usually not run by themselves but imported by scripts.
632
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000633Scripts are files containing Python source code, intended to be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000634started from the command line. Scripts don't require Distutils to do
635anything very complicated. The only clever feature is that if the
636first line of the script starts with \code{\#!} and contains the word
637``python'', the Distutils will adjust the first line to refer to the
638current interpreter location.
Thomas Heller5f52f722001-02-19 17:48:03 +0000639
640The \option{scripts} option simply is a list of files to be handled
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000641in this way. From the PyXML setup script:
642
643\begin{verbatim}
644setup (...
645 scripts = ['scripts/xmlproc_parse', 'scripts/xmlproc_val']
646 )
647\end{verbatim}
Thomas Heller5f52f722001-02-19 17:48:03 +0000648
649
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000650\subsection{Installing Additional Files}
Fred Drakea09262e2001-03-01 18:35:43 +0000651
Thomas Heller5f52f722001-02-19 17:48:03 +0000652The \option{data\_files} option can be used to specify additional
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000653files needed by the module distribution: configuration files, message
654catalogs, data files, anything which doesn't fit in the previous
655categories.
Thomas Heller5f52f722001-02-19 17:48:03 +0000656
Fred Drake632bda32002-03-08 22:02:06 +0000657\option{data\_files} specifies a sequence of (\var{directory},
658\var{files}) pairs in the following way:
Fred Drakea09262e2001-03-01 18:35:43 +0000659
Thomas Heller5f52f722001-02-19 17:48:03 +0000660\begin{verbatim}
661setup(...
662 data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000663 ('config', ['cfg/data.cfg']),
664 ('/etc/init.d', ['init-script'])]
665 )
Thomas Heller5f52f722001-02-19 17:48:03 +0000666\end{verbatim}
667
668Note that you can specify the directory names where the data files
669will be installed, but you cannot rename the data files themselves.
670
Fred Drake632bda32002-03-08 22:02:06 +0000671Each (\var{directory}, \var{files}) pair in the sequence specifies the
672installation directory and the files to install there. If
673\var{directory} is a relative path, it is interpreted relative to the
674installation prefix (Python's \code{sys.prefix} for pure-Python
675packages, \code{sys.exec_prefix} for packages that contain extension
676modules). Each file name in \var{files} is interpreted relative to
677the \file{setup.py} script at the top of the package source
678distribution. No directory information from \var{files} is used to
679determine the final location of the installed file; only the name of
680the file is used.
681
Thomas Heller5f52f722001-02-19 17:48:03 +0000682You can specify the \option{data\_files} options as a simple sequence
683of files without specifying a target directory, but this is not recommended,
684and the \command{install} command will print a warning in this case.
685To install data files directly in the target directory, an empty
686string should be given as the directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000687
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000688\subsection{Additional meta-data}
689\label{meta-data}
690
691The setup script may include additional meta-data beyond the name and
692version. This information includes:
693
Fred Drakec440af52003-04-25 16:43:28 +0000694\begin{tableiv}{l|l|l|c}{code}%
695 {Meta-Data}{Description}{Value}{Notes}
696 \lineiv{name}{name of the package}
697 {short string}{(1)}
698 \lineiv{version}{version of this release}
699 {short string}{(1)(2)}
700 \lineiv{author}{package author's name}
701 {short string}{(3)}
702 \lineiv{author_email}{email address of the package author}
703 {email address}{(3)}
704 \lineiv{maintainer}{package maintainer's name}
705 {short string}{(3)}
706 \lineiv{maintainer_email}{email address of the package maintainer}
707 {email address}{(3)}
708 \lineiv{url}{home page for the package}
709 {URL}{(1)}
710 \lineiv{description}{short, summary description of the package}
711 {short string}{}
712 \lineiv{long_description}{longer description of the package}
713 {long string}{}
714 \lineiv{download_url}{location where the package may be downloaded}
715 {URL}{(4)}
716 \lineiv{classifiers}{a list of Trove classifiers}
717 {list of strings}{(4)}
718\end{tableiv}
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000719
720\noindent Notes:
721\begin{description}
Fred Drakec440af52003-04-25 16:43:28 +0000722\item[(1)] These fields are required.
723\item[(2)] It is recommended that versions take the form
724 \emph{major.minor\optional{.patch\optional{.sub}}}.
725\item[(3)] Either the author or the maintainer must be identified.
726\item[(4)] These fields should not be used if your package is to be
727 compatible with Python versions prior to 2.2.3 or 2.3. The list is
728 available from the \ulink{PyPI website}{http://www.python.org/pypi}.
729
730\item["short string"] A single line of text, not more than 200 characters.
731\item["long string"] Multiple lines of plain text in ReStructuredText
732 format (see \url{http://docutils.sf.net/}).
733\item["list of strings"] See below.
734\end{description}
735
736None of the string values may be Unicode.
737
738Encoding the version information is an art in itself. Python packages
739generally adhere to the version format
740\emph{major.minor\optional{.patch}\optional{sub}}. The major number is
7410 for
742initial, experimental releases of software. It is incremented for
743releases that represent major milestones in a package. The minor
744number is incremented when important new features are added to the
745package. The patch number increments when bug-fix releases are
746made. Additional trailing version information is sometimes used to
747indicate sub-releases. These are "a1,a2,...,aN" (for alpha releases,
748where functionality and API may change), "b1,b2,...,bN" (for beta
749releases, which only fix bugs) and "pr1,pr2,...,prN" (for final
750pre-release release testing). Some examples:
751
752\begin{description}
753\item[0.1.0] the first, experimental release of a package
754\item[1.0.1a2] the second alpha release of the first patch version of 1.0
755\end{description}
756
757\option{classifiers} are specified in a python list:
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000758
759\begin{verbatim}
760setup(...
Fred Drakec440af52003-04-25 16:43:28 +0000761 classifiers = [
Fred Drake2a046232003-03-31 16:23:09 +0000762 'Development Status :: 4 - Beta',
763 'Environment :: Console',
764 'Environment :: Web Environment',
765 'Intended Audience :: End Users/Desktop',
766 'Intended Audience :: Developers',
767 'Intended Audience :: System Administrators',
768 'License :: OSI Approved :: Python Software Foundation License',
769 'Operating System :: MacOS :: MacOS X',
770 'Operating System :: Microsoft :: Windows',
771 'Operating System :: POSIX',
772 'Programming Language :: Python',
773 'Topic :: Communications :: Email',
774 'Topic :: Office/Business',
775 'Topic :: Software Development :: Bug Tracking',
776 ],
Fred Drake2a046232003-03-31 16:23:09 +0000777 )
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000778\end{verbatim}
779
Fred Drakec440af52003-04-25 16:43:28 +0000780If you wish to include classifiers in your \file{setup.py} file and also
781wish to remain backwards-compatible with Python releases prior to 2.2.3,
782then you can include the following code fragment in your \file{setup.py}
783before the \code{setup()} call.
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000784
785\begin{verbatim}
Fred Drakec440af52003-04-25 16:43:28 +0000786# patch distutils if it can't cope with the "classifiers" or
787# "download_url" keywords
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000788if sys.version < '2.2.3':
789 from distutils.dist import DistributionMetadata
790 DistributionMetadata.classifiers = None
Fred Drake2a046232003-03-31 16:23:09 +0000791 DistributionMetadata.download_url = None
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000792\end{verbatim}
793
Greg Ward16aafcd2000-04-09 04:06:44 +0000794
Thomas Heller675580f2003-06-30 19:33:29 +0000795\subsection{Debugging the setup script}
796\label{meta-data}
797
798Sometimes things go wrong, and the setup script doesn't do what the
799developer wants.
800
801Distutils catches any exceptions when running the setup script, and
802print a simple error message before the script is terminated. The
803motivation for this behaviour is to not confuse administrators who
804don't know much about Python and are trying to install a package. If
805they get a big long traceback from deep inside the guts of Distutils,
806they may think the package or the Python installation is broken
807because they don't read all the way down to the bottom and see that
808it's a permission problem.
809
810On the other hand, this doesn't help the developer to find the cause
811of the failure. For this purpose, the DISTUTILS_DEBUG environment
812variable can be set to anything except an empty string, and distutils
813will now print detailed information what it is doing, and prints the
Martin v. Löwis95cf84a2003-10-19 07:32:24 +0000814full traceback in case an exception occurs.
Thomas Heller675580f2003-06-30 19:33:29 +0000815
Fred Drake211a2eb2004-03-22 21:44:43 +0000816\chapter{Writing the Setup Configuration File}
Greg Warde78298a2000-04-28 17:12:24 +0000817\label{setup-config}
Greg Ward16aafcd2000-04-09 04:06:44 +0000818
Greg Ward16aafcd2000-04-09 04:06:44 +0000819Often, it's not possible to write down everything needed to build a
Greg Ward47f99a62000-09-04 20:07:15 +0000820distribution \emph{a priori}: you may need to get some information from
821the user, or from the user's system, in order to proceed. As long as
822that information is fairly simple---a list of directories to search for
823C header files or libraries, for example---then providing a
824configuration file, \file{setup.cfg}, for users to edit is a cheap and
825easy way to solicit it. Configuration files also let you provide
826default values for any command option, which the installer can then
827override either on the command-line or by editing the config file.
Greg Ward16aafcd2000-04-09 04:06:44 +0000828
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000829% (If you have more advanced needs, such as determining which extensions
830% to build based on what capabilities are present on the target system,
831% then you need the Distutils ``auto-configuration'' facility. This
832% started to appear in Distutils 0.9 but, as of this writing, isn't mature
833% or stable enough yet for real-world use.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000834
Greg Ward47f99a62000-09-04 20:07:15 +0000835The setup configuration file is a useful middle-ground between the setup
836script---which, ideally, would be opaque to installers\footnote{This
837 ideal probably won't be achieved until auto-configuration is fully
838 supported by the Distutils.}---and the command-line to the setup
839script, which is outside of your control and entirely up to the
840installer. In fact, \file{setup.cfg} (and any other Distutils
841configuration files present on the target system) are processed after
842the contents of the setup script, but before the command-line. This has
843several useful consequences:
844\begin{itemize}
845\item installers can override some of what you put in \file{setup.py} by
846 editing \file{setup.cfg}
847\item you can provide non-standard defaults for options that are not
848 easily set in \file{setup.py}
849\item installers can override anything in \file{setup.cfg} using the
850 command-line options to \file{setup.py}
851\end{itemize}
852
853The basic syntax of the configuration file is simple:
Fred Drakea09262e2001-03-01 18:35:43 +0000854
Greg Ward47f99a62000-09-04 20:07:15 +0000855\begin{verbatim}
856[command]
857option=value
858...
859\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000860
Greg Ward47f99a62000-09-04 20:07:15 +0000861where \var{command} is one of the Distutils commands (e.g.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000862\command{build\_py}, \command{install}), and \var{option} is one of
863the options that command supports. Any number of options can be
864supplied for each command, and any number of command sections can be
865included in the file. Blank lines are ignored, as are comments, which
866run from a \character{\#} character until the end of the line. Long
867option values can be split across multiple lines simply by indenting
868the continuation lines.
Greg Ward47f99a62000-09-04 20:07:15 +0000869
870You can find out the list of options supported by a particular command
871with the universal \longprogramopt{help} option, e.g.
Fred Drakea09262e2001-03-01 18:35:43 +0000872
Greg Ward47f99a62000-09-04 20:07:15 +0000873\begin{verbatim}
874> python setup.py --help build_ext
875[...]
876Options for 'build_ext' command:
877 --build-lib (-b) directory for compiled extension modules
878 --build-temp (-t) directory for temporary files (build by-products)
879 --inplace (-i) ignore build-lib and put compiled extensions into the
880 source directory alongside your pure Python modules
881 --include-dirs (-I) list of directories to search for header files
882 --define (-D) C preprocessor macros to define
883 --undef (-U) C preprocessor macros to undefine
884[...]
885\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000886
Greg Ward47f99a62000-09-04 20:07:15 +0000887Note that an option spelled \longprogramopt{foo-bar} on the command-line
888is spelled \option{foo\_bar} in configuration files.
889
890For example, say you want your extensions to be built
891``in-place''---that is, you have an extension \module{pkg.ext}, and you
Fred Drakeeff9a872000-10-26 16:41:03 +0000892want the compiled extension file (\file{ext.so} on \UNIX, say) to be put
Greg Ward47f99a62000-09-04 20:07:15 +0000893in the same source directory as your pure Python modules
894\module{pkg.mod1} and \module{pkg.mod2}. You can always use the
895\longprogramopt{inplace} option on the command-line to ensure this:
Fred Drakea09262e2001-03-01 18:35:43 +0000896
Greg Ward47f99a62000-09-04 20:07:15 +0000897\begin{verbatim}
898python setup.py build_ext --inplace
899\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000900
Greg Ward47f99a62000-09-04 20:07:15 +0000901But this requires that you always specify the \command{build\_ext}
902command explicitly, and remember to provide \longprogramopt{inplace}.
903An easier way is to ``set and forget'' this option, by encoding it in
904\file{setup.cfg}, the configuration file for this distribution:
Fred Drakea09262e2001-03-01 18:35:43 +0000905
Greg Ward47f99a62000-09-04 20:07:15 +0000906\begin{verbatim}
907[build_ext]
908inplace=1
909\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000910
Greg Ward47f99a62000-09-04 20:07:15 +0000911This will affect all builds of this module distribution, whether or not
912you explcitly specify \command{build\_ext}. If you include
913\file{setup.cfg} in your source distribution, it will also affect
914end-user builds---which is probably a bad idea for this option, since
915always building extensions in-place would break installation of the
916module distribution. In certain peculiar cases, though, modules are
917built right in their installation directory, so this is conceivably a
918useful ability. (Distributing extensions that expect to be built in
919their installation directory is almost always a bad idea, though.)
920
921Another example: certain commands take a lot of options that don't
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000922change from run to run; for example, \command{bdist\_rpm} needs to know
Greg Ward47f99a62000-09-04 20:07:15 +0000923everything required to generate a ``spec'' file for creating an RPM
924distribution. Some of this information comes from the setup script, and
925some is automatically generated by the Distutils (such as the list of
926files installed). But some of it has to be supplied as options to
927\command{bdist\_rpm}, which would be very tedious to do on the
928command-line for every run. Hence, here is a snippet from the
929Distutils' own \file{setup.cfg}:
Fred Drakea09262e2001-03-01 18:35:43 +0000930
Greg Ward47f99a62000-09-04 20:07:15 +0000931\begin{verbatim}
932[bdist_rpm]
933release = 1
934packager = Greg Ward <gward@python.net>
935doc_files = CHANGES.txt
936 README.txt
937 USAGE.txt
938 doc/
939 examples/
940\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000941
Greg Ward47f99a62000-09-04 20:07:15 +0000942Note that the \option{doc\_files} option is simply a
943whitespace-separated string split across multiple lines for readability.
Greg Ward16aafcd2000-04-09 04:06:44 +0000944
945
Fred Drakea09262e2001-03-01 18:35:43 +0000946\begin{seealso}
947 \seetitle[../inst/config-syntax.html]{Installing Python
948 Modules}{More information on the configuration files is
949 available in the manual for system administrators.}
950\end{seealso}
951
952
Fred Drake211a2eb2004-03-22 21:44:43 +0000953\chapter{Creating a Source Distribution}
Greg Warde78298a2000-04-28 17:12:24 +0000954\label{source-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +0000955
Greg Warde78298a2000-04-28 17:12:24 +0000956As shown in section~\ref{simple-example}, you use the
Greg Ward16aafcd2000-04-09 04:06:44 +0000957\command{sdist} command to create a source distribution. In the
958simplest case,
Fred Drakea09262e2001-03-01 18:35:43 +0000959
Greg Ward16aafcd2000-04-09 04:06:44 +0000960\begin{verbatim}
961python setup.py sdist
962\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000963
Greg Ward19c67f82000-06-24 01:33:16 +0000964(assuming you haven't specified any \command{sdist} options in the setup
965script or config file), \command{sdist} creates the archive of the
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000966default format for the current platform. The default format is a gzip'ed
Fred Drakeeff9a872000-10-26 16:41:03 +0000967tar file (\file{.tar.gz}) on \UNIX, and ZIP file on Windows.
Fred Drake781380c2004-02-19 23:17:46 +0000968\XXX{no Mac OS support here}
Greg Ward54589d42000-09-06 01:37:35 +0000969
Greg Wardd5767a52000-04-19 22:48:09 +0000970You can specify as many formats as you like using the
971\longprogramopt{formats} option, for example:
Fred Drakea09262e2001-03-01 18:35:43 +0000972
Greg Ward16aafcd2000-04-09 04:06:44 +0000973\begin{verbatim}
974python setup.py sdist --formats=gztar,zip
975\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000976
Greg Ward16aafcd2000-04-09 04:06:44 +0000977to create a gzipped tarball and a zip file. The available formats are:
Fred Drake781380c2004-02-19 23:17:46 +0000978
Greg Ward46b98e32000-04-14 01:53:36 +0000979\begin{tableiii}{l|l|c}{code}%
980 {Format}{Description}{Notes}
Greg Ward54589d42000-09-06 01:37:35 +0000981 \lineiii{zip}{zip file (\file{.zip})}{(1),(3)}
982 \lineiii{gztar}{gzip'ed tar file (\file{.tar.gz})}{(2),(4)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000983 \lineiii{bztar}{bzip2'ed tar file (\file{.tar.bz2})}{(4)}
Greg Ward47f99a62000-09-04 20:07:15 +0000984 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(4)}
Greg Ward54589d42000-09-06 01:37:35 +0000985 \lineiii{tar}{tar file (\file{.tar})}{(4)}
Greg Ward46b98e32000-04-14 01:53:36 +0000986\end{tableiii}
987
988\noindent Notes:
989\begin{description}
990\item[(1)] default on Windows
Fred Drakeeff9a872000-10-26 16:41:03 +0000991\item[(2)] default on \UNIX
Greg Wardb6528972000-09-07 02:40:37 +0000992\item[(3)] requires either external \program{zip} utility or
Greg Ward954ce8b2002-05-10 14:42:10 +0000993 \module{zipfile} module (part of the standard Python library since
994 Python~1.6)
Greg Ward47f99a62000-09-04 20:07:15 +0000995\item[(4)] requires external utilities: \program{tar} and possibly one
996 of \program{gzip}, \program{bzip2}, or \program{compress}
Greg Ward46b98e32000-04-14 01:53:36 +0000997\end{description}
Greg Ward16aafcd2000-04-09 04:06:44 +0000998
999
Greg Ward54589d42000-09-06 01:37:35 +00001000
1001\subsection{Specifying the files to distribute}
Greg Warde78298a2000-04-28 17:12:24 +00001002\label{manifest}
Greg Ward16aafcd2000-04-09 04:06:44 +00001003
Greg Ward54589d42000-09-06 01:37:35 +00001004If you don't supply an explicit list of files (or instructions on how to
1005generate one), the \command{sdist} command puts a minimal default set
1006into the source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +00001007\begin{itemize}
Greg Wardfacb8db2000-04-09 04:32:40 +00001008\item all Python source files implied by the \option{py\_modules} and
Greg Ward16aafcd2000-04-09 04:06:44 +00001009 \option{packages} options
Greg Wardfacb8db2000-04-09 04:32:40 +00001010\item all C source files mentioned in the \option{ext\_modules} or
Greg Ward16aafcd2000-04-09 04:06:44 +00001011 \option{libraries} options (\XXX{getting C library sources currently
Fred Drake781380c2004-02-19 23:17:46 +00001012 broken---no \method{get_source_files()} method in \file{build_clib.py}!})
Greg Ward16aafcd2000-04-09 04:06:44 +00001013\item anything that looks like a test script: \file{test/test*.py}
1014 (currently, the Distutils don't do anything with test scripts except
1015 include them in source distributions, but in the future there will be
1016 a standard for testing Python module distributions)
Greg Ward54589d42000-09-06 01:37:35 +00001017\item \file{README.txt} (or \file{README}), \file{setup.py} (or whatever
1018 you called your setup script), and \file{setup.cfg}
Greg Ward16aafcd2000-04-09 04:06:44 +00001019\end{itemize}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001020
Greg Ward16aafcd2000-04-09 04:06:44 +00001021Sometimes this is enough, but usually you will want to specify
1022additional files to distribute. The typical way to do this is to write
1023a \emph{manifest template}, called \file{MANIFEST.in} by default. The
Greg Ward54589d42000-09-06 01:37:35 +00001024manifest template is just a list of instructions for how to generate
1025your manifest file, \file{MANIFEST}, which is the exact list of files to
1026include in your source distribution. The \command{sdist} command
1027processes this template and generates a manifest based on its
1028instructions and what it finds in the filesystem.
1029
1030If you prefer to roll your own manifest file, the format is simple: one
1031filename per line, regular files (or symlinks to them) only. If you do
1032supply your own \file{MANIFEST}, you must specify everything: the
1033default set of files described above does not apply in this case.
Greg Ward16aafcd2000-04-09 04:06:44 +00001034
1035The manifest template has one command per line, where each command
1036specifies a set of files to include or exclude from the source
1037distribution. For an example, again we turn to the Distutils' own
1038manifest template:
Fred Drakea09262e2001-03-01 18:35:43 +00001039
Greg Ward16aafcd2000-04-09 04:06:44 +00001040\begin{verbatim}
1041include *.txt
Greg Ward87da1ea2000-04-21 04:35:25 +00001042recursive-include examples *.txt *.py
Greg Ward16aafcd2000-04-09 04:06:44 +00001043prune examples/sample?/build
1044\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001045
Greg Ward16aafcd2000-04-09 04:06:44 +00001046The meanings should be fairly clear: include all files in the
1047distribution root matching \code{*.txt}, all files anywhere under the
1048\file{examples} directory matching \code{*.txt} or \code{*.py}, and
Greg Ward54589d42000-09-06 01:37:35 +00001049exclude all directories matching \code{examples/sample?/build}. All of
1050this is done \emph{after} the standard include set, so you can exclude
1051files from the standard set with explicit instructions in the manifest
1052template. (Or, you can use the \longprogramopt{no-defaults} option to
1053disable the standard set entirely.) There are several other commands
1054available in the manifest template mini-language; see
1055section~\ref{sdist-cmd}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001056
Greg Ward54589d42000-09-06 01:37:35 +00001057The order of commands in the manifest template matters: initially, we
1058have the list of default files as described above, and each command in
1059the template adds to or removes from that list of files. Once we have
1060fully processed the manifest template, we remove files that should not
1061be included in the source distribution:
1062\begin{itemize}
1063\item all files in the Distutils ``build'' tree (default \file{build/})
1064\item all files in directories named \file{RCS} or \file{CVS}
1065\end{itemize}
1066Now we have our complete list of files, which is written to the manifest
1067for future reference, and then used to build the source distribution
1068archive(s).
1069
1070You can disable the default set of included files with the
1071\longprogramopt{no-defaults} option, and you can disable the standard
1072exclude set with \longprogramopt{no-prune}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001073
Greg Ward46b98e32000-04-14 01:53:36 +00001074Following the Distutils' own manifest template, let's trace how the
Greg Ward47f99a62000-09-04 20:07:15 +00001075\command{sdist} command builds the list of files to include in the
Greg Ward46b98e32000-04-14 01:53:36 +00001076Distutils source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +00001077\begin{enumerate}
1078\item include all Python source files in the \file{distutils} and
1079 \file{distutils/command} subdirectories (because packages
1080 corresponding to those two directories were mentioned in the
Greg Ward54589d42000-09-06 01:37:35 +00001081 \option{packages} option in the setup script---see
1082 section~\ref{setup-script})
1083\item include \file{README.txt}, \file{setup.py}, and \file{setup.cfg}
1084 (standard files)
1085\item include \file{test/test*.py} (standard files)
Greg Ward16aafcd2000-04-09 04:06:44 +00001086\item include \file{*.txt} in the distribution root (this will find
1087 \file{README.txt} a second time, but such redundancies are weeded out
1088 later)
Greg Ward54589d42000-09-06 01:37:35 +00001089\item include anything matching \file{*.txt} or \file{*.py} in the
1090 sub-tree under \file{examples},
1091\item exclude all files in the sub-trees starting at directories
1092 matching \file{examples/sample?/build}---this may exclude files
1093 included by the previous two steps, so it's important that the
1094 \code{prune} command in the manifest template comes after the
1095 \code{recursive-include} command
1096\item exclude the entire \file{build} tree, and any \file{RCS} or
1097 \file{CVS} directories
Greg Wardfacb8db2000-04-09 04:32:40 +00001098\end{enumerate}
Greg Ward46b98e32000-04-14 01:53:36 +00001099Just like in the setup script, file and directory names in the manifest
1100template should always be slash-separated; the Distutils will take care
1101of converting them to the standard representation on your platform.
1102That way, the manifest template is portable across operating systems.
1103
Greg Ward16aafcd2000-04-09 04:06:44 +00001104
1105\subsection{Manifest-related options}
Greg Warde78298a2000-04-28 17:12:24 +00001106\label{manifest-options}
Greg Ward16aafcd2000-04-09 04:06:44 +00001107
1108The normal course of operations for the \command{sdist} command is as
1109follows:
1110\begin{itemize}
Greg Ward46b98e32000-04-14 01:53:36 +00001111\item if the manifest file, \file{MANIFEST} doesn't exist, read
1112 \file{MANIFEST.in} and create the manifest
Greg Ward54589d42000-09-06 01:37:35 +00001113\item if neither \file{MANIFEST} nor \file{MANIFEST.in} exist, create a
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001114 manifest with just the default file set
Greg Ward1d8f57a2000-08-05 00:43:11 +00001115\item if either \file{MANIFEST.in} or the setup script (\file{setup.py})
1116 are more recent than \file{MANIFEST}, recreate \file{MANIFEST} by
1117 reading \file{MANIFEST.in}
Greg Ward16aafcd2000-04-09 04:06:44 +00001118\item use the list of files now in \file{MANIFEST} (either just
1119 generated or read in) to create the source distribution archive(s)
1120\end{itemize}
Greg Ward54589d42000-09-06 01:37:35 +00001121There are a couple of options that modify this behaviour. First, use
1122the \longprogramopt{no-defaults} and \longprogramopt{no-prune} to
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001123disable the standard ``include'' and ``exclude'' sets.
Greg Ward16aafcd2000-04-09 04:06:44 +00001124
Greg Ward54589d42000-09-06 01:37:35 +00001125Second, you might want to force the manifest to be regenerated---for
Greg Ward16aafcd2000-04-09 04:06:44 +00001126example, if you have added or removed files or directories that match an
1127existing pattern in the manifest template, you should regenerate the
1128manifest:
Fred Drakea09262e2001-03-01 18:35:43 +00001129
Greg Ward16aafcd2000-04-09 04:06:44 +00001130\begin{verbatim}
1131python setup.py sdist --force-manifest
1132\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +00001133
1134Or, you might just want to (re)generate the manifest, but not create a
1135source distribution:
Fred Drakea09262e2001-03-01 18:35:43 +00001136
Greg Ward16aafcd2000-04-09 04:06:44 +00001137\begin{verbatim}
1138python setup.py sdist --manifest-only
1139\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001140
Greg Ward54589d42000-09-06 01:37:35 +00001141\longprogramopt{manifest-only} implies \longprogramopt{force-manifest}.
1142\programopt{-o} is a shortcut for \longprogramopt{manifest-only}, and
1143\programopt{-f} for \longprogramopt{force-manifest}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001144
1145
Fred Drake211a2eb2004-03-22 21:44:43 +00001146\chapter{Creating Built Distributions}
Greg Warde78298a2000-04-28 17:12:24 +00001147\label{built-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +00001148
Greg Ward46b98e32000-04-14 01:53:36 +00001149A ``built distribution'' is what you're probably used to thinking of
1150either as a ``binary package'' or an ``installer'' (depending on your
1151background). It's not necessarily binary, though, because it might
1152contain only Python source code and/or byte-code; and we don't call it a
1153package, because that word is already spoken for in Python. (And
Fred Drake2a1bc502004-02-19 23:03:29 +00001154``installer'' is a term specific to the world of mainstream desktop
1155systems.)
Greg Ward16aafcd2000-04-09 04:06:44 +00001156
Greg Ward46b98e32000-04-14 01:53:36 +00001157A built distribution is how you make life as easy as possible for
1158installers of your module distribution: for users of RPM-based Linux
1159systems, it's a binary RPM; for Windows users, it's an executable
1160installer; for Debian-based Linux users, it's a Debian package; and so
1161forth. Obviously, no one person will be able to create built
Greg Wardb6528972000-09-07 02:40:37 +00001162distributions for every platform under the sun, so the Distutils are
Greg Ward46b98e32000-04-14 01:53:36 +00001163designed to enable module developers to concentrate on their
1164specialty---writing code and creating source distributions---while an
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001165intermediary species called \emph{packagers} springs up to turn source
Greg Ward19c67f82000-06-24 01:33:16 +00001166distributions into built distributions for as many platforms as there
Greg Ward46b98e32000-04-14 01:53:36 +00001167are packagers.
1168
1169Of course, the module developer could be his own packager; or the
1170packager could be a volunteer ``out there'' somewhere who has access to
1171a platform which the original developer does not; or it could be
1172software periodically grabbing new source distributions and turning them
1173into built distributions for as many platforms as the software has
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001174access to. Regardless of who they are, a packager uses the
Greg Ward46b98e32000-04-14 01:53:36 +00001175setup script and the \command{bdist} command family to generate built
1176distributions.
1177
1178As a simple example, if I run the following command in the Distutils
1179source tree:
Fred Drakea09262e2001-03-01 18:35:43 +00001180
Greg Ward46b98e32000-04-14 01:53:36 +00001181\begin{verbatim}
1182python setup.py bdist
1183\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001184
Greg Ward46b98e32000-04-14 01:53:36 +00001185then the Distutils builds my module distribution (the Distutils itself
1186in this case), does a ``fake'' installation (also in the \file{build}
1187directory), and creates the default type of built distribution for my
Greg Wardb6528972000-09-07 02:40:37 +00001188platform. The default format for built distributions is a ``dumb'' tar
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001189file on \UNIX, and a simple executable installer on Windows. (That tar
Greg Wardb6528972000-09-07 02:40:37 +00001190file is considered ``dumb'' because it has to be unpacked in a specific
1191location to work.)
Greg Ward1d8f57a2000-08-05 00:43:11 +00001192
Fred Drakeeff9a872000-10-26 16:41:03 +00001193Thus, the above command on a \UNIX{} system creates
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001194\file{Distutils-1.0.\filevar{plat}.tar.gz}; unpacking this tarball
Greg Wardb6528972000-09-07 02:40:37 +00001195from the right place installs the Distutils just as though you had
1196downloaded the source distribution and run \code{python setup.py
1197 install}. (The ``right place'' is either the root of the filesystem or
1198Python's \filevar{prefix} directory, depending on the options given to
1199the \command{bdist\_dumb} command; the default is to make dumb
1200distributions relative to \filevar{prefix}.)
Greg Ward46b98e32000-04-14 01:53:36 +00001201
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001202Obviously, for pure Python distributions, this isn't any simpler than
1203just running \code{python setup.py install}---but for non-pure
1204distributions, which include extensions that would need to be
1205compiled, it can mean the difference between someone being able to use
1206your extensions or not. And creating ``smart'' built distributions,
1207such as an RPM package or an executable installer for Windows, is far
1208more convenient for users even if your distribution doesn't include
1209any extensions.
Greg Ward46b98e32000-04-14 01:53:36 +00001210
Greg Wardb6528972000-09-07 02:40:37 +00001211The \command{bdist} command has a \longprogramopt{formats} option,
Greg Ward1d8f57a2000-08-05 00:43:11 +00001212similar to the \command{sdist} command, which you can use to select the
1213types of built distribution to generate: for example,
Fred Drakea09262e2001-03-01 18:35:43 +00001214
Greg Ward46b98e32000-04-14 01:53:36 +00001215\begin{verbatim}
1216python setup.py bdist --format=zip
1217\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001218
Fred Drakeeff9a872000-10-26 16:41:03 +00001219would, when run on a \UNIX{} system, create
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001220\file{Distutils-1.0.\filevar{plat}.zip}---again, this archive would be
Greg Ward1d8f57a2000-08-05 00:43:11 +00001221unpacked from the root directory to install the Distutils.
Greg Ward46b98e32000-04-14 01:53:36 +00001222
1223The available formats for built distributions are:
Fred Drake781380c2004-02-19 23:17:46 +00001224
Greg Ward46b98e32000-04-14 01:53:36 +00001225\begin{tableiii}{l|l|c}{code}%
1226 {Format}{Description}{Notes}
Greg Wardb6528972000-09-07 02:40:37 +00001227 \lineiii{gztar}{gzipped tar file (\file{.tar.gz})}{(1),(3)}
1228 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(3)}
1229 \lineiii{tar}{tar file (\file{.tar})}{(3)}
1230 \lineiii{zip}{zip file (\file{.zip})}{(4)}
1231 \lineiii{rpm}{RPM}{(5)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001232 \lineiii{pkgtool}{Solaris \program{pkgtool}}{}
1233 \lineiii{sdux}{HP-UX \program{swinstall}}{}
1234 \lineiii{rpm}{RPM}{(5)}
1235% \lineiii{srpm}{source RPM}{(5) \XXX{to do!}}
Thomas Heller5f52f722001-02-19 17:48:03 +00001236 \lineiii{wininst}{self-extracting ZIP file for Windows}{(2),(4)}
Greg Ward46b98e32000-04-14 01:53:36 +00001237\end{tableiii}
1238
1239\noindent Notes:
1240\begin{description}
Fred Drakeeff9a872000-10-26 16:41:03 +00001241\item[(1)] default on \UNIX
Greg Ward1d8f57a2000-08-05 00:43:11 +00001242\item[(2)] default on Windows \XXX{to-do!}
Greg Wardb6528972000-09-07 02:40:37 +00001243\item[(3)] requires external utilities: \program{tar} and possibly one
1244 of \program{gzip}, \program{bzip2}, or \program{compress}
1245\item[(4)] requires either external \program{zip} utility or
Greg Ward954ce8b2002-05-10 14:42:10 +00001246 \module{zipfile} module (part of the standard Python library since
1247 Python~1.6)
Greg Wardb6528972000-09-07 02:40:37 +00001248\item[(5)] requires external \program{rpm} utility, version 3.0.4 or
1249 better (use \code{rpm --version} to find out which version you have)
Greg Ward46b98e32000-04-14 01:53:36 +00001250\end{description}
1251
1252You don't have to use the \command{bdist} command with the
Greg Wardd5767a52000-04-19 22:48:09 +00001253\longprogramopt{formats} option; you can also use the command that
Greg Ward1d8f57a2000-08-05 00:43:11 +00001254directly implements the format you're interested in. Some of these
Greg Ward46b98e32000-04-14 01:53:36 +00001255\command{bdist} ``sub-commands'' actually generate several similar
1256formats; for instance, the \command{bdist\_dumb} command generates all
1257the ``dumb'' archive formats (\code{tar}, \code{ztar}, \code{gztar}, and
1258\code{zip}), and \command{bdist\_rpm} generates both binary and source
1259RPMs. The \command{bdist} sub-commands, and the formats generated by
1260each, are:
Fred Drake781380c2004-02-19 23:17:46 +00001261
Greg Ward46b98e32000-04-14 01:53:36 +00001262\begin{tableii}{l|l}{command}%
1263 {Command}{Formats}
1264 \lineii{bdist\_dumb}{tar, ztar, gztar, zip}
1265 \lineii{bdist\_rpm}{rpm, srpm}
Greg Ward1d8f57a2000-08-05 00:43:11 +00001266 \lineii{bdist\_wininst}{wininst}
Greg Ward46b98e32000-04-14 01:53:36 +00001267\end{tableii}
Greg Ward16aafcd2000-04-09 04:06:44 +00001268
Greg Wardb6528972000-09-07 02:40:37 +00001269The following sections give details on the individual \command{bdist\_*}
1270commands.
1271
1272
1273\subsection{Creating dumb built distributions}
1274\label{creating-dumb}
1275
1276\XXX{Need to document absolute vs. prefix-relative packages here, but
1277 first I have to implement it!}
1278
1279
1280\subsection{Creating RPM packages}
1281\label{creating-rpms}
1282
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001283The RPM format is used by many popular Linux distributions, including
Greg Wardb6528972000-09-07 02:40:37 +00001284Red Hat, SuSE, and Mandrake. If one of these (or any of the other
1285RPM-based Linux distributions) is your usual environment, creating RPM
1286packages for other users of that same distribution is trivial.
1287Depending on the complexity of your module distribution and differences
1288between Linux distributions, you may also be able to create RPMs that
1289work on different RPM-based distributions.
1290
1291The usual way to create an RPM of your module distribution is to run the
1292\command{bdist\_rpm} command:
Fred Drakea09262e2001-03-01 18:35:43 +00001293
Greg Wardb6528972000-09-07 02:40:37 +00001294\begin{verbatim}
1295python setup.py bdist_rpm
1296\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001297
Greg Wardb6528972000-09-07 02:40:37 +00001298or the \command{bdist} command with the \longprogramopt{format} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001299
Greg Wardb6528972000-09-07 02:40:37 +00001300\begin{verbatim}
1301python setup.py bdist --formats=rpm
1302\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001303
Greg Wardb6528972000-09-07 02:40:37 +00001304The former allows you to specify RPM-specific options; the latter allows
1305you to easily specify multiple formats in one run. If you need to do
1306both, you can explicitly specify multiple \command{bdist\_*} commands
1307and their options:
Fred Drakea09262e2001-03-01 18:35:43 +00001308
Greg Wardb6528972000-09-07 02:40:37 +00001309\begin{verbatim}
1310python setup.py bdist_rpm --packager="John Doe <jdoe@python.net>" \
1311 bdist_wininst --target_version="2.0"
1312\end{verbatim}
1313
1314Creating RPM packages is driven by a \file{.spec} file, much as using
1315the Distutils is driven by the setup script. To make your life easier,
1316the \command{bdist\_rpm} command normally creates a \file{.spec} file
1317based on the information you supply in the setup script, on the command
1318line, and in any Distutils configuration files. Various options and
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001319sections in the \file{.spec} file are derived from options in the setup
Greg Wardb6528972000-09-07 02:40:37 +00001320script as follows:
Fred Drake781380c2004-02-19 23:17:46 +00001321
Greg Wardb6528972000-09-07 02:40:37 +00001322\begin{tableii}{l|l}{textrm}%
1323 {RPM \file{.spec} file option or section}{Distutils setup script option}
1324 \lineii{Name}{\option{name}}
1325 \lineii{Summary (in preamble)}{\option{description}}
1326 \lineii{Version}{\option{version}}
1327 \lineii{Vendor}{\option{author} and \option{author\_email}, or \\&
1328 \option{maintainer} and \option{maintainer\_email}}
1329 \lineii{Copyright}{\option{licence}}
1330 \lineii{Url}{\option{url}}
1331 \lineii{\%description (section)}{\option{long\_description}}
1332\end{tableii}
1333
1334Additionally, there many options in \file{.spec} files that don't have
1335corresponding options in the setup script. Most of these are handled
1336through options to the \command{bdist\_rpm} command as follows:
Fred Drake781380c2004-02-19 23:17:46 +00001337
Greg Wardb6528972000-09-07 02:40:37 +00001338\begin{tableiii}{l|l|l}{textrm}%
1339 {RPM \file{.spec} file option or section}%
1340 {\command{bdist\_rpm} option}%
1341 {default value}
1342 \lineiii{Release}{\option{release}}{``1''}
1343 \lineiii{Group}{\option{group}}{``Development/Libraries''}
1344 \lineiii{Vendor}{\option{vendor}}{(see above)}
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001345 \lineiii{Packager}{\option{packager}}{(none)}
1346 \lineiii{Provides}{\option{provides}}{(none)}
1347 \lineiii{Requires}{\option{requires}}{(none)}
1348 \lineiii{Conflicts}{\option{conflicts}}{(none)}
1349 \lineiii{Obsoletes}{\option{obsoletes}}{(none)}
Greg Wardb6528972000-09-07 02:40:37 +00001350 \lineiii{Distribution}{\option{distribution\_name}}{(none)}
1351 \lineiii{BuildRequires}{\option{build\_requires}}{(none)}
1352 \lineiii{Icon}{\option{icon}}{(none)}
1353\end{tableiii}
Fred Drake781380c2004-02-19 23:17:46 +00001354
Greg Wardb6528972000-09-07 02:40:37 +00001355Obviously, supplying even a few of these options on the command-line
1356would be tedious and error-prone, so it's usually best to put them in
1357the setup configuration file, \file{setup.cfg}---see
1358section~\ref{setup-config}. If you distribute or package many Python
1359module distributions, you might want to put options that apply to all of
1360them in your personal Distutils configuration file
1361(\file{\textasciitilde/.pydistutils.cfg}).
1362
1363There are three steps to building a binary RPM package, all of which are
1364handled automatically by the Distutils:
Fred Drake781380c2004-02-19 23:17:46 +00001365
Greg Wardb6528972000-09-07 02:40:37 +00001366\begin{enumerate}
1367\item create a \file{.spec} file, which describes the package (analogous
1368 to the Distutils setup script; in fact, much of the information in the
1369 setup script winds up in the \file{.spec} file)
1370\item create the source RPM
1371\item create the ``binary'' RPM (which may or may not contain binary
1372 code, depending on whether your module distribution contains Python
1373 extensions)
1374\end{enumerate}
Fred Drake781380c2004-02-19 23:17:46 +00001375
Greg Wardb6528972000-09-07 02:40:37 +00001376Normally, RPM bundles the last two steps together; when you use the
1377Distutils, all three steps are typically bundled together.
1378
1379If you wish, you can separate these three steps. You can use the
Fred Drake781380c2004-02-19 23:17:46 +00001380\longprogramopt{spec-only} option to make \command{bdist_rpm} just
Greg Wardb6528972000-09-07 02:40:37 +00001381create the \file{.spec} file and exit; in this case, the \file{.spec}
1382file will be written to the ``distribution directory''---normally
1383\file{dist/}, but customizable with the \longprogramopt{dist-dir}
1384option. (Normally, the \file{.spec} file winds up deep in the ``build
Fred Drake781380c2004-02-19 23:17:46 +00001385tree,'' in a temporary directory created by \command{bdist_rpm}.)
Greg Wardb6528972000-09-07 02:40:37 +00001386
Fred Drake781380c2004-02-19 23:17:46 +00001387% \XXX{this isn't implemented yet---is it needed?!}
1388% You can also specify a custom \file{.spec} file with the
1389% \longprogramopt{spec-file} option; used in conjunction with
1390% \longprogramopt{spec-only}, this gives you an opportunity to customize
1391% the \file{.spec} file manually:
1392%
1393% \begin{verbatim}
1394% > python setup.py bdist_rpm --spec-only
1395% # ...edit dist/FooBar-1.0.spec
1396% > python setup.py bdist_rpm --spec-file=dist/FooBar-1.0.spec
1397% \end{verbatim}
1398%
1399% (Although a better way to do this is probably to override the standard
1400% \command{bdist\_rpm} command with one that writes whatever else you want
1401% to the \file{.spec} file.)
Greg Wardb6528972000-09-07 02:40:37 +00001402
1403
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001404\subsection{Creating Windows Installers}
Greg Wardb6528972000-09-07 02:40:37 +00001405\label{creating-wininst}
1406
Thomas Hellere61f3652002-11-15 20:13:26 +00001407Executable installers are the natural format for binary distributions
1408on Windows. They display a nice graphical user interface, display
1409some information about the module distribution to be installed taken
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +00001410from the metadata in the setup script, let the user select a few
Thomas Hellere61f3652002-11-15 20:13:26 +00001411options, and start or cancel the installation.
Greg Wardb6528972000-09-07 02:40:37 +00001412
Thomas Hellere61f3652002-11-15 20:13:26 +00001413Since the metadata is taken from the setup script, creating Windows
1414installers is usually as easy as running:
Fred Drakea09262e2001-03-01 18:35:43 +00001415
Thomas Heller5f52f722001-02-19 17:48:03 +00001416\begin{verbatim}
1417python setup.py bdist_wininst
1418\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001419
Thomas Heller36343f62002-11-15 19:20:56 +00001420or the \command{bdist} command with the \longprogramopt{formats} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001421
Thomas Heller5f52f722001-02-19 17:48:03 +00001422\begin{verbatim}
1423python setup.py bdist --formats=wininst
1424\end{verbatim}
1425
Thomas Hellere61f3652002-11-15 20:13:26 +00001426If you have a pure module distribution (only containing pure Python
1427modules and packages), the resulting installer will be version
1428independent and have a name like \file{foo-1.0.win32.exe}. These
Fred Drakec54d9252004-02-19 22:16:05 +00001429installers can even be created on \UNIX{} or Mac OS platforms.
Thomas Heller5f52f722001-02-19 17:48:03 +00001430
1431If you have a non-pure distribution, the extensions can only be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001432created on a Windows platform, and will be Python version dependent.
Thomas Heller5f52f722001-02-19 17:48:03 +00001433The installer filename will reflect this and now has the form
Thomas Hellere61f3652002-11-15 20:13:26 +00001434\file{foo-1.0.win32-py2.0.exe}. You have to create a separate installer
Thomas Heller5f52f722001-02-19 17:48:03 +00001435for every Python version you want to support.
1436
1437The installer will try to compile pure modules into bytecode after
Thomas Hellere61f3652002-11-15 20:13:26 +00001438installation on the target system in normal and optimizing mode. If
1439you don't want this to happen for some reason, you can run the
Fred Drake0e9bfa32002-11-15 20:34:52 +00001440\command{bdist_wininst} command with the
1441\longprogramopt{no-target-compile} and/or the
1442\longprogramopt{no-target-optimize} option.
Thomas Hellere61f3652002-11-15 20:13:26 +00001443
Fred Drake0e9bfa32002-11-15 20:34:52 +00001444By default the installer will display the cool ``Python Powered'' logo
Thomas Hellere61f3652002-11-15 20:13:26 +00001445when it is run, but you can also supply your own bitmap which must be
Fred Drake0e9bfa32002-11-15 20:34:52 +00001446a Windows \file{.bmp} file with the \longprogramopt{bitmap} option.
Thomas Hellere61f3652002-11-15 20:13:26 +00001447
1448The installer will also display a large title on the desktop
1449background window when it is run, which is constructed from the name
1450of your distribution and the version number. This can be changed to
1451another text by using the \longprogramopt{title} option.
1452
1453The installer file will be written to the ``distribution directory''
1454--- normally \file{dist/}, but customizable with the
1455\longprogramopt{dist-dir} option.
1456
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001457\subsubsection{The Postinstallation script}
1458\label{postinstallation-script}
1459
1460Starting with Python 2.3, a postinstallation script can be specified
1461which the \longprogramopt{install-script} option. The basename of the
1462script must be specified, and the script filename must also be listed
1463in the scripts argument to the setup function.
1464
1465This script will be run at installation time on the target system
Fred Drakec54d9252004-02-19 22:16:05 +00001466after all the files have been copied, with \code{argv[1]} set to
1467\programopt{-install}, and again at uninstallation time before the
1468files are removed with \code{argv[1]} set to \programopt{-remove}.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001469
1470The installation script runs embedded in the windows installer, every
Fred Drakec54d9252004-02-19 22:16:05 +00001471output (\code{sys.stdout}, \code{sys.stderr}) is redirected into a
1472buffer and will be displayed in the GUI after the script has finished.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001473
Fred Drakea9ee0da2004-02-19 22:28:15 +00001474Some functions especially useful in this context are available as
1475additional built-in functions in the installation script.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001476
Fred Drakea9ee0da2004-02-19 22:28:15 +00001477\begin{funcdesc}{directory_created}{path}
1478\funcline{file_created}{path}
1479 These functions should be called when a directory or file is created
1480 by the postinstall script at installation time. It will register
1481 \var{path} with the uninstaller, so that it will be removed when the
1482 distribution is uninstalled. To be safe, directories are only removed
1483 if they are empty.
1484\end{funcdesc}
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001485
Fred Drakea9ee0da2004-02-19 22:28:15 +00001486\begin{funcdesc}{get_special_folder_path}{csidl_string}
1487 This function can be used to retrieve special folder locations on
1488 Windows like the Start Menu or the Desktop. It returns the full
1489 path to the folder. \var{csidl_string} must be one of the following
1490 strings:
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001491
1492\begin{verbatim}
1493"CSIDL_APPDATA"
1494
1495"CSIDL_COMMON_STARTMENU"
1496"CSIDL_STARTMENU"
1497
1498"CSIDL_COMMON_DESKTOPDIRECTORY"
1499"CSIDL_DESKTOPDIRECTORY"
1500
1501"CSIDL_COMMON_STARTUP"
1502"CSIDL_STARTUP"
1503
1504"CSIDL_COMMON_PROGRAMS"
1505"CSIDL_PROGRAMS"
1506
1507"CSIDL_FONTS"
1508\end{verbatim}
1509
Fred Drakea9ee0da2004-02-19 22:28:15 +00001510 If the folder cannot be retrieved, \exception{OSError} is raised.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001511
Fred Drakea9ee0da2004-02-19 22:28:15 +00001512 Which folders are available depends on the exact Windows version,
1513 and probably also the configuration. For details refer to
1514 Microsoft's documentation of the
1515 \cfunction{SHGetSpecialFolderPath()} function.
1516\end{funcdesc}
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001517
Fred Drakea9ee0da2004-02-19 22:28:15 +00001518\begin{funcdesc}{create_shortcut}{target, description,
1519 filename\optional{,
1520 arguments\optional{,
1521 workdir\optional{,
1522 iconpath\optional{, iconindex}}}}}
1523 This function creates a shortcut.
1524 \var{target} is the path to the program to be started by the shortcut.
1525 \var{description} is the description of the sortcut.
1526 \var{filename} is the title of the shortcut that the user will see.
1527 \var{arguments} specifies the command line arguments, if any.
1528 \var{workdir} is the working directory for the program.
1529 \var{iconpath} is the file containing the icon for the shortcut,
1530 and \var{iconindex} is the index of the icon in the file
1531 \var{iconpath}. Again, for details consult the Microsoft
1532 documentation for the \class{IShellLink} interface.
1533\end{funcdesc}
Greg Wardb6528972000-09-07 02:40:37 +00001534
Fred Drake211a2eb2004-03-22 21:44:43 +00001535\chapter{Registering with the Package Index}
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +00001536\label{package-index}
1537
1538The Python Package Index (PyPI) holds meta-data describing distributions
1539packaged with distutils. The distutils command \command{register} is
1540used to submit your distribution's meta-data to the index. It is invoked
1541as follows:
1542
1543\begin{verbatim}
1544python setup.py register
1545\end{verbatim}
1546
1547Distutils will respond with the following prompt:
1548
1549\begin{verbatim}
1550running register
1551We need to know who you are, so please choose either:
1552 1. use your existing login,
1553 2. register as a new user,
1554 3. have the server generate a new password for you (and email it to you), or
1555 4. quit
1556Your selection [default 1]:
1557\end{verbatim}
1558
1559\noindent Note: if your username and password are saved locally, you will
1560not see this menu.
1561
1562If you have not registered with PyPI, then you will need to do so now. You
1563should choose option 2, and enter your details as required. Soon after
1564submitting your details, you will receive an email which will be used to
1565confirm your registration.
1566
1567Once you are registered, you may choose option 1 from the menu. You will
1568be prompted for your PyPI username and password, and \command{register}
1569will then submit your meta-data to the index.
1570
1571You may submit any number of versions of your distribution to the index. If
1572you alter the meta-data for a particular version, you may submit it again
1573and the index will be updated.
1574
1575PyPI holds a record for each (name, version) combination submitted. The
1576first user to submit information for a given name is designated the Owner
1577of that name. They may submit changes through the \command{register}
1578command or through the web interface. They may also designate other users
1579as Owners or Maintainers. Maintainers may edit the package information, but
1580not designate other Owners or Maintainers.
1581
1582By default PyPI will list all versions of a given package. To hide certain
1583versions, the Hidden property should be set to yes. This must be edited
1584through the web interface.
1585
1586
1587
Fred Drake211a2eb2004-03-22 21:44:43 +00001588\chapter{Examples}
Greg Ward007c04a2002-05-10 14:45:59 +00001589\label{examples}
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +00001590
Fred Drake211a2eb2004-03-22 21:44:43 +00001591\section{Pure Python distribution (by module)}
Greg Ward007c04a2002-05-10 14:45:59 +00001592\label{pure-mod}
1593
1594If you're just distributing a couple of modules, especially if they
1595don't live in a particular package, you can specify them individually
1596using the \option{py\_modules} option in the setup script.
1597
1598In the simplest case, you'll have two files to worry about: a setup
1599script and the single module you're distributing, \file{foo.py} in this
1600example:
1601\begin{verbatim}
1602<root>/
1603 setup.py
1604 foo.py
1605\end{verbatim}
1606(In all diagrams in this section, \verb|<root>| will refer to the
1607distribution root directory.) A minimal setup script to describe this
1608situation would be:
1609\begin{verbatim}
1610from distutils.core import setup
1611setup(name = "foo", version = "1.0",
1612 py_modules = ["foo"])
1613\end{verbatim}
1614Note that the name of the distribution is specified independently with
1615the \option{name} option, and there's no rule that says it has to be the
1616same as the name of the sole module in the distribution (although that's
1617probably a good convention to follow). However, the distribution name
1618is used to generate filenames, so you should stick to letters, digits,
1619underscores, and hyphens.
1620
1621Since \option{py\_modules} is a list, you can of course specify multiple
1622modules, eg. if you're distributing modules \module{foo} and
1623\module{bar}, your setup might look like this:
1624\begin{verbatim}
1625<root>/
1626 setup.py
1627 foo.py
1628 bar.py
1629\end{verbatim}
1630and the setup script might be
1631\begin{verbatim}
1632from distutils.core import setup
1633setup(name = "foobar", version = "1.0",
1634 py_modules = ["foo", "bar"])
1635\end{verbatim}
1636
1637You can put module source files into another directory, but if you have
1638enough modules to do that, it's probably easier to specify modules by
1639package rather than listing them individually.
Greg Ward16aafcd2000-04-09 04:06:44 +00001640
1641
Fred Drake211a2eb2004-03-22 21:44:43 +00001642\section{Pure Python distribution (by package)}
Greg Ward007c04a2002-05-10 14:45:59 +00001643\label{pure-pkg}
1644
1645If you have more than a couple of modules to distribute, especially if
1646they are in multiple packages, it's probably easier to specify whole
1647packages rather than individual modules. This works even if your
1648modules are not in a package; you can just tell the Distutils to process
1649modules from the root package, and that works the same as any other
1650package (except that you don't have to have an \file{\_\_init\_\_.py}
1651file).
1652
1653The setup script from the last example could also be written as
1654\begin{verbatim}
1655from distutils.core import setup
1656setup(name = "foobar", version = "1.0",
1657 packages = [""])
1658\end{verbatim}
1659(The empty string stands for the root package.)
1660
1661If those two files are moved into a subdirectory, but remain in the root
1662package, e.g.:
1663\begin{verbatim}
1664<root>/
1665 setup.py
1666 src/ foo.py
1667 bar.py
1668\end{verbatim}
1669then you would still specify the root package, but you have to tell the
1670Distutils where source files in the root package live:
1671\begin{verbatim}
1672from distutils.core import setup
1673setup(name = "foobar", version = "1.0",
1674 package_dir = {"": "src"},
1675 packages = [""])
1676\end{verbatim}
1677
1678More typically, though, you will want to distribute multiple modules in
1679the same package (or in sub-packages). For example, if the \module{foo}
1680and \module{bar} modules belong in package \module{foobar}, one way to
1681layout your source tree is
1682\begin{verbatim}
1683<root>/
1684 setup.py
1685 foobar/
1686 __init__.py
1687 foo.py
1688 bar.py
1689\end{verbatim}
1690This is in fact the default layout expected by the Distutils, and the
1691one that requires the least work to describe in your setup script:
1692\begin{verbatim}
1693from distutils.core import setup
1694setup(name = "foobar", version = "1.0",
1695 packages = ["foobar"])
1696\end{verbatim}
1697
1698If you want to put modules in directories not named for their package,
1699then you need to use the \option{package\_dir} option again. For
1700example, if the \file{src} directory holds modules in the
1701\module{foobar} package:
1702\begin{verbatim}
1703<root>/
1704 setup.py
1705 src/
1706 __init__.py
1707 foo.py
1708 bar.py
1709\end{verbatim}
1710an appropriate setup script would be
1711\begin{verbatim}
1712from distutils.core import setup
1713setup(name = "foobar", version = "1.0",
1714 package_dir = {"foobar" : "src"},
1715 packages = ["foobar"])
1716\end{verbatim}
1717
1718Or, you might put modules from your main package right in the
1719distribution root:
1720\begin{verbatim}
1721<root>/
1722 setup.py
1723 __init__.py
1724 foo.py
1725 bar.py
1726\end{verbatim}
1727in which case your setup script would be
1728\begin{verbatim}
1729from distutils.core import setup
1730setup(name = "foobar", version = "1.0",
1731 package_dir = {"foobar" : ""},
1732 packages = ["foobar"])
1733\end{verbatim}
1734(The empty string also stands for the current directory.)
1735
1736If you have sub-packages, they must be explicitly listed in
1737\option{packages}, but any entries in \option{package\_dir}
1738automatically extend to sub-packages. (In other words, the Distutils
1739does \emph{not} scan your source tree, trying to figure out which
1740directories correspond to Python packages by looking for
1741\file{\_\_init\_\_.py} files.) Thus, if the default layout grows a
1742sub-package:
1743\begin{verbatim}
1744<root>/
1745 setup.py
1746 foobar/
1747 __init__.py
1748 foo.py
1749 bar.py
1750 subfoo/
1751 __init__.py
1752 blah.py
1753\end{verbatim}
1754then the corresponding setup script would be
1755\begin{verbatim}
1756from distutils.core import setup
1757setup(name = "foobar", version = "1.0",
1758 packages = ["foobar", "foobar.subfoo"])
1759\end{verbatim}
1760(Again, the empty string in \option{package\_dir} stands for the current
1761directory.)
Greg Ward16aafcd2000-04-09 04:06:44 +00001762
1763
Fred Drake211a2eb2004-03-22 21:44:43 +00001764\section{Single extension module}
Greg Ward007c04a2002-05-10 14:45:59 +00001765\label{single-ext}
1766
1767Extension modules are specified using the \option{ext\_modules} option.
1768\option{package\_dir} has no effect on where extension source files are
1769found; it only affects the source for pure Python modules. The simplest
1770case, a single extension module in a single C source file, is:
1771\begin{verbatim}
1772<root>/
1773 setup.py
1774 foo.c
1775\end{verbatim}
1776If the \module{foo} extension belongs in the root package, the setup
1777script for this could be
1778\begin{verbatim}
1779from distutils.core import setup
1780setup(name = "foobar", version = "1.0",
1781 ext_modules = [Extension("foo", ["foo.c"])])
1782\end{verbatim}
1783
1784If the extension actually belongs in a package, say \module{foopkg},
1785then
1786
1787With exactly the same source tree layout, this extension can be put in
1788the \module{foopkg} package simply by changing the name of the
1789extension:
1790\begin{verbatim}
1791from distutils.core import setup
1792setup(name = "foobar", version = "1.0",
1793 ext_modules = [Extension("foopkg.foo", ["foo.c"])])
1794\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +00001795
1796
Fred Drake211a2eb2004-03-22 21:44:43 +00001797%\section{Multiple extension modules}
Fred Drakea09262e2001-03-01 18:35:43 +00001798%\label{multiple-ext}
Greg Ward16aafcd2000-04-09 04:06:44 +00001799
1800
Fred Drake211a2eb2004-03-22 21:44:43 +00001801%\section{Putting it all together}
Greg Ward16aafcd2000-04-09 04:06:44 +00001802
1803
Fred Drake211a2eb2004-03-22 21:44:43 +00001804%\chapter{Extending the Distutils}
Fred Drakea09262e2001-03-01 18:35:43 +00001805%\label{extending}
Greg Ward4a9e7222000-04-25 02:57:36 +00001806
1807
Fred Drake211a2eb2004-03-22 21:44:43 +00001808%\section{Extending existing commands}
Fred Drakea09262e2001-03-01 18:35:43 +00001809%\label{extend-existing}
Greg Ward4a9e7222000-04-25 02:57:36 +00001810
1811
Fred Drake211a2eb2004-03-22 21:44:43 +00001812%\section{Writing new commands}
Fred Drakea09262e2001-03-01 18:35:43 +00001813%\label{new-commands}
Greg Ward4a9e7222000-04-25 02:57:36 +00001814
Fred Drakea09262e2001-03-01 18:35:43 +00001815%\XXX{Would an uninstall command be a good example here?}
Thomas Heller5f52f722001-02-19 17:48:03 +00001816
Greg Ward4a9e7222000-04-25 02:57:36 +00001817
1818
Fred Drake211a2eb2004-03-22 21:44:43 +00001819\chapter{Command Reference}
Greg Ward47f99a62000-09-04 20:07:15 +00001820\label{reference}
Greg Ward16aafcd2000-04-09 04:06:44 +00001821
1822
Fred Drakea09262e2001-03-01 18:35:43 +00001823%\subsection{Building modules: the \protect\command{build} command family}
1824%\label{build-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001825
Fred Drakea09262e2001-03-01 18:35:43 +00001826%\subsubsection{\protect\command{build}}
1827%\label{build-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001828
Fred Drakea09262e2001-03-01 18:35:43 +00001829%\subsubsection{\protect\command{build\_py}}
1830%\label{build-py-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001831
Fred Drakea09262e2001-03-01 18:35:43 +00001832%\subsubsection{\protect\command{build\_ext}}
1833%\label{build-ext-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001834
Fred Drakea09262e2001-03-01 18:35:43 +00001835%\subsubsection{\protect\command{build\_clib}}
1836%\label{build-clib-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001837
1838
Fred Drake211a2eb2004-03-22 21:44:43 +00001839\section{Installing modules: the \protect\command{install} command family}
Greg Warde78298a2000-04-28 17:12:24 +00001840\label{install-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001841
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001842The install command ensures that the build commands have been run and then
1843runs the subcommands \command{install\_lib},
1844\command{install\_data} and
1845\command{install\_scripts}.
1846
Fred Drakea09262e2001-03-01 18:35:43 +00001847%\subsubsection{\protect\command{install\_lib}}
1848%\label{install-lib-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001849
Fred Drake211a2eb2004-03-22 21:44:43 +00001850\subsection{\protect\command{install\_data}}
Greg Ward1365a302000-08-31 14:47:05 +00001851\label{install-data-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001852This command installs all data files provided with the distribution.
1853
Fred Drake211a2eb2004-03-22 21:44:43 +00001854\subsection{\protect\command{install\_scripts}}
Greg Ward1365a302000-08-31 14:47:05 +00001855\label{install-scripts-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001856This command installs all (Python) scripts in the distribution.
1857
Greg Ward16aafcd2000-04-09 04:06:44 +00001858
Fred Drakea09262e2001-03-01 18:35:43 +00001859%\subsection{Cleaning up: the \protect\command{clean} command}
1860%\label{clean-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001861
1862
Fred Drake211a2eb2004-03-22 21:44:43 +00001863\section{Creating a source distribution: the
Fred Drakeeff9a872000-10-26 16:41:03 +00001864 \protect\command{sdist} command}
Greg Warde78298a2000-04-28 17:12:24 +00001865\label{sdist-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001866
1867
1868\XXX{fragment moved down from above: needs context!}
Greg Wardb6528972000-09-07 02:40:37 +00001869
Greg Ward16aafcd2000-04-09 04:06:44 +00001870The manifest template commands are:
Fred Drake781380c2004-02-19 23:17:46 +00001871
Greg Ward16aafcd2000-04-09 04:06:44 +00001872\begin{tableii}{ll}{command}{Command}{Description}
Greg Ward87da1ea2000-04-21 04:35:25 +00001873 \lineii{include \var{pat1} \var{pat2} ... }
1874 {include all files matching any of the listed patterns}
1875 \lineii{exclude \var{pat1} \var{pat2} ... }
1876 {exclude all files matching any of the listed patterns}
1877 \lineii{recursive-include \var{dir} \var{pat1} \var{pat2} ... }
1878 {include all files under \var{dir} matching any of the listed patterns}
1879 \lineii{recursive-exclude \var{dir} \var{pat1} \var{pat2} ...}
1880 {exclude all files under \var{dir} matching any of the listed patterns}
1881 \lineii{global-include \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001882 {include all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001883 any of the listed patterns}
1884 \lineii{global-exclude \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001885 {exclude all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001886 any of the listed patterns}
Greg Ward16aafcd2000-04-09 04:06:44 +00001887 \lineii{prune \var{dir}}{exclude all files under \var{dir}}
1888 \lineii{graft \var{dir}}{include all files under \var{dir}}
1889\end{tableii}
Fred Drake781380c2004-02-19 23:17:46 +00001890
Fred Drakeeff9a872000-10-26 16:41:03 +00001891The patterns here are \UNIX-style ``glob'' patterns: \code{*} matches any
Greg Ward16aafcd2000-04-09 04:06:44 +00001892sequence of regular filename characters, \code{?} matches any single
1893regular filename character, and \code{[\var{range}]} matches any of the
1894characters in \var{range} (e.g., \code{a-z}, \code{a-zA-Z},
Greg Wardfacb8db2000-04-09 04:32:40 +00001895\code{a-f0-9\_.}). The definition of ``regular filename character'' is
Fred Drakeeff9a872000-10-26 16:41:03 +00001896platform-specific: on \UNIX{} it is anything except slash; on Windows
Fred Drake781380c2004-02-19 23:17:46 +00001897anything except backslash or colon; on Mac OS anything except colon.
Greg Wardb6528972000-09-07 02:40:37 +00001898
Fred Drake781380c2004-02-19 23:17:46 +00001899\XXX{Windows and Mac OS support not there yet}
Greg Ward16aafcd2000-04-09 04:06:44 +00001900
1901
Fred Drake211a2eb2004-03-22 21:44:43 +00001902%\section{Creating a built distribution: the
Fred Drakea09262e2001-03-01 18:35:43 +00001903% \protect\command{bdist} command family}
1904%\label{bdist-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001905
1906
Fred Drake211a2eb2004-03-22 21:44:43 +00001907%\subsection{\protect\command{bdist}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001908
Fred Drake211a2eb2004-03-22 21:44:43 +00001909%\subsection{\protect\command{bdist\_dumb}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001910
Fred Drake211a2eb2004-03-22 21:44:43 +00001911%\subsection{\protect\command{bdist\_rpm}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001912
Fred Drake211a2eb2004-03-22 21:44:43 +00001913%\subsection{\protect\command{bdist\_wininst}}
Fred Drakeab70b382001-08-02 15:13:15 +00001914
1915
Fred Drake6fca7cc2004-03-23 18:43:03 +00001916\chapter{API Reference \label{api-reference}}
1917
1918\section{\module{distutils.core} --- Core Distutils functionality}
1919
1920\declaremodule{standard}{distutils.core}
1921\modulesynopsis{The core Distutils functionality}
1922
1923The \module{distutils.core} module is the only module that needs to be
1924installed to use the Distutils. It provides the \function{setup()} (which
1925is called from the setup script). Indirectly provides the
1926\class{distutils.dist.Distribution} and \class{distutils.cmd.Command} class.
1927
1928\begin{funcdesc}{setup}{arguments}
1929The basic do-everything function that does most everything you could ever
1930ask for from a Distutils method. See XXXXX
1931
1932The setup function takes a large number of arguments. These
1933are laid out in the following table.
1934
1935\begin{tableiii}{c|l|l}{argument name}{argument name}{value}{type}
1936\lineiii{name}{The name of the package}{a string}
1937\lineiii{version}{The version number of the package}{See \refmodule{distutils.version}}
1938\lineiii{description}{A single line describing the package}{a string}
1939\lineiii{long_description}{Longer description of the package}{a string}
1940\lineiii{author}{The name of the package author}{a string}
1941\lineiii{author_email}{The email address of the package author}{a string}
1942\lineiii{maintainer}{The name of the current maintainer, if different from the author}{a string}
1943\lineiii{maintainer_email}{The email address of the current maintainer, if different from the author}{}
1944\lineiii{url}{A URL for the package (homepage)}{a URL}
1945\lineiii{download_url}{A URL to download the package}{a URL}
1946\lineiii{packages}{A list of Python packages that distutils will manipulate}{a list of strings}
1947\lineiii{py_modules}{A list of Python modules that distutils will manipulate}{a list of strings}
1948\lineiii{scripts}{A list of standalone script files to be built and installed}{a list of strings}
1949\lineiii{ext_modules}{A list of Python extensions to be built}{A list of
1950instances of \class{distutils.core.Extension}}
1951\lineiii{classifiers}{A list of Trove categories for the package}{XXX link to better definition}
1952\lineiii{distclass}{the \class{Distribution} class to use}{A subclass of \class{distutils.core.Distribution}}
1953% What on earth is the use case for script_name?
1954\lineiii{script_name}{The name of the setup.py script - defaults to \code{sys.argv[0]}}{a string}
1955\lineiii{script_args}{Arguments to supply to the setup script}{a list of strings}
1956\lineiii{options}{default options for the setup script}{a string}
1957\lineiii{license}{The license for the package}{}
1958\lineiii{keywords}{Descriptive meta-data. See \pep{314}}{}
1959\lineiii{platforms}{}{}
1960\lineiii{cmdclass}{A mapping of command names to \class{Command} subclasses}{a dictionary}
1961\end{tableiii}
1962
1963\end{funcdesc}
1964
1965\begin{funcdesc}{run_setup}{script_name\optional{, script_args=\code{None}, stop_after=\code{'run'}}}
1966Run a setup script in a somewhat controlled environment, and return
1967the \class{distutils.dist.Distribution} instance that drives things.
1968This is useful if you need to find out the distribution meta-data
1969(passed as keyword args from \var{script} to \function{setup()}), or
1970the contents of the config files or command-line.
1971
1972\var{script_name} is a file that will be run with \function{execfile()}
1973\var{sys.argv[0]} will be replaced with \var{script} for the duration of the
1974call. \var{script_args} is a list of strings; if supplied,
1975\var{sys.argv[1:]} will be replaced by \var{script_args} for the duration
1976of the call.
1977
1978\var{stop_after} tells \function{setup()} when to stop processing; possible
1979values:
1980
1981\begin{tableii}{c|l}{value}{value}{description}
1982\lineii{init}{Stop after the \class{Distribution} instance has been created
1983and populated with the keyword arguments to \function{setup()}}
1984\lineii{config}{Stop after config files have been parsed (and their data
1985stored in the \class{Distribution} instance)}
1986\lineii{commandline}{Stop after the command-line (\code{sys.argv[1:]} or
1987\var{script_args}) have been parsed (and the data stored in the
1988\class{Distribution} instance.)}
1989\lineii{run}{Stop after all commands have been run (the same as
1990if \function{setup()} had been called in the usual way). This is the default
1991value.}
1992\end{tableii}
1993\end{funcdesc}
1994
1995In addition, the \module{distutils.core} module exposed a number of
1996classes that live elsewhere.
1997
1998\begin{itemize}
1999\item \class{Extension} from \refmodule{distutils.extension}
2000\item \class{Command} from \refmodule{distutils.cmd}
2001\item \class{Distribution} from \refmodule{distutils.dist}
2002\end{itemize}
2003
2004A short description of each of these follows, but see the relevant
2005module for the full reference.
2006
2007\begin{classdesc*}{Extension}
2008
2009The Extension class describes a single C or \Cpp extension module in a
2010setup script. It accepts the following keyword arguments in it's
2011constructor
2012
2013\begin{tableiii}{c|l|l}{argument name}{argument name}{value}{type}
2014\lineiii{name}{the full name of the extension, including any packages
2015--- ie. \emph{not} a filename or pathname, but Python dotted name}{string}
2016\lineiii{sources}{list of source filenames, relative to the distribution
2017root (where the setup script lives), in Unix form (slash-separated) for
2018portability. Source files may be C, \Cpp, SWIG (.i), platform-specific
2019resource files, or whatever else is recognized by the \command{build_ext}
2020command as source for a Python extension.}{string}
2021\lineiii{include_dirs}{list of directories to search for C/\Cpp{} header
2022files (in \UNIX{} form for portability)}{string}
2023\lineiii{define_macros}{list of macros to define; each macro is defined
2024using a 2-tuple, where 'value' is either the string to define it to or
2025\code{None} to define it without a particular value (equivalent of
2026\code{\#define FOO} in source or \programopt{-DFOO} on \UNIX{} C
2027compiler command line) }{ (string,string)
2028tuple or (name,\code{None}) }
2029\lineiii{undef_macros}{list of macros to undefine explicitly}{string}
2030\lineiii{library_dirs}{list of directories to search for C/\Cpp{} libraries
2031at link time }{string}
2032\lineiii{libraries}{list of library names (not filenames or paths) to
2033link against }{string}
2034\lineiii{runtime_library_dirs}{list of directories to search for C/\Cpp{}
2035libraries at run time (for shared extensions, this is when the extension
2036is loaded)}{string}
2037\lineiii{extra_objects}{list of extra files to link with (eg. object
2038files not implied by 'sources', static library that must be explicitly
2039specified, binary resource files, etc.)}{string}
2040\lineiii{extra_compile_args}{any extra platform- and compiler-specific
2041information to use when compiling the source files in 'sources'. For
2042platforms and compilers where a command line makes sense, this is
2043typically a list of command-line arguments, but for other platforms it
2044could be anything.}{string}
2045\lineiii{extra_link_args}{any extra platform- and compiler-specific
2046information to use when linking object files together to create the
2047extension (or to create a new static Python interpreter). Similar
2048interpretation as for 'extra_compile_args'.}{string}
2049\lineiii{export_symbols}{list of symbols to be exported from a shared
2050extension. Not used on all platforms, and not generally necessary for
2051Python extensions, which typically export exactly one symbol: \code{init} +
2052extension_name. }{string}
2053\lineiii{depends}{list of files that the extension depends on }{string}
2054\lineiii{language}{extension language (i.e. \code{'c'}, \code{'c++'},
2055\code{'objc'}). Will be detected from the source extensions if not provided.
2056}{string}
2057\end{tableiii}
2058\end{classdesc*}
2059
2060\begin{classdesc*}{Distribution}
2061A \class{Distribution} describes how to build, install and package up a
2062Python software package.
2063
2064See the \function{setup()} function for a list of keyword arguments accepted
2065by the Distribution constructor. \function{setup()} creates a Distribution
2066instance.
2067\end{classdesc*}
2068
2069\begin{classdesc*}{Command}
2070A \class{Command} class (or rather, an instance of one of it's subclasses)
2071implement a single distutils command.
2072\end{classdesc*}
2073
2074
2075\section{\module{distutils.ccompiler} --- CCompiler base class}
2076\declaremodule{standard}{distutils.ccompiler}
2077\modulesynopsis{Abstract CCompiler class}
2078
2079This module provides the abstract base class for the \class{CCompiler}
2080classes. A \class{CCompiler} instance can be used for all the compile
2081and link steps needed to build a single project. Methods are provided to
2082set options for the compiler --- macro definitions, include directories,
2083link path, libraries and the like.
2084
2085This module provides the following functions.
2086
2087\begin{funcdesc}{gen_lib_options}{compiler, library_dirs, runtime_library_dirs, libraries}
2088Generate linker options for searching library directories and
2089linking with specific libraries. \var{libraries} and \var{library_dirs} are,
2090respectively, lists of library names (not filenames!) and search
2091directories. Returns a list of command-line options suitable for use
2092with some compiler (depending on the two format strings passed in).
2093\end{funcdesc}
2094
2095\begin{funcdesc}{gen_preprocess_options}{macros, include_dirs}
2096Generate C pre-processor options (-D, -U, -I) as used by at least
2097two types of compilers: the typical \UNIX{} compiler and Visual \Cpp.
2098\var{macros} is the usual thing, a list of 1- or 2-tuples, where \var{(name,)}
2099means undefine (-U) macro \var{name}, and \var{(name,value)} means define (-D)
2100macro \var{name} to \var{value}. \var{include_dirs} is just a list of directory
2101names to be added to the header file search path (-I). Returns a list
2102of command-line options suitable for either \UNIX{} compilers or Visual
2103\Cpp.
2104\end{funcdesc}
2105
2106\begin{funcdesc}{get_default_compiler}{osname, platform}
2107Determine the default compiler to use for the given platform.
2108
2109\var{osname} should be one of the standard Python OS names (i.e. the
2110ones returned by \var{os.name}) and \var{platform} the common value
2111returned by \var{sys.platform} for the platform in question.
2112
2113The default values are \code{os.name} and \code{sys.platform} in case the
2114parameters are not given.
2115\end{funcdesc}
2116
2117\begin{funcdesc}{new_compiler}{plat=\code{None}, compiler=\code{None}, verbose=\code{0}, dry_run=\code{0}, force=\code{0}}
2118Factory function to generate an instance of some CCompiler subclass
2119for the supplied platform/compiler combination. \var{plat} defaults
2120to \code{os.name} (eg. \code{'posix'}, \code{'nt'}), and \var{compiler}
2121defaults to the default compiler for that platform. Currently only
2122\code{'posix'} and \code{'nt'} are supported, and the default
2123compilers are ``traditional \UNIX{} interface'' (\class{UnixCCompiler}
2124class) and Visual \Cpp (\class{MSVCCompiler} class). Note that it's
2125perfectly possible to ask for a \UNIX{} compiler object under Windows,
2126and a Microsoft compiler object under \UNIX---if you supply a value
2127for \var{compiler}, \var{plat} is ignored.
2128% Is the posix/nt only thing still true? Mac OS X seems to work, and
2129% returns a UnixCCompiler instance. How to document this... hmm.
2130\end{funcdesc}
2131
2132\begin{funcdesc}{show_compilers}{}
2133Print list of available compilers (used by the
2134\longprogramopt{help-compiler} options to \command{build},
2135\command{build_ext}, \command{build_clib}).
2136\end{funcdesc}
2137
2138\begin{classdesc}{CCompiler}{\optional{verbose=\code{0}, dry_run=\code{0}, force=\code{0}}}
2139
2140The abstract base class \class{CCompiler} defines the interface that
2141must be implemented by real compiler classes. The class also has
2142some utility methods used by several compiler classes.
2143
2144The basic idea behind a compiler abstraction class is that each
2145instance can be used for all the compile/link steps in building a
2146single project. Thus, attributes common to all of those compile and
2147link steps --- include directories, macros to define, libraries to link
2148against, etc. --- are attributes of the compiler instance. To allow for
2149variability in how individual files are treated, most of those
2150attributes may be varied on a per-compilation or per-link basis.
2151
2152The constructor for each subclass creates an instance of the Compiler
2153object. Flags are \var{verbose} (show verbose output), \var{dry_run}
2154(don't actually execute the steps) and \var{force} (rebuild
2155everything, regardless of dependencies). All of these flags default to
2156\code{0} (off). Note that you probably don't want to instantiate
2157\class{CCompiler} or one of it's subclasses directly - use the
2158\function{distutils.CCompiler.new_compiler()} factory function
2159instead.
2160
2161The following methods allow you to manually alter compiler options for
2162the instance of the Compiler class.
2163
2164\begin{methoddesc}{add_include_dir}{dir}
2165Add \var{dir} to the list of directories that will be searched for
2166header files. The compiler is instructed to search directories in
2167the order in which they are supplied by successive calls to
2168\method{add_include_dir()}.
2169\end{methoddesc}
2170
2171\begin{methoddesc}{set_include_dirs}{dirs}
2172Set the list of directories that will be searched to \var{dirs} (a
2173list of strings). Overrides any preceding calls to
2174\method{add_include_dir()}; subsequent calls to
2175\method{add_include_dir()} add to the list passed to
2176\method{set_include_dirs()}. This does not affect any list of
2177standard include directories that the compiler may search by default.
2178\end{methoddesc}
2179
2180\begin{methoddesc}{add_library}{libname}
2181
2182Add \var{libname} to the list of libraries that will be included in
2183all links driven by this compiler object. Note that \var{libname}
2184should *not* be the name of a file containing a library, but the
2185name of the library itself: the actual filename will be inferred by
2186the linker, the compiler, or the compiler class (depending on the
2187platform).
2188
2189The linker will be instructed to link against libraries in the
2190order they were supplied to \method{add_library()} and/or
2191\method{set_libraries()}. It is perfectly valid to duplicate library
2192names; the linker will be instructed to link against libraries as
2193many times as they are mentioned.
2194\end{methoddesc}
2195
2196\begin{methoddesc}{set_libraries}{libnames}
2197Set the list of libraries to be included in all links driven by
2198this compiler object to \var{libnames} (a list of strings). This does
2199not affect any standard system libraries that the linker may
2200include by default.
2201\end{methoddesc}
2202
2203\begin{methoddesc}{add_library_dir}{dir}
2204Add \var{dir} to the list of directories that will be searched for
2205libraries specified to \method{add_library()} and
2206\method{set_libraries()}. The linker will be instructed to search for
2207libraries in the order they are supplied to \method{add_library_dir()}
2208and/or \method{set_library_dirs()}.
2209\end{methoddesc}
2210
2211\begin{methoddesc}{set_library_dirs}{dirs}
2212Set the list of library search directories to \var{dirs} (a list of
2213strings). This does not affect any standard library search path
2214that the linker may search by default.
2215\end{methoddesc}
2216
2217\begin{methoddesc}{add_runtime_library_dir}{dir}
2218Add \var{dir} to the list of directories that will be searched for
2219shared libraries at runtime.
2220\end{methoddesc}
2221
2222\begin{methoddesc}{set_runtime_library_dirs}{dirs}
2223Set the list of directories to search for shared libraries at
2224runtime to \var{dirs} (a list of strings). This does not affect any
2225standard search path that the runtime linker may search by
2226default.
2227\end{methoddesc}
2228
2229\begin{methoddesc}{define_macro}{name\optional{, value=\code{None}}}
2230Define a preprocessor macro for all compilations driven by this
2231compiler object. The optional parameter \var{value} should be a
2232string; if it is not supplied, then the macro will be defined
2233without an explicit value and the exact outcome depends on the
2234compiler used (XXX true? does ANSI say anything about this?)
2235\end{methoddesc}
2236
2237\begin{methoddesc}{undefine_macro}{name}
2238Undefine a preprocessor macro for all compilations driven by
2239this compiler object. If the same macro is defined by
2240\method{define_macro()} and undefined by \method{undefine_macro()}
2241the last call takes precedence (including multiple redefinitions or
2242undefinitions). If the macro is redefined/undefined on a
2243per-compilation basis (ie. in the call to \method{compile()}), then that
2244takes precedence.
2245\end{methoddesc}
2246
2247\begin{methoddesc}{add_link_object}{object}
2248Add \var{object} to the list of object files (or analogues, such as
2249explicitly named library files or the output of ``resource
2250compilers'') to be included in every link driven by this compiler
2251object.
2252\end{methoddesc}
2253
2254\begin{methoddesc}{set_link_objects}{objects}
2255Set the list of object files (or analogues) to be included in
2256every link to \var{objects}. This does not affect any standard object
2257files that the linker may include by default (such as system
2258libraries).
2259\end{methoddesc}
2260
2261The following methods implement methods for autodetection of compiler
2262options, providing some functionality similar to GNU \program{autoconf}.
2263
2264\begin{methoddesc}{detect_language}{sources}
2265Detect the language of a given file, or list of files. Uses the
2266instance attributes \member{language_map} (a dictionary), and
2267\member{language_order} (a list) to do the job.
2268\end{methoddesc}
2269
2270\begin{methoddesc}{find_library_file}{dirs, lib\optional{, debug=\code{0}}}
2271Search the specified list of directories for a static or shared
2272library file \var{lib} and return the full path to that file. If
2273\var{debug} is true, look for a debugging version (if that makes sense on
2274the current platform). Return \code{None} if \var{lib} wasn't found in any of
2275the specified directories.
2276\end{methoddesc}
2277
2278\begin{methoddesc}{has_function}{funcname \optional{, includes=\code{None}, include_dirs=\code{None}, libraries=\code{None}, library_dirs=\code{None}}}
2279Return a boolean indicating whether \var{funcname} is supported on
2280the current platform. The optional arguments can be used to
2281augment the compilation environment by providing additional include
2282files and paths and libraries and paths.
2283\end{methoddesc}
2284
2285\begin{methoddesc}{library_dir_option}{dir}
2286Return the compiler option to add \var{dir} to the list of
2287directories searched for libraries.
2288\end{methoddesc}
2289
2290\begin{methoddesc}{library_option}{lib}
2291Return the compiler option to add \var{dir} to the list of libraries
2292linked into the shared library or executable.
2293\end{methoddesc}
2294
2295\begin{methoddesc}{runtime_library_dir_option}{dir}
2296Return the compiler option to add \var{dir} to the list of
2297directories searched for runtime libraries.
2298\end{methoddesc}
2299
2300\begin{methoddesc}{set_executables}{**args}
2301Define the executables (and options for them) that will be run
2302to perform the various stages of compilation. The exact set of
2303executables that may be specified here depends on the compiler
2304class (via the 'executables' class attribute), but most will have:
2305
2306\begin{tableii}{l|l}{attribute}{attribute}{description}
2307\lineii{compiler}{the C/\Cpp{} compiler}
2308\lineii{linker_so}{linker used to create shared objects and libraries}
2309\lineii{linker_exe}{linker used to create binary executables}
2310\lineii{archiver}{static library creator}
2311\end{tableii}
2312
2313On platforms with a command-line (\UNIX, DOS/Windows), each of these
2314is a string that will be split into executable name and (optional)
2315list of arguments. (Splitting the string is done similarly to how
2316\UNIX{} shells operate: words are delimited by spaces, but quotes and
2317backslashes can override this. See
2318\function{distutils.util.split_quoted()}.)
2319\end{methoddesc}
2320
2321The following methods invoke stages in the build process.
2322
2323\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}}}
2324Compile one or more source files. Generates object files (e.g.
2325transforms a \file{.c} file to a \file{.o} file.)
2326
2327\var{sources} must be a list of filenames, most likely C/\Cpp
2328files, but in reality anything that can be handled by a
2329particular compiler and compiler class (eg. \class{MSVCCompiler} can
2330handle resource files in \var{sources}). Return a list of object
2331filenames, one per source filename in \var{sources}. Depending on
2332the implementation, not all source files will necessarily be
2333compiled, but all corresponding object filenames will be
2334returned.
2335
2336If \var{output_dir} is given, object files will be put under it, while
2337retaining their original path component. That is, \file{foo/bar.c}
2338normally compiles to \file{foo/bar.o} (for a \UNIX{} implementation); if
2339\var{output_dir} is \var{build}, then it would compile to
2340\file{build/foo/bar.o}.
2341
2342\var{macros}, if given, must be a list of macro definitions. A macro
2343definition is either a \var{(name, value)} 2-tuple or a \var{(name,)} 1-tuple.
2344The former defines a macro; if the value is \code{None}, the macro is
2345defined without an explicit value. The 1-tuple case undefines a
2346macro. Later definitions/redefinitions/undefinitions take
2347precedence.
2348
2349\var{include_dirs}, if given, must be a list of strings, the
2350directories to add to the default include file search path for this
2351compilation only.
2352
2353\var{debug} is a boolean; if true, the compiler will be instructed to
2354output debug symbols in (or alongside) the object file(s).
2355
2356\var{extra_preargs} and \var{extra_postargs} are implementation- dependent.
2357On platforms that have the notion of a command-line (e.g. \UNIX,
2358DOS/Windows), they are most likely lists of strings: extra
2359command-line arguments to prepand/append to the compiler command
2360line. On other platforms, consult the implementation class
2361documentation. In any event, they are intended as an escape hatch
2362for those occasions when the abstract compiler framework doesn't
2363cut the mustard.
2364
2365\var{depends}, if given, is a list of filenames that all targets
2366depend on. If a source file is older than any file in
2367depends, then the source file will be recompiled. This
2368supports dependency tracking, but only at a coarse
2369granularity.
2370
2371Raises \exception{CompileError} on failure.
2372\end{methoddesc}
2373
2374\begin{methoddesc}{create_static_lib}{objects, output_libname\optional{, output_dir=\code{None}, debug=\code{0}, target_lang=\code{None}}}
2375Link a bunch of stuff together to create a static library file.
2376The ``bunch of stuff'' consists of the list of object files supplied
2377as \var{objects}, the extra object files supplied to
2378\method{add_link_object()} and/or \method{set_link_objects()}, the libraries
2379supplied to \method{add_library()} and/or \method{set_libraries()}, and the
2380libraries supplied as \var{libraries} (if any).
2381
2382\var{output_libname} should be a library name, not a filename; the
2383filename will be inferred from the library name. \var{output_dir} is
2384the directory where the library file will be put. XXX defaults to what?
2385
2386\var{debug} is a boolean; if true, debugging information will be
2387included in the library (note that on most platforms, it is the
2388compile step where this matters: the \var{debug} flag is included here
2389just for consistency).
2390
2391\var{target_lang} is the target language for which the given objects
2392are being compiled. This allows specific linkage time treatment of
2393certain languages.
2394
2395Raises \exception{LibError} on failure.
2396\end{methoddesc}
2397
2398\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}}}
2399Link a bunch of stuff together to create an executable or
2400shared library file.
2401
2402The ``bunch of stuff'' consists of the list of object files supplied
2403as \var{objects}. \var{output_filename} should be a filename. If
2404\var{output_dir} is supplied, \var{output_filename} is relative to it
2405(i.e. \var{output_filename} can provide directory components if
2406needed).
2407
2408\var{libraries} is a list of libraries to link against. These are
2409library names, not filenames, since they're translated into
2410filenames in a platform-specific way (eg. \var{foo} becomes \file{libfoo.a}
2411on \UNIX{} and \file{foo.lib} on DOS/Windows). However, they can include a
2412directory component, which means the linker will look in that
2413specific directory rather than searching all the normal locations.
2414
2415\var{library_dirs}, if supplied, should be a list of directories to
2416search for libraries that were specified as bare library names
2417(ie. no directory component). These are on top of the system
2418default and those supplied to \method{add_library_dir()} and/or
2419\method{set_library_dirs()}. \var{runtime_library_dirs} is a list of
2420directories that will be embedded into the shared library and used
2421to search for other shared libraries that *it* depends on at
2422run-time. (This may only be relevant on \UNIX.)
2423
2424\var{export_symbols} is a list of symbols that the shared library will
2425export. (This appears to be relevant only on Windows.)
2426
2427\var{debug} is as for \method{compile()} and \method{create_static_lib()},
2428with the slight distinction that it actually matters on most platforms (as
2429opposed to \method{create_static_lib()}, which includes a \var{debug} flag
2430mostly for form's sake).
2431
2432\var{extra_preargs} and \var{extra_postargs} are as for \method{compile()}
2433(except of course that they supply command-line arguments for the
2434particular linker being used).
2435
2436\var{target_lang} is the target language for which the given objects
2437are being compiled. This allows specific linkage time treatment of
2438certain languages.
2439
2440Raises \exception{LinkError} on failure.
2441\end{methoddesc}
2442
2443\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}}}
2444Link an executable.
2445\var{output_progname} is the name of the file executable,
2446while \var{objects} are a list of object filenames to link in. Other arguments
2447are as for the \method{link} method.
2448\end{methoddesc}
2449
2450\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}}}
2451Link a shared library. \var{output_libname} is the name of the output
2452library, while \var{objects} is a list of object filenames to link in.
2453Other arguments are as for the \method{link} method.
2454\end{methoddesc}
2455
2456\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}}}
2457Link a shared object. \var{output_filename} is the name of the shared object
2458that will be created, while \var{objects} is a list of object filenames
2459to link in. Other arguments are as for the \method{link} method.
2460\end{methoddesc}
2461
2462\begin{methoddesc}{preprocess}{source\optional{, output_file=\code{None}, macros=\code{None}, include_dirs=\code{None}, extra_preargs=\code{None}, extra_postargs=\code{None}}}
2463Preprocess a single C/\Cpp{} source file, named in \var{source}.
2464Output will be written to file named \var{output_file}, or \var{stdout} if
2465\var{output_file} not supplied. \var{macros} is a list of macro
2466definitions as for \method{compile()}, which will augment the macros set
2467with \method{define_macro()} and \method{undefine_macro()}.
2468\var{include_dirs} is a list of directory names that will be added to the
2469default list, in the same way as \method{add_include_dir()}.
2470
2471Raises \exception{PreprocessError} on failure.
2472\end{methoddesc}
2473
2474The following utility methods are defined by the \class{CCompiler} class,
2475for use by the various concrete subclasses.
2476
2477\begin{methoddesc}{executable_filename}{basename\optional{, strip_dir=\code{0}, output_dir=\code{''}}}
2478Returns the filename of the executable for the given \var{basename}.
2479Typically for non-Windows platforms this is the same as the basename,
2480while Windows will get a \file{.exe} added.
2481\end{methoddesc}
2482
2483\begin{methoddesc}{library_filename}{libname\optional{, lib_type=\code{'static'}, strip_dir=\code{0}, output_dir=\code{''}}}
2484Returns the filename for the given library name on the current platform.
2485On \UNIX{} a library with \var{lib_type} of \code{'static'} will typically
2486be of the form \file{liblibname.a}, while a \var{lib_type} of \code{'dynamic'}
2487will be of the form \file{liblibname.so}.
2488\end{methoddesc}
2489
2490\begin{methoddesc}{object_filenames}{source_filenames\optional{, strip_dir=\code{0}, output_dir=\code{''}}}
2491Returns the name of the object files for the given source files.
2492\var{source_filenames} should be a list of filenames.
2493\end{methoddesc}
2494
2495\begin{methoddesc}{shared_object_filename}{basename\optional{, strip_dir=\code{0}, output_dir=\code{''}}}
2496Returns the name of a shared object file for the given file name \var{basename}.
2497\end{methoddesc}
2498
2499\begin{methoddesc}{execute}{func, args\optional{, msg=\code{None}, level=\code{1}}}
2500Invokes \function{distutils.util.execute()} This method invokes a
2501Python function \var{func} with the given arguments \var{args}, after
2502logging and taking into account the \var{dry_run} flag. XXX see also.
2503\end{methoddesc}
2504
2505\begin{methoddesc}{spawn}{cmd}
2506Invokes \function{distutils.util.spawn()}. This invokes an external
2507process to run the given command. XXX see also.
2508\end{methoddesc}
2509
2510\begin{methoddesc}{mkpath}{name\optional{, mode=\code{511}}}
2511
2512Invokes \function{distutils.dir_util.mkpath()}. This creates a directory
2513and any missing ancestor directories. XXX see also.
2514\end{methoddesc}
2515
2516\begin{methoddesc}{move_file}{src, dst}
2517Invokes \method{distutils.file_util.move_file()}. Renames \var{src} to
2518\var{dst}. XXX see also.
2519\end{methoddesc}
2520
2521\begin{methoddesc}{announce}{msg\optional{, level=\code{1}}}
2522Write a message using \function{distutils.log.debug()}. XXX see also.
2523\end{methoddesc}
2524
2525\begin{methoddesc}{warn}{msg}
2526Write a warning message \var{msg} to standard error.
2527\end{methoddesc}
2528
2529\begin{methoddesc}{debug_print}{msg}
2530If the \var{debug} flag is set on this \class{CCompiler} instance, print
2531\var{msg} to standard output, otherwise do nothing.
2532\end{methoddesc}
2533
2534\end{classdesc}
2535
2536%\subsection{Compiler-specific modules}
2537%
2538%The following modules implement concrete subclasses of the abstract
2539%\class{CCompiler} class. They should not be instantiated directly, but should
2540%be created using \function{distutils.ccompiler.new_compiler()} factory
2541%function.
2542
2543\section{\module{distutils.unixccompiler} --- Unix C Compiler}
2544\declaremodule{standard}{distutils.unixccompiler}
2545\modulesynopsis{UNIX C Compiler}
2546
2547This module provides the \class{UnixCCompiler} class, a subclass of
2548\class{CCompiler} that handles the typical \UNIX-style command-line
2549C compiler:
2550
2551\begin{itemize}
2552\item macros defined with \programopt{-D\var{name}\optional{=value}}
2553\item macros undefined with \programopt{-U\var{name}}
2554\item include search directories specified with
2555 \programopt{-I\var{dir}}
2556\item libraries specified with \programopt{-l\var{lib}}
2557\item library search directories specified with \programopt{-L\var{dir}}
2558\item compile handled by \program{cc} (or similar) executable with
2559 \programopt{-c} option: compiles \file{.c} to \file{.o}
2560\item link static library handled by \program{ar} command (possibly
2561 with \program{ranlib})
2562\item link shared library handled by \program{cc} \programopt{-shared}
2563\end{itemize}
2564
2565\section{\module{distutils.msvccompiler} --- Microsoft Compiler}
2566\declaremodule{standard}{distutils.msvccompiler}
2567\modulesynopsis{Microsoft Compiler}
2568
2569This module provides \class{MSVCCompiler}, an implementation of the abstract
2570\class{CCompiler} class for Microsoft Visual Studio. It should also work using
2571the freely available compiler provided as part of the .Net SDK download. XXX
2572download link.
2573
2574\section{\module{distutils.bcppcompiler} --- Borland Compiler}
2575\declaremodule{standard}{distutils.bcppcompiler}
2576This module provides \class{BorlandCCompiler}, an subclass of the abstract \class{CCompiler} class for the Borland \Cpp{} compiler.
2577
2578\section{\module{distutils.cygwincompiler} --- Cygwin Compiler}
2579\declaremodule{standard}{distutils.cygwinccompiler}
2580
2581This module provides the \class{CygwinCCompiler} class, a subclass of \class{UnixCCompiler} that
2582handles the Cygwin port of the GNU C compiler to Windows. It also contains
2583the Mingw32CCompiler class which handles the mingw32 port of GCC (same as
2584cygwin in no-cygwin mode).
2585
2586\section{\module{distutils.emxccompiler} --- OS/2 EMX Compiler}
2587\declaremodule{standard}{distutils.emxccompiler}
2588\modulesynopsis{OS/2 EMX Compiler support}
2589
2590This module provides the EMXCCompiler class, a subclass of \class{UnixCCompiler} that handles the EMX port of the GNU C compiler to OS/2.
2591
2592\section{\module{distutils.mwerkscompiler} --- Metrowerks CodeWarrior support}
2593\declaremodule{standard}{distutils.mwerkscompiler}
2594\modulesynopsis{Metrowerks CodeWarrior support}
2595
2596Contains \class{MWerksCompiler}, an implementation of the abstract
2597\class{CCompiler} class for MetroWerks CodeWarrior on the Macintosh. Needs work to support CW on Windows.
2598
2599
2600%\subsection{Utility modules}
2601%
2602%The following modules all provide general utility functions. They haven't
2603%all been documented yet.
2604
2605\section{\module{distutils.archive_util} ---
2606 Archiving utilities}
2607\declaremodule[distutils.archiveutil]{standard}{distutils.archive_util}
2608\modulesynopsis{Utility functions for creating archive files (tarballs, zip files, ...)}
2609
2610This module provides a few functions for creating archive files, such as
2611tarballs or zipfiles.
2612
2613\begin{funcdesc}{make_archive}{base_name, format\optional{, root_dir=\code{None}, base_dir=\code{None}, verbose=\code{0}, dry_run=\code{0}}}
2614Create an archive file (eg. \code{zip} or \code{tar}). \var{base_name}
2615is the name of the file to create, minus any format-specific extension;
2616\var{format} is the archive format: one of \code{zip}, \code{tar},
2617\code{ztar}, or \code{gztar}.
2618\var{root_dir} is a directory that will be the root directory of the
2619archive; ie. we typically \code{chdir} into \var{root_dir} before
2620creating the archive. \var{base_dir} is the directory where we start
2621archiving from; ie. \var{base_dir} will be the common prefix of all files and
2622directories in the archive. \var{root_dir} and \var{base_dir} both default
2623to the current directory. Returns the name of the archive file.
2624
2625\warning{This should be changed to support bz2 files}
2626\end{funcdesc}
2627
2628\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),
2629\code{'compress'}, \code{'bzip2'}, or \code{None}. Both \code{'tar'}
2630and the compression utility named by \var{'compress'} must be on the
2631default program search path, so this is probably \UNIX-specific. The
2632output tar file will be named \file{\var{base_dir}.tar}, possibly plus
2633the appropriate compression extension (\file{.gz}, \file{.bz2} or
2634\file{.Z}). Return the output filename.
2635
2636\warning{This should be replaced with calls to the \module{tarfile} module.}
2637\end{funcdesc}
2638
2639\begin{funcdesc}{make_zipfile}{base_name, base_dir\optional{, verbose=\code{0}, dry_run=\code{0}}}
2640Create a zip file from all files in and under \var{base_dir}. The output
2641zip file will be named \var{base_dir} + \file{.zip}. Uses either the
2642\module{zipfile} Python module (if available) or the InfoZIP \file{zip}
2643utility (if installed and found on the default search path). If neither
2644tool is available, raises \exception{DistutilsExecError}.
2645Returns the name of the output zip file.
2646\end{funcdesc}
2647
2648\section{\module{distutils.dep_util} --- Dependency checking}
2649\declaremodule[distutils.deputil]{standard}{distutils.dep_util}
2650\modulesynopsis{Utility functions for simple dependency checking}
2651
2652This module provides functions for performing simple, timestamp-based
2653dependency of files and groups of files; also, functions based entirely
2654on such timestamp dependency analysis.
2655
2656\begin{funcdesc}{newer}{source, target}
2657Return true if \var{source} exists and is more recently modified than
2658\var{target}, or if \var{source} exists and \var{target} doesn't.
2659Return false if both exist and \var{target} is the same age or newer
2660than \var{source}.
2661Raise \exception{DistutilsFileError} if \var{source} does not exist.
2662\end{funcdesc}
2663
2664\begin{funcdesc}{newer_pairwise}{sources, targets}
2665Walk two filename lists in parallel, testing if each source is newer
2666than its corresponding target. Return a pair of lists (\var{sources},
2667\var{targets}) where source is newer than target, according to the semantics
2668of \function{newer()}
2669%% equivalent to a listcomp...
2670\end{funcdesc}
2671
2672\begin{funcdesc}{newer_group}{sources, target\optional{, missing=\code{'error'}}}
2673Return true if \var{target} is out-of-date with respect to any file
2674listed in \var{sources} In other words, if \var{target} exists and is newer
2675than every file in \var{sources}, return false; otherwise return true.
2676\var{missing} controls what we do when a source file is missing; the
2677default (\code{'error'}) is to blow up with an \exception{OSError} from
2678inside \function{os.stat()};
2679if it is \code{'ignore'}, we silently drop any missing source files; if it is
2680\code{'newer'}, any missing source files make us assume that \var{target} is
2681out-of-date (this is handy in ``dry-run'' mode: it'll make you pretend to
2682carry out commands that wouldn't work because inputs are missing, but
2683that doesn't matter because you're not actually going to run the
2684commands).
2685\end{funcdesc}
2686
2687\section{\module{distutils.dir_util} --- Directory tree operations}
2688\declaremodule[distutils.dirutil]{standard}{distutils.dir_util}
2689\modulesynopsis{Utility functions for operating on directories and directory trees}
2690
2691This module provides functions for operating on directories and trees
2692of directories.
2693
2694\begin{funcdesc}{mkpath}{name\optional{, mode=\code{0777}, verbose=\code{0}, dry_run=\code{0}}}
2695Create a directory and any missing ancestor directories. If the
2696directory already exists (or if \var{name} is the empty string, which
2697means the current directory, which of course exists), then do
2698nothing. Raise \exception{DistutilsFileError} if unable to create some
2699directory along the way (eg. some sub-path exists, but is a file
2700rather than a directory). If \var{verbose} is true, print a one-line
2701summary of each mkdir to stdout. Return the list of directories
2702actually created.
2703\end{funcdesc}
2704
2705\begin{funcdesc}{create_tree}{base_dir, files\optional{, mode=\code{0777}, verbose=\code{0}, dry_run=\code{0}}}
2706Create all the empty directories under \var{base_dir} needed to
2707put \var{files} there. \var{base_dir} is just the a name of a directory
2708which doesn't necessarily exist yet; \var{files} is a list of filenames
2709to be interpreted relative to \var{base_dir}. \var{base_dir} + the
2710directory portion of every file in \var{files} will be created if it
2711doesn't already exist. \var{mode}, \var{verbose} and \var{dry_run} flags
2712are as for \function{mkpath()}.
2713\end{funcdesc}
2714
2715\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}}}
2716Copy an entire directory tree \var{src} to a new location \var{dst}. Both
2717\var{src} and \var{dst} must be directory names. If \var{src} is not a
2718directory, raise \exception{DistutilsFileError}. If \var{dst} does
2719not exist, it is created with \var{mkpath()}. The end result of the
2720copy is that every file in \var{src} is copied to \var{dst}, and
2721directories under \var{src} are recursively copied to \var{dst}.
2722Return the list of files that were copied or might have been copied,
2723using their output name. The return value is unaffected by \var{update}
2724or \var{dry_run}: it is simply the list of all files under \var{src},
2725with the names changed to be under \var{dst}.
2726
2727\var{preserve_mode} and \var{preserve_times} are the same as for
2728\function{copy_file} in \refmodule[distutils.fileutil]{distutils.file_util};
2729note that they only apply to regular files, not to directories. If
2730\var{preserve_symlinks} is true, symlinks will be copied as symlinks
2731(on platforms that support them!); otherwise (the default), the
2732destination of the symlink will be copied. \var{update} and
2733\var{verbose} are the same as for
2734\function{copy_file()}.
2735\end{funcdesc}
2736
2737\begin{funcdesc}{remove_tree}{directory\optional{verbose=\code{0}, dry_run=\code{0}}}
2738Recursively remove \var{directory} and all files and directories underneath
2739it. Any errors are ignored (apart from being reported to \code{stdout} if
2740\var{verbose} is true).
2741\end{funcdesc}
2742
2743\XXX{Some of this could be replaced with the shutil module?}
2744
2745\section{\module{distutils.file_util} --- Single file operations}
2746\declaremodule[distutils.fileutil]{standard}{distutils.file_util}
2747\modulesynopsis{Utility functions for operating on single files}
2748
2749This module contains some utility functions for operating on individual files.
2750
2751\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}}}
2752Copy file \var{src} to \var{dst}. If \var{dst} is a directory, then
2753\var{src} is copied there with the same name; otherwise, it must be a
2754filename. (If the file exists, it will be ruthlessly clobbered.) If
2755\var{preserve_mode} is true (the default), the file's mode (type and
2756permission bits, or whatever is analogous on the current platform) is
2757copied. If \var{preserve_times} is true (the default), the last-modified
2758and last-access times are copied as well. If \var{update} is true,
2759\var{src} will only be copied if \var{dst} does not exist, or if
2760\var{dst} does exist but is older than \var{src}.
2761
2762\var{link} allows you to make hard links (using \function{os.link}) or
2763symbolic links (using \function{os.symlink}) instead of copying: set it
2764to \code{'hard'} or \code{'sym'}; if it is \code{None} (the default),
2765files are copied. Don't set \var{link} on systems that don't support
2766it: \function{copy_file()} doesn't check if hard or symbolic linking is
2767available.
2768
2769Under Mac OS 9, uses the native file copy function in \module{macostools};
2770on other systems, uses \var{_copy_file_contents()} to copy file contents.
2771
2772Return a tuple \samp{(dest_name, copied)}: \var{dest_name} is the actual
2773name of the output file, and \var{copied} is true if the file was copied
2774(or would have been copied, if \var{dry_run} true).
2775% XXX if the destination file already exists, we clobber it if
2776% copying, but blow up if linking. Hmmm. And I don't know what
2777% macostools.copyfile() does. Should definitely be consistent, and
2778% should probably blow up if destination exists and we would be
2779% changing it (ie. it's not already a hard/soft link to src OR
2780% (not update) and (src newer than dst)).
2781\end{funcdesc}
2782
2783\begin{funcdesc}{move_file}{src, dst\optional{verbose, dry_run}}
2784Move file \var{src} to \var{dst}. If \var{dst} is a directory, the file will
2785be moved into it with the same name; otherwise, \var{src} is just renamed
2786to \var{dst}. Returns the new full name of the file.
2787\warning{Handles cross-device moves on Unix using \function{copy_file()}.
2788What about other systems???}
2789\end{funcdesc}
2790
2791\begin{funcdesc}{write_file}{filename, contents}
2792Create a file called \var{filename} and write \var{contents} (a
2793sequence of strings without line terminators) to it.
2794\end{funcdesc}
2795
2796\section{\module{distutils.utils} --- Miscellaneous other utility functions}
2797\declaremodule{standard}{distutils.util}
2798\modulesynopsis{Miscellaneous other utility functions}
2799
2800This module contains other assorted bits and pieces that don't fit into
2801any other utility module.
2802
2803\begin{funcdesc}{get_platform}{}
2804Return a string that identifies the current platform. This is used
2805mainly to distinguish platform-specific build directories and
2806platform-specific built distributions. Typically includes the OS name
2807and version and the architecture (as supplied by 'os.uname()'),
2808although the exact information included depends on the OS; eg. for IRIX
2809the architecture isn't particularly important (IRIX only runs on SGI
2810hardware), but for Linux the kernel version isn't particularly
2811important.
2812
2813Examples of returned values:
2814\begin{itemize}
2815\item \code{linux-i586}
2816\item \code{linux-alpha}
2817\item \code{solaris-2.6-sun4u}
2818\item \code{irix-5.3}
2819\item \code{irix64-6.2}
2820\end{itemize}
2821
2822For non-\POSIX{} platforms, currently just returns \code{sys.platform}.
2823% XXX isn't this also provided by some other non-distutils module?
2824\end{funcdesc}
2825
2826\begin{funcdesc}{convert_path}{pathname}
2827Return 'pathname' as a name that will work on the native filesystem,
2828i.e. split it on '/' and put it back together again using the current
2829directory separator. Needed because filenames in the setup script are
2830always supplied in Unix style, and have to be converted to the local
2831convention before we can actually use them in the filesystem. Raises
2832\exception{ValueError} on non-\UNIX-ish systems if \var{pathname} either
2833starts or ends with a slash.
2834\end{funcdesc}
2835
2836\begin{funcdesc}{change_root}{new_root, pathname}
2837Return \var{pathname} with \var{new_root} prepended. If \var{pathname} is
2838relative, this is equivalent to \samp{os.path.join(new_root,pathname)}
2839Otherwise, it requires making \var{pathname} relative and then joining the
2840two, which is tricky on DOS/Windows and Mac OS.
2841\end{funcdesc}
2842
2843\begin{funcdesc}{check_environ}{}
2844Ensure that 'os.environ' has all the environment variables we
2845guarantee that users can use in config files, command-line options,
2846etc. Currently this includes:
2847\begin{itemize}
2848\item \envvar{HOME} - user's home directory (\UNIX{} only)
2849\item \envvar{PLAT} - description of the current platform, including
2850 hardware and OS (see \function{get_platform()})
2851\end{itemize}
2852\end{funcdesc}
2853
2854\begin{funcdesc}{subst_vars}{s, local_vars}
2855Perform shell/Perl-style variable substitution on \var{s}. Every
2856occurrence of \code{\$} followed by a name is considered a variable, and
2857variable is substituted by the value found in the \var{local_vars}
2858dictionary, or in \code{os.environ} if it's not in \var{local_vars}.
2859\var{os.environ} is first checked/augmented to guarantee that it contains
2860certain values: see \function{check_environ()}. Raise \exception{ValueError}
2861for any variables not found in either \var{local_vars} or \code{os.environ}.
2862
2863Note that this is not a fully-fledged string interpolation function. A
2864valid \code{\$variable} can consist only of upper and lower case letters,
2865numbers and an underscore. No \{ \} or \( \) style quoting is available.
2866\end{funcdesc}
2867
2868\begin{funcdesc}{grok_environment_error}{exc\optional{, prefix=\samp{'error: '}}}
2869Generate a useful error message from an \exception{EnvironmentError}
2870(\exception{IOError} or \exception{OSError}) exception object.
2871Handles Python 1.5.1 and later styles, and does what it can to deal with
2872exception objects that don't have a filename (which happens when the error
2873is due to a two-file operation, such as \function{rename()} or
2874\function{link()}). Returns the error message as a string prefixed
2875with \var{prefix}.
2876\end{funcdesc}
2877
2878\begin{funcdesc}{split_quoted}{s}
2879Split a string up according to Unix shell-like rules for quotes and
2880backslashes. In short: words are delimited by spaces, as long as those
2881spaces are not escaped by a backslash, or inside a quoted string.
2882Single and double quotes are equivalent, and the quote characters can
2883be backslash-escaped. The backslash is stripped from any two-character
2884escape sequence, leaving only the escaped character. The quote
2885characters are stripped from any quoted string. Returns a list of
2886words.
2887% Should probably be moved into the standard library.
2888\end{funcdesc}
2889
2890\begin{funcdesc}{execute}{func, args\optional{, msg=\code{None}, verbose=\code{0}, dry_run=\code{0}}}
2891Perform some action that affects the outside world (for instance,
2892writing to the filesystem). Such actions are special because they
2893are disabled by the \var{dry_run} flag. This method takes
2894care of all that bureaucracy for you; all you have to do is supply the
2895function to call and an argument tuple for it (to embody the
2896``external action'' being performed), and an optional message to
2897print.
2898\end{funcdesc}
2899
2900\begin{funcdesc}{strtobool}{val}
2901Convert a string representation of truth to true (1) or false (0).
2902
2903True values are \code{y}, \code{yes}, \code{t}, \code{true}, \code{on}
2904and \code{1}; false values are \code{n}, \code{no}, \code{f}, \code{false},
2905\code{off} and \code{0}. Raises \exception{ValueError} if \var{val}
2906is anything else.
2907\end{funcdesc}
2908
2909\begin{funcdesc}{byte_compile}{py_files\optional{,
2910 optimize=\code{0}, force=\code{0},
2911 prefix=\code{None}, base_dir=\code{None},
2912 verbose=\code{1}, dry_run=\code{0},
2913 direct=\code{None}}}
2914Byte-compile a collection of Python source files to either \file{.pyc}
2915or \file{.pyo} files in the same directory. \var{py_files} is a list of files
2916to compile; any files that don't end in \file{.py} are silently skipped.
2917\var{optimize} must be one of the following:
2918\begin{itemize}
2919\item \code{0} - don't optimize (generate \file{.pyc})
2920\item \code{1} - normal optimization (like \samp{python -O})
2921\item \code{2} - extra optimization (like \samp{python -OO})
2922\end{itemize}
2923
2924If \var{force} is true, all files are recompiled regardless of
2925timestamps.
2926
2927The source filename encoded in each bytecode file defaults to the
2928filenames listed in \var{py_files}; you can modify these with \var{prefix} and
2929\var{basedir}. \var{prefix} is a string that will be stripped off of each
2930source filename, and \var{base_dir} is a directory name that will be
2931prepended (after \var{prefix} is stripped). You can supply either or both
2932(or neither) of \var{prefix} and \var{base_dir}, as you wish.
2933
2934If \var{dry_run} is true, doesn't actually do anything that would
2935affect the filesystem.
2936
2937Byte-compilation is either done directly in this interpreter process
2938with the standard \module{py_compile} module, or indirectly by writing a
2939temporary script and executing it. Normally, you should let
2940\function{byte_compile()} figure out to use direct compilation or not (see
2941the source for details). The \var{direct} flag is used by the script
2942generated in indirect mode; unless you know what you're doing, leave
2943it set to \code{None}.
2944\end{funcdesc}
2945
2946\begin{funcdesc}{rfc822_escape}{header}
2947Return a version of \var{header} escaped for inclusion in an
2948\rfc{822} header, by ensuring there are 8 spaces space after each newline.
2949Note that it does no other modification of the string.
2950% this _can_ be replaced
2951\end{funcdesc}
2952
2953%\subsection{Distutils objects}
2954
2955\section{\module{distutils.dist} --- The Distribution class}
2956\declaremodule{standard}{distutils.dist}
2957\modulesynopsis{Provides the Distribution class, which represents the
2958 module distribution being built/installed/distributed}
2959
2960This module provides the \class{Distribution} class, which represents
2961the module distribution being built/installed/distributed.
2962
2963
2964\section{\module{distutils.extension} --- The Extension class}
2965\declaremodule{standard}{distutils.extension}
2966\modulesynopsis{Provides the Extension class, used to describe
2967 C/\Cpp{} extension modules in setup scripts}
2968
2969This module provides the \class{Extension} class, used to describe
2970C/\Cpp{} extension modules in setup scripts.
2971
2972%\subsection{Ungrouped modules}
2973%The following haven't been moved into a more appropriate section yet.
2974
2975\section{\module{distutils.debug} --- Distutils debug mode}
2976\declaremodule{standard}{distutils.debug}
2977\modulesynopsis{Provides the debug flag for distutils}
2978
2979This module provides the DEBUG flag.
2980
2981\section{\module{distutils.errors} --- Distutils exceptions}
2982\declaremodule{standard}{distutils.errors}
2983\modulesynopsis{Provides standard distutils exceptions}
2984
2985Provides exceptions used by the Distutils modules. Note that Distutils
2986modules may raise standard exceptions; in particular, SystemExit is
2987usually raised for errors that are obviously the end-user's fault
2988(eg. bad command-line arguments).
2989
2990This module is safe to use in \samp{from ... import *} mode; it only exports
2991symbols whose names start with \code{Distutils} and end with \code{Error}.
2992
2993\section{\module{distutils.fancy_getopt}
2994 --- Wrapper around the standard getopt module}
2995\declaremodule[distutils.fancygetopt]{standard}{distutils.fancy_getopt}
2996\modulesynopsis{Additional \module{getopt} functionality}
2997
2998This module provides a wrapper around the standard \module{getopt}
2999module that provides the following additional features:
3000
3001\begin{itemize}
3002\item short and long options are tied together
3003\item options have help strings, so \function{fancy_getopt} could potentially
3004create a complete usage summary
3005\item options set attributes of a passed-in object
3006\item boolean options can have ``negative aliases'' --- eg. if
3007\longprogramopt{quiet} is the ``negative alias'' of
3008\longprogramopt{verbose}, then \longprogramopt{quiet} on the command
3009line sets \var{verbose} to false.
3010
3011\end{itemize}
3012
3013\XXX{Should be replaced with \module{optik} (which is also now
3014known as \module{optparse} in Python 2.3 and later).}
3015
3016\begin{funcdesc}{fancy_getopt}{options, negative_opt, object, args}
3017Wrapper function. \var{options} is a list of
3018\samp{(long_option, short_option, help_string)} 3-tuples as described in the
3019constructor for \class{FancyGetopt}. \var{negative_opt} should be a dictionary
3020mapping option names to option names, both the key and value should be in the
3021\var{options} list. \var{object} is an object which will be used to store
3022values (see the \method{getopt()} method of the \class{FancyGetopt} class).
3023\var{args} is the argument list. Will use \code{sys.argv[1:]} if you
3024pass \code{None} as \var{args}.
3025\end{funcdesc}
3026
3027\begin{funcdesc}{wrap_text}{text, width}
3028Wraps \var{text} to less than \var{width} wide.
3029
3030\warning{Should be replaced with \module{textwrap} (which is available
3031in Python 2.3 and later).}
3032\end{funcdesc}
3033
3034\begin{classdesc}{FancyGetopt}{\optional{option_table=\code{None}}}
3035The option_table is a list of 3-tuples: \samp{(long_option,
3036short_option, help_string)}
3037
3038If an option takes an argument, it's \var{long_option} should have \code{'='}
3039appended; \var{short_option} should just be a single character, no \code{':'}
3040in any case. \var{short_option} should be \code{None} if a \var{long_option}
3041doesn't have a corresponding \var{short_option}. All option tuples must have
3042long options.
3043\end{classdesc}
3044
3045The \class{FancyGetopt} class provides the following methods:
3046
3047\begin{methoddesc}{getopt}{\optional{args=\code{None}, object=\code{None}}}
3048Parse command-line options in args. Store as attributes on \var{object}.
3049
3050If \var{args} is \code{None} or not supplied, uses \code{sys.argv[1:]}. If
3051\var{object} is \code{None} or not supplied, creates a new \class{OptionDummy}
3052instance, stores option values there, and returns a tuple \samp{(args,
3053object)}. If \var{object} is supplied, it is modified in place and
3054\function{getopt()} just returns \var{args}; in both cases, the returned
3055\var{args} is a modified copy of the passed-in \var{args} list, which
3056is left untouched.
3057% and args returned are?
3058\end{methoddesc}
3059
3060\begin{methoddesc}{get_option_order}{}
3061Returns the list of \samp{(option, value)} tuples processed by the
3062previous run of \method{getopt()} Raises \exception{RuntimeError} if
3063\method{getopt()} hasn't been called yet.
3064\end{methoddesc}
3065
3066\begin{methoddesc}{generate_help}{\optional{header=\code{None}}}
3067Generate help text (a list of strings, one per suggested line of
3068output) from the option table for this \class{FancyGetopt} object.
3069
3070If supplied, prints the supplied \var{header} at the top of the help.
3071\end{methoddesc}
3072
3073\section{\module{distutils.filelist} --- The FileList class}
3074\declaremodule{standard}{distutils.filelist}
3075\modulesynopsis{The \class{FileList} class, used for poking about the
3076 file system and building lists of files.}
3077
3078This module provides the \class{FileList} class, used for poking about
3079the filesystem and building lists of files.
3080
3081
3082\section{\module{distutils.log} --- Simple PEP 282-style logging}
3083\declaremodule{standard}{distutils.log}
3084\modulesynopsis{A simple logging mechanism, \pep{282}-style}
3085
3086\warning{Should be replaced with standard \module{logging} module.}
3087
3088%\subsubsection{\module{} --- }
3089%\declaremodule{standard}{distutils.magic}
3090%\modulesynopsis{ }
3091
3092
3093\section{\module{distutils.spawn} --- Spawn a sub-process}
3094\declaremodule{standard}{distutils.spawn}
3095\modulesynopsis{Provides the spawn() function}
3096
3097This module provides the \function{spawn()} function, a front-end to
3098various platform-specific functions for launching another program in a
3099sub-process.
3100Also provides \function{find_executable()} to search the path for a given
3101executable name.
3102
3103
Fred Drakeab70b382001-08-02 15:13:15 +00003104\input{sysconfig}
Greg Ward16aafcd2000-04-09 04:06:44 +00003105
3106
Fred Drake6fca7cc2004-03-23 18:43:03 +00003107\section{\module{distutils.text_file} --- The TextFile class}
3108\declaremodule[distutils.textfile]{standard}{distutils.text_file}
3109\modulesynopsis{provides the TextFile class, a simple interface to text files}
3110
3111This module provides the \class{TextFile} class, which gives an interface
3112to text files that (optionally) takes care of stripping comments, ignoring
3113blank lines, and joining lines with backslashes.
3114
3115\begin{classdesc}{TextFile}{\optional{filename=\code{None}, file=\code{None}, **options}}
3116This class provides a file-like object that takes care of all
3117the things you commonly want to do when processing a text file
3118that has some line-by-line syntax: strip comments (as long as \code{\#}
3119is your comment character), skip blank lines, join adjacent lines by
3120escaping the newline (ie. backslash at end of line), strip
3121leading and/or trailing whitespace. All of these are optional
3122and independently controllable.
3123
3124The class provides a \method{warn()} method so you can generate
3125warning messages that report physical line number, even if the
3126logical line in question spans multiple physical lines. Also
3127provides \method{unreadline()} for implementing line-at-a-time lookahead.
3128
3129\class{TextFile} instances are create with either \var{filename}, \var{file},
3130or both. \exception{RuntimeError} is raised if both are \code{None}.
3131\var{filename} should be a string, and \var{file} a file object (or
3132something that provides \method{readline()} and \method{close()}
3133methods). It is recommended that you supply at least \var{filename},
3134so that \class{TextFile} can include it in warning messages. If
3135\var{file} is not supplied, TextFile creates its own using the
3136\var{open()} builtin.
3137
3138The options are all boolean, and affect the values returned by
3139\var{readline()}
3140
3141\begin{tableiii}{c|l|l}{option name}{option name}{description}{default}
3142\lineiii{strip_comments}{
3143strip from \character{\#} to end-of-line, as well as any whitespace
3144leading up to the \character{\#}---unless it is escaped by a backslash}
3145{true}
3146\lineiii{lstrip_ws}{
3147strip leading whitespace from each line before returning it}
3148{false}
3149\lineiii{rstrip_ws}{
3150strip trailing whitespace (including line terminator!) from
3151each line before returning it.}
3152{true}
3153\lineiii{skip_blanks}{
3154skip lines that are empty *after* stripping comments and
3155whitespace. (If both lstrip_ws and rstrip_ws are false,
3156then some lines may consist of solely whitespace: these will
3157*not* be skipped, even if \var{skip_blanks} is true.)}
3158{true}
3159\lineiii{join_lines}{
3160if a backslash is the last non-newline character on a line
3161after stripping comments and whitespace, join the following line
3162to it to form one logical line; if N consecutive lines end
3163with a backslash, then N+1 physical lines will be joined to
3164form one logical line.}
3165{false}
3166\lineiii{collapse_join}{
3167strip leading whitespace from lines that are joined to their
3168predecessor; only matters if \samp{(join_lines and not lstrip_ws)}}
3169{false}
3170\end{tableiii}
3171
3172Note that since \var{rstrip_ws} can strip the trailing newline, the
3173semantics of \method{readline()} must differ from those of the builtin file
3174object's \method{readline()} method! In particular, \method{readline()}
3175returns \code{None} for end-of-file: an empty string might just be a
3176blank line (or an all-whitespace line), if \var{rstrip_ws} is true
3177but \var{skip_blanks} is not.
3178
3179\begin{methoddesc}{open}{filename}
3180Open a new file \var{filename}. This overrides any \var{file} or
3181\var{filename} constructor arguments.
3182\end{methoddesc}
3183
3184\begin{methoddesc}{close}{}
3185Close the current file and forget everything we know about it (including
3186the filename and the current line number).
3187\end{methoddesc}
3188
3189\begin{methoddesc}{warn}{msg\optional{,line=\code{None}}}
3190Print (to stderr) a warning message tied to the current logical
3191line in the current file. If the current logical line in the
3192file spans multiple physical lines, the warning refers to the
3193whole range, such as \samp{"lines 3-5"}. If \var{line} is supplied,
3194it overrides the current line number; it may be a list or tuple
3195to indicate a range of physical lines, or an integer for a
3196single physical line.
3197\end{methoddesc}
3198
3199\begin{methoddesc}{readline}{}
3200Read and return a single logical line from the current file (or
3201from an internal buffer if lines have previously been ``unread''
3202with \method{unreadline()}). If the \var{join_lines} option
3203is true, this may involve reading multiple physical lines
3204concatenated into a single string. Updates the current line number,
3205so calling \method{warn()} after \method{readline()} emits a warning
3206about the physical line(s) just read. Returns \code{None} on end-of-file,
3207since the empty string can occur if \var{rstrip_ws} is true but
3208\var{strip_blanks} is not.
3209\end{methoddesc}
3210\begin{methoddesc}{readlines}{}
3211Read and return the list of all logical lines remaining in the current file.
3212This updates the current line number to the last line of the file.
3213\end{methoddesc}
3214\begin{methoddesc}{unreadline}{line}
3215Push \var{line} (a string) onto an internal buffer that will be
3216checked by future \method{readline()} calls. Handy for implementing
3217a parser with line-at-a-time lookahead. Note that lines that are ``unread''
3218with \method{unreadline} are not subsequently re-cleansed (whitespace
3219stripped, or whatever) when read with \method{readline}. If multiple
3220calls are made to \method{unreadline} before a call to \method{readline},
3221the lines will be returned most in most recent first order.
3222\end{methoddesc}
3223
3224\end{classdesc}
3225
3226
3227\section{\module{distutils.version} --- Version number classes}
3228\declaremodule{standard}{distutils.version}
3229\modulesynopsis{implements classes that represent module version numbers. }
3230
3231% todo
3232
3233%\section{Distutils Commands}
3234%
3235%This part of Distutils implements the various Distutils commands, such
3236%as \code{build}, \code{install} \&c. Each command is implemented as a
3237%separate module, with the command name as the name of the module.
3238
3239\section{\module{distutils.cmd} --- Abstract base class for Distutils commands}
3240\declaremodule{standard}{distutils.cmd}
3241\modulesynopsis{This module provides the abstract base class Command. This
3242class is subclassed by the modules in the \refmodule{distutils.command}
3243subpackage. }
3244
3245This module supplies the abstract base class \class{Command}.
3246
3247\begin{classdesc}{Command}{dist}
3248Abstract base class for defining command classes, the ``worker bees''
3249of the Distutils. A useful analogy for command classes is to think of
3250them as subroutines with local variables called \var{options}. The
3251options are declared in \method{initialize_options()} and defined
3252(given their final values) in \method{finalize_options()}, both of
3253which must be defined by every command class. The distinction between
3254the two is necessary because option values might come from the outside
3255world (command line, config file, ...), and any options dependent on
3256other options must be computed after these outside influences have
3257been processed --- hence \method{finalize_options()}. The body of the
3258subroutine, where it does all its work based on the values of its
3259options, is the \method{run()} method, which must also be implemented
3260by every command class.
3261
3262The class constructor takes a single argument \var{dist}, a
3263\class{Distribution} instance.
3264\end{classdesc}
3265
3266
3267\section{\module{distutils.command} --- Individual Distutils commands}
3268\declaremodule{standard}{distutils.command}
3269\modulesynopsis{This subpackage contains one module for each standard Distutils command.}
3270
3271%\subsubsection{Individual Distutils commands}
3272
3273% todo
3274
3275\section{\module{distutils.command.bdist} --- Build a binary installer}
3276\declaremodule{standard}{distutils.command.bdist}
3277\modulesynopsis{Build a binary installer for a package}
3278
3279% todo
3280
3281\section{\module{distutils.command.bdist_packager} --- Abstract base class for packagers}
3282\declaremodule[distutils.command.bdistpackager]{standard}{distutils.command.bdist_packager}
3283\modulesynopsis{Abstract base class for packagers}
3284
3285% todo
3286
3287\section{\module{distutils.command.bdist_dumb} --- Build a ``dumb'' installer}
3288\declaremodule[distutils.command.bdistdumb]{standard}{distutils.command.bdist_dumb}
3289\modulesynopsis{Build a ``dumb'' installer - a simple archive of files}
3290
3291% todo
3292
3293
3294\section{\module{distutils.command.bdist_rpm} --- Build a binary distribution as a Redhat RPM and SRPM}
3295\declaremodule[distutils.command.bdistrpm]{standard}{distutils.command.bdist_rpm}
3296\modulesynopsis{Build a binary distribution as a Redhat RPM and SRPM}
3297
3298% todo
3299
3300\section{\module{distutils.command.bdist_wininst} --- Build a Windows installer}
3301\declaremodule[distutils.command.bdistwininst]{standard}{distutils.command.bdist_wininst}
3302\modulesynopsis{Build a Windows installer}
3303
3304% todo
3305
3306\section{\module{distutils.command.sdist} --- Build a source distribution}
3307\declaremodule{standard}{distutils.command.sdist}
3308\modulesynopsis{Build a source distribution}
3309
3310% todo
3311
3312\section{\module{distutils.command.build} --- Build all files of a package}
3313\declaremodule{standard}{distutils.command.build}
3314\modulesynopsis{Build all files of a package}
3315
3316% todo
3317
3318\section{\module{distutils.command.build_clib} --- Build any C libraries in a package}
3319\declaremodule[distutils.command.buildclib]{standard}{distutils.command.build_clib}
3320\modulesynopsis{Build any C libraries in a package}
3321
3322% todo
3323
3324\section{\module{distutils.command.build_ext} --- Build any extensions in a package}
3325\declaremodule[distutils.command.buildext]{standard}{distutils.command.build_ext}
3326\modulesynopsis{Build any extensions in a package}
3327
3328% todo
3329
3330\section{\module{distutils.command.build_py} --- Build the .py/.pyc files of a package}
3331\declaremodule[distutils.command.buildpy]{standard}{distutils.command.build_py}
3332\modulesynopsis{Build the .py/.pyc files of a package}
3333
3334% todo
3335
3336\section{\module{distutils.command.build_scripts} --- Build the scripts of a package}
3337\declaremodule[distutils.command.buildscripts]{standard}{distutils.command.build_scripts}
3338\modulesynopsis{Build the scripts of a package}
3339
3340% todo
3341
3342\section{\module{distutils.command.clean} --- Clean a package build area}
3343\declaremodule{standard}{distutils.command.clean}
3344\modulesynopsis{Clean a package build area}
3345
3346% todo
3347
3348\section{\module{distutils.command.config} --- Perform package configuration}
3349\declaremodule{standard}{distutils.command.config}
3350\modulesynopsis{Perform package configuration}
3351
3352% todo
3353
3354\subsubsection{\module{distutils.command.install} --- Install a package}
3355\declaremodule{standard}{distutils.command.install}
3356\modulesynopsis{Install a package}
3357
3358% todo
3359
3360\subsubsection{\module{distutils.command.install_data}
3361 --- Install data files from a package}
3362\declaremodule[distutils.command.installdata]{standard}{distutils.command.install_data}
3363\modulesynopsis{Install data files from a package}
3364
3365% todo
3366
3367\subsubsection{\module{distutils.command.install_headers}
3368 --- Install C/\Cpp{} header files from a package}
3369\declaremodule[distutils.command.installheaders]{standard}{distutils.command.install_headers}
3370\modulesynopsis{Install C/\Cpp{} header files from a package}
3371
3372% todo
3373
3374\subsubsection{\module{distutils.command.install_lib}
3375 --- Install library files from a package}
3376\declaremodule[distutils.command.installlib]{standard}{distutils.command.install_lib}
3377\modulesynopsis{Install library files from a package}
3378
3379% todo
3380
3381\subsubsection{\module{distutils.command.install_scripts}
3382 --- Install script files from a package}
3383\declaremodule[distutils.command.installscripts]{standard}{distutils.command.install_scripts}
3384\modulesynopsis{Install script files from a package}
3385
3386% todo
3387
3388\subsubsection{\module{distutils.command.register}
3389 --- Register a module with the Python Package Index}
3390\declaremodule{standard}{distutils.command.register}
3391\modulesynopsis{Register a module with the Python Package Index}
3392
3393The \code{register} command registers the package with the Python Package
3394Index. This is described in more detail in \pep{301}.
3395% todo
3396
3397\subsubsection{Creating a new Distutils command}
3398
3399This section outlines the steps to create a new Distutils command.
3400
3401A new command lives in a module in the \module{distutils.command}
3402package. There is a sample template in that directory called
3403\file{command_template}. Copy this file to a new module with the
3404same name as the new command you're implementing. This module should
3405implement a class with the same name as the module (and the command).
3406So, for instance, to create the command \code{peel_banana} (so that users
3407can run \samp{setup.py peel_banana}), you'd copy \file{command_template}
3408to \file{distutils/command/peel_banana.py}, then edit it so that it's
3409implementing the class \class{peel_banana}, a subclass of
3410\class{distutils.cmd.Command}.
3411
3412Subclasses of \class{Command} must define the following methods.
3413
3414\begin{methoddesc}{initialize_options()}
3415Set default values for all the options that this command
3416supports. Note that these defaults may be overridden by other
3417commands, by the setup script, by config files, or by the
3418command-line. Thus, this is not the place to code dependencies
3419between options; generally, \method{initialize_options()} implementations
3420are just a bunch of \samp{self.foo = None} assignments.
3421\end{methoddesc}
3422
3423\begin{methoddesc}{finalize_options}{}
3424Set final values for all the options that this command supports.
3425This is always called as late as possible, ie. after any option
3426assignments from the command-line or from other commands have been
3427done. Thus, this is the place to to code option dependencies: if
3428\var{foo} depends on \var{bar}, then it is safe to set \var{foo} from
3429\var{bar} as long as \var{foo} still has the same value it was assigned in
3430\method{initialize_options()}.
3431\end{methoddesc}
3432\begin{methoddesc}{run}{}
3433A command's raison d'etre: carry out the action it exists to
3434perform, controlled by the options initialized in
3435\method{initialize_options()}, customized by other commands, the setup
3436script, the command-line, and config files, and finalized in
3437\method{finalize_options()}. All terminal output and filesystem
3438interaction should be done by \method{run()}.
3439\end{methoddesc}
3440
3441\var{sub_commands} formalizes the notion of a ``family'' of commands,
3442eg. \code{install} as the parent with sub-commands \code{install_lib},
3443\code{install_headers}, etc. The parent of a family of commands
3444defines \var{sub_commands} as a class attribute; it's a list of
34452-tuples \samp{(command_name, predicate)}, with \var{command_name} a string
3446and \var{predicate} an unbound method, a string or None.
3447\var{predicate} is a method of the parent command that
3448determines whether the corresponding command is applicable in the
3449current situation. (Eg. we \code{install_headers} is only applicable if
3450we have any C header files to install.) If \var{predicate} is None,
3451that command is always applicable.
3452
3453\var{sub_commands} is usually defined at the *end* of a class, because
3454predicates can be unbound methods, so they must already have been
3455defined. The canonical example is the \command{install} command.
3456
3457
Greg Wardabc52162000-02-26 00:52:48 +00003458\end{document}