blob: a39478ac0d853e1ef575111383fca7eb329c8754 [file] [log] [blame]
Fred Drake211a2eb2004-03-22 21:44:43 +00001\documentclass{manual}
Greg Ward16aafcd2000-04-09 04:06:44 +00002\usepackage{distutils}
Greg Wardabc52162000-02-26 00:52:48 +00003
Greg Wardb6528972000-09-07 02:40:37 +00004% $Id$
5
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00006% TODO
7% Document extension.read_setup_file
8% Document build_clib command
9%
10
Greg Ward16aafcd2000-04-09 04:06:44 +000011\title{Distributing Python Modules}
Greg Wardabc52162000-02-26 00:52:48 +000012
Fred Drake20d47382004-01-23 15:23:49 +000013\input{boilerplate}
14
Fred Drake6fca7cc2004-03-23 18:43:03 +000015\author{Greg Ward\\
16 Anthony Baxter}
Fred Drakeb914ef02004-01-02 06:57:50 +000017\authoraddress{
18 \strong{Python Software Foundation}\\
19 Email: \email{distutils-sig@python.org}
20}
Greg Wardabc52162000-02-26 00:52:48 +000021
Greg Warde3cca262000-08-31 16:36:31 +000022\makeindex
Fred Drake6356fff2004-03-23 19:02:38 +000023\makemodindex
Greg Ward16aafcd2000-04-09 04:06:44 +000024
Greg Wardabc52162000-02-26 00:52:48 +000025\begin{document}
26
Greg Wardfacb8db2000-04-09 04:32:40 +000027\maketitle
Greg Warde3cca262000-08-31 16:36:31 +000028\begin{abstract}
29 \noindent
30 This document describes the Python Distribution Utilities
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000031 (``Distutils'') from the module developer's point of view, describing
Greg Warde3cca262000-08-31 16:36:31 +000032 how to use the Distutils to make Python modules and extensions easily
33 available to a wider audience with very little overhead for
34 build/release/install mechanics.
35\end{abstract}
36
Raymond Hettinger68804312005-01-01 00:28:46 +000037% The ugly "%begin{latexonly}" pseudo-environment suppresses the table
Fred Drakea09262e2001-03-01 18:35:43 +000038% of contents for HTML generation.
39%
40%begin{latexonly}
Greg Wardfacb8db2000-04-09 04:32:40 +000041\tableofcontents
Fred Drakea09262e2001-03-01 18:35:43 +000042%end{latexonly}
43
Greg Ward16aafcd2000-04-09 04:06:44 +000044
Fred Drake211a2eb2004-03-22 21:44:43 +000045\chapter{An Introduction to Distutils}
Greg Warde78298a2000-04-28 17:12:24 +000046\label{intro}
Greg Ward16aafcd2000-04-09 04:06:44 +000047
Andrew M. Kuchling40df7102002-05-08 13:39:03 +000048This document covers using the Distutils to distribute your Python
49modules, concentrating on the role of developer/distributor: if
Fred Drake01df4532000-06-30 03:36:41 +000050you're looking for information on installing Python modules, you
51should refer to the \citetitle[../inst/inst.html]{Installing Python
52Modules} manual.
Greg Ward16aafcd2000-04-09 04:06:44 +000053
54
Greg Wardfacb8db2000-04-09 04:32:40 +000055\section{Concepts \& Terminology}
Greg Warde78298a2000-04-28 17:12:24 +000056\label{concepts}
Greg Ward16aafcd2000-04-09 04:06:44 +000057
58Using the Distutils is quite simple, both for module developers and for
59users/administrators installing third-party modules. As a developer,
Thomas Heller5f52f722001-02-19 17:48:03 +000060your responsibilities (apart from writing solid, well-documented and
Greg Ward16aafcd2000-04-09 04:06:44 +000061well-tested code, of course!) are:
62\begin{itemize}
63\item write a setup script (\file{setup.py} by convention)
64\item (optional) write a setup configuration file
65\item create a source distribution
66\item (optional) create one or more built (binary) distributions
67\end{itemize}
68Each of these tasks is covered in this document.
69
70Not all module developers have access to a multitude of platforms, so
71it's not always feasible to expect them to create a multitude of built
72distributions. It is hoped that a class of intermediaries, called
Greg Ward19c67f82000-06-24 01:33:16 +000073\emph{packagers}, will arise to address this need. Packagers will take
74source distributions released by module developers, build them on one or
75more platforms, and release the resulting built distributions. Thus,
76users on the most popular platforms will be able to install most popular
77Python module distributions in the most natural way for their platform,
78without having to run a single setup script or compile a line of code.
Greg Ward16aafcd2000-04-09 04:06:44 +000079
80
Fred Drake211a2eb2004-03-22 21:44:43 +000081\section{A Simple Example}
Greg Warde78298a2000-04-28 17:12:24 +000082\label{simple-example}
Greg Ward16aafcd2000-04-09 04:06:44 +000083
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000084The setup script is usually quite simple, although since it's written
85in Python, there are no arbitrary limits to what you can do with it,
86though you should be careful about putting arbitrarily expensive
87operations in your setup script. Unlike, say, Autoconf-style configure
88scripts, the setup script may be run multiple times in the course of
Andrew M. Kuchlinge9a54a32003-05-13 15:02:06 +000089building and installing your module distribution.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +000090
91If all you want to do is distribute a module called \module{foo},
92contained in a file \file{foo.py}, then your setup script can be as
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +000093simple as this:
Fred Drakea09262e2001-03-01 18:35:43 +000094
Greg Ward16aafcd2000-04-09 04:06:44 +000095\begin{verbatim}
96from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +000097setup(name='foo',
98 version='1.0',
99 py_modules=['foo'],
100 )
Greg Ward16aafcd2000-04-09 04:06:44 +0000101\end{verbatim}
Greg Ward370248d2000-06-24 01:45:47 +0000102
Greg Ward16aafcd2000-04-09 04:06:44 +0000103Some observations:
104\begin{itemize}
Greg Ward370248d2000-06-24 01:45:47 +0000105\item most information that you supply to the Distutils is supplied as
Greg Wardfacb8db2000-04-09 04:32:40 +0000106 keyword arguments to the \function{setup()} function
Greg Ward16aafcd2000-04-09 04:06:44 +0000107\item those keyword arguments fall into two categories: package
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000108 metadata (name, version number) and information about what's in the
Greg Ward370248d2000-06-24 01:45:47 +0000109 package (a list of pure Python modules, in this case)
Greg Ward16aafcd2000-04-09 04:06:44 +0000110\item modules are specified by module name, not filename (the same will
111 hold true for packages and extensions)
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000112\item it's recommended that you supply a little more metadata, in
Greg Ward16aafcd2000-04-09 04:06:44 +0000113 particular your name, email address and a URL for the project
Greg Ward47f99a62000-09-04 20:07:15 +0000114 (see section~\ref{setup-script} for an example)
Greg Ward16aafcd2000-04-09 04:06:44 +0000115\end{itemize}
116
Greg Ward370248d2000-06-24 01:45:47 +0000117To create a source distribution for this module, you would create a
118setup script, \file{setup.py}, containing the above code, and run:
Fred Drakea09262e2001-03-01 18:35:43 +0000119
Greg Ward16aafcd2000-04-09 04:06:44 +0000120\begin{verbatim}
121python setup.py sdist
122\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000123
Fred Drakeeff9a872000-10-26 16:41:03 +0000124which will create an archive file (e.g., tarball on \UNIX, ZIP file on
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000125Windows) containing your setup script \file{setup.py}, and your module
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000126\file{foo.py}. The archive file will be named \file{foo-1.0.tar.gz} (or
127\file{.zip}), and will unpack into a directory \file{foo-1.0}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000128
129If an end-user wishes to install your \module{foo} module, all she has
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000130to do is download \file{foo-1.0.tar.gz} (or \file{.zip}), unpack it,
131and---from the \file{foo-1.0} directory---run
Fred Drakea09262e2001-03-01 18:35:43 +0000132
Greg Ward16aafcd2000-04-09 04:06:44 +0000133\begin{verbatim}
134python setup.py install
135\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000136
Greg Ward16aafcd2000-04-09 04:06:44 +0000137which will ultimately copy \file{foo.py} to the appropriate directory
138for third-party modules in their Python installation.
139
140This simple example demonstrates some fundamental concepts of the
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000141Distutils. First, both developers and installers have the same basic
Greg Ward16aafcd2000-04-09 04:06:44 +0000142user interface, i.e. the setup script. The difference is which
143Distutils \emph{commands} they use: the \command{sdist} command is
144almost exclusively for module developers, while \command{install} is
145more often for installers (although most developers will want to install
146their own code occasionally).
147
Greg Ward16aafcd2000-04-09 04:06:44 +0000148If you want to make things really easy for your users, you can create
149one or more built distributions for them. For instance, if you are
150running on a Windows machine, and want to make things easy for other
151Windows users, you can create an executable installer (the most
152appropriate type of built distribution for this platform) with the
Greg Ward59d382e2000-05-26 01:04:47 +0000153\command{bdist\_wininst} command. For example:
Fred Drakea09262e2001-03-01 18:35:43 +0000154
Greg Ward16aafcd2000-04-09 04:06:44 +0000155\begin{verbatim}
Greg Ward59d382e2000-05-26 01:04:47 +0000156python setup.py bdist_wininst
Greg Ward16aafcd2000-04-09 04:06:44 +0000157\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000158
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000159will create an executable installer, \file{foo-1.0.win32.exe}, in the
Greg Ward1d8f57a2000-08-05 00:43:11 +0000160current directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000161
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000162Other useful built distribution formats are RPM, implemented by the
163\command{bdist\_rpm} command, Solaris \program{pkgtool}
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000164(\command{bdist\_pkgtool}), and HP-UX \program{swinstall}
165(\command{bdist_sdux}). For example, the following command will
166create an RPM file called \file{foo-1.0.noarch.rpm}:
Fred Drakea09262e2001-03-01 18:35:43 +0000167
Greg Ward1d8f57a2000-08-05 00:43:11 +0000168\begin{verbatim}
169python setup.py bdist_rpm
170\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000171
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000172(The \command{bdist\_rpm} command uses the \command{rpm} executable,
173therefore this has to be run on an RPM-based system such as Red Hat
174Linux, SuSE Linux, or Mandrake Linux.)
Greg Ward1d8f57a2000-08-05 00:43:11 +0000175
176You can find out what distribution formats are available at any time by
177running
Fred Drakea09262e2001-03-01 18:35:43 +0000178
Greg Ward1d8f57a2000-08-05 00:43:11 +0000179\begin{verbatim}
180python setup.py bdist --help-formats
181\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000182
183
Fred Drake211a2eb2004-03-22 21:44:43 +0000184\section{General Python terminology}
Greg Warde78298a2000-04-28 17:12:24 +0000185\label{python-terms}
Greg Ward16aafcd2000-04-09 04:06:44 +0000186
187If you're reading this document, you probably have a good idea of what
188modules, extensions, and so forth are. Nevertheless, just to be sure
189that everyone is operating from a common starting point, we offer the
190following glossary of common Python terms:
191\begin{description}
192\item[module] the basic unit of code reusability in Python: a block of
Greg Ward1d8f57a2000-08-05 00:43:11 +0000193 code imported by some other code. Three types of modules concern us
194 here: pure Python modules, extension modules, and packages.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000195
Greg Ward16aafcd2000-04-09 04:06:44 +0000196\item[pure Python module] a module written in Python and contained in a
197 single \file{.py} file (and possibly associated \file{.pyc} and/or
198 \file{.pyo} files). Sometimes referred to as a ``pure module.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000199
Greg Ward16aafcd2000-04-09 04:06:44 +0000200\item[extension module] a module written in the low-level language of
Fred Drake2884d6d2003-07-02 12:27:43 +0000201 the Python implementation: C/\Cpp{} for Python, Java for Jython.
Greg Ward16aafcd2000-04-09 04:06:44 +0000202 Typically contained in a single dynamically loadable pre-compiled
Fred Drakeeff9a872000-10-26 16:41:03 +0000203 file, e.g. a shared object (\file{.so}) file for Python extensions on
204 \UNIX, a DLL (given the \file{.pyd} extension) for Python extensions
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000205 on Windows, or a Java class file for Jython extensions. (Note that
Fred Drake2884d6d2003-07-02 12:27:43 +0000206 currently, the Distutils only handles C/\Cpp{} extensions for Python.)
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000207
Greg Ward16aafcd2000-04-09 04:06:44 +0000208\item[package] a module that contains other modules; typically contained
209 in a directory in the filesystem and distinguished from other
210 directories by the presence of a file \file{\_\_init\_\_.py}.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000211
Greg Ward6153fa12000-05-26 02:24:28 +0000212\item[root package] the root of the hierarchy of packages. (This isn't
213 really a package, since it doesn't have an \file{\_\_init\_\_.py}
214 file. But we have to call it something.) The vast majority of the
215 standard library is in the root package, as are many small, standalone
216 third-party modules that don't belong to a larger module collection.
217 Unlike regular packages, modules in the root package can be found in
218 many directories: in fact, every directory listed in \code{sys.path}
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000219 contributes modules to the root package.
Greg Ward16aafcd2000-04-09 04:06:44 +0000220\end{description}
221
222
Fred Drake211a2eb2004-03-22 21:44:43 +0000223\section{Distutils-specific terminology}
Greg Warde78298a2000-04-28 17:12:24 +0000224\label{distutils-term}
Greg Ward16aafcd2000-04-09 04:06:44 +0000225
226The following terms apply more specifically to the domain of
227distributing Python modules using the Distutils:
228\begin{description}
229\item[module distribution] a collection of Python modules distributed
230 together as a single downloadable resource and meant to be installed
231 \emph{en masse}. Examples of some well-known module distributions are
232 Numeric Python, PyXML, PIL (the Python Imaging Library), or
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000233 mxBase. (This would be called a \emph{package}, except that term
Greg Ward59d382e2000-05-26 01:04:47 +0000234 is already taken in the Python context: a single module distribution
235 may contain zero, one, or many Python packages.)
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000236
Greg Ward16aafcd2000-04-09 04:06:44 +0000237\item[pure module distribution] a module distribution that contains only
238 pure Python modules and packages. Sometimes referred to as a ``pure
239 distribution.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000240
Greg Ward16aafcd2000-04-09 04:06:44 +0000241\item[non-pure module distribution] a module distribution that contains
242 at least one extension module. Sometimes referred to as a ``non-pure
243 distribution.''
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000244
Greg Ward16aafcd2000-04-09 04:06:44 +0000245\item[distribution root] the top-level directory of your source tree (or
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000246 source distribution); the directory where \file{setup.py} exists. Generally
247 \file{setup.py} will be run from this directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000248\end{description}
249
250
Fred Drake211a2eb2004-03-22 21:44:43 +0000251\chapter{Writing the Setup Script}
Greg Warde78298a2000-04-28 17:12:24 +0000252\label{setup-script}
Greg Ward16aafcd2000-04-09 04:06:44 +0000253
254The setup script is the centre of all activity in building,
255distributing, and installing modules using the Distutils. The main
256purpose of the setup script is to describe your module distribution to
Greg Wardd5767a52000-04-19 22:48:09 +0000257the Distutils, so that the various commands that operate on your modules
Greg Ward59d382e2000-05-26 01:04:47 +0000258do the right thing. As we saw in section~\ref{simple-example} above,
259the setup script consists mainly of a call to \function{setup()}, and
Greg Ward1bbe3292000-06-25 03:14:13 +0000260most information supplied to the Distutils by the module developer is
261supplied as keyword arguments to \function{setup()}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000262
263Here's a slightly more involved example, which we'll follow for the next
264couple of sections: the Distutils' own setup script. (Keep in mind that
Greg Ward1d8f57a2000-08-05 00:43:11 +0000265although the Distutils are included with Python 1.6 and later, they also
266have an independent existence so that Python 1.5.2 users can use them to
267install other module distributions. The Distutils' own setup script,
268shown here, is used to install the package into Python 1.5.2.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000269
270\begin{verbatim}
271#!/usr/bin/env python
272
273from distutils.core import setup
274
Fred Drake630e5bd2004-03-23 18:54:12 +0000275setup(name='Distutils',
276 version='1.0',
277 description='Python Distribution Utilities',
278 author='Greg Ward',
279 author_email='gward@python.net',
280 url='http://www.python.org/sigs/distutils-sig/',
Fred Drakea09262e2001-03-01 18:35:43 +0000281 packages=['distutils', 'distutils.command'],
282 )
Greg Ward16aafcd2000-04-09 04:06:44 +0000283\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000284
Greg Ward16aafcd2000-04-09 04:06:44 +0000285There are only two differences between this and the trivial one-file
Greg Warde78298a2000-04-28 17:12:24 +0000286distribution presented in section~\ref{simple-example}: more
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000287metadata, and the specification of pure Python modules by package,
Greg Ward16aafcd2000-04-09 04:06:44 +0000288rather than by module. This is important since the Distutils consist of
289a couple of dozen modules split into (so far) two packages; an explicit
290list of every module would be tedious to generate and difficult to
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000291maintain. For more information on the additional meta-data, see
292section~\ref{meta-data}.
Greg Ward16aafcd2000-04-09 04:06:44 +0000293
Greg Ward46b98e32000-04-14 01:53:36 +0000294Note that any pathnames (files or directories) supplied in the setup
Fred Drakeeff9a872000-10-26 16:41:03 +0000295script should be written using the \UNIX{} convention, i.e.
Greg Ward46b98e32000-04-14 01:53:36 +0000296slash-separated. The Distutils will take care of converting this
Greg Ward59d382e2000-05-26 01:04:47 +0000297platform-neutral representation into whatever is appropriate on your
Greg Ward46b98e32000-04-14 01:53:36 +0000298current platform before actually using the pathname. This makes your
299setup script portable across operating systems, which of course is one
300of the major goals of the Distutils. In this spirit, all pathnames in
Brett Cannon7706c2d2005-02-13 22:50:04 +0000301this document are slash-separated. (Mac OS 9 programmers should keep in
Greg Ward59d382e2000-05-26 01:04:47 +0000302mind that the \emph{absence} of a leading slash indicates a relative
Fred Drake781380c2004-02-19 23:17:46 +0000303path, the opposite of the Mac OS convention with colons.)
Greg Ward46b98e32000-04-14 01:53:36 +0000304
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000305This, of course, only applies to pathnames given to Distutils
Fred Drake2a046232003-03-31 16:23:09 +0000306functions. If you, for example, use standard Python functions such as
307\function{glob.glob()} or \function{os.listdir()} to specify files, you
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000308should be careful to write portable code instead of hardcoding path
309separators:
Fred Drakea09262e2001-03-01 18:35:43 +0000310
Thomas Heller5f52f722001-02-19 17:48:03 +0000311\begin{verbatim}
312 glob.glob(os.path.join('mydir', 'subdir', '*.html'))
313 os.listdir(os.path.join('mydir', 'subdir'))
314\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +0000315
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000316
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000317\section{Listing whole packages}
Greg Ward2afffd42000-08-06 20:37:24 +0000318\label{listing-packages}
Greg Ward16aafcd2000-04-09 04:06:44 +0000319
320The \option{packages} option tells the Distutils to process (build,
321distribute, install, etc.) all pure Python modules found in each package
322mentioned in the \option{packages} list. In order to do this, of
323course, there has to be a correspondence between package names and
324directories in the filesystem. The default correspondence is the most
Greg Ward1ecc2512000-04-19 22:36:24 +0000325obvious one, i.e. package \module{distutils} is found in the directory
Greg Ward16aafcd2000-04-09 04:06:44 +0000326\file{distutils} relative to the distribution root. Thus, when you say
327\code{packages = ['foo']} in your setup script, you are promising that
328the Distutils will find a file \file{foo/\_\_init\_\_.py} (which might
329be spelled differently on your system, but you get the idea) relative to
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000330the directory where your setup script lives. If you break this
331promise, the Distutils will issue a warning but still process the broken
332package anyways.
Greg Ward16aafcd2000-04-09 04:06:44 +0000333
334If you use a different convention to lay out your source directory,
335that's no problem: you just have to supply the \option{package\_dir}
336option to tell the Distutils about your convention. For example, say
Greg Ward1d8f57a2000-08-05 00:43:11 +0000337you keep all Python source under \file{lib}, so that modules in the
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000338``root package'' (i.e., not in any package at all) are in
Greg Ward1d8f57a2000-08-05 00:43:11 +0000339\file{lib}, modules in the \module{foo} package are in \file{lib/foo},
340and so forth. Then you would put
Fred Drakea09262e2001-03-01 18:35:43 +0000341
Greg Ward16aafcd2000-04-09 04:06:44 +0000342\begin{verbatim}
343package_dir = {'': 'lib'}
344\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000345
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000346in your setup script. The keys to this dictionary are package names,
Greg Ward1d8f57a2000-08-05 00:43:11 +0000347and an empty package name stands for the root package. The values are
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000348directory names relative to your distribution root. In this case, when
Greg Ward1d8f57a2000-08-05 00:43:11 +0000349you say \code{packages = ['foo']}, you are promising that the file
Greg Ward16aafcd2000-04-09 04:06:44 +0000350\file{lib/foo/\_\_init\_\_.py} exists.
351
Greg Ward1ecc2512000-04-19 22:36:24 +0000352Another possible convention is to put the \module{foo} package right in
353\file{lib}, the \module{foo.bar} package in \file{lib/bar}, etc. This
Greg Ward16aafcd2000-04-09 04:06:44 +0000354would be written in the setup script as
Fred Drakea09262e2001-03-01 18:35:43 +0000355
Greg Ward16aafcd2000-04-09 04:06:44 +0000356\begin{verbatim}
357package_dir = {'foo': 'lib'}
358\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000359
Greg Ward59d382e2000-05-26 01:04:47 +0000360A \code{\var{package}: \var{dir}} entry in the \option{package\_dir}
361dictionary implicitly applies to all packages below \var{package}, so
362the \module{foo.bar} case is automatically handled here. In this
363example, having \code{packages = ['foo', 'foo.bar']} tells the Distutils
364to look for \file{lib/\_\_init\_\_.py} and
365\file{lib/bar/\_\_init\_\_.py}. (Keep in mind that although
366\option{package\_dir} applies recursively, you must explicitly list all
367packages in \option{packages}: the Distutils will \emph{not} recursively
368scan your source tree looking for any directory with an
369\file{\_\_init\_\_.py} file.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000370
371
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000372\section{Listing individual modules}
Greg Warde78298a2000-04-28 17:12:24 +0000373\label{listing-modules}
Greg Ward16aafcd2000-04-09 04:06:44 +0000374
375For a small module distribution, you might prefer to list all modules
376rather than listing packages---especially the case of a single module
377that goes in the ``root package'' (i.e., no package at all). This
Greg Warde78298a2000-04-28 17:12:24 +0000378simplest case was shown in section~\ref{simple-example}; here is a
Greg Ward16aafcd2000-04-09 04:06:44 +0000379slightly more involved example:
Fred Drakea09262e2001-03-01 18:35:43 +0000380
Greg Ward16aafcd2000-04-09 04:06:44 +0000381\begin{verbatim}
382py_modules = ['mod1', 'pkg.mod2']
383\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000384
Greg Ward16aafcd2000-04-09 04:06:44 +0000385This describes two modules, one of them in the ``root'' package, the
Greg Wardd5767a52000-04-19 22:48:09 +0000386other in the \module{pkg} package. Again, the default package/directory
387layout implies that these two modules can be found in \file{mod1.py} and
388\file{pkg/mod2.py}, and that \file{pkg/\_\_init\_\_.py} exists as well.
Greg Ward2afffd42000-08-06 20:37:24 +0000389And again, you can override the package/directory correspondence using
390the \option{package\_dir} option.
Greg Ward59d382e2000-05-26 01:04:47 +0000391
392
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000393\section{Describing extension modules}
Greg Ward1365a302000-08-31 14:47:05 +0000394\label{describing-extensions}
Greg Ward59d382e2000-05-26 01:04:47 +0000395
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000396% XXX read over this section
Greg Ward2afffd42000-08-06 20:37:24 +0000397Just as writing Python extension modules is a bit more complicated than
398writing pure Python modules, describing them to the Distutils is a bit
399more complicated. Unlike pure modules, it's not enough just to list
400modules or packages and expect the Distutils to go out and find the
401right files; you have to specify the extension name, source file(s), and
402any compile/link requirements (include directories, libraries to link
403with, etc.).
404
405All of this is done through another keyword argument to
406\function{setup()}, the \option{extensions} option. \option{extensions}
407is just a list of \class{Extension} instances, each of which describes a
408single extension module. Suppose your distribution includes a single
409extension, called \module{foo} and implemented by \file{foo.c}. If no
410additional instructions to the compiler/linker are needed, describing
411this extension is quite simple:
Fred Drakea09262e2001-03-01 18:35:43 +0000412
Greg Ward2afffd42000-08-06 20:37:24 +0000413\begin{verbatim}
Fred Drake630e5bd2004-03-23 18:54:12 +0000414Extension('foo', ['foo.c'])
Greg Ward2afffd42000-08-06 20:37:24 +0000415\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000416
Greg Ward2afffd42000-08-06 20:37:24 +0000417The \class{Extension} class can be imported from
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000418\module{distutils.core} along with \function{setup()}. Thus, the setup
Greg Ward2afffd42000-08-06 20:37:24 +0000419script for a module distribution that contains only this one extension
420and nothing else might be:
Fred Drakea09262e2001-03-01 18:35:43 +0000421
Greg Ward2afffd42000-08-06 20:37:24 +0000422\begin{verbatim}
423from distutils.core import setup, Extension
Fred Drake630e5bd2004-03-23 18:54:12 +0000424setup(name='foo',
425 version='1.0',
426 ext_modules=[Extension('foo', ['foo.c'])],
427 )
Greg Ward2afffd42000-08-06 20:37:24 +0000428\end{verbatim}
429
430The \class{Extension} class (actually, the underlying extension-building
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +0000431machinery implemented by the \command{build\_ext} command) supports a
Greg Ward2afffd42000-08-06 20:37:24 +0000432great deal of flexibility in describing Python extensions, which is
433explained in the following sections.
434
435
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000436\subsection{Extension names and packages}
Greg Ward2afffd42000-08-06 20:37:24 +0000437
438The first argument to the \class{Extension} constructor is always the
439name of the extension, including any package names. For example,
Fred Drakea09262e2001-03-01 18:35:43 +0000440
Greg Ward2afffd42000-08-06 20:37:24 +0000441\begin{verbatim}
Fred Drake630e5bd2004-03-23 18:54:12 +0000442Extension('foo', ['src/foo1.c', 'src/foo2.c'])
Greg Ward2afffd42000-08-06 20:37:24 +0000443\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000444
Greg Ward2afffd42000-08-06 20:37:24 +0000445describes an extension that lives in the root package, while
Fred Drakea09262e2001-03-01 18:35:43 +0000446
Greg Ward2afffd42000-08-06 20:37:24 +0000447\begin{verbatim}
Fred Drake630e5bd2004-03-23 18:54:12 +0000448Extension('pkg.foo', ['src/foo1.c', 'src/foo2.c'])
Greg Ward2afffd42000-08-06 20:37:24 +0000449\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000450
Greg Ward2afffd42000-08-06 20:37:24 +0000451describes the same extension in the \module{pkg} package. The source
452files and resulting object code are identical in both cases; the only
453difference is where in the filesystem (and therefore where in Python's
454namespace hierarchy) the resulting extension lives.
455
456If you have a number of extensions all in the same package (or all under
457the same base package), use the \option{ext\_package} keyword argument
458to \function{setup()}. For example,
Fred Drakea09262e2001-03-01 18:35:43 +0000459
Greg Ward2afffd42000-08-06 20:37:24 +0000460\begin{verbatim}
461setup(...
Fred Drake630e5bd2004-03-23 18:54:12 +0000462 ext_package='pkg',
463 ext_modules=[Extension('foo', ['foo.c']),
464 Extension('subpkg.bar', ['bar.c'])],
Greg Ward2afffd42000-08-06 20:37:24 +0000465 )
466\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000467
Greg Ward2afffd42000-08-06 20:37:24 +0000468will compile \file{foo.c} to the extension \module{pkg.foo}, and
469\file{bar.c} to \module{pkg.subpkg.bar}.
470
471
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000472\subsection{Extension source files}
Greg Ward2afffd42000-08-06 20:37:24 +0000473
474The second argument to the \class{Extension} constructor is a list of
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000475source files. Since the Distutils currently only support C, \Cpp, and
476Objective-C extensions, these are normally C/\Cpp/Objective-C source
477files. (Be sure to use appropriate extensions to distinguish \Cpp\
478source files: \file{.cc} and \file{.cpp} seem to be recognized by both
479\UNIX{} and Windows compilers.)
Greg Ward2afffd42000-08-06 20:37:24 +0000480
481However, you can also include SWIG interface (\file{.i}) files in the
482list; the \command{build\_ext} command knows how to deal with SWIG
483extensions: it will run SWIG on the interface file and compile the
Fred Drake2884d6d2003-07-02 12:27:43 +0000484resulting C/\Cpp{} file into your extension.
Greg Ward2afffd42000-08-06 20:37:24 +0000485
486\XXX{SWIG support is rough around the edges and largely untested;
Fred Drake2884d6d2003-07-02 12:27:43 +0000487 especially SWIG support for \Cpp{} extensions! Explain in more detail
Greg Ward2afffd42000-08-06 20:37:24 +0000488 here when the interface firms up.}
489
490On some platforms, you can include non-source files that are processed
491by the compiler and included in your extension. Currently, this just
Thomas Heller5f52f722001-02-19 17:48:03 +0000492means Windows message text (\file{.mc}) files and resource definition
Fred Drake2884d6d2003-07-02 12:27:43 +0000493(\file{.rc}) files for Visual \Cpp. These will be compiled to binary resource
Thomas Heller5f52f722001-02-19 17:48:03 +0000494(\file{.res}) files and linked into the executable.
Greg Ward2afffd42000-08-06 20:37:24 +0000495
496
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000497\subsection{Preprocessor options}
Greg Ward2afffd42000-08-06 20:37:24 +0000498
499Three optional arguments to \class{Extension} will help if you need to
500specify include directories to search or preprocessor macros to
501define/undefine: \code{include\_dirs}, \code{define\_macros}, and
502\code{undef\_macros}.
503
504For example, if your extension requires header files in the
505\file{include} directory under your distribution root, use the
506\code{include\_dirs} option:
Fred Drakea09262e2001-03-01 18:35:43 +0000507
Greg Ward2afffd42000-08-06 20:37:24 +0000508\begin{verbatim}
Fred Drake630e5bd2004-03-23 18:54:12 +0000509Extension('foo', ['foo.c'], include_dirs=['include'])
Greg Ward2afffd42000-08-06 20:37:24 +0000510\end{verbatim}
511
512You can specify absolute directories there; if you know that your
Fred Drakeeff9a872000-10-26 16:41:03 +0000513extension will only be built on \UNIX{} systems with X11R6 installed to
Greg Ward2afffd42000-08-06 20:37:24 +0000514\file{/usr}, you can get away with
Fred Drakea09262e2001-03-01 18:35:43 +0000515
Greg Ward2afffd42000-08-06 20:37:24 +0000516\begin{verbatim}
Fred Drake630e5bd2004-03-23 18:54:12 +0000517Extension('foo', ['foo.c'], include_dirs=['/usr/include/X11'])
Greg Ward2afffd42000-08-06 20:37:24 +0000518\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000519
Greg Ward2afffd42000-08-06 20:37:24 +0000520You should avoid this sort of non-portable usage if you plan to
Greg Ward58437f22002-05-10 14:40:22 +0000521distribute your code: it's probably better to write C code like
522\begin{verbatim}
523#include <X11/Xlib.h>
524\end{verbatim}
Greg Ward2afffd42000-08-06 20:37:24 +0000525
526If you need to include header files from some other Python extension,
Greg Ward58437f22002-05-10 14:40:22 +0000527you can take advantage of the fact that header files are installed in a
528consistent way by the Distutils \command{install\_header} command. For
529example, the Numerical Python header files are installed (on a standard
530Unix installation) to \file{/usr/local/include/python1.5/Numerical}.
531(The exact location will differ according to your platform and Python
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000532installation.) Since the Python include
Greg Ward58437f22002-05-10 14:40:22 +0000533directory---\file{/usr/local/include/python1.5} in this case---is always
534included in the search path when building Python extensions, the best
535approach is to write C code like
536\begin{verbatim}
537#include <Numerical/arrayobject.h>
538\end{verbatim}
539If you must put the \file{Numerical} include directory right into your
540header search path, though, you can find that directory using the
Fred Drake630e5bd2004-03-23 18:54:12 +0000541Distutils \refmodule{distutils.sysconfig} module:
Fred Drakea09262e2001-03-01 18:35:43 +0000542
Greg Ward2afffd42000-08-06 20:37:24 +0000543\begin{verbatim}
544from distutils.sysconfig import get_python_inc
Fred Drake630e5bd2004-03-23 18:54:12 +0000545incdir = os.path.join(get_python_inc(plat_specific=1), 'Numerical')
Greg Ward2afffd42000-08-06 20:37:24 +0000546setup(...,
Fred Drake630e5bd2004-03-23 18:54:12 +0000547 Extension(..., include_dirs=[incdir]),
548 )
Greg Ward2afffd42000-08-06 20:37:24 +0000549\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000550
Greg Ward2afffd42000-08-06 20:37:24 +0000551Even though this is quite portable---it will work on any Python
552installation, regardless of platform---it's probably easier to just
553write your C code in the sensible way.
554
555You can define and undefine pre-processor macros with the
556\code{define\_macros} and \code{undef\_macros} options.
557\code{define\_macros} takes a list of \code{(name, value)} tuples, where
558\code{name} is the name of the macro to define (a string) and
559\code{value} is its value: either a string or \code{None}. (Defining a
560macro \code{FOO} to \code{None} is the equivalent of a bare
561\code{\#define FOO} in your C source: with most compilers, this sets
562\code{FOO} to the string \code{1}.) \code{undef\_macros} is just
563a list of macros to undefine.
564
565For example:
Fred Drakea09262e2001-03-01 18:35:43 +0000566
Greg Ward2afffd42000-08-06 20:37:24 +0000567\begin{verbatim}
568Extension(...,
Thomas Heller95a97d52003-10-08 12:01:33 +0000569 define_macros=[('NDEBUG', '1'),
570 ('HAVE_STRFTIME', None)],
Greg Ward2afffd42000-08-06 20:37:24 +0000571 undef_macros=['HAVE_FOO', 'HAVE_BAR'])
572\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000573
Greg Ward2afffd42000-08-06 20:37:24 +0000574is the equivalent of having this at the top of every C source file:
Fred Drakea09262e2001-03-01 18:35:43 +0000575
Greg Ward2afffd42000-08-06 20:37:24 +0000576\begin{verbatim}
577#define NDEBUG 1
578#define HAVE_STRFTIME
579#undef HAVE_FOO
580#undef HAVE_BAR
581\end{verbatim}
582
583
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000584\subsection{Library options}
Greg Ward2afffd42000-08-06 20:37:24 +0000585
586You can also specify the libraries to link against when building your
587extension, and the directories to search for those libraries. The
588\code{libraries} option is a list of libraries to link against,
589\code{library\_dirs} is a list of directories to search for libraries at
590link-time, and \code{runtime\_library\_dirs} is a list of directories to
591search for shared (dynamically loaded) libraries at run-time.
592
593For example, if you need to link against libraries known to be in the
594standard library search path on target systems
Fred Drakea09262e2001-03-01 18:35:43 +0000595
Greg Ward2afffd42000-08-06 20:37:24 +0000596\begin{verbatim}
597Extension(...,
Fred Drake630e5bd2004-03-23 18:54:12 +0000598 libraries=['gdbm', 'readline'])
Greg Ward2afffd42000-08-06 20:37:24 +0000599\end{verbatim}
600
601If you need to link with libraries in a non-standard location, you'll
602have to include the location in \code{library\_dirs}:
Fred Drakea09262e2001-03-01 18:35:43 +0000603
Greg Ward2afffd42000-08-06 20:37:24 +0000604\begin{verbatim}
605Extension(...,
Fred Drake630e5bd2004-03-23 18:54:12 +0000606 library_dirs=['/usr/X11R6/lib'],
607 libraries=['X11', 'Xt'])
Greg Ward2afffd42000-08-06 20:37:24 +0000608\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000609
Greg Ward2afffd42000-08-06 20:37:24 +0000610(Again, this sort of non-portable construct should be avoided if you
611intend to distribute your code.)
612
Thomas Heller5f52f722001-02-19 17:48:03 +0000613\XXX{Should mention clib libraries here or somewhere else!}
614
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000615\subsection{Other options}
Thomas Heller5f52f722001-02-19 17:48:03 +0000616
617There are still some other options which can be used to handle special
618cases.
619
620The \option{extra\_objects} option is a list of object files to be passed
621to the linker. These files must not have extensions, as the default
622extension for the compiler is used.
623
624\option{extra\_compile\_args} and \option{extra\_link\_args} can be used
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000625to specify additional command line options for the respective compiler and
626linker command lines.
Thomas Heller5f52f722001-02-19 17:48:03 +0000627
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000628\option{export\_symbols} is only useful on Windows. It can contain a list
Thomas Heller5f52f722001-02-19 17:48:03 +0000629of symbols (functions or variables) to be exported. This option
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000630is not needed when building compiled extensions: Distutils
631will automatically add \code{initmodule}
632to the list of exported symbols.
Thomas Heller5f52f722001-02-19 17:48:03 +0000633
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000634\section{Installing Scripts}
Thomas Heller5f52f722001-02-19 17:48:03 +0000635So far we have been dealing with pure and non-pure Python modules,
636which are usually not run by themselves but imported by scripts.
637
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000638Scripts are files containing Python source code, intended to be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000639started from the command line. Scripts don't require Distutils to do
640anything very complicated. The only clever feature is that if the
641first line of the script starts with \code{\#!} and contains the word
642``python'', the Distutils will adjust the first line to refer to the
Martin v. Löwis9f5c0c42004-08-25 11:37:43 +0000643current interpreter location. By default, it is replaced with the
Fred Drakee3a1b482004-08-25 14:01:32 +0000644current interpreter location. The \longprogramopt{executable} (or
645\programopt{-e}) option will allow the interpreter path to be
646explicitly overridden.
Thomas Heller5f52f722001-02-19 17:48:03 +0000647
648The \option{scripts} option simply is a list of files to be handled
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000649in this way. From the PyXML setup script:
650
651\begin{verbatim}
Fred Drake630e5bd2004-03-23 18:54:12 +0000652setup(...
653 scripts=['scripts/xmlproc_parse', 'scripts/xmlproc_val']
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000654 )
655\end{verbatim}
Thomas Heller5f52f722001-02-19 17:48:03 +0000656
657
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000658\section{Installing Package Data}
Fred Drake0eb32a62004-06-11 21:50:33 +0000659
660Often, additional files need to be installed into a package. These
661files are often data that's closely related to the package's
662implementation, or text files containing documentation that might be
663of interest to programmers using the package. These files are called
664\dfn{package data}.
665
666Package data can be added to packages using the \code{package_data}
667keyword argument to the \function{setup()} function. The value must
668be a mapping from package name to a list of relative path names that
669should be copied into the package. The paths are interpreted as
670relative to the directory containing the package (information from the
671\code{package_dir} mapping is used if appropriate); that is, the files
672are expected to be part of the package in the source directories.
673They may contain glob patterns as well.
674
675The path names may contain directory portions; any necessary
676directories will be created in the installation.
677
678For example, if a package should contain a subdirectory with several
679data files, the files can be arranged like this in the source tree:
680
681\begin{verbatim}
682setup.py
683src/
684 mypkg/
685 __init__.py
686 module.py
687 data/
688 tables.dat
689 spoons.dat
690 forks.dat
691\end{verbatim}
692
693The corresponding call to \function{setup()} might be:
694
695\begin{verbatim}
696setup(...,
697 packages=['mypkg'],
698 package_dir={'mypkg': 'src/mypkg'},
Thomas Hellerdd6d2072004-06-18 17:31:23 +0000699 package_data={'mypkg': ['data/*.dat']},
Fred Drake0eb32a62004-06-11 21:50:33 +0000700 )
701\end{verbatim}
702
703
704\versionadded{2.4}
705
706
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000707\section{Installing Additional Files}
Fred Drakea09262e2001-03-01 18:35:43 +0000708
Thomas Heller5f52f722001-02-19 17:48:03 +0000709The \option{data\_files} option can be used to specify additional
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000710files needed by the module distribution: configuration files, message
711catalogs, data files, anything which doesn't fit in the previous
712categories.
Thomas Heller5f52f722001-02-19 17:48:03 +0000713
Fred Drake632bda32002-03-08 22:02:06 +0000714\option{data\_files} specifies a sequence of (\var{directory},
715\var{files}) pairs in the following way:
Fred Drakea09262e2001-03-01 18:35:43 +0000716
Thomas Heller5f52f722001-02-19 17:48:03 +0000717\begin{verbatim}
718setup(...
719 data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000720 ('config', ['cfg/data.cfg']),
721 ('/etc/init.d', ['init-script'])]
722 )
Thomas Heller5f52f722001-02-19 17:48:03 +0000723\end{verbatim}
724
725Note that you can specify the directory names where the data files
726will be installed, but you cannot rename the data files themselves.
727
Fred Drake632bda32002-03-08 22:02:06 +0000728Each (\var{directory}, \var{files}) pair in the sequence specifies the
729installation directory and the files to install there. If
730\var{directory} is a relative path, it is interpreted relative to the
731installation prefix (Python's \code{sys.prefix} for pure-Python
732packages, \code{sys.exec_prefix} for packages that contain extension
733modules). Each file name in \var{files} is interpreted relative to
734the \file{setup.py} script at the top of the package source
735distribution. No directory information from \var{files} is used to
736determine the final location of the installed file; only the name of
737the file is used.
738
Thomas Heller5f52f722001-02-19 17:48:03 +0000739You can specify the \option{data\_files} options as a simple sequence
740of files without specifying a target directory, but this is not recommended,
741and the \command{install} command will print a warning in this case.
742To install data files directly in the target directory, an empty
743string should be given as the directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000744
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000745\section{Additional meta-data}
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000746\label{meta-data}
747
748The setup script may include additional meta-data beyond the name and
749version. This information includes:
750
Fred Drakec440af52003-04-25 16:43:28 +0000751\begin{tableiv}{l|l|l|c}{code}%
752 {Meta-Data}{Description}{Value}{Notes}
753 \lineiv{name}{name of the package}
754 {short string}{(1)}
755 \lineiv{version}{version of this release}
756 {short string}{(1)(2)}
757 \lineiv{author}{package author's name}
758 {short string}{(3)}
759 \lineiv{author_email}{email address of the package author}
760 {email address}{(3)}
761 \lineiv{maintainer}{package maintainer's name}
762 {short string}{(3)}
763 \lineiv{maintainer_email}{email address of the package maintainer}
764 {email address}{(3)}
765 \lineiv{url}{home page for the package}
766 {URL}{(1)}
767 \lineiv{description}{short, summary description of the package}
768 {short string}{}
769 \lineiv{long_description}{longer description of the package}
770 {long string}{}
771 \lineiv{download_url}{location where the package may be downloaded}
772 {URL}{(4)}
773 \lineiv{classifiers}{a list of Trove classifiers}
774 {list of strings}{(4)}
775\end{tableiv}
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000776
777\noindent Notes:
778\begin{description}
Fred Drakec440af52003-04-25 16:43:28 +0000779\item[(1)] These fields are required.
780\item[(2)] It is recommended that versions take the form
781 \emph{major.minor\optional{.patch\optional{.sub}}}.
782\item[(3)] Either the author or the maintainer must be identified.
783\item[(4)] These fields should not be used if your package is to be
784 compatible with Python versions prior to 2.2.3 or 2.3. The list is
785 available from the \ulink{PyPI website}{http://www.python.org/pypi}.
786
Fred Drake630e5bd2004-03-23 18:54:12 +0000787\item['short string'] A single line of text, not more than 200 characters.
788\item['long string'] Multiple lines of plain text in reStructuredText
Fred Drakec440af52003-04-25 16:43:28 +0000789 format (see \url{http://docutils.sf.net/}).
Fred Drake630e5bd2004-03-23 18:54:12 +0000790\item['list of strings'] See below.
Fred Drakec440af52003-04-25 16:43:28 +0000791\end{description}
792
793None of the string values may be Unicode.
794
795Encoding the version information is an art in itself. Python packages
796generally adhere to the version format
797\emph{major.minor\optional{.patch}\optional{sub}}. The major number is
7980 for
799initial, experimental releases of software. It is incremented for
800releases that represent major milestones in a package. The minor
801number is incremented when important new features are added to the
802package. The patch number increments when bug-fix releases are
803made. Additional trailing version information is sometimes used to
804indicate sub-releases. These are "a1,a2,...,aN" (for alpha releases,
805where functionality and API may change), "b1,b2,...,bN" (for beta
806releases, which only fix bugs) and "pr1,pr2,...,prN" (for final
807pre-release release testing). Some examples:
808
809\begin{description}
810\item[0.1.0] the first, experimental release of a package
811\item[1.0.1a2] the second alpha release of the first patch version of 1.0
812\end{description}
813
814\option{classifiers} are specified in a python list:
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000815
816\begin{verbatim}
817setup(...
Fred Drake630e5bd2004-03-23 18:54:12 +0000818 classifiers=[
Fred Drake2a046232003-03-31 16:23:09 +0000819 'Development Status :: 4 - Beta',
820 'Environment :: Console',
821 'Environment :: Web Environment',
822 'Intended Audience :: End Users/Desktop',
823 'Intended Audience :: Developers',
824 'Intended Audience :: System Administrators',
825 'License :: OSI Approved :: Python Software Foundation License',
826 'Operating System :: MacOS :: MacOS X',
827 'Operating System :: Microsoft :: Windows',
828 'Operating System :: POSIX',
829 'Programming Language :: Python',
830 'Topic :: Communications :: Email',
831 'Topic :: Office/Business',
832 'Topic :: Software Development :: Bug Tracking',
833 ],
Fred Drake2a046232003-03-31 16:23:09 +0000834 )
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000835\end{verbatim}
836
Fred Drakec440af52003-04-25 16:43:28 +0000837If you wish to include classifiers in your \file{setup.py} file and also
838wish to remain backwards-compatible with Python releases prior to 2.2.3,
839then you can include the following code fragment in your \file{setup.py}
Fred Drake630e5bd2004-03-23 18:54:12 +0000840before the \function{setup()} call.
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000841
842\begin{verbatim}
Fred Drakec440af52003-04-25 16:43:28 +0000843# patch distutils if it can't cope with the "classifiers" or
844# "download_url" keywords
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000845if sys.version < '2.2.3':
846 from distutils.dist import DistributionMetadata
847 DistributionMetadata.classifiers = None
Fred Drake2a046232003-03-31 16:23:09 +0000848 DistributionMetadata.download_url = None
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000849\end{verbatim}
850
Greg Ward16aafcd2000-04-09 04:06:44 +0000851
Neal Norwitz2e56c8a2004-08-13 02:56:16 +0000852\section{Debugging the setup script}
Thomas Heller675580f2003-06-30 19:33:29 +0000853
854Sometimes things go wrong, and the setup script doesn't do what the
855developer wants.
856
857Distutils catches any exceptions when running the setup script, and
858print a simple error message before the script is terminated. The
859motivation for this behaviour is to not confuse administrators who
860don't know much about Python and are trying to install a package. If
861they get a big long traceback from deep inside the guts of Distutils,
862they may think the package or the Python installation is broken
863because they don't read all the way down to the bottom and see that
864it's a permission problem.
865
866On the other hand, this doesn't help the developer to find the cause
867of the failure. For this purpose, the DISTUTILS_DEBUG environment
868variable can be set to anything except an empty string, and distutils
869will now print detailed information what it is doing, and prints the
Martin v. Löwis95cf84a2003-10-19 07:32:24 +0000870full traceback in case an exception occurs.
Thomas Heller675580f2003-06-30 19:33:29 +0000871
Fred Drake211a2eb2004-03-22 21:44:43 +0000872\chapter{Writing the Setup Configuration File}
Greg Warde78298a2000-04-28 17:12:24 +0000873\label{setup-config}
Greg Ward16aafcd2000-04-09 04:06:44 +0000874
Greg Ward16aafcd2000-04-09 04:06:44 +0000875Often, it's not possible to write down everything needed to build a
Greg Ward47f99a62000-09-04 20:07:15 +0000876distribution \emph{a priori}: you may need to get some information from
877the user, or from the user's system, in order to proceed. As long as
878that information is fairly simple---a list of directories to search for
879C header files or libraries, for example---then providing a
880configuration file, \file{setup.cfg}, for users to edit is a cheap and
881easy way to solicit it. Configuration files also let you provide
882default values for any command option, which the installer can then
883override either on the command-line or by editing the config file.
Greg Ward16aafcd2000-04-09 04:06:44 +0000884
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000885% (If you have more advanced needs, such as determining which extensions
886% to build based on what capabilities are present on the target system,
887% then you need the Distutils ``auto-configuration'' facility. This
888% started to appear in Distutils 0.9 but, as of this writing, isn't mature
889% or stable enough yet for real-world use.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000890
Greg Ward47f99a62000-09-04 20:07:15 +0000891The setup configuration file is a useful middle-ground between the setup
892script---which, ideally, would be opaque to installers\footnote{This
893 ideal probably won't be achieved until auto-configuration is fully
894 supported by the Distutils.}---and the command-line to the setup
895script, which is outside of your control and entirely up to the
896installer. In fact, \file{setup.cfg} (and any other Distutils
897configuration files present on the target system) are processed after
898the contents of the setup script, but before the command-line. This has
899several useful consequences:
900\begin{itemize}
901\item installers can override some of what you put in \file{setup.py} by
902 editing \file{setup.cfg}
903\item you can provide non-standard defaults for options that are not
904 easily set in \file{setup.py}
905\item installers can override anything in \file{setup.cfg} using the
906 command-line options to \file{setup.py}
907\end{itemize}
908
909The basic syntax of the configuration file is simple:
Fred Drakea09262e2001-03-01 18:35:43 +0000910
Greg Ward47f99a62000-09-04 20:07:15 +0000911\begin{verbatim}
912[command]
913option=value
914...
915\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000916
Greg Ward47f99a62000-09-04 20:07:15 +0000917where \var{command} is one of the Distutils commands (e.g.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000918\command{build\_py}, \command{install}), and \var{option} is one of
919the options that command supports. Any number of options can be
920supplied for each command, and any number of command sections can be
921included in the file. Blank lines are ignored, as are comments, which
922run from a \character{\#} character until the end of the line. Long
923option values can be split across multiple lines simply by indenting
924the continuation lines.
Greg Ward47f99a62000-09-04 20:07:15 +0000925
926You can find out the list of options supported by a particular command
927with the universal \longprogramopt{help} option, e.g.
Fred Drakea09262e2001-03-01 18:35:43 +0000928
Greg Ward47f99a62000-09-04 20:07:15 +0000929\begin{verbatim}
930> python setup.py --help build_ext
931[...]
932Options for 'build_ext' command:
933 --build-lib (-b) directory for compiled extension modules
934 --build-temp (-t) directory for temporary files (build by-products)
935 --inplace (-i) ignore build-lib and put compiled extensions into the
936 source directory alongside your pure Python modules
937 --include-dirs (-I) list of directories to search for header files
938 --define (-D) C preprocessor macros to define
939 --undef (-U) C preprocessor macros to undefine
940[...]
941\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000942
Greg Ward47f99a62000-09-04 20:07:15 +0000943Note that an option spelled \longprogramopt{foo-bar} on the command-line
944is spelled \option{foo\_bar} in configuration files.
945
946For example, say you want your extensions to be built
947``in-place''---that is, you have an extension \module{pkg.ext}, and you
Fred Drakeeff9a872000-10-26 16:41:03 +0000948want the compiled extension file (\file{ext.so} on \UNIX, say) to be put
Greg Ward47f99a62000-09-04 20:07:15 +0000949in the same source directory as your pure Python modules
950\module{pkg.mod1} and \module{pkg.mod2}. You can always use the
951\longprogramopt{inplace} option on the command-line to ensure this:
Fred Drakea09262e2001-03-01 18:35:43 +0000952
Greg Ward47f99a62000-09-04 20:07:15 +0000953\begin{verbatim}
954python setup.py build_ext --inplace
955\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000956
Greg Ward47f99a62000-09-04 20:07:15 +0000957But this requires that you always specify the \command{build\_ext}
958command explicitly, and remember to provide \longprogramopt{inplace}.
959An easier way is to ``set and forget'' this option, by encoding it in
960\file{setup.cfg}, the configuration file for this distribution:
Fred Drakea09262e2001-03-01 18:35:43 +0000961
Greg Ward47f99a62000-09-04 20:07:15 +0000962\begin{verbatim}
963[build_ext]
964inplace=1
965\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000966
Greg Ward47f99a62000-09-04 20:07:15 +0000967This will affect all builds of this module distribution, whether or not
Raymond Hettinger68804312005-01-01 00:28:46 +0000968you explicitly specify \command{build\_ext}. If you include
Greg Ward47f99a62000-09-04 20:07:15 +0000969\file{setup.cfg} in your source distribution, it will also affect
970end-user builds---which is probably a bad idea for this option, since
971always building extensions in-place would break installation of the
972module distribution. In certain peculiar cases, though, modules are
973built right in their installation directory, so this is conceivably a
974useful ability. (Distributing extensions that expect to be built in
975their installation directory is almost always a bad idea, though.)
976
977Another example: certain commands take a lot of options that don't
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000978change from run to run; for example, \command{bdist\_rpm} needs to know
Greg Ward47f99a62000-09-04 20:07:15 +0000979everything required to generate a ``spec'' file for creating an RPM
980distribution. Some of this information comes from the setup script, and
981some is automatically generated by the Distutils (such as the list of
982files installed). But some of it has to be supplied as options to
983\command{bdist\_rpm}, which would be very tedious to do on the
984command-line for every run. Hence, here is a snippet from the
985Distutils' own \file{setup.cfg}:
Fred Drakea09262e2001-03-01 18:35:43 +0000986
Greg Ward47f99a62000-09-04 20:07:15 +0000987\begin{verbatim}
988[bdist_rpm]
989release = 1
990packager = Greg Ward <gward@python.net>
991doc_files = CHANGES.txt
992 README.txt
993 USAGE.txt
994 doc/
995 examples/
996\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000997
Greg Ward47f99a62000-09-04 20:07:15 +0000998Note that the \option{doc\_files} option is simply a
999whitespace-separated string split across multiple lines for readability.
Greg Ward16aafcd2000-04-09 04:06:44 +00001000
1001
Fred Drakea09262e2001-03-01 18:35:43 +00001002\begin{seealso}
1003 \seetitle[../inst/config-syntax.html]{Installing Python
1004 Modules}{More information on the configuration files is
1005 available in the manual for system administrators.}
1006\end{seealso}
1007
1008
Fred Drake211a2eb2004-03-22 21:44:43 +00001009\chapter{Creating a Source Distribution}
Greg Warde78298a2000-04-28 17:12:24 +00001010\label{source-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +00001011
Greg Warde78298a2000-04-28 17:12:24 +00001012As shown in section~\ref{simple-example}, you use the
Greg Ward16aafcd2000-04-09 04:06:44 +00001013\command{sdist} command to create a source distribution. In the
1014simplest case,
Fred Drakea09262e2001-03-01 18:35:43 +00001015
Greg Ward16aafcd2000-04-09 04:06:44 +00001016\begin{verbatim}
1017python setup.py sdist
1018\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001019
Greg Ward19c67f82000-06-24 01:33:16 +00001020(assuming you haven't specified any \command{sdist} options in the setup
1021script or config file), \command{sdist} creates the archive of the
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001022default format for the current platform. The default format is a gzip'ed
Fred Drakeeff9a872000-10-26 16:41:03 +00001023tar file (\file{.tar.gz}) on \UNIX, and ZIP file on Windows.
Greg Ward54589d42000-09-06 01:37:35 +00001024
Greg Wardd5767a52000-04-19 22:48:09 +00001025You can specify as many formats as you like using the
1026\longprogramopt{formats} option, for example:
Fred Drakea09262e2001-03-01 18:35:43 +00001027
Greg Ward16aafcd2000-04-09 04:06:44 +00001028\begin{verbatim}
1029python setup.py sdist --formats=gztar,zip
1030\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001031
Greg Ward16aafcd2000-04-09 04:06:44 +00001032to create a gzipped tarball and a zip file. The available formats are:
Fred Drake781380c2004-02-19 23:17:46 +00001033
Greg Ward46b98e32000-04-14 01:53:36 +00001034\begin{tableiii}{l|l|c}{code}%
1035 {Format}{Description}{Notes}
Greg Ward54589d42000-09-06 01:37:35 +00001036 \lineiii{zip}{zip file (\file{.zip})}{(1),(3)}
1037 \lineiii{gztar}{gzip'ed tar file (\file{.tar.gz})}{(2),(4)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001038 \lineiii{bztar}{bzip2'ed tar file (\file{.tar.bz2})}{(4)}
Greg Ward47f99a62000-09-04 20:07:15 +00001039 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(4)}
Greg Ward54589d42000-09-06 01:37:35 +00001040 \lineiii{tar}{tar file (\file{.tar})}{(4)}
Greg Ward46b98e32000-04-14 01:53:36 +00001041\end{tableiii}
1042
1043\noindent Notes:
1044\begin{description}
1045\item[(1)] default on Windows
Fred Drakeeff9a872000-10-26 16:41:03 +00001046\item[(2)] default on \UNIX
Greg Wardb6528972000-09-07 02:40:37 +00001047\item[(3)] requires either external \program{zip} utility or
Greg Ward954ce8b2002-05-10 14:42:10 +00001048 \module{zipfile} module (part of the standard Python library since
1049 Python~1.6)
Greg Ward47f99a62000-09-04 20:07:15 +00001050\item[(4)] requires external utilities: \program{tar} and possibly one
1051 of \program{gzip}, \program{bzip2}, or \program{compress}
Greg Ward46b98e32000-04-14 01:53:36 +00001052\end{description}
Greg Ward16aafcd2000-04-09 04:06:44 +00001053
1054
Greg Ward54589d42000-09-06 01:37:35 +00001055
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00001056\section{Specifying the files to distribute}
Greg Warde78298a2000-04-28 17:12:24 +00001057\label{manifest}
Greg Ward16aafcd2000-04-09 04:06:44 +00001058
Greg Ward54589d42000-09-06 01:37:35 +00001059If you don't supply an explicit list of files (or instructions on how to
1060generate one), the \command{sdist} command puts a minimal default set
1061into the source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +00001062\begin{itemize}
Greg Wardfacb8db2000-04-09 04:32:40 +00001063\item all Python source files implied by the \option{py\_modules} and
Greg Ward16aafcd2000-04-09 04:06:44 +00001064 \option{packages} options
Greg Wardfacb8db2000-04-09 04:32:40 +00001065\item all C source files mentioned in the \option{ext\_modules} or
Greg Ward16aafcd2000-04-09 04:06:44 +00001066 \option{libraries} options (\XXX{getting C library sources currently
Fred Drake781380c2004-02-19 23:17:46 +00001067 broken---no \method{get_source_files()} method in \file{build_clib.py}!})
Fred Drake203b10c2004-03-31 01:50:37 +00001068\item scripts identified by the \option{scripts} option
Greg Ward16aafcd2000-04-09 04:06:44 +00001069\item anything that looks like a test script: \file{test/test*.py}
1070 (currently, the Distutils don't do anything with test scripts except
1071 include them in source distributions, but in the future there will be
1072 a standard for testing Python module distributions)
Greg Ward54589d42000-09-06 01:37:35 +00001073\item \file{README.txt} (or \file{README}), \file{setup.py} (or whatever
1074 you called your setup script), and \file{setup.cfg}
Greg Ward16aafcd2000-04-09 04:06:44 +00001075\end{itemize}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001076
Greg Ward16aafcd2000-04-09 04:06:44 +00001077Sometimes this is enough, but usually you will want to specify
1078additional files to distribute. The typical way to do this is to write
1079a \emph{manifest template}, called \file{MANIFEST.in} by default. The
Greg Ward54589d42000-09-06 01:37:35 +00001080manifest template is just a list of instructions for how to generate
1081your manifest file, \file{MANIFEST}, which is the exact list of files to
1082include in your source distribution. The \command{sdist} command
1083processes this template and generates a manifest based on its
1084instructions and what it finds in the filesystem.
1085
1086If you prefer to roll your own manifest file, the format is simple: one
1087filename per line, regular files (or symlinks to them) only. If you do
1088supply your own \file{MANIFEST}, you must specify everything: the
1089default set of files described above does not apply in this case.
Greg Ward16aafcd2000-04-09 04:06:44 +00001090
1091The manifest template has one command per line, where each command
1092specifies a set of files to include or exclude from the source
1093distribution. For an example, again we turn to the Distutils' own
1094manifest template:
Fred Drakea09262e2001-03-01 18:35:43 +00001095
Greg Ward16aafcd2000-04-09 04:06:44 +00001096\begin{verbatim}
1097include *.txt
Greg Ward87da1ea2000-04-21 04:35:25 +00001098recursive-include examples *.txt *.py
Greg Ward16aafcd2000-04-09 04:06:44 +00001099prune examples/sample?/build
1100\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001101
Greg Ward16aafcd2000-04-09 04:06:44 +00001102The meanings should be fairly clear: include all files in the
Fred Drake630e5bd2004-03-23 18:54:12 +00001103distribution root matching \file{*.txt}, all files anywhere under the
1104\file{examples} directory matching \file{*.txt} or \file{*.py}, and
1105exclude all directories matching \file{examples/sample?/build}. All of
Greg Ward54589d42000-09-06 01:37:35 +00001106this is done \emph{after} the standard include set, so you can exclude
1107files from the standard set with explicit instructions in the manifest
1108template. (Or, you can use the \longprogramopt{no-defaults} option to
1109disable the standard set entirely.) There are several other commands
1110available in the manifest template mini-language; see
1111section~\ref{sdist-cmd}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001112
Greg Ward54589d42000-09-06 01:37:35 +00001113The order of commands in the manifest template matters: initially, we
1114have the list of default files as described above, and each command in
1115the template adds to or removes from that list of files. Once we have
1116fully processed the manifest template, we remove files that should not
1117be included in the source distribution:
1118\begin{itemize}
1119\item all files in the Distutils ``build'' tree (default \file{build/})
Tim Peters2f50e902004-05-31 19:27:59 +00001120\item all files in directories named \file{RCS}, \file{CVS} or \file{.svn}
Greg Ward54589d42000-09-06 01:37:35 +00001121\end{itemize}
1122Now we have our complete list of files, which is written to the manifest
1123for future reference, and then used to build the source distribution
1124archive(s).
1125
1126You can disable the default set of included files with the
1127\longprogramopt{no-defaults} option, and you can disable the standard
1128exclude set with \longprogramopt{no-prune}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001129
Greg Ward46b98e32000-04-14 01:53:36 +00001130Following the Distutils' own manifest template, let's trace how the
Greg Ward47f99a62000-09-04 20:07:15 +00001131\command{sdist} command builds the list of files to include in the
Greg Ward46b98e32000-04-14 01:53:36 +00001132Distutils source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +00001133\begin{enumerate}
1134\item include all Python source files in the \file{distutils} and
1135 \file{distutils/command} subdirectories (because packages
1136 corresponding to those two directories were mentioned in the
Greg Ward54589d42000-09-06 01:37:35 +00001137 \option{packages} option in the setup script---see
1138 section~\ref{setup-script})
1139\item include \file{README.txt}, \file{setup.py}, and \file{setup.cfg}
1140 (standard files)
1141\item include \file{test/test*.py} (standard files)
Greg Ward16aafcd2000-04-09 04:06:44 +00001142\item include \file{*.txt} in the distribution root (this will find
1143 \file{README.txt} a second time, but such redundancies are weeded out
1144 later)
Greg Ward54589d42000-09-06 01:37:35 +00001145\item include anything matching \file{*.txt} or \file{*.py} in the
1146 sub-tree under \file{examples},
1147\item exclude all files in the sub-trees starting at directories
1148 matching \file{examples/sample?/build}---this may exclude files
1149 included by the previous two steps, so it's important that the
1150 \code{prune} command in the manifest template comes after the
1151 \code{recursive-include} command
Tim Peters2f50e902004-05-31 19:27:59 +00001152\item exclude the entire \file{build} tree, and any \file{RCS},
1153 \file{CVS} and \file{.svn} directories
Greg Wardfacb8db2000-04-09 04:32:40 +00001154\end{enumerate}
Greg Ward46b98e32000-04-14 01:53:36 +00001155Just like in the setup script, file and directory names in the manifest
1156template should always be slash-separated; the Distutils will take care
1157of converting them to the standard representation on your platform.
1158That way, the manifest template is portable across operating systems.
1159
Greg Ward16aafcd2000-04-09 04:06:44 +00001160
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00001161\section{Manifest-related options}
Greg Warde78298a2000-04-28 17:12:24 +00001162\label{manifest-options}
Greg Ward16aafcd2000-04-09 04:06:44 +00001163
1164The normal course of operations for the \command{sdist} command is as
1165follows:
1166\begin{itemize}
Greg Ward46b98e32000-04-14 01:53:36 +00001167\item if the manifest file, \file{MANIFEST} doesn't exist, read
1168 \file{MANIFEST.in} and create the manifest
Greg Ward54589d42000-09-06 01:37:35 +00001169\item if neither \file{MANIFEST} nor \file{MANIFEST.in} exist, create a
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001170 manifest with just the default file set
Greg Ward1d8f57a2000-08-05 00:43:11 +00001171\item if either \file{MANIFEST.in} or the setup script (\file{setup.py})
1172 are more recent than \file{MANIFEST}, recreate \file{MANIFEST} by
1173 reading \file{MANIFEST.in}
Greg Ward16aafcd2000-04-09 04:06:44 +00001174\item use the list of files now in \file{MANIFEST} (either just
1175 generated or read in) to create the source distribution archive(s)
1176\end{itemize}
Greg Ward54589d42000-09-06 01:37:35 +00001177There are a couple of options that modify this behaviour. First, use
1178the \longprogramopt{no-defaults} and \longprogramopt{no-prune} to
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001179disable the standard ``include'' and ``exclude'' sets.
Greg Ward16aafcd2000-04-09 04:06:44 +00001180
Greg Ward54589d42000-09-06 01:37:35 +00001181Second, you might want to force the manifest to be regenerated---for
Greg Ward16aafcd2000-04-09 04:06:44 +00001182example, if you have added or removed files or directories that match an
1183existing pattern in the manifest template, you should regenerate the
1184manifest:
Fred Drakea09262e2001-03-01 18:35:43 +00001185
Greg Ward16aafcd2000-04-09 04:06:44 +00001186\begin{verbatim}
1187python setup.py sdist --force-manifest
1188\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +00001189
1190Or, you might just want to (re)generate the manifest, but not create a
1191source distribution:
Fred Drakea09262e2001-03-01 18:35:43 +00001192
Greg Ward16aafcd2000-04-09 04:06:44 +00001193\begin{verbatim}
1194python setup.py sdist --manifest-only
1195\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001196
Greg Ward54589d42000-09-06 01:37:35 +00001197\longprogramopt{manifest-only} implies \longprogramopt{force-manifest}.
1198\programopt{-o} is a shortcut for \longprogramopt{manifest-only}, and
1199\programopt{-f} for \longprogramopt{force-manifest}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001200
1201
Fred Drake211a2eb2004-03-22 21:44:43 +00001202\chapter{Creating Built Distributions}
Greg Warde78298a2000-04-28 17:12:24 +00001203\label{built-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +00001204
Greg Ward46b98e32000-04-14 01:53:36 +00001205A ``built distribution'' is what you're probably used to thinking of
1206either as a ``binary package'' or an ``installer'' (depending on your
1207background). It's not necessarily binary, though, because it might
1208contain only Python source code and/or byte-code; and we don't call it a
1209package, because that word is already spoken for in Python. (And
Fred Drake2a1bc502004-02-19 23:03:29 +00001210``installer'' is a term specific to the world of mainstream desktop
1211systems.)
Greg Ward16aafcd2000-04-09 04:06:44 +00001212
Greg Ward46b98e32000-04-14 01:53:36 +00001213A built distribution is how you make life as easy as possible for
1214installers of your module distribution: for users of RPM-based Linux
1215systems, it's a binary RPM; for Windows users, it's an executable
1216installer; for Debian-based Linux users, it's a Debian package; and so
1217forth. Obviously, no one person will be able to create built
Greg Wardb6528972000-09-07 02:40:37 +00001218distributions for every platform under the sun, so the Distutils are
Greg Ward46b98e32000-04-14 01:53:36 +00001219designed to enable module developers to concentrate on their
1220specialty---writing code and creating source distributions---while an
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001221intermediary species called \emph{packagers} springs up to turn source
Greg Ward19c67f82000-06-24 01:33:16 +00001222distributions into built distributions for as many platforms as there
Greg Ward46b98e32000-04-14 01:53:36 +00001223are packagers.
1224
1225Of course, the module developer could be his own packager; or the
1226packager could be a volunteer ``out there'' somewhere who has access to
1227a platform which the original developer does not; or it could be
1228software periodically grabbing new source distributions and turning them
1229into built distributions for as many platforms as the software has
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001230access to. Regardless of who they are, a packager uses the
Greg Ward46b98e32000-04-14 01:53:36 +00001231setup script and the \command{bdist} command family to generate built
1232distributions.
1233
1234As a simple example, if I run the following command in the Distutils
1235source tree:
Fred Drakea09262e2001-03-01 18:35:43 +00001236
Greg Ward46b98e32000-04-14 01:53:36 +00001237\begin{verbatim}
1238python setup.py bdist
1239\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001240
Greg Ward46b98e32000-04-14 01:53:36 +00001241then the Distutils builds my module distribution (the Distutils itself
1242in this case), does a ``fake'' installation (also in the \file{build}
1243directory), and creates the default type of built distribution for my
Greg Wardb6528972000-09-07 02:40:37 +00001244platform. The default format for built distributions is a ``dumb'' tar
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001245file on \UNIX, and a simple executable installer on Windows. (That tar
Greg Wardb6528972000-09-07 02:40:37 +00001246file is considered ``dumb'' because it has to be unpacked in a specific
1247location to work.)
Greg Ward1d8f57a2000-08-05 00:43:11 +00001248
Fred Drakeeff9a872000-10-26 16:41:03 +00001249Thus, the above command on a \UNIX{} system creates
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001250\file{Distutils-1.0.\filevar{plat}.tar.gz}; unpacking this tarball
Greg Wardb6528972000-09-07 02:40:37 +00001251from the right place installs the Distutils just as though you had
1252downloaded the source distribution and run \code{python setup.py
1253 install}. (The ``right place'' is either the root of the filesystem or
1254Python's \filevar{prefix} directory, depending on the options given to
1255the \command{bdist\_dumb} command; the default is to make dumb
1256distributions relative to \filevar{prefix}.)
Greg Ward46b98e32000-04-14 01:53:36 +00001257
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001258Obviously, for pure Python distributions, this isn't any simpler than
1259just running \code{python setup.py install}---but for non-pure
1260distributions, which include extensions that would need to be
1261compiled, it can mean the difference between someone being able to use
1262your extensions or not. And creating ``smart'' built distributions,
1263such as an RPM package or an executable installer for Windows, is far
1264more convenient for users even if your distribution doesn't include
1265any extensions.
Greg Ward46b98e32000-04-14 01:53:36 +00001266
Greg Wardb6528972000-09-07 02:40:37 +00001267The \command{bdist} command has a \longprogramopt{formats} option,
Greg Ward1d8f57a2000-08-05 00:43:11 +00001268similar to the \command{sdist} command, which you can use to select the
1269types of built distribution to generate: for example,
Fred Drakea09262e2001-03-01 18:35:43 +00001270
Greg Ward46b98e32000-04-14 01:53:36 +00001271\begin{verbatim}
1272python setup.py bdist --format=zip
1273\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001274
Fred Drakeeff9a872000-10-26 16:41:03 +00001275would, when run on a \UNIX{} system, create
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001276\file{Distutils-1.0.\filevar{plat}.zip}---again, this archive would be
Greg Ward1d8f57a2000-08-05 00:43:11 +00001277unpacked from the root directory to install the Distutils.
Greg Ward46b98e32000-04-14 01:53:36 +00001278
1279The available formats for built distributions are:
Fred Drake781380c2004-02-19 23:17:46 +00001280
Greg Ward46b98e32000-04-14 01:53:36 +00001281\begin{tableiii}{l|l|c}{code}%
1282 {Format}{Description}{Notes}
Greg Wardb6528972000-09-07 02:40:37 +00001283 \lineiii{gztar}{gzipped tar file (\file{.tar.gz})}{(1),(3)}
1284 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(3)}
1285 \lineiii{tar}{tar file (\file{.tar})}{(3)}
1286 \lineiii{zip}{zip file (\file{.zip})}{(4)}
1287 \lineiii{rpm}{RPM}{(5)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001288 \lineiii{pkgtool}{Solaris \program{pkgtool}}{}
1289 \lineiii{sdux}{HP-UX \program{swinstall}}{}
1290 \lineiii{rpm}{RPM}{(5)}
1291% \lineiii{srpm}{source RPM}{(5) \XXX{to do!}}
Thomas Heller5f52f722001-02-19 17:48:03 +00001292 \lineiii{wininst}{self-extracting ZIP file for Windows}{(2),(4)}
Greg Ward46b98e32000-04-14 01:53:36 +00001293\end{tableiii}
1294
1295\noindent Notes:
1296\begin{description}
Fred Drakeeff9a872000-10-26 16:41:03 +00001297\item[(1)] default on \UNIX
Greg Ward1d8f57a2000-08-05 00:43:11 +00001298\item[(2)] default on Windows \XXX{to-do!}
Greg Wardb6528972000-09-07 02:40:37 +00001299\item[(3)] requires external utilities: \program{tar} and possibly one
1300 of \program{gzip}, \program{bzip2}, or \program{compress}
1301\item[(4)] requires either external \program{zip} utility or
Greg Ward954ce8b2002-05-10 14:42:10 +00001302 \module{zipfile} module (part of the standard Python library since
1303 Python~1.6)
Greg Wardb6528972000-09-07 02:40:37 +00001304\item[(5)] requires external \program{rpm} utility, version 3.0.4 or
1305 better (use \code{rpm --version} to find out which version you have)
Greg Ward46b98e32000-04-14 01:53:36 +00001306\end{description}
1307
1308You don't have to use the \command{bdist} command with the
Greg Wardd5767a52000-04-19 22:48:09 +00001309\longprogramopt{formats} option; you can also use the command that
Greg Ward1d8f57a2000-08-05 00:43:11 +00001310directly implements the format you're interested in. Some of these
Greg Ward46b98e32000-04-14 01:53:36 +00001311\command{bdist} ``sub-commands'' actually generate several similar
1312formats; for instance, the \command{bdist\_dumb} command generates all
1313the ``dumb'' archive formats (\code{tar}, \code{ztar}, \code{gztar}, and
1314\code{zip}), and \command{bdist\_rpm} generates both binary and source
1315RPMs. The \command{bdist} sub-commands, and the formats generated by
1316each, are:
Fred Drake781380c2004-02-19 23:17:46 +00001317
Greg Ward46b98e32000-04-14 01:53:36 +00001318\begin{tableii}{l|l}{command}%
1319 {Command}{Formats}
1320 \lineii{bdist\_dumb}{tar, ztar, gztar, zip}
1321 \lineii{bdist\_rpm}{rpm, srpm}
Greg Ward1d8f57a2000-08-05 00:43:11 +00001322 \lineii{bdist\_wininst}{wininst}
Greg Ward46b98e32000-04-14 01:53:36 +00001323\end{tableii}
Greg Ward16aafcd2000-04-09 04:06:44 +00001324
Greg Wardb6528972000-09-07 02:40:37 +00001325The following sections give details on the individual \command{bdist\_*}
1326commands.
1327
1328
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00001329\section{Creating dumb built distributions}
Greg Wardb6528972000-09-07 02:40:37 +00001330\label{creating-dumb}
1331
1332\XXX{Need to document absolute vs. prefix-relative packages here, but
1333 first I have to implement it!}
1334
1335
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00001336\section{Creating RPM packages}
Greg Wardb6528972000-09-07 02:40:37 +00001337\label{creating-rpms}
1338
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001339The RPM format is used by many popular Linux distributions, including
Greg Wardb6528972000-09-07 02:40:37 +00001340Red Hat, SuSE, and Mandrake. If one of these (or any of the other
1341RPM-based Linux distributions) is your usual environment, creating RPM
1342packages for other users of that same distribution is trivial.
1343Depending on the complexity of your module distribution and differences
1344between Linux distributions, you may also be able to create RPMs that
1345work on different RPM-based distributions.
1346
1347The usual way to create an RPM of your module distribution is to run the
1348\command{bdist\_rpm} command:
Fred Drakea09262e2001-03-01 18:35:43 +00001349
Greg Wardb6528972000-09-07 02:40:37 +00001350\begin{verbatim}
1351python setup.py bdist_rpm
1352\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001353
Greg Wardb6528972000-09-07 02:40:37 +00001354or the \command{bdist} command with the \longprogramopt{format} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001355
Greg Wardb6528972000-09-07 02:40:37 +00001356\begin{verbatim}
1357python setup.py bdist --formats=rpm
1358\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001359
Greg Wardb6528972000-09-07 02:40:37 +00001360The former allows you to specify RPM-specific options; the latter allows
1361you to easily specify multiple formats in one run. If you need to do
1362both, you can explicitly specify multiple \command{bdist\_*} commands
1363and their options:
Fred Drakea09262e2001-03-01 18:35:43 +00001364
Greg Wardb6528972000-09-07 02:40:37 +00001365\begin{verbatim}
Fred Drake630e5bd2004-03-23 18:54:12 +00001366python setup.py bdist_rpm --packager="John Doe <jdoe@example.org>" \
Greg Wardb6528972000-09-07 02:40:37 +00001367 bdist_wininst --target_version="2.0"
1368\end{verbatim}
1369
1370Creating RPM packages is driven by a \file{.spec} file, much as using
1371the Distutils is driven by the setup script. To make your life easier,
1372the \command{bdist\_rpm} command normally creates a \file{.spec} file
1373based on the information you supply in the setup script, on the command
1374line, and in any Distutils configuration files. Various options and
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001375sections in the \file{.spec} file are derived from options in the setup
Greg Wardb6528972000-09-07 02:40:37 +00001376script as follows:
Fred Drake781380c2004-02-19 23:17:46 +00001377
Greg Wardb6528972000-09-07 02:40:37 +00001378\begin{tableii}{l|l}{textrm}%
1379 {RPM \file{.spec} file option or section}{Distutils setup script option}
1380 \lineii{Name}{\option{name}}
1381 \lineii{Summary (in preamble)}{\option{description}}
1382 \lineii{Version}{\option{version}}
1383 \lineii{Vendor}{\option{author} and \option{author\_email}, or \\&
1384 \option{maintainer} and \option{maintainer\_email}}
1385 \lineii{Copyright}{\option{licence}}
1386 \lineii{Url}{\option{url}}
1387 \lineii{\%description (section)}{\option{long\_description}}
1388\end{tableii}
1389
1390Additionally, there many options in \file{.spec} files that don't have
1391corresponding options in the setup script. Most of these are handled
1392through options to the \command{bdist\_rpm} command as follows:
Fred Drake781380c2004-02-19 23:17:46 +00001393
Greg Wardb6528972000-09-07 02:40:37 +00001394\begin{tableiii}{l|l|l}{textrm}%
1395 {RPM \file{.spec} file option or section}%
1396 {\command{bdist\_rpm} option}%
1397 {default value}
1398 \lineiii{Release}{\option{release}}{``1''}
1399 \lineiii{Group}{\option{group}}{``Development/Libraries''}
1400 \lineiii{Vendor}{\option{vendor}}{(see above)}
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001401 \lineiii{Packager}{\option{packager}}{(none)}
1402 \lineiii{Provides}{\option{provides}}{(none)}
1403 \lineiii{Requires}{\option{requires}}{(none)}
1404 \lineiii{Conflicts}{\option{conflicts}}{(none)}
1405 \lineiii{Obsoletes}{\option{obsoletes}}{(none)}
Greg Wardb6528972000-09-07 02:40:37 +00001406 \lineiii{Distribution}{\option{distribution\_name}}{(none)}
1407 \lineiii{BuildRequires}{\option{build\_requires}}{(none)}
1408 \lineiii{Icon}{\option{icon}}{(none)}
1409\end{tableiii}
Fred Drake781380c2004-02-19 23:17:46 +00001410
Greg Wardb6528972000-09-07 02:40:37 +00001411Obviously, supplying even a few of these options on the command-line
1412would be tedious and error-prone, so it's usually best to put them in
1413the setup configuration file, \file{setup.cfg}---see
1414section~\ref{setup-config}. If you distribute or package many Python
1415module distributions, you might want to put options that apply to all of
1416them in your personal Distutils configuration file
1417(\file{\textasciitilde/.pydistutils.cfg}).
1418
1419There are three steps to building a binary RPM package, all of which are
1420handled automatically by the Distutils:
Fred Drake781380c2004-02-19 23:17:46 +00001421
Greg Wardb6528972000-09-07 02:40:37 +00001422\begin{enumerate}
1423\item create a \file{.spec} file, which describes the package (analogous
1424 to the Distutils setup script; in fact, much of the information in the
1425 setup script winds up in the \file{.spec} file)
1426\item create the source RPM
1427\item create the ``binary'' RPM (which may or may not contain binary
1428 code, depending on whether your module distribution contains Python
1429 extensions)
1430\end{enumerate}
Fred Drake781380c2004-02-19 23:17:46 +00001431
Greg Wardb6528972000-09-07 02:40:37 +00001432Normally, RPM bundles the last two steps together; when you use the
1433Distutils, all three steps are typically bundled together.
1434
1435If you wish, you can separate these three steps. You can use the
Fred Drake781380c2004-02-19 23:17:46 +00001436\longprogramopt{spec-only} option to make \command{bdist_rpm} just
Greg Wardb6528972000-09-07 02:40:37 +00001437create the \file{.spec} file and exit; in this case, the \file{.spec}
1438file will be written to the ``distribution directory''---normally
1439\file{dist/}, but customizable with the \longprogramopt{dist-dir}
1440option. (Normally, the \file{.spec} file winds up deep in the ``build
Fred Drake781380c2004-02-19 23:17:46 +00001441tree,'' in a temporary directory created by \command{bdist_rpm}.)
Greg Wardb6528972000-09-07 02:40:37 +00001442
Fred Drake781380c2004-02-19 23:17:46 +00001443% \XXX{this isn't implemented yet---is it needed?!}
1444% You can also specify a custom \file{.spec} file with the
1445% \longprogramopt{spec-file} option; used in conjunction with
1446% \longprogramopt{spec-only}, this gives you an opportunity to customize
1447% the \file{.spec} file manually:
1448%
Matthias Klose4c8fa422004-08-04 23:18:49 +00001449% \ begin{verbatim}
Fred Drake781380c2004-02-19 23:17:46 +00001450% > python setup.py bdist_rpm --spec-only
1451% # ...edit dist/FooBar-1.0.spec
1452% > python setup.py bdist_rpm --spec-file=dist/FooBar-1.0.spec
Matthias Klose4c8fa422004-08-04 23:18:49 +00001453% \ end{verbatim}
Fred Drake781380c2004-02-19 23:17:46 +00001454%
1455% (Although a better way to do this is probably to override the standard
1456% \command{bdist\_rpm} command with one that writes whatever else you want
1457% to the \file{.spec} file.)
Greg Wardb6528972000-09-07 02:40:37 +00001458
1459
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00001460\section{Creating Windows Installers}
Greg Wardb6528972000-09-07 02:40:37 +00001461\label{creating-wininst}
1462
Thomas Hellere61f3652002-11-15 20:13:26 +00001463Executable installers are the natural format for binary distributions
1464on Windows. They display a nice graphical user interface, display
1465some information about the module distribution to be installed taken
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +00001466from the metadata in the setup script, let the user select a few
Thomas Hellere61f3652002-11-15 20:13:26 +00001467options, and start or cancel the installation.
Greg Wardb6528972000-09-07 02:40:37 +00001468
Thomas Hellere61f3652002-11-15 20:13:26 +00001469Since the metadata is taken from the setup script, creating Windows
1470installers is usually as easy as running:
Fred Drakea09262e2001-03-01 18:35:43 +00001471
Thomas Heller5f52f722001-02-19 17:48:03 +00001472\begin{verbatim}
1473python setup.py bdist_wininst
1474\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001475
Thomas Heller36343f62002-11-15 19:20:56 +00001476or the \command{bdist} command with the \longprogramopt{formats} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001477
Thomas Heller5f52f722001-02-19 17:48:03 +00001478\begin{verbatim}
1479python setup.py bdist --formats=wininst
1480\end{verbatim}
1481
Thomas Hellere61f3652002-11-15 20:13:26 +00001482If you have a pure module distribution (only containing pure Python
1483modules and packages), the resulting installer will be version
1484independent and have a name like \file{foo-1.0.win32.exe}. These
Fred Drakec54d9252004-02-19 22:16:05 +00001485installers can even be created on \UNIX{} or Mac OS platforms.
Thomas Heller5f52f722001-02-19 17:48:03 +00001486
1487If you have a non-pure distribution, the extensions can only be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001488created on a Windows platform, and will be Python version dependent.
Thomas Heller5f52f722001-02-19 17:48:03 +00001489The installer filename will reflect this and now has the form
Thomas Hellere61f3652002-11-15 20:13:26 +00001490\file{foo-1.0.win32-py2.0.exe}. You have to create a separate installer
Thomas Heller5f52f722001-02-19 17:48:03 +00001491for every Python version you want to support.
1492
1493The installer will try to compile pure modules into bytecode after
Thomas Hellere61f3652002-11-15 20:13:26 +00001494installation on the target system in normal and optimizing mode. If
1495you don't want this to happen for some reason, you can run the
Fred Drake0e9bfa32002-11-15 20:34:52 +00001496\command{bdist_wininst} command with the
1497\longprogramopt{no-target-compile} and/or the
1498\longprogramopt{no-target-optimize} option.
Thomas Hellere61f3652002-11-15 20:13:26 +00001499
Fred Drake0e9bfa32002-11-15 20:34:52 +00001500By default the installer will display the cool ``Python Powered'' logo
Thomas Hellere61f3652002-11-15 20:13:26 +00001501when it is run, but you can also supply your own bitmap which must be
Fred Drake0e9bfa32002-11-15 20:34:52 +00001502a Windows \file{.bmp} file with the \longprogramopt{bitmap} option.
Thomas Hellere61f3652002-11-15 20:13:26 +00001503
1504The installer will also display a large title on the desktop
1505background window when it is run, which is constructed from the name
1506of your distribution and the version number. This can be changed to
1507another text by using the \longprogramopt{title} option.
1508
1509The installer file will be written to the ``distribution directory''
1510--- normally \file{dist/}, but customizable with the
1511\longprogramopt{dist-dir} option.
1512
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00001513\subsection{The Postinstallation script}
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001514\label{postinstallation-script}
1515
1516Starting with Python 2.3, a postinstallation script can be specified
1517which the \longprogramopt{install-script} option. The basename of the
1518script must be specified, and the script filename must also be listed
1519in the scripts argument to the setup function.
1520
1521This script will be run at installation time on the target system
Fred Drakec54d9252004-02-19 22:16:05 +00001522after all the files have been copied, with \code{argv[1]} set to
1523\programopt{-install}, and again at uninstallation time before the
1524files are removed with \code{argv[1]} set to \programopt{-remove}.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001525
1526The installation script runs embedded in the windows installer, every
Fred Drakec54d9252004-02-19 22:16:05 +00001527output (\code{sys.stdout}, \code{sys.stderr}) is redirected into a
1528buffer and will be displayed in the GUI after the script has finished.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001529
Fred Drakea9ee0da2004-02-19 22:28:15 +00001530Some functions especially useful in this context are available as
1531additional built-in functions in the installation script.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001532
Fred Drakea9ee0da2004-02-19 22:28:15 +00001533\begin{funcdesc}{directory_created}{path}
1534\funcline{file_created}{path}
1535 These functions should be called when a directory or file is created
1536 by the postinstall script at installation time. It will register
1537 \var{path} with the uninstaller, so that it will be removed when the
1538 distribution is uninstalled. To be safe, directories are only removed
1539 if they are empty.
1540\end{funcdesc}
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001541
Fred Drakea9ee0da2004-02-19 22:28:15 +00001542\begin{funcdesc}{get_special_folder_path}{csidl_string}
1543 This function can be used to retrieve special folder locations on
1544 Windows like the Start Menu or the Desktop. It returns the full
1545 path to the folder. \var{csidl_string} must be one of the following
1546 strings:
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001547
1548\begin{verbatim}
1549"CSIDL_APPDATA"
1550
1551"CSIDL_COMMON_STARTMENU"
1552"CSIDL_STARTMENU"
1553
1554"CSIDL_COMMON_DESKTOPDIRECTORY"
1555"CSIDL_DESKTOPDIRECTORY"
1556
1557"CSIDL_COMMON_STARTUP"
1558"CSIDL_STARTUP"
1559
1560"CSIDL_COMMON_PROGRAMS"
1561"CSIDL_PROGRAMS"
1562
1563"CSIDL_FONTS"
1564\end{verbatim}
1565
Fred Drakea9ee0da2004-02-19 22:28:15 +00001566 If the folder cannot be retrieved, \exception{OSError} is raised.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001567
Fred Drakea9ee0da2004-02-19 22:28:15 +00001568 Which folders are available depends on the exact Windows version,
1569 and probably also the configuration. For details refer to
1570 Microsoft's documentation of the
1571 \cfunction{SHGetSpecialFolderPath()} function.
1572\end{funcdesc}
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001573
Fred Drakea9ee0da2004-02-19 22:28:15 +00001574\begin{funcdesc}{create_shortcut}{target, description,
1575 filename\optional{,
1576 arguments\optional{,
1577 workdir\optional{,
1578 iconpath\optional{, iconindex}}}}}
1579 This function creates a shortcut.
1580 \var{target} is the path to the program to be started by the shortcut.
1581 \var{description} is the description of the sortcut.
1582 \var{filename} is the title of the shortcut that the user will see.
1583 \var{arguments} specifies the command line arguments, if any.
1584 \var{workdir} is the working directory for the program.
1585 \var{iconpath} is the file containing the icon for the shortcut,
1586 and \var{iconindex} is the index of the icon in the file
1587 \var{iconpath}. Again, for details consult the Microsoft
1588 documentation for the \class{IShellLink} interface.
1589\end{funcdesc}
Greg Wardb6528972000-09-07 02:40:37 +00001590
Fred Drake211a2eb2004-03-22 21:44:43 +00001591\chapter{Registering with the Package Index}
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +00001592\label{package-index}
1593
1594The Python Package Index (PyPI) holds meta-data describing distributions
1595packaged with distutils. The distutils command \command{register} is
1596used to submit your distribution's meta-data to the index. It is invoked
1597as follows:
1598
1599\begin{verbatim}
1600python setup.py register
1601\end{verbatim}
1602
1603Distutils will respond with the following prompt:
1604
1605\begin{verbatim}
1606running register
1607We need to know who you are, so please choose either:
1608 1. use your existing login,
1609 2. register as a new user,
1610 3. have the server generate a new password for you (and email it to you), or
1611 4. quit
1612Your selection [default 1]:
1613\end{verbatim}
1614
1615\noindent Note: if your username and password are saved locally, you will
1616not see this menu.
1617
1618If you have not registered with PyPI, then you will need to do so now. You
1619should choose option 2, and enter your details as required. Soon after
1620submitting your details, you will receive an email which will be used to
1621confirm your registration.
1622
1623Once you are registered, you may choose option 1 from the menu. You will
1624be prompted for your PyPI username and password, and \command{register}
1625will then submit your meta-data to the index.
1626
1627You may submit any number of versions of your distribution to the index. If
1628you alter the meta-data for a particular version, you may submit it again
1629and the index will be updated.
1630
1631PyPI holds a record for each (name, version) combination submitted. The
1632first user to submit information for a given name is designated the Owner
1633of that name. They may submit changes through the \command{register}
1634command or through the web interface. They may also designate other users
1635as Owners or Maintainers. Maintainers may edit the package information, but
1636not designate other Owners or Maintainers.
1637
1638By default PyPI will list all versions of a given package. To hide certain
1639versions, the Hidden property should be set to yes. This must be edited
1640through the web interface.
1641
1642
1643
Fred Drake211a2eb2004-03-22 21:44:43 +00001644\chapter{Examples}
Greg Ward007c04a2002-05-10 14:45:59 +00001645\label{examples}
Fred Drake40333ce2004-06-14 22:07:50 +00001646
1647This chapter provides a number of basic examples to help get started
1648with distutils. Additional information about using distutils can be
1649found in the Distutils Cookbook.
1650
1651\begin{seealso}
1652 \seelink{http://www.python.org/cgi-bin/moinmoin/DistutilsCookbook}
1653 {Distutils Cookbook}
1654 {Collection of recipes showing how to achieve more control
1655 over distutils.}
1656\end{seealso}
1657
1658
Fred Drake211a2eb2004-03-22 21:44:43 +00001659\section{Pure Python distribution (by module)}
Greg Ward007c04a2002-05-10 14:45:59 +00001660\label{pure-mod}
1661
1662If you're just distributing a couple of modules, especially if they
1663don't live in a particular package, you can specify them individually
1664using the \option{py\_modules} option in the setup script.
1665
1666In the simplest case, you'll have two files to worry about: a setup
1667script and the single module you're distributing, \file{foo.py} in this
1668example:
1669\begin{verbatim}
1670<root>/
1671 setup.py
1672 foo.py
1673\end{verbatim}
1674(In all diagrams in this section, \verb|<root>| will refer to the
1675distribution root directory.) A minimal setup script to describe this
1676situation would be:
1677\begin{verbatim}
1678from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001679setup(name='foo',
1680 version='1.0',
1681 py_modules=['foo'],
1682 )
Greg Ward007c04a2002-05-10 14:45:59 +00001683\end{verbatim}
1684Note that the name of the distribution is specified independently with
1685the \option{name} option, and there's no rule that says it has to be the
1686same as the name of the sole module in the distribution (although that's
1687probably a good convention to follow). However, the distribution name
1688is used to generate filenames, so you should stick to letters, digits,
1689underscores, and hyphens.
1690
1691Since \option{py\_modules} is a list, you can of course specify multiple
1692modules, eg. if you're distributing modules \module{foo} and
1693\module{bar}, your setup might look like this:
1694\begin{verbatim}
1695<root>/
1696 setup.py
1697 foo.py
1698 bar.py
1699\end{verbatim}
1700and the setup script might be
1701\begin{verbatim}
1702from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001703setup(name='foobar',
1704 version='1.0',
1705 py_modules=['foo', 'bar'],
1706 )
Greg Ward007c04a2002-05-10 14:45:59 +00001707\end{verbatim}
1708
1709You can put module source files into another directory, but if you have
1710enough modules to do that, it's probably easier to specify modules by
1711package rather than listing them individually.
Greg Ward16aafcd2000-04-09 04:06:44 +00001712
1713
Fred Drake211a2eb2004-03-22 21:44:43 +00001714\section{Pure Python distribution (by package)}
Greg Ward007c04a2002-05-10 14:45:59 +00001715\label{pure-pkg}
1716
1717If you have more than a couple of modules to distribute, especially if
1718they are in multiple packages, it's probably easier to specify whole
1719packages rather than individual modules. This works even if your
1720modules are not in a package; you can just tell the Distutils to process
1721modules from the root package, and that works the same as any other
1722package (except that you don't have to have an \file{\_\_init\_\_.py}
1723file).
1724
1725The setup script from the last example could also be written as
1726\begin{verbatim}
1727from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001728setup(name='foobar',
1729 version='1.0',
1730 packages=[''],
1731 )
Greg Ward007c04a2002-05-10 14:45:59 +00001732\end{verbatim}
1733(The empty string stands for the root package.)
1734
1735If those two files are moved into a subdirectory, but remain in the root
1736package, e.g.:
1737\begin{verbatim}
1738<root>/
1739 setup.py
1740 src/ foo.py
1741 bar.py
1742\end{verbatim}
1743then you would still specify the root package, but you have to tell the
1744Distutils where source files in the root package live:
1745\begin{verbatim}
1746from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001747setup(name='foobar',
1748 version='1.0',
1749 package_dir={'': 'src'},
1750 packages=[''],
1751 )
Greg Ward007c04a2002-05-10 14:45:59 +00001752\end{verbatim}
1753
1754More typically, though, you will want to distribute multiple modules in
1755the same package (or in sub-packages). For example, if the \module{foo}
1756and \module{bar} modules belong in package \module{foobar}, one way to
1757layout your source tree is
1758\begin{verbatim}
1759<root>/
1760 setup.py
1761 foobar/
1762 __init__.py
1763 foo.py
1764 bar.py
1765\end{verbatim}
1766This is in fact the default layout expected by the Distutils, and the
1767one that requires the least work to describe in your setup script:
1768\begin{verbatim}
1769from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001770setup(name='foobar',
1771 version='1.0',
1772 packages=['foobar'],
1773 )
Greg Ward007c04a2002-05-10 14:45:59 +00001774\end{verbatim}
1775
1776If you want to put modules in directories not named for their package,
1777then you need to use the \option{package\_dir} option again. For
1778example, if the \file{src} directory holds modules in the
1779\module{foobar} package:
1780\begin{verbatim}
1781<root>/
1782 setup.py
1783 src/
1784 __init__.py
1785 foo.py
1786 bar.py
1787\end{verbatim}
1788an appropriate setup script would be
1789\begin{verbatim}
1790from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001791setup(name='foobar',
1792 version='1.0',
1793 package_dir={'foobar': 'src'},
1794 packages=['foobar'],
1795 )
Greg Ward007c04a2002-05-10 14:45:59 +00001796\end{verbatim}
1797
1798Or, you might put modules from your main package right in the
1799distribution root:
1800\begin{verbatim}
1801<root>/
1802 setup.py
1803 __init__.py
1804 foo.py
1805 bar.py
1806\end{verbatim}
1807in which case your setup script would be
1808\begin{verbatim}
1809from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001810setup(name='foobar',
1811 version='1.0',
1812 package_dir={'foobar': ''},
1813 packages=['foobar'],
1814 )
Greg Ward007c04a2002-05-10 14:45:59 +00001815\end{verbatim}
1816(The empty string also stands for the current directory.)
1817
1818If you have sub-packages, they must be explicitly listed in
1819\option{packages}, but any entries in \option{package\_dir}
1820automatically extend to sub-packages. (In other words, the Distutils
1821does \emph{not} scan your source tree, trying to figure out which
1822directories correspond to Python packages by looking for
1823\file{\_\_init\_\_.py} files.) Thus, if the default layout grows a
1824sub-package:
1825\begin{verbatim}
1826<root>/
1827 setup.py
1828 foobar/
1829 __init__.py
1830 foo.py
1831 bar.py
1832 subfoo/
1833 __init__.py
1834 blah.py
1835\end{verbatim}
1836then the corresponding setup script would be
1837\begin{verbatim}
1838from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001839setup(name='foobar',
1840 version='1.0',
1841 packages=['foobar', 'foobar.subfoo'],
1842 )
Greg Ward007c04a2002-05-10 14:45:59 +00001843\end{verbatim}
1844(Again, the empty string in \option{package\_dir} stands for the current
1845directory.)
Greg Ward16aafcd2000-04-09 04:06:44 +00001846
1847
Fred Drake211a2eb2004-03-22 21:44:43 +00001848\section{Single extension module}
Greg Ward007c04a2002-05-10 14:45:59 +00001849\label{single-ext}
1850
1851Extension modules are specified using the \option{ext\_modules} option.
1852\option{package\_dir} has no effect on where extension source files are
1853found; it only affects the source for pure Python modules. The simplest
1854case, a single extension module in a single C source file, is:
1855\begin{verbatim}
1856<root>/
1857 setup.py
1858 foo.c
1859\end{verbatim}
1860If the \module{foo} extension belongs in the root package, the setup
1861script for this could be
1862\begin{verbatim}
1863from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001864setup(name='foobar',
1865 version='1.0',
1866 ext_modules=[Extension('foo', ['foo.c'])],
1867 )
Greg Ward007c04a2002-05-10 14:45:59 +00001868\end{verbatim}
1869
1870If the extension actually belongs in a package, say \module{foopkg},
1871then
1872
1873With exactly the same source tree layout, this extension can be put in
1874the \module{foopkg} package simply by changing the name of the
1875extension:
1876\begin{verbatim}
1877from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001878setup(name='foobar',
1879 version='1.0',
1880 ext_modules=[Extension('foopkg.foo', ['foo.c'])],
1881 )
Greg Ward007c04a2002-05-10 14:45:59 +00001882\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +00001883
1884
Fred Drake211a2eb2004-03-22 21:44:43 +00001885%\section{Multiple extension modules}
Fred Drakea09262e2001-03-01 18:35:43 +00001886%\label{multiple-ext}
Greg Ward16aafcd2000-04-09 04:06:44 +00001887
1888
Fred Drake211a2eb2004-03-22 21:44:43 +00001889%\section{Putting it all together}
Greg Ward16aafcd2000-04-09 04:06:44 +00001890
1891
Fred Drake0c84c7f2004-08-02 21:39:11 +00001892\chapter{Extending Distutils \label{extending}}
1893
1894Distutils can be extended in various ways. Most extensions take the
1895form of new commands or replacements for existing commands. New
1896commands may be written to support new types of platform-specific
1897packaging, for example, while replacements for existing commands may
1898be made to modify details of how the command operates on a package.
1899
1900Most extensions of the distutils are made within \file{setup.py}
1901scripts that want to modify existing commands; many simply add a few
1902file extensions that should be copied into packages in addition to
1903\file{.py} files as a convenience.
1904
1905Most distutils command implementations are subclasses of the
1906\class{Command} class from \refmodule{distutils.cmd}. New commands
1907may directly inherit from \class{Command}, while replacements often
1908derive from \class{Command} indirectly, directly subclassing the
Fred Drakebec69f62004-08-02 23:05:25 +00001909command they are replacing. Commands are required to derive from
1910\class{Command}.
Greg Ward4a9e7222000-04-25 02:57:36 +00001911
1912
Fred Drake211a2eb2004-03-22 21:44:43 +00001913%\section{Extending existing commands}
Fred Drakea09262e2001-03-01 18:35:43 +00001914%\label{extend-existing}
Greg Ward4a9e7222000-04-25 02:57:36 +00001915
1916
Fred Drake211a2eb2004-03-22 21:44:43 +00001917%\section{Writing new commands}
Fred Drakea09262e2001-03-01 18:35:43 +00001918%\label{new-commands}
Greg Ward4a9e7222000-04-25 02:57:36 +00001919
Fred Drakea09262e2001-03-01 18:35:43 +00001920%\XXX{Would an uninstall command be a good example here?}
Thomas Heller5f52f722001-02-19 17:48:03 +00001921
Fred Drake0c84c7f2004-08-02 21:39:11 +00001922\section{Integrating new commands}
1923
1924There are different ways to integrate new command implementations into
1925distutils. The most difficult is to lobby for the inclusion of the
1926new features in distutils itself, and wait for (and require) a version
1927of Python that provides that support. This is really hard for many
1928reasons.
1929
1930The most common, and possibly the most reasonable for most needs, is
1931to include the new implementations with your \file{setup.py} script,
1932and cause the \function{distutils.core.setup()} function use them:
1933
1934\begin{verbatim}
1935from distutils.command.build_py import build_py as _build_py
1936from distutils.core import setup
1937
1938class build_py(_build_py):
1939 """Specialized Python source builder."""
1940
1941 # implement whatever needs to be different...
1942
1943setup(cmdclass={'build_py': build_py},
1944 ...)
1945\end{verbatim}
1946
1947This approach is most valuable if the new implementations must be used
1948to use a particular package, as everyone interested in the package
1949will need to have the new command implementation.
Greg Ward4a9e7222000-04-25 02:57:36 +00001950
Fred Draked04573f2004-08-03 16:37:40 +00001951Beginning with Python 2.4, a third option is available, intended to
1952allow new commands to be added which can support existing
1953\file{setup.py} scripts without requiring modifications to the Python
1954installation. This is expected to allow third-party extensions to
1955provide support for additional packaging systems, but the commands can
1956be used for anything distutils commands can be used for. A new
1957configuration option, \option{command\_packages} (command-line option
1958\longprogramopt{command-packages}), can be used to specify additional
1959packages to be searched for modules implementing commands. Like all
1960distutils options, this can be specified on the command line or in a
1961configuration file. This option can only be set in the
1962\code{[global]} section of a configuration file, or before any
1963commands on the command line. If set in a configuration file, it can
1964be overridden from the command line; setting it to an empty string on
1965the command line causes the default to be used. This should never be
1966set in a configuration file provided with a package.
1967
1968This new option can be used to add any number of packages to the list
1969of packages searched for command implementations; multiple package
1970names should be separated by commas. When not specified, the search
1971is only performed in the \module{distutils.command} package. When
1972\file{setup.py} is run with the option
1973\longprogramopt{command-packages} \programopt{distcmds,buildcmds},
1974however, the packages \module{distutils.command}, \module{distcmds},
1975and \module{buildcmds} will be searched in that order. New commands
1976are expected to be implemented in modules of the same name as the
1977command by classes sharing the same name. Given the example command
1978line option above, the command \command{bdist\_openpkg} could be
1979implemented by the class \class{distcmds.bdist_openpkg.bdist_openpkg}
1980or \class{buildcmds.bdist_openpkg.bdist_openpkg}.
1981
Greg Ward4a9e7222000-04-25 02:57:36 +00001982
Fred Drake211a2eb2004-03-22 21:44:43 +00001983\chapter{Command Reference}
Greg Ward47f99a62000-09-04 20:07:15 +00001984\label{reference}
Greg Ward16aafcd2000-04-09 04:06:44 +00001985
1986
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00001987%\section{Building modules: the \protect\command{build} command family}
Fred Drakea09262e2001-03-01 18:35:43 +00001988%\label{build-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001989
Fred Drakea09262e2001-03-01 18:35:43 +00001990%\subsubsection{\protect\command{build}}
1991%\label{build-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001992
Fred Drakea09262e2001-03-01 18:35:43 +00001993%\subsubsection{\protect\command{build\_py}}
1994%\label{build-py-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001995
Fred Drakea09262e2001-03-01 18:35:43 +00001996%\subsubsection{\protect\command{build\_ext}}
1997%\label{build-ext-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001998
Fred Drakea09262e2001-03-01 18:35:43 +00001999%\subsubsection{\protect\command{build\_clib}}
2000%\label{build-clib-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00002001
2002
Fred Drake211a2eb2004-03-22 21:44:43 +00002003\section{Installing modules: the \protect\command{install} command family}
Greg Warde78298a2000-04-28 17:12:24 +00002004\label{install-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00002005
Gregory P. Smith147e5f32000-05-12 00:58:18 +00002006The install command ensures that the build commands have been run and then
2007runs the subcommands \command{install\_lib},
2008\command{install\_data} and
2009\command{install\_scripts}.
2010
Fred Drakea09262e2001-03-01 18:35:43 +00002011%\subsubsection{\protect\command{install\_lib}}
2012%\label{install-lib-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00002013
Fred Drake211a2eb2004-03-22 21:44:43 +00002014\subsection{\protect\command{install\_data}}
Greg Ward1365a302000-08-31 14:47:05 +00002015\label{install-data-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00002016This command installs all data files provided with the distribution.
2017
Fred Drake211a2eb2004-03-22 21:44:43 +00002018\subsection{\protect\command{install\_scripts}}
Greg Ward1365a302000-08-31 14:47:05 +00002019\label{install-scripts-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00002020This command installs all (Python) scripts in the distribution.
2021
Greg Ward16aafcd2000-04-09 04:06:44 +00002022
Fred Drakea09262e2001-03-01 18:35:43 +00002023%\subsection{Cleaning up: the \protect\command{clean} command}
2024%\label{clean-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00002025
2026
Fred Drake211a2eb2004-03-22 21:44:43 +00002027\section{Creating a source distribution: the
Fred Drakeeff9a872000-10-26 16:41:03 +00002028 \protect\command{sdist} command}
Greg Warde78298a2000-04-28 17:12:24 +00002029\label{sdist-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00002030
2031
2032\XXX{fragment moved down from above: needs context!}
Greg Wardb6528972000-09-07 02:40:37 +00002033
Greg Ward16aafcd2000-04-09 04:06:44 +00002034The manifest template commands are:
Fred Drake781380c2004-02-19 23:17:46 +00002035
Greg Ward16aafcd2000-04-09 04:06:44 +00002036\begin{tableii}{ll}{command}{Command}{Description}
Greg Ward87da1ea2000-04-21 04:35:25 +00002037 \lineii{include \var{pat1} \var{pat2} ... }
2038 {include all files matching any of the listed patterns}
2039 \lineii{exclude \var{pat1} \var{pat2} ... }
2040 {exclude all files matching any of the listed patterns}
2041 \lineii{recursive-include \var{dir} \var{pat1} \var{pat2} ... }
2042 {include all files under \var{dir} matching any of the listed patterns}
2043 \lineii{recursive-exclude \var{dir} \var{pat1} \var{pat2} ...}
2044 {exclude all files under \var{dir} matching any of the listed patterns}
2045 \lineii{global-include \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00002046 {include all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00002047 any of the listed patterns}
2048 \lineii{global-exclude \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00002049 {exclude all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00002050 any of the listed patterns}
Greg Ward16aafcd2000-04-09 04:06:44 +00002051 \lineii{prune \var{dir}}{exclude all files under \var{dir}}
2052 \lineii{graft \var{dir}}{include all files under \var{dir}}
2053\end{tableii}
Fred Drake781380c2004-02-19 23:17:46 +00002054
Fred Drakeeff9a872000-10-26 16:41:03 +00002055The patterns here are \UNIX-style ``glob'' patterns: \code{*} matches any
Greg Ward16aafcd2000-04-09 04:06:44 +00002056sequence of regular filename characters, \code{?} matches any single
2057regular filename character, and \code{[\var{range}]} matches any of the
2058characters in \var{range} (e.g., \code{a-z}, \code{a-zA-Z},
Greg Wardfacb8db2000-04-09 04:32:40 +00002059\code{a-f0-9\_.}). The definition of ``regular filename character'' is
Fred Drakeeff9a872000-10-26 16:41:03 +00002060platform-specific: on \UNIX{} it is anything except slash; on Windows
Brett Cannon7706c2d2005-02-13 22:50:04 +00002061anything except backslash or colon; on Mac OS 9 anything except colon.
Greg Wardb6528972000-09-07 02:40:37 +00002062
Brett Cannon7706c2d2005-02-13 22:50:04 +00002063\XXX{Windows support not there yet}
Greg Ward16aafcd2000-04-09 04:06:44 +00002064
2065
Fred Drake211a2eb2004-03-22 21:44:43 +00002066%\section{Creating a built distribution: the
Fred Drakea09262e2001-03-01 18:35:43 +00002067% \protect\command{bdist} command family}
2068%\label{bdist-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00002069
2070
Fred Drake211a2eb2004-03-22 21:44:43 +00002071%\subsection{\protect\command{bdist}}
Greg Ward16aafcd2000-04-09 04:06:44 +00002072
Fred Drake211a2eb2004-03-22 21:44:43 +00002073%\subsection{\protect\command{bdist\_dumb}}
Greg Ward16aafcd2000-04-09 04:06:44 +00002074
Fred Drake211a2eb2004-03-22 21:44:43 +00002075%\subsection{\protect\command{bdist\_rpm}}
Greg Ward16aafcd2000-04-09 04:06:44 +00002076
Fred Drake211a2eb2004-03-22 21:44:43 +00002077%\subsection{\protect\command{bdist\_wininst}}
Fred Drakeab70b382001-08-02 15:13:15 +00002078
2079
Fred Drake6fca7cc2004-03-23 18:43:03 +00002080\chapter{API Reference \label{api-reference}}
2081
2082\section{\module{distutils.core} --- Core Distutils functionality}
2083
2084\declaremodule{standard}{distutils.core}
2085\modulesynopsis{The core Distutils functionality}
2086
2087The \module{distutils.core} module is the only module that needs to be
2088installed to use the Distutils. It provides the \function{setup()} (which
2089is called from the setup script). Indirectly provides the
2090\class{distutils.dist.Distribution} and \class{distutils.cmd.Command} class.
2091
2092\begin{funcdesc}{setup}{arguments}
2093The basic do-everything function that does most everything you could ever
2094ask for from a Distutils method. See XXXXX
2095
2096The setup function takes a large number of arguments. These
2097are laid out in the following table.
2098
2099\begin{tableiii}{c|l|l}{argument name}{argument name}{value}{type}
2100\lineiii{name}{The name of the package}{a string}
2101\lineiii{version}{The version number of the package}{See \refmodule{distutils.version}}
2102\lineiii{description}{A single line describing the package}{a string}
2103\lineiii{long_description}{Longer description of the package}{a string}
2104\lineiii{author}{The name of the package author}{a string}
2105\lineiii{author_email}{The email address of the package author}{a string}
2106\lineiii{maintainer}{The name of the current maintainer, if different from the author}{a string}
2107\lineiii{maintainer_email}{The email address of the current maintainer, if different from the author}{}
2108\lineiii{url}{A URL for the package (homepage)}{a URL}
2109\lineiii{download_url}{A URL to download the package}{a URL}
2110\lineiii{packages}{A list of Python packages that distutils will manipulate}{a list of strings}
2111\lineiii{py_modules}{A list of Python modules that distutils will manipulate}{a list of strings}
2112\lineiii{scripts}{A list of standalone script files to be built and installed}{a list of strings}
2113\lineiii{ext_modules}{A list of Python extensions to be built}{A list of
2114instances of \class{distutils.core.Extension}}
2115\lineiii{classifiers}{A list of Trove categories for the package}{XXX link to better definition}
2116\lineiii{distclass}{the \class{Distribution} class to use}{A subclass of \class{distutils.core.Distribution}}
2117% What on earth is the use case for script_name?
2118\lineiii{script_name}{The name of the setup.py script - defaults to \code{sys.argv[0]}}{a string}
2119\lineiii{script_args}{Arguments to supply to the setup script}{a list of strings}
2120\lineiii{options}{default options for the setup script}{a string}
2121\lineiii{license}{The license for the package}{}
2122\lineiii{keywords}{Descriptive meta-data. See \pep{314}}{}
2123\lineiii{platforms}{}{}
2124\lineiii{cmdclass}{A mapping of command names to \class{Command} subclasses}{a dictionary}
2125\end{tableiii}
2126
2127\end{funcdesc}
2128
2129\begin{funcdesc}{run_setup}{script_name\optional{, script_args=\code{None}, stop_after=\code{'run'}}}
2130Run a setup script in a somewhat controlled environment, and return
2131the \class{distutils.dist.Distribution} instance that drives things.
2132This is useful if you need to find out the distribution meta-data
2133(passed as keyword args from \var{script} to \function{setup()}), or
2134the contents of the config files or command-line.
2135
2136\var{script_name} is a file that will be run with \function{execfile()}
2137\var{sys.argv[0]} will be replaced with \var{script} for the duration of the
2138call. \var{script_args} is a list of strings; if supplied,
2139\var{sys.argv[1:]} will be replaced by \var{script_args} for the duration
2140of the call.
2141
2142\var{stop_after} tells \function{setup()} when to stop processing; possible
2143values:
2144
2145\begin{tableii}{c|l}{value}{value}{description}
2146\lineii{init}{Stop after the \class{Distribution} instance has been created
2147and populated with the keyword arguments to \function{setup()}}
2148\lineii{config}{Stop after config files have been parsed (and their data
2149stored in the \class{Distribution} instance)}
2150\lineii{commandline}{Stop after the command-line (\code{sys.argv[1:]} or
2151\var{script_args}) have been parsed (and the data stored in the
2152\class{Distribution} instance.)}
2153\lineii{run}{Stop after all commands have been run (the same as
2154if \function{setup()} had been called in the usual way). This is the default
2155value.}
2156\end{tableii}
2157\end{funcdesc}
2158
2159In addition, the \module{distutils.core} module exposed a number of
2160classes that live elsewhere.
2161
2162\begin{itemize}
2163\item \class{Extension} from \refmodule{distutils.extension}
2164\item \class{Command} from \refmodule{distutils.cmd}
2165\item \class{Distribution} from \refmodule{distutils.dist}
2166\end{itemize}
2167
2168A short description of each of these follows, but see the relevant
2169module for the full reference.
2170
2171\begin{classdesc*}{Extension}
2172
2173The Extension class describes a single C or \Cpp extension module in a
2174setup script. It accepts the following keyword arguments in it's
2175constructor
2176
2177\begin{tableiii}{c|l|l}{argument name}{argument name}{value}{type}
2178\lineiii{name}{the full name of the extension, including any packages
2179--- ie. \emph{not} a filename or pathname, but Python dotted name}{string}
2180\lineiii{sources}{list of source filenames, relative to the distribution
2181root (where the setup script lives), in Unix form (slash-separated) for
2182portability. Source files may be C, \Cpp, SWIG (.i), platform-specific
2183resource files, or whatever else is recognized by the \command{build_ext}
2184command as source for a Python extension.}{string}
2185\lineiii{include_dirs}{list of directories to search for C/\Cpp{} header
2186files (in \UNIX{} form for portability)}{string}
2187\lineiii{define_macros}{list of macros to define; each macro is defined
2188using a 2-tuple, where 'value' is either the string to define it to or
2189\code{None} to define it without a particular value (equivalent of
2190\code{\#define FOO} in source or \programopt{-DFOO} on \UNIX{} C
2191compiler command line) }{ (string,string)
2192tuple or (name,\code{None}) }
2193\lineiii{undef_macros}{list of macros to undefine explicitly}{string}
2194\lineiii{library_dirs}{list of directories to search for C/\Cpp{} libraries
2195at link time }{string}
2196\lineiii{libraries}{list of library names (not filenames or paths) to
2197link against }{string}
2198\lineiii{runtime_library_dirs}{list of directories to search for C/\Cpp{}
2199libraries at run time (for shared extensions, this is when the extension
2200is loaded)}{string}
2201\lineiii{extra_objects}{list of extra files to link with (eg. object
2202files not implied by 'sources', static library that must be explicitly
2203specified, binary resource files, etc.)}{string}
2204\lineiii{extra_compile_args}{any extra platform- and compiler-specific
2205information to use when compiling the source files in 'sources'. For
2206platforms and compilers where a command line makes sense, this is
2207typically a list of command-line arguments, but for other platforms it
2208could be anything.}{string}
2209\lineiii{extra_link_args}{any extra platform- and compiler-specific
2210information to use when linking object files together to create the
2211extension (or to create a new static Python interpreter). Similar
2212interpretation as for 'extra_compile_args'.}{string}
2213\lineiii{export_symbols}{list of symbols to be exported from a shared
2214extension. Not used on all platforms, and not generally necessary for
2215Python extensions, which typically export exactly one symbol: \code{init} +
2216extension_name. }{string}
2217\lineiii{depends}{list of files that the extension depends on }{string}
2218\lineiii{language}{extension language (i.e. \code{'c'}, \code{'c++'},
2219\code{'objc'}). Will be detected from the source extensions if not provided.
2220}{string}
2221\end{tableiii}
2222\end{classdesc*}
2223
2224\begin{classdesc*}{Distribution}
2225A \class{Distribution} describes how to build, install and package up a
2226Python software package.
2227
2228See the \function{setup()} function for a list of keyword arguments accepted
2229by the Distribution constructor. \function{setup()} creates a Distribution
2230instance.
2231\end{classdesc*}
2232
2233\begin{classdesc*}{Command}
2234A \class{Command} class (or rather, an instance of one of it's subclasses)
2235implement a single distutils command.
2236\end{classdesc*}
2237
2238
2239\section{\module{distutils.ccompiler} --- CCompiler base class}
2240\declaremodule{standard}{distutils.ccompiler}
2241\modulesynopsis{Abstract CCompiler class}
2242
2243This module provides the abstract base class for the \class{CCompiler}
2244classes. A \class{CCompiler} instance can be used for all the compile
2245and link steps needed to build a single project. Methods are provided to
2246set options for the compiler --- macro definitions, include directories,
2247link path, libraries and the like.
2248
2249This module provides the following functions.
2250
2251\begin{funcdesc}{gen_lib_options}{compiler, library_dirs, runtime_library_dirs, libraries}
2252Generate linker options for searching library directories and
2253linking with specific libraries. \var{libraries} and \var{library_dirs} are,
2254respectively, lists of library names (not filenames!) and search
2255directories. Returns a list of command-line options suitable for use
2256with some compiler (depending on the two format strings passed in).
2257\end{funcdesc}
2258
2259\begin{funcdesc}{gen_preprocess_options}{macros, include_dirs}
2260Generate C pre-processor options (-D, -U, -I) as used by at least
2261two types of compilers: the typical \UNIX{} compiler and Visual \Cpp.
2262\var{macros} is the usual thing, a list of 1- or 2-tuples, where \var{(name,)}
2263means undefine (-U) macro \var{name}, and \var{(name,value)} means define (-D)
2264macro \var{name} to \var{value}. \var{include_dirs} is just a list of directory
2265names to be added to the header file search path (-I). Returns a list
2266of command-line options suitable for either \UNIX{} compilers or Visual
2267\Cpp.
2268\end{funcdesc}
2269
2270\begin{funcdesc}{get_default_compiler}{osname, platform}
2271Determine the default compiler to use for the given platform.
2272
2273\var{osname} should be one of the standard Python OS names (i.e. the
2274ones returned by \var{os.name}) and \var{platform} the common value
2275returned by \var{sys.platform} for the platform in question.
2276
2277The default values are \code{os.name} and \code{sys.platform} in case the
2278parameters are not given.
2279\end{funcdesc}
2280
2281\begin{funcdesc}{new_compiler}{plat=\code{None}, compiler=\code{None}, verbose=\code{0}, dry_run=\code{0}, force=\code{0}}
2282Factory function to generate an instance of some CCompiler subclass
2283for the supplied platform/compiler combination. \var{plat} defaults
2284to \code{os.name} (eg. \code{'posix'}, \code{'nt'}), and \var{compiler}
2285defaults to the default compiler for that platform. Currently only
2286\code{'posix'} and \code{'nt'} are supported, and the default
2287compilers are ``traditional \UNIX{} interface'' (\class{UnixCCompiler}
2288class) and Visual \Cpp (\class{MSVCCompiler} class). Note that it's
2289perfectly possible to ask for a \UNIX{} compiler object under Windows,
2290and a Microsoft compiler object under \UNIX---if you supply a value
2291for \var{compiler}, \var{plat} is ignored.
2292% Is the posix/nt only thing still true? Mac OS X seems to work, and
2293% returns a UnixCCompiler instance. How to document this... hmm.
2294\end{funcdesc}
2295
2296\begin{funcdesc}{show_compilers}{}
2297Print list of available compilers (used by the
2298\longprogramopt{help-compiler} options to \command{build},
2299\command{build_ext}, \command{build_clib}).
2300\end{funcdesc}
2301
2302\begin{classdesc}{CCompiler}{\optional{verbose=\code{0}, dry_run=\code{0}, force=\code{0}}}
2303
2304The abstract base class \class{CCompiler} defines the interface that
2305must be implemented by real compiler classes. The class also has
2306some utility methods used by several compiler classes.
2307
2308The basic idea behind a compiler abstraction class is that each
2309instance can be used for all the compile/link steps in building a
2310single project. Thus, attributes common to all of those compile and
2311link steps --- include directories, macros to define, libraries to link
2312against, etc. --- are attributes of the compiler instance. To allow for
2313variability in how individual files are treated, most of those
2314attributes may be varied on a per-compilation or per-link basis.
2315
2316The constructor for each subclass creates an instance of the Compiler
2317object. Flags are \var{verbose} (show verbose output), \var{dry_run}
2318(don't actually execute the steps) and \var{force} (rebuild
2319everything, regardless of dependencies). All of these flags default to
2320\code{0} (off). Note that you probably don't want to instantiate
2321\class{CCompiler} or one of it's subclasses directly - use the
2322\function{distutils.CCompiler.new_compiler()} factory function
2323instead.
2324
2325The following methods allow you to manually alter compiler options for
2326the instance of the Compiler class.
2327
2328\begin{methoddesc}{add_include_dir}{dir}
2329Add \var{dir} to the list of directories that will be searched for
2330header files. The compiler is instructed to search directories in
2331the order in which they are supplied by successive calls to
2332\method{add_include_dir()}.
2333\end{methoddesc}
2334
2335\begin{methoddesc}{set_include_dirs}{dirs}
2336Set the list of directories that will be searched to \var{dirs} (a
2337list of strings). Overrides any preceding calls to
2338\method{add_include_dir()}; subsequent calls to
2339\method{add_include_dir()} add to the list passed to
2340\method{set_include_dirs()}. This does not affect any list of
2341standard include directories that the compiler may search by default.
2342\end{methoddesc}
2343
2344\begin{methoddesc}{add_library}{libname}
2345
2346Add \var{libname} to the list of libraries that will be included in
2347all links driven by this compiler object. Note that \var{libname}
2348should *not* be the name of a file containing a library, but the
2349name of the library itself: the actual filename will be inferred by
2350the linker, the compiler, or the compiler class (depending on the
2351platform).
2352
2353The linker will be instructed to link against libraries in the
2354order they were supplied to \method{add_library()} and/or
2355\method{set_libraries()}. It is perfectly valid to duplicate library
2356names; the linker will be instructed to link against libraries as
2357many times as they are mentioned.
2358\end{methoddesc}
2359
2360\begin{methoddesc}{set_libraries}{libnames}
2361Set the list of libraries to be included in all links driven by
2362this compiler object to \var{libnames} (a list of strings). This does
2363not affect any standard system libraries that the linker may
2364include by default.
2365\end{methoddesc}
2366
2367\begin{methoddesc}{add_library_dir}{dir}
2368Add \var{dir} to the list of directories that will be searched for
2369libraries specified to \method{add_library()} and
2370\method{set_libraries()}. The linker will be instructed to search for
2371libraries in the order they are supplied to \method{add_library_dir()}
2372and/or \method{set_library_dirs()}.
2373\end{methoddesc}
2374
2375\begin{methoddesc}{set_library_dirs}{dirs}
2376Set the list of library search directories to \var{dirs} (a list of
2377strings). This does not affect any standard library search path
2378that the linker may search by default.
2379\end{methoddesc}
2380
2381\begin{methoddesc}{add_runtime_library_dir}{dir}
2382Add \var{dir} to the list of directories that will be searched for
2383shared libraries at runtime.
2384\end{methoddesc}
2385
2386\begin{methoddesc}{set_runtime_library_dirs}{dirs}
2387Set the list of directories to search for shared libraries at
2388runtime to \var{dirs} (a list of strings). This does not affect any
2389standard search path that the runtime linker may search by
2390default.
2391\end{methoddesc}
2392
2393\begin{methoddesc}{define_macro}{name\optional{, value=\code{None}}}
2394Define a preprocessor macro for all compilations driven by this
2395compiler object. The optional parameter \var{value} should be a
2396string; if it is not supplied, then the macro will be defined
2397without an explicit value and the exact outcome depends on the
2398compiler used (XXX true? does ANSI say anything about this?)
2399\end{methoddesc}
2400
2401\begin{methoddesc}{undefine_macro}{name}
2402Undefine a preprocessor macro for all compilations driven by
2403this compiler object. If the same macro is defined by
2404\method{define_macro()} and undefined by \method{undefine_macro()}
2405the last call takes precedence (including multiple redefinitions or
2406undefinitions). If the macro is redefined/undefined on a
2407per-compilation basis (ie. in the call to \method{compile()}), then that
2408takes precedence.
2409\end{methoddesc}
2410
2411\begin{methoddesc}{add_link_object}{object}
2412Add \var{object} to the list of object files (or analogues, such as
2413explicitly named library files or the output of ``resource
2414compilers'') to be included in every link driven by this compiler
2415object.
2416\end{methoddesc}
2417
2418\begin{methoddesc}{set_link_objects}{objects}
2419Set the list of object files (or analogues) to be included in
2420every link to \var{objects}. This does not affect any standard object
2421files that the linker may include by default (such as system
2422libraries).
2423\end{methoddesc}
2424
2425The following methods implement methods for autodetection of compiler
2426options, providing some functionality similar to GNU \program{autoconf}.
2427
2428\begin{methoddesc}{detect_language}{sources}
2429Detect the language of a given file, or list of files. Uses the
2430instance attributes \member{language_map} (a dictionary), and
2431\member{language_order} (a list) to do the job.
2432\end{methoddesc}
2433
2434\begin{methoddesc}{find_library_file}{dirs, lib\optional{, debug=\code{0}}}
2435Search the specified list of directories for a static or shared
2436library file \var{lib} and return the full path to that file. If
2437\var{debug} is true, look for a debugging version (if that makes sense on
2438the current platform). Return \code{None} if \var{lib} wasn't found in any of
2439the specified directories.
2440\end{methoddesc}
2441
2442\begin{methoddesc}{has_function}{funcname \optional{, includes=\code{None}, include_dirs=\code{None}, libraries=\code{None}, library_dirs=\code{None}}}
2443Return a boolean indicating whether \var{funcname} is supported on
2444the current platform. The optional arguments can be used to
2445augment the compilation environment by providing additional include
2446files and paths and libraries and paths.
2447\end{methoddesc}
2448
2449\begin{methoddesc}{library_dir_option}{dir}
2450Return the compiler option to add \var{dir} to the list of
2451directories searched for libraries.
2452\end{methoddesc}
2453
2454\begin{methoddesc}{library_option}{lib}
2455Return the compiler option to add \var{dir} to the list of libraries
2456linked into the shared library or executable.
2457\end{methoddesc}
2458
2459\begin{methoddesc}{runtime_library_dir_option}{dir}
2460Return the compiler option to add \var{dir} to the list of
2461directories searched for runtime libraries.
2462\end{methoddesc}
2463
2464\begin{methoddesc}{set_executables}{**args}
2465Define the executables (and options for them) that will be run
2466to perform the various stages of compilation. The exact set of
2467executables that may be specified here depends on the compiler
2468class (via the 'executables' class attribute), but most will have:
2469
2470\begin{tableii}{l|l}{attribute}{attribute}{description}
2471\lineii{compiler}{the C/\Cpp{} compiler}
2472\lineii{linker_so}{linker used to create shared objects and libraries}
2473\lineii{linker_exe}{linker used to create binary executables}
2474\lineii{archiver}{static library creator}
2475\end{tableii}
2476
2477On platforms with a command-line (\UNIX, DOS/Windows), each of these
2478is a string that will be split into executable name and (optional)
2479list of arguments. (Splitting the string is done similarly to how
2480\UNIX{} shells operate: words are delimited by spaces, but quotes and
2481backslashes can override this. See
2482\function{distutils.util.split_quoted()}.)
2483\end{methoddesc}
2484
2485The following methods invoke stages in the build process.
2486
2487\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}}}
2488Compile one or more source files. Generates object files (e.g.
2489transforms a \file{.c} file to a \file{.o} file.)
2490
2491\var{sources} must be a list of filenames, most likely C/\Cpp
2492files, but in reality anything that can be handled by a
2493particular compiler and compiler class (eg. \class{MSVCCompiler} can
2494handle resource files in \var{sources}). Return a list of object
2495filenames, one per source filename in \var{sources}. Depending on
2496the implementation, not all source files will necessarily be
2497compiled, but all corresponding object filenames will be
2498returned.
2499
2500If \var{output_dir} is given, object files will be put under it, while
2501retaining their original path component. That is, \file{foo/bar.c}
2502normally compiles to \file{foo/bar.o} (for a \UNIX{} implementation); if
2503\var{output_dir} is \var{build}, then it would compile to
2504\file{build/foo/bar.o}.
2505
2506\var{macros}, if given, must be a list of macro definitions. A macro
2507definition is either a \var{(name, value)} 2-tuple or a \var{(name,)} 1-tuple.
2508The former defines a macro; if the value is \code{None}, the macro is
2509defined without an explicit value. The 1-tuple case undefines a
2510macro. Later definitions/redefinitions/undefinitions take
2511precedence.
2512
2513\var{include_dirs}, if given, must be a list of strings, the
2514directories to add to the default include file search path for this
2515compilation only.
2516
2517\var{debug} is a boolean; if true, the compiler will be instructed to
2518output debug symbols in (or alongside) the object file(s).
2519
2520\var{extra_preargs} and \var{extra_postargs} are implementation- dependent.
2521On platforms that have the notion of a command-line (e.g. \UNIX,
2522DOS/Windows), they are most likely lists of strings: extra
Raymond Hettinger68804312005-01-01 00:28:46 +00002523command-line arguments to prepend/append to the compiler command
Fred Drake6fca7cc2004-03-23 18:43:03 +00002524line. On other platforms, consult the implementation class
2525documentation. In any event, they are intended as an escape hatch
2526for those occasions when the abstract compiler framework doesn't
2527cut the mustard.
2528
2529\var{depends}, if given, is a list of filenames that all targets
2530depend on. If a source file is older than any file in
2531depends, then the source file will be recompiled. This
2532supports dependency tracking, but only at a coarse
2533granularity.
2534
2535Raises \exception{CompileError} on failure.
2536\end{methoddesc}
2537
2538\begin{methoddesc}{create_static_lib}{objects, output_libname\optional{, output_dir=\code{None}, debug=\code{0}, target_lang=\code{None}}}
2539Link a bunch of stuff together to create a static library file.
2540The ``bunch of stuff'' consists of the list of object files supplied
2541as \var{objects}, the extra object files supplied to
2542\method{add_link_object()} and/or \method{set_link_objects()}, the libraries
2543supplied to \method{add_library()} and/or \method{set_libraries()}, and the
2544libraries supplied as \var{libraries} (if any).
2545
2546\var{output_libname} should be a library name, not a filename; the
2547filename will be inferred from the library name. \var{output_dir} is
2548the directory where the library file will be put. XXX defaults to what?
2549
2550\var{debug} is a boolean; if true, debugging information will be
2551included in the library (note that on most platforms, it is the
2552compile step where this matters: the \var{debug} flag is included here
2553just for consistency).
2554
2555\var{target_lang} is the target language for which the given objects
2556are being compiled. This allows specific linkage time treatment of
2557certain languages.
2558
2559Raises \exception{LibError} on failure.
2560\end{methoddesc}
2561
2562\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}}}
2563Link a bunch of stuff together to create an executable or
2564shared library file.
2565
2566The ``bunch of stuff'' consists of the list of object files supplied
2567as \var{objects}. \var{output_filename} should be a filename. If
2568\var{output_dir} is supplied, \var{output_filename} is relative to it
2569(i.e. \var{output_filename} can provide directory components if
2570needed).
2571
2572\var{libraries} is a list of libraries to link against. These are
2573library names, not filenames, since they're translated into
2574filenames in a platform-specific way (eg. \var{foo} becomes \file{libfoo.a}
2575on \UNIX{} and \file{foo.lib} on DOS/Windows). However, they can include a
2576directory component, which means the linker will look in that
2577specific directory rather than searching all the normal locations.
2578
2579\var{library_dirs}, if supplied, should be a list of directories to
2580search for libraries that were specified as bare library names
2581(ie. no directory component). These are on top of the system
2582default and those supplied to \method{add_library_dir()} and/or
2583\method{set_library_dirs()}. \var{runtime_library_dirs} is a list of
2584directories that will be embedded into the shared library and used
2585to search for other shared libraries that *it* depends on at
2586run-time. (This may only be relevant on \UNIX.)
2587
2588\var{export_symbols} is a list of symbols that the shared library will
2589export. (This appears to be relevant only on Windows.)
2590
2591\var{debug} is as for \method{compile()} and \method{create_static_lib()},
2592with the slight distinction that it actually matters on most platforms (as
2593opposed to \method{create_static_lib()}, which includes a \var{debug} flag
2594mostly for form's sake).
2595
2596\var{extra_preargs} and \var{extra_postargs} are as for \method{compile()}
2597(except of course that they supply command-line arguments for the
2598particular linker being used).
2599
2600\var{target_lang} is the target language for which the given objects
2601are being compiled. This allows specific linkage time treatment of
2602certain languages.
2603
2604Raises \exception{LinkError} on failure.
2605\end{methoddesc}
2606
2607\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}}}
2608Link an executable.
2609\var{output_progname} is the name of the file executable,
2610while \var{objects} are a list of object filenames to link in. Other arguments
2611are as for the \method{link} method.
2612\end{methoddesc}
2613
2614\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}}}
2615Link a shared library. \var{output_libname} is the name of the output
2616library, while \var{objects} is a list of object filenames to link in.
2617Other arguments are as for the \method{link} method.
2618\end{methoddesc}
2619
2620\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}}}
2621Link a shared object. \var{output_filename} is the name of the shared object
2622that will be created, while \var{objects} is a list of object filenames
2623to link in. Other arguments are as for the \method{link} method.
2624\end{methoddesc}
2625
2626\begin{methoddesc}{preprocess}{source\optional{, output_file=\code{None}, macros=\code{None}, include_dirs=\code{None}, extra_preargs=\code{None}, extra_postargs=\code{None}}}
2627Preprocess a single C/\Cpp{} source file, named in \var{source}.
2628Output will be written to file named \var{output_file}, or \var{stdout} if
2629\var{output_file} not supplied. \var{macros} is a list of macro
2630definitions as for \method{compile()}, which will augment the macros set
2631with \method{define_macro()} and \method{undefine_macro()}.
2632\var{include_dirs} is a list of directory names that will be added to the
2633default list, in the same way as \method{add_include_dir()}.
2634
2635Raises \exception{PreprocessError} on failure.
2636\end{methoddesc}
2637
2638The following utility methods are defined by the \class{CCompiler} class,
2639for use by the various concrete subclasses.
2640
2641\begin{methoddesc}{executable_filename}{basename\optional{, strip_dir=\code{0}, output_dir=\code{''}}}
2642Returns the filename of the executable for the given \var{basename}.
2643Typically for non-Windows platforms this is the same as the basename,
2644while Windows will get a \file{.exe} added.
2645\end{methoddesc}
2646
2647\begin{methoddesc}{library_filename}{libname\optional{, lib_type=\code{'static'}, strip_dir=\code{0}, output_dir=\code{''}}}
2648Returns the filename for the given library name on the current platform.
2649On \UNIX{} a library with \var{lib_type} of \code{'static'} will typically
2650be of the form \file{liblibname.a}, while a \var{lib_type} of \code{'dynamic'}
2651will be of the form \file{liblibname.so}.
2652\end{methoddesc}
2653
2654\begin{methoddesc}{object_filenames}{source_filenames\optional{, strip_dir=\code{0}, output_dir=\code{''}}}
2655Returns the name of the object files for the given source files.
2656\var{source_filenames} should be a list of filenames.
2657\end{methoddesc}
2658
2659\begin{methoddesc}{shared_object_filename}{basename\optional{, strip_dir=\code{0}, output_dir=\code{''}}}
2660Returns the name of a shared object file for the given file name \var{basename}.
2661\end{methoddesc}
2662
2663\begin{methoddesc}{execute}{func, args\optional{, msg=\code{None}, level=\code{1}}}
2664Invokes \function{distutils.util.execute()} This method invokes a
2665Python function \var{func} with the given arguments \var{args}, after
2666logging and taking into account the \var{dry_run} flag. XXX see also.
2667\end{methoddesc}
2668
2669\begin{methoddesc}{spawn}{cmd}
2670Invokes \function{distutils.util.spawn()}. This invokes an external
2671process to run the given command. XXX see also.
2672\end{methoddesc}
2673
2674\begin{methoddesc}{mkpath}{name\optional{, mode=\code{511}}}
2675
2676Invokes \function{distutils.dir_util.mkpath()}. This creates a directory
2677and any missing ancestor directories. XXX see also.
2678\end{methoddesc}
2679
2680\begin{methoddesc}{move_file}{src, dst}
2681Invokes \method{distutils.file_util.move_file()}. Renames \var{src} to
2682\var{dst}. XXX see also.
2683\end{methoddesc}
2684
2685\begin{methoddesc}{announce}{msg\optional{, level=\code{1}}}
2686Write a message using \function{distutils.log.debug()}. XXX see also.
2687\end{methoddesc}
2688
2689\begin{methoddesc}{warn}{msg}
2690Write a warning message \var{msg} to standard error.
2691\end{methoddesc}
2692
2693\begin{methoddesc}{debug_print}{msg}
2694If the \var{debug} flag is set on this \class{CCompiler} instance, print
2695\var{msg} to standard output, otherwise do nothing.
2696\end{methoddesc}
2697
2698\end{classdesc}
2699
2700%\subsection{Compiler-specific modules}
2701%
2702%The following modules implement concrete subclasses of the abstract
2703%\class{CCompiler} class. They should not be instantiated directly, but should
2704%be created using \function{distutils.ccompiler.new_compiler()} factory
2705%function.
2706
2707\section{\module{distutils.unixccompiler} --- Unix C Compiler}
2708\declaremodule{standard}{distutils.unixccompiler}
2709\modulesynopsis{UNIX C Compiler}
2710
2711This module provides the \class{UnixCCompiler} class, a subclass of
2712\class{CCompiler} that handles the typical \UNIX-style command-line
2713C compiler:
2714
2715\begin{itemize}
2716\item macros defined with \programopt{-D\var{name}\optional{=value}}
2717\item macros undefined with \programopt{-U\var{name}}
2718\item include search directories specified with
2719 \programopt{-I\var{dir}}
2720\item libraries specified with \programopt{-l\var{lib}}
2721\item library search directories specified with \programopt{-L\var{dir}}
2722\item compile handled by \program{cc} (or similar) executable with
2723 \programopt{-c} option: compiles \file{.c} to \file{.o}
2724\item link static library handled by \program{ar} command (possibly
2725 with \program{ranlib})
2726\item link shared library handled by \program{cc} \programopt{-shared}
2727\end{itemize}
2728
2729\section{\module{distutils.msvccompiler} --- Microsoft Compiler}
2730\declaremodule{standard}{distutils.msvccompiler}
2731\modulesynopsis{Microsoft Compiler}
2732
2733This module provides \class{MSVCCompiler}, an implementation of the abstract
2734\class{CCompiler} class for Microsoft Visual Studio. It should also work using
2735the freely available compiler provided as part of the .Net SDK download. XXX
2736download link.
2737
2738\section{\module{distutils.bcppcompiler} --- Borland Compiler}
2739\declaremodule{standard}{distutils.bcppcompiler}
2740This module provides \class{BorlandCCompiler}, an subclass of the abstract \class{CCompiler} class for the Borland \Cpp{} compiler.
2741
2742\section{\module{distutils.cygwincompiler} --- Cygwin Compiler}
2743\declaremodule{standard}{distutils.cygwinccompiler}
2744
2745This module provides the \class{CygwinCCompiler} class, a subclass of \class{UnixCCompiler} that
2746handles the Cygwin port of the GNU C compiler to Windows. It also contains
2747the Mingw32CCompiler class which handles the mingw32 port of GCC (same as
2748cygwin in no-cygwin mode).
2749
2750\section{\module{distutils.emxccompiler} --- OS/2 EMX Compiler}
2751\declaremodule{standard}{distutils.emxccompiler}
2752\modulesynopsis{OS/2 EMX Compiler support}
2753
2754This module provides the EMXCCompiler class, a subclass of \class{UnixCCompiler} that handles the EMX port of the GNU C compiler to OS/2.
2755
2756\section{\module{distutils.mwerkscompiler} --- Metrowerks CodeWarrior support}
2757\declaremodule{standard}{distutils.mwerkscompiler}
2758\modulesynopsis{Metrowerks CodeWarrior support}
2759
2760Contains \class{MWerksCompiler}, an implementation of the abstract
Brett Cannon7706c2d2005-02-13 22:50:04 +00002761\class{CCompiler} class for MetroWerks CodeWarrior on the pre-Mac OS X Macintosh.
2762Needs work to support CW on Windows or Mac OS X.
Fred Drake6fca7cc2004-03-23 18:43:03 +00002763
2764
2765%\subsection{Utility modules}
2766%
2767%The following modules all provide general utility functions. They haven't
2768%all been documented yet.
2769
2770\section{\module{distutils.archive_util} ---
2771 Archiving utilities}
2772\declaremodule[distutils.archiveutil]{standard}{distutils.archive_util}
2773\modulesynopsis{Utility functions for creating archive files (tarballs, zip files, ...)}
2774
2775This module provides a few functions for creating archive files, such as
2776tarballs or zipfiles.
2777
2778\begin{funcdesc}{make_archive}{base_name, format\optional{, root_dir=\code{None}, base_dir=\code{None}, verbose=\code{0}, dry_run=\code{0}}}
2779Create an archive file (eg. \code{zip} or \code{tar}). \var{base_name}
2780is the name of the file to create, minus any format-specific extension;
2781\var{format} is the archive format: one of \code{zip}, \code{tar},
2782\code{ztar}, or \code{gztar}.
2783\var{root_dir} is a directory that will be the root directory of the
2784archive; ie. we typically \code{chdir} into \var{root_dir} before
2785creating the archive. \var{base_dir} is the directory where we start
2786archiving from; ie. \var{base_dir} will be the common prefix of all files and
2787directories in the archive. \var{root_dir} and \var{base_dir} both default
2788to the current directory. Returns the name of the archive file.
2789
2790\warning{This should be changed to support bz2 files}
2791\end{funcdesc}
2792
2793\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),
2794\code{'compress'}, \code{'bzip2'}, or \code{None}. Both \code{'tar'}
2795and the compression utility named by \var{'compress'} must be on the
2796default program search path, so this is probably \UNIX-specific. The
2797output tar file will be named \file{\var{base_dir}.tar}, possibly plus
2798the appropriate compression extension (\file{.gz}, \file{.bz2} or
2799\file{.Z}). Return the output filename.
2800
2801\warning{This should be replaced with calls to the \module{tarfile} module.}
2802\end{funcdesc}
2803
2804\begin{funcdesc}{make_zipfile}{base_name, base_dir\optional{, verbose=\code{0}, dry_run=\code{0}}}
2805Create a zip file from all files in and under \var{base_dir}. The output
2806zip file will be named \var{base_dir} + \file{.zip}. Uses either the
2807\module{zipfile} Python module (if available) or the InfoZIP \file{zip}
2808utility (if installed and found on the default search path). If neither
2809tool is available, raises \exception{DistutilsExecError}.
2810Returns the name of the output zip file.
2811\end{funcdesc}
2812
2813\section{\module{distutils.dep_util} --- Dependency checking}
2814\declaremodule[distutils.deputil]{standard}{distutils.dep_util}
2815\modulesynopsis{Utility functions for simple dependency checking}
2816
2817This module provides functions for performing simple, timestamp-based
2818dependency of files and groups of files; also, functions based entirely
2819on such timestamp dependency analysis.
2820
2821\begin{funcdesc}{newer}{source, target}
2822Return true if \var{source} exists and is more recently modified than
2823\var{target}, or if \var{source} exists and \var{target} doesn't.
2824Return false if both exist and \var{target} is the same age or newer
2825than \var{source}.
2826Raise \exception{DistutilsFileError} if \var{source} does not exist.
2827\end{funcdesc}
2828
2829\begin{funcdesc}{newer_pairwise}{sources, targets}
2830Walk two filename lists in parallel, testing if each source is newer
2831than its corresponding target. Return a pair of lists (\var{sources},
2832\var{targets}) where source is newer than target, according to the semantics
2833of \function{newer()}
2834%% equivalent to a listcomp...
2835\end{funcdesc}
2836
2837\begin{funcdesc}{newer_group}{sources, target\optional{, missing=\code{'error'}}}
2838Return true if \var{target} is out-of-date with respect to any file
2839listed in \var{sources} In other words, if \var{target} exists and is newer
2840than every file in \var{sources}, return false; otherwise return true.
2841\var{missing} controls what we do when a source file is missing; the
2842default (\code{'error'}) is to blow up with an \exception{OSError} from
2843inside \function{os.stat()};
2844if it is \code{'ignore'}, we silently drop any missing source files; if it is
2845\code{'newer'}, any missing source files make us assume that \var{target} is
2846out-of-date (this is handy in ``dry-run'' mode: it'll make you pretend to
2847carry out commands that wouldn't work because inputs are missing, but
2848that doesn't matter because you're not actually going to run the
2849commands).
2850\end{funcdesc}
2851
2852\section{\module{distutils.dir_util} --- Directory tree operations}
2853\declaremodule[distutils.dirutil]{standard}{distutils.dir_util}
2854\modulesynopsis{Utility functions for operating on directories and directory trees}
2855
2856This module provides functions for operating on directories and trees
2857of directories.
2858
2859\begin{funcdesc}{mkpath}{name\optional{, mode=\code{0777}, verbose=\code{0}, dry_run=\code{0}}}
2860Create a directory and any missing ancestor directories. If the
2861directory already exists (or if \var{name} is the empty string, which
2862means the current directory, which of course exists), then do
2863nothing. Raise \exception{DistutilsFileError} if unable to create some
2864directory along the way (eg. some sub-path exists, but is a file
2865rather than a directory). If \var{verbose} is true, print a one-line
2866summary of each mkdir to stdout. Return the list of directories
2867actually created.
2868\end{funcdesc}
2869
2870\begin{funcdesc}{create_tree}{base_dir, files\optional{, mode=\code{0777}, verbose=\code{0}, dry_run=\code{0}}}
2871Create all the empty directories under \var{base_dir} needed to
2872put \var{files} there. \var{base_dir} is just the a name of a directory
2873which doesn't necessarily exist yet; \var{files} is a list of filenames
2874to be interpreted relative to \var{base_dir}. \var{base_dir} + the
2875directory portion of every file in \var{files} will be created if it
2876doesn't already exist. \var{mode}, \var{verbose} and \var{dry_run} flags
2877are as for \function{mkpath()}.
2878\end{funcdesc}
2879
2880\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}}}
2881Copy an entire directory tree \var{src} to a new location \var{dst}. Both
2882\var{src} and \var{dst} must be directory names. If \var{src} is not a
2883directory, raise \exception{DistutilsFileError}. If \var{dst} does
2884not exist, it is created with \var{mkpath()}. The end result of the
2885copy is that every file in \var{src} is copied to \var{dst}, and
2886directories under \var{src} are recursively copied to \var{dst}.
2887Return the list of files that were copied or might have been copied,
2888using their output name. The return value is unaffected by \var{update}
2889or \var{dry_run}: it is simply the list of all files under \var{src},
2890with the names changed to be under \var{dst}.
2891
2892\var{preserve_mode} and \var{preserve_times} are the same as for
2893\function{copy_file} in \refmodule[distutils.fileutil]{distutils.file_util};
2894note that they only apply to regular files, not to directories. If
2895\var{preserve_symlinks} is true, symlinks will be copied as symlinks
2896(on platforms that support them!); otherwise (the default), the
2897destination of the symlink will be copied. \var{update} and
2898\var{verbose} are the same as for
2899\function{copy_file()}.
2900\end{funcdesc}
2901
2902\begin{funcdesc}{remove_tree}{directory\optional{verbose=\code{0}, dry_run=\code{0}}}
2903Recursively remove \var{directory} and all files and directories underneath
2904it. Any errors are ignored (apart from being reported to \code{stdout} if
2905\var{verbose} is true).
2906\end{funcdesc}
2907
2908\XXX{Some of this could be replaced with the shutil module?}
2909
2910\section{\module{distutils.file_util} --- Single file operations}
2911\declaremodule[distutils.fileutil]{standard}{distutils.file_util}
2912\modulesynopsis{Utility functions for operating on single files}
2913
2914This module contains some utility functions for operating on individual files.
2915
2916\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}}}
2917Copy file \var{src} to \var{dst}. If \var{dst} is a directory, then
2918\var{src} is copied there with the same name; otherwise, it must be a
2919filename. (If the file exists, it will be ruthlessly clobbered.) If
2920\var{preserve_mode} is true (the default), the file's mode (type and
2921permission bits, or whatever is analogous on the current platform) is
2922copied. If \var{preserve_times} is true (the default), the last-modified
2923and last-access times are copied as well. If \var{update} is true,
2924\var{src} will only be copied if \var{dst} does not exist, or if
2925\var{dst} does exist but is older than \var{src}.
2926
2927\var{link} allows you to make hard links (using \function{os.link}) or
2928symbolic links (using \function{os.symlink}) instead of copying: set it
2929to \code{'hard'} or \code{'sym'}; if it is \code{None} (the default),
2930files are copied. Don't set \var{link} on systems that don't support
2931it: \function{copy_file()} doesn't check if hard or symbolic linking is
Andrew M. Kuchling7219cbe2004-08-07 21:35:06 +00002932available. It uses \var{_copy_file_contents()} to copy file contents.
Fred Drake6fca7cc2004-03-23 18:43:03 +00002933
2934Return a tuple \samp{(dest_name, copied)}: \var{dest_name} is the actual
2935name of the output file, and \var{copied} is true if the file was copied
2936(or would have been copied, if \var{dry_run} true).
2937% XXX if the destination file already exists, we clobber it if
2938% copying, but blow up if linking. Hmmm. And I don't know what
2939% macostools.copyfile() does. Should definitely be consistent, and
2940% should probably blow up if destination exists and we would be
2941% changing it (ie. it's not already a hard/soft link to src OR
2942% (not update) and (src newer than dst)).
2943\end{funcdesc}
2944
2945\begin{funcdesc}{move_file}{src, dst\optional{verbose, dry_run}}
2946Move file \var{src} to \var{dst}. If \var{dst} is a directory, the file will
2947be moved into it with the same name; otherwise, \var{src} is just renamed
2948to \var{dst}. Returns the new full name of the file.
2949\warning{Handles cross-device moves on Unix using \function{copy_file()}.
2950What about other systems???}
2951\end{funcdesc}
2952
2953\begin{funcdesc}{write_file}{filename, contents}
2954Create a file called \var{filename} and write \var{contents} (a
2955sequence of strings without line terminators) to it.
2956\end{funcdesc}
2957
Thomas Heller949f6612004-06-18 06:55:28 +00002958\section{\module{distutils.util} --- Miscellaneous other utility functions}
Fred Drake6fca7cc2004-03-23 18:43:03 +00002959\declaremodule{standard}{distutils.util}
2960\modulesynopsis{Miscellaneous other utility functions}
2961
2962This module contains other assorted bits and pieces that don't fit into
2963any other utility module.
2964
2965\begin{funcdesc}{get_platform}{}
2966Return a string that identifies the current platform. This is used
2967mainly to distinguish platform-specific build directories and
2968platform-specific built distributions. Typically includes the OS name
2969and version and the architecture (as supplied by 'os.uname()'),
2970although the exact information included depends on the OS; eg. for IRIX
2971the architecture isn't particularly important (IRIX only runs on SGI
2972hardware), but for Linux the kernel version isn't particularly
2973important.
2974
2975Examples of returned values:
2976\begin{itemize}
2977\item \code{linux-i586}
2978\item \code{linux-alpha}
2979\item \code{solaris-2.6-sun4u}
2980\item \code{irix-5.3}
2981\item \code{irix64-6.2}
2982\end{itemize}
2983
2984For non-\POSIX{} platforms, currently just returns \code{sys.platform}.
2985% XXX isn't this also provided by some other non-distutils module?
2986\end{funcdesc}
2987
2988\begin{funcdesc}{convert_path}{pathname}
2989Return 'pathname' as a name that will work on the native filesystem,
2990i.e. split it on '/' and put it back together again using the current
2991directory separator. Needed because filenames in the setup script are
2992always supplied in Unix style, and have to be converted to the local
2993convention before we can actually use them in the filesystem. Raises
2994\exception{ValueError} on non-\UNIX-ish systems if \var{pathname} either
2995starts or ends with a slash.
2996\end{funcdesc}
2997
2998\begin{funcdesc}{change_root}{new_root, pathname}
2999Return \var{pathname} with \var{new_root} prepended. If \var{pathname} is
3000relative, this is equivalent to \samp{os.path.join(new_root,pathname)}
3001Otherwise, it requires making \var{pathname} relative and then joining the
Brett Cannon7706c2d2005-02-13 22:50:04 +00003002two, which is tricky on DOS/Windows.
Fred Drake6fca7cc2004-03-23 18:43:03 +00003003\end{funcdesc}
3004
3005\begin{funcdesc}{check_environ}{}
3006Ensure that 'os.environ' has all the environment variables we
3007guarantee that users can use in config files, command-line options,
3008etc. Currently this includes:
3009\begin{itemize}
3010\item \envvar{HOME} - user's home directory (\UNIX{} only)
3011\item \envvar{PLAT} - description of the current platform, including
3012 hardware and OS (see \function{get_platform()})
3013\end{itemize}
3014\end{funcdesc}
3015
3016\begin{funcdesc}{subst_vars}{s, local_vars}
3017Perform shell/Perl-style variable substitution on \var{s}. Every
3018occurrence of \code{\$} followed by a name is considered a variable, and
3019variable is substituted by the value found in the \var{local_vars}
3020dictionary, or in \code{os.environ} if it's not in \var{local_vars}.
3021\var{os.environ} is first checked/augmented to guarantee that it contains
3022certain values: see \function{check_environ()}. Raise \exception{ValueError}
3023for any variables not found in either \var{local_vars} or \code{os.environ}.
3024
3025Note that this is not a fully-fledged string interpolation function. A
3026valid \code{\$variable} can consist only of upper and lower case letters,
3027numbers and an underscore. No \{ \} or \( \) style quoting is available.
3028\end{funcdesc}
3029
3030\begin{funcdesc}{grok_environment_error}{exc\optional{, prefix=\samp{'error: '}}}
3031Generate a useful error message from an \exception{EnvironmentError}
3032(\exception{IOError} or \exception{OSError}) exception object.
3033Handles Python 1.5.1 and later styles, and does what it can to deal with
3034exception objects that don't have a filename (which happens when the error
3035is due to a two-file operation, such as \function{rename()} or
3036\function{link()}). Returns the error message as a string prefixed
3037with \var{prefix}.
3038\end{funcdesc}
3039
3040\begin{funcdesc}{split_quoted}{s}
3041Split a string up according to Unix shell-like rules for quotes and
3042backslashes. In short: words are delimited by spaces, as long as those
3043spaces are not escaped by a backslash, or inside a quoted string.
3044Single and double quotes are equivalent, and the quote characters can
3045be backslash-escaped. The backslash is stripped from any two-character
3046escape sequence, leaving only the escaped character. The quote
3047characters are stripped from any quoted string. Returns a list of
3048words.
3049% Should probably be moved into the standard library.
3050\end{funcdesc}
3051
3052\begin{funcdesc}{execute}{func, args\optional{, msg=\code{None}, verbose=\code{0}, dry_run=\code{0}}}
3053Perform some action that affects the outside world (for instance,
3054writing to the filesystem). Such actions are special because they
3055are disabled by the \var{dry_run} flag. This method takes
3056care of all that bureaucracy for you; all you have to do is supply the
3057function to call and an argument tuple for it (to embody the
3058``external action'' being performed), and an optional message to
3059print.
3060\end{funcdesc}
3061
3062\begin{funcdesc}{strtobool}{val}
3063Convert a string representation of truth to true (1) or false (0).
3064
3065True values are \code{y}, \code{yes}, \code{t}, \code{true}, \code{on}
3066and \code{1}; false values are \code{n}, \code{no}, \code{f}, \code{false},
3067\code{off} and \code{0}. Raises \exception{ValueError} if \var{val}
3068is anything else.
3069\end{funcdesc}
3070
3071\begin{funcdesc}{byte_compile}{py_files\optional{,
3072 optimize=\code{0}, force=\code{0},
3073 prefix=\code{None}, base_dir=\code{None},
3074 verbose=\code{1}, dry_run=\code{0},
3075 direct=\code{None}}}
3076Byte-compile a collection of Python source files to either \file{.pyc}
3077or \file{.pyo} files in the same directory. \var{py_files} is a list of files
3078to compile; any files that don't end in \file{.py} are silently skipped.
3079\var{optimize} must be one of the following:
3080\begin{itemize}
3081\item \code{0} - don't optimize (generate \file{.pyc})
3082\item \code{1} - normal optimization (like \samp{python -O})
3083\item \code{2} - extra optimization (like \samp{python -OO})
3084\end{itemize}
3085
3086If \var{force} is true, all files are recompiled regardless of
3087timestamps.
3088
3089The source filename encoded in each bytecode file defaults to the
3090filenames listed in \var{py_files}; you can modify these with \var{prefix} and
3091\var{basedir}. \var{prefix} is a string that will be stripped off of each
3092source filename, and \var{base_dir} is a directory name that will be
3093prepended (after \var{prefix} is stripped). You can supply either or both
3094(or neither) of \var{prefix} and \var{base_dir}, as you wish.
3095
3096If \var{dry_run} is true, doesn't actually do anything that would
3097affect the filesystem.
3098
3099Byte-compilation is either done directly in this interpreter process
3100with the standard \module{py_compile} module, or indirectly by writing a
3101temporary script and executing it. Normally, you should let
3102\function{byte_compile()} figure out to use direct compilation or not (see
3103the source for details). The \var{direct} flag is used by the script
3104generated in indirect mode; unless you know what you're doing, leave
3105it set to \code{None}.
3106\end{funcdesc}
3107
3108\begin{funcdesc}{rfc822_escape}{header}
3109Return a version of \var{header} escaped for inclusion in an
3110\rfc{822} header, by ensuring there are 8 spaces space after each newline.
3111Note that it does no other modification of the string.
3112% this _can_ be replaced
3113\end{funcdesc}
3114
3115%\subsection{Distutils objects}
3116
3117\section{\module{distutils.dist} --- The Distribution class}
3118\declaremodule{standard}{distutils.dist}
3119\modulesynopsis{Provides the Distribution class, which represents the
3120 module distribution being built/installed/distributed}
3121
3122This module provides the \class{Distribution} class, which represents
3123the module distribution being built/installed/distributed.
3124
3125
3126\section{\module{distutils.extension} --- The Extension class}
3127\declaremodule{standard}{distutils.extension}
3128\modulesynopsis{Provides the Extension class, used to describe
3129 C/\Cpp{} extension modules in setup scripts}
3130
3131This module provides the \class{Extension} class, used to describe
3132C/\Cpp{} extension modules in setup scripts.
3133
3134%\subsection{Ungrouped modules}
3135%The following haven't been moved into a more appropriate section yet.
3136
3137\section{\module{distutils.debug} --- Distutils debug mode}
3138\declaremodule{standard}{distutils.debug}
3139\modulesynopsis{Provides the debug flag for distutils}
3140
3141This module provides the DEBUG flag.
3142
3143\section{\module{distutils.errors} --- Distutils exceptions}
3144\declaremodule{standard}{distutils.errors}
3145\modulesynopsis{Provides standard distutils exceptions}
3146
3147Provides exceptions used by the Distutils modules. Note that Distutils
3148modules may raise standard exceptions; in particular, SystemExit is
3149usually raised for errors that are obviously the end-user's fault
3150(eg. bad command-line arguments).
3151
3152This module is safe to use in \samp{from ... import *} mode; it only exports
3153symbols whose names start with \code{Distutils} and end with \code{Error}.
3154
3155\section{\module{distutils.fancy_getopt}
3156 --- Wrapper around the standard getopt module}
3157\declaremodule[distutils.fancygetopt]{standard}{distutils.fancy_getopt}
3158\modulesynopsis{Additional \module{getopt} functionality}
3159
3160This module provides a wrapper around the standard \module{getopt}
3161module that provides the following additional features:
3162
3163\begin{itemize}
3164\item short and long options are tied together
3165\item options have help strings, so \function{fancy_getopt} could potentially
3166create a complete usage summary
3167\item options set attributes of a passed-in object
3168\item boolean options can have ``negative aliases'' --- eg. if
3169\longprogramopt{quiet} is the ``negative alias'' of
3170\longprogramopt{verbose}, then \longprogramopt{quiet} on the command
3171line sets \var{verbose} to false.
3172
3173\end{itemize}
3174
3175\XXX{Should be replaced with \module{optik} (which is also now
3176known as \module{optparse} in Python 2.3 and later).}
3177
3178\begin{funcdesc}{fancy_getopt}{options, negative_opt, object, args}
3179Wrapper function. \var{options} is a list of
3180\samp{(long_option, short_option, help_string)} 3-tuples as described in the
3181constructor for \class{FancyGetopt}. \var{negative_opt} should be a dictionary
3182mapping option names to option names, both the key and value should be in the
3183\var{options} list. \var{object} is an object which will be used to store
3184values (see the \method{getopt()} method of the \class{FancyGetopt} class).
3185\var{args} is the argument list. Will use \code{sys.argv[1:]} if you
3186pass \code{None} as \var{args}.
3187\end{funcdesc}
3188
3189\begin{funcdesc}{wrap_text}{text, width}
3190Wraps \var{text} to less than \var{width} wide.
3191
3192\warning{Should be replaced with \module{textwrap} (which is available
3193in Python 2.3 and later).}
3194\end{funcdesc}
3195
3196\begin{classdesc}{FancyGetopt}{\optional{option_table=\code{None}}}
3197The option_table is a list of 3-tuples: \samp{(long_option,
3198short_option, help_string)}
3199
3200If an option takes an argument, it's \var{long_option} should have \code{'='}
3201appended; \var{short_option} should just be a single character, no \code{':'}
3202in any case. \var{short_option} should be \code{None} if a \var{long_option}
3203doesn't have a corresponding \var{short_option}. All option tuples must have
3204long options.
3205\end{classdesc}
3206
3207The \class{FancyGetopt} class provides the following methods:
3208
3209\begin{methoddesc}{getopt}{\optional{args=\code{None}, object=\code{None}}}
3210Parse command-line options in args. Store as attributes on \var{object}.
3211
3212If \var{args} is \code{None} or not supplied, uses \code{sys.argv[1:]}. If
3213\var{object} is \code{None} or not supplied, creates a new \class{OptionDummy}
3214instance, stores option values there, and returns a tuple \samp{(args,
3215object)}. If \var{object} is supplied, it is modified in place and
3216\function{getopt()} just returns \var{args}; in both cases, the returned
3217\var{args} is a modified copy of the passed-in \var{args} list, which
3218is left untouched.
3219% and args returned are?
3220\end{methoddesc}
3221
3222\begin{methoddesc}{get_option_order}{}
3223Returns the list of \samp{(option, value)} tuples processed by the
3224previous run of \method{getopt()} Raises \exception{RuntimeError} if
3225\method{getopt()} hasn't been called yet.
3226\end{methoddesc}
3227
3228\begin{methoddesc}{generate_help}{\optional{header=\code{None}}}
3229Generate help text (a list of strings, one per suggested line of
3230output) from the option table for this \class{FancyGetopt} object.
3231
3232If supplied, prints the supplied \var{header} at the top of the help.
3233\end{methoddesc}
3234
3235\section{\module{distutils.filelist} --- The FileList class}
3236\declaremodule{standard}{distutils.filelist}
3237\modulesynopsis{The \class{FileList} class, used for poking about the
3238 file system and building lists of files.}
3239
3240This module provides the \class{FileList} class, used for poking about
3241the filesystem and building lists of files.
3242
3243
3244\section{\module{distutils.log} --- Simple PEP 282-style logging}
3245\declaremodule{standard}{distutils.log}
3246\modulesynopsis{A simple logging mechanism, \pep{282}-style}
3247
3248\warning{Should be replaced with standard \module{logging} module.}
3249
3250%\subsubsection{\module{} --- }
3251%\declaremodule{standard}{distutils.magic}
3252%\modulesynopsis{ }
3253
3254
3255\section{\module{distutils.spawn} --- Spawn a sub-process}
3256\declaremodule{standard}{distutils.spawn}
3257\modulesynopsis{Provides the spawn() function}
3258
3259This module provides the \function{spawn()} function, a front-end to
3260various platform-specific functions for launching another program in a
3261sub-process.
3262Also provides \function{find_executable()} to search the path for a given
3263executable name.
3264
3265
Fred Drakeab70b382001-08-02 15:13:15 +00003266\input{sysconfig}
Greg Ward16aafcd2000-04-09 04:06:44 +00003267
3268
Fred Drake6fca7cc2004-03-23 18:43:03 +00003269\section{\module{distutils.text_file} --- The TextFile class}
3270\declaremodule[distutils.textfile]{standard}{distutils.text_file}
3271\modulesynopsis{provides the TextFile class, a simple interface to text files}
3272
3273This module provides the \class{TextFile} class, which gives an interface
3274to text files that (optionally) takes care of stripping comments, ignoring
3275blank lines, and joining lines with backslashes.
3276
3277\begin{classdesc}{TextFile}{\optional{filename=\code{None}, file=\code{None}, **options}}
3278This class provides a file-like object that takes care of all
3279the things you commonly want to do when processing a text file
3280that has some line-by-line syntax: strip comments (as long as \code{\#}
3281is your comment character), skip blank lines, join adjacent lines by
3282escaping the newline (ie. backslash at end of line), strip
3283leading and/or trailing whitespace. All of these are optional
3284and independently controllable.
3285
3286The class provides a \method{warn()} method so you can generate
3287warning messages that report physical line number, even if the
3288logical line in question spans multiple physical lines. Also
3289provides \method{unreadline()} for implementing line-at-a-time lookahead.
3290
3291\class{TextFile} instances are create with either \var{filename}, \var{file},
3292or both. \exception{RuntimeError} is raised if both are \code{None}.
3293\var{filename} should be a string, and \var{file} a file object (or
3294something that provides \method{readline()} and \method{close()}
3295methods). It is recommended that you supply at least \var{filename},
3296so that \class{TextFile} can include it in warning messages. If
3297\var{file} is not supplied, TextFile creates its own using the
3298\var{open()} builtin.
3299
3300The options are all boolean, and affect the values returned by
3301\var{readline()}
3302
3303\begin{tableiii}{c|l|l}{option name}{option name}{description}{default}
3304\lineiii{strip_comments}{
3305strip from \character{\#} to end-of-line, as well as any whitespace
3306leading up to the \character{\#}---unless it is escaped by a backslash}
3307{true}
3308\lineiii{lstrip_ws}{
3309strip leading whitespace from each line before returning it}
3310{false}
3311\lineiii{rstrip_ws}{
3312strip trailing whitespace (including line terminator!) from
3313each line before returning it.}
3314{true}
3315\lineiii{skip_blanks}{
3316skip lines that are empty *after* stripping comments and
3317whitespace. (If both lstrip_ws and rstrip_ws are false,
3318then some lines may consist of solely whitespace: these will
3319*not* be skipped, even if \var{skip_blanks} is true.)}
3320{true}
3321\lineiii{join_lines}{
3322if a backslash is the last non-newline character on a line
3323after stripping comments and whitespace, join the following line
3324to it to form one logical line; if N consecutive lines end
3325with a backslash, then N+1 physical lines will be joined to
3326form one logical line.}
3327{false}
3328\lineiii{collapse_join}{
3329strip leading whitespace from lines that are joined to their
3330predecessor; only matters if \samp{(join_lines and not lstrip_ws)}}
3331{false}
3332\end{tableiii}
3333
3334Note that since \var{rstrip_ws} can strip the trailing newline, the
3335semantics of \method{readline()} must differ from those of the builtin file
3336object's \method{readline()} method! In particular, \method{readline()}
3337returns \code{None} for end-of-file: an empty string might just be a
3338blank line (or an all-whitespace line), if \var{rstrip_ws} is true
3339but \var{skip_blanks} is not.
3340
3341\begin{methoddesc}{open}{filename}
3342Open a new file \var{filename}. This overrides any \var{file} or
3343\var{filename} constructor arguments.
3344\end{methoddesc}
3345
3346\begin{methoddesc}{close}{}
3347Close the current file and forget everything we know about it (including
3348the filename and the current line number).
3349\end{methoddesc}
3350
3351\begin{methoddesc}{warn}{msg\optional{,line=\code{None}}}
3352Print (to stderr) a warning message tied to the current logical
3353line in the current file. If the current logical line in the
3354file spans multiple physical lines, the warning refers to the
3355whole range, such as \samp{"lines 3-5"}. If \var{line} is supplied,
3356it overrides the current line number; it may be a list or tuple
3357to indicate a range of physical lines, or an integer for a
3358single physical line.
3359\end{methoddesc}
3360
3361\begin{methoddesc}{readline}{}
3362Read and return a single logical line from the current file (or
3363from an internal buffer if lines have previously been ``unread''
3364with \method{unreadline()}). If the \var{join_lines} option
3365is true, this may involve reading multiple physical lines
3366concatenated into a single string. Updates the current line number,
3367so calling \method{warn()} after \method{readline()} emits a warning
3368about the physical line(s) just read. Returns \code{None} on end-of-file,
3369since the empty string can occur if \var{rstrip_ws} is true but
3370\var{strip_blanks} is not.
3371\end{methoddesc}
3372\begin{methoddesc}{readlines}{}
3373Read and return the list of all logical lines remaining in the current file.
3374This updates the current line number to the last line of the file.
3375\end{methoddesc}
3376\begin{methoddesc}{unreadline}{line}
3377Push \var{line} (a string) onto an internal buffer that will be
3378checked by future \method{readline()} calls. Handy for implementing
3379a parser with line-at-a-time lookahead. Note that lines that are ``unread''
3380with \method{unreadline} are not subsequently re-cleansed (whitespace
3381stripped, or whatever) when read with \method{readline}. If multiple
3382calls are made to \method{unreadline} before a call to \method{readline},
3383the lines will be returned most in most recent first order.
3384\end{methoddesc}
3385
3386\end{classdesc}
3387
3388
3389\section{\module{distutils.version} --- Version number classes}
3390\declaremodule{standard}{distutils.version}
3391\modulesynopsis{implements classes that represent module version numbers. }
3392
3393% todo
3394
3395%\section{Distutils Commands}
3396%
3397%This part of Distutils implements the various Distutils commands, such
3398%as \code{build}, \code{install} \&c. Each command is implemented as a
3399%separate module, with the command name as the name of the module.
3400
3401\section{\module{distutils.cmd} --- Abstract base class for Distutils commands}
3402\declaremodule{standard}{distutils.cmd}
3403\modulesynopsis{This module provides the abstract base class Command. This
3404class is subclassed by the modules in the \refmodule{distutils.command}
3405subpackage. }
3406
3407This module supplies the abstract base class \class{Command}.
3408
3409\begin{classdesc}{Command}{dist}
3410Abstract base class for defining command classes, the ``worker bees''
3411of the Distutils. A useful analogy for command classes is to think of
3412them as subroutines with local variables called \var{options}. The
3413options are declared in \method{initialize_options()} and defined
3414(given their final values) in \method{finalize_options()}, both of
3415which must be defined by every command class. The distinction between
3416the two is necessary because option values might come from the outside
3417world (command line, config file, ...), and any options dependent on
3418other options must be computed after these outside influences have
3419been processed --- hence \method{finalize_options()}. The body of the
3420subroutine, where it does all its work based on the values of its
3421options, is the \method{run()} method, which must also be implemented
3422by every command class.
3423
3424The class constructor takes a single argument \var{dist}, a
3425\class{Distribution} instance.
3426\end{classdesc}
3427
3428
3429\section{\module{distutils.command} --- Individual Distutils commands}
3430\declaremodule{standard}{distutils.command}
3431\modulesynopsis{This subpackage contains one module for each standard Distutils command.}
3432
3433%\subsubsection{Individual Distutils commands}
3434
3435% todo
3436
3437\section{\module{distutils.command.bdist} --- Build a binary installer}
3438\declaremodule{standard}{distutils.command.bdist}
3439\modulesynopsis{Build a binary installer for a package}
3440
3441% todo
3442
3443\section{\module{distutils.command.bdist_packager} --- Abstract base class for packagers}
3444\declaremodule[distutils.command.bdistpackager]{standard}{distutils.command.bdist_packager}
3445\modulesynopsis{Abstract base class for packagers}
3446
3447% todo
3448
3449\section{\module{distutils.command.bdist_dumb} --- Build a ``dumb'' installer}
3450\declaremodule[distutils.command.bdistdumb]{standard}{distutils.command.bdist_dumb}
3451\modulesynopsis{Build a ``dumb'' installer - a simple archive of files}
3452
3453% todo
3454
3455
3456\section{\module{distutils.command.bdist_rpm} --- Build a binary distribution as a Redhat RPM and SRPM}
3457\declaremodule[distutils.command.bdistrpm]{standard}{distutils.command.bdist_rpm}
3458\modulesynopsis{Build a binary distribution as a Redhat RPM and SRPM}
3459
3460% todo
3461
3462\section{\module{distutils.command.bdist_wininst} --- Build a Windows installer}
3463\declaremodule[distutils.command.bdistwininst]{standard}{distutils.command.bdist_wininst}
3464\modulesynopsis{Build a Windows installer}
3465
3466% todo
3467
3468\section{\module{distutils.command.sdist} --- Build a source distribution}
3469\declaremodule{standard}{distutils.command.sdist}
3470\modulesynopsis{Build a source distribution}
3471
3472% todo
3473
3474\section{\module{distutils.command.build} --- Build all files of a package}
3475\declaremodule{standard}{distutils.command.build}
3476\modulesynopsis{Build all files of a package}
3477
3478% todo
3479
3480\section{\module{distutils.command.build_clib} --- Build any C libraries in a package}
3481\declaremodule[distutils.command.buildclib]{standard}{distutils.command.build_clib}
3482\modulesynopsis{Build any C libraries in a package}
3483
3484% todo
3485
3486\section{\module{distutils.command.build_ext} --- Build any extensions in a package}
3487\declaremodule[distutils.command.buildext]{standard}{distutils.command.build_ext}
3488\modulesynopsis{Build any extensions in a package}
3489
3490% todo
3491
3492\section{\module{distutils.command.build_py} --- Build the .py/.pyc files of a package}
3493\declaremodule[distutils.command.buildpy]{standard}{distutils.command.build_py}
3494\modulesynopsis{Build the .py/.pyc files of a package}
3495
3496% todo
3497
3498\section{\module{distutils.command.build_scripts} --- Build the scripts of a package}
3499\declaremodule[distutils.command.buildscripts]{standard}{distutils.command.build_scripts}
3500\modulesynopsis{Build the scripts of a package}
3501
3502% todo
3503
3504\section{\module{distutils.command.clean} --- Clean a package build area}
3505\declaremodule{standard}{distutils.command.clean}
3506\modulesynopsis{Clean a package build area}
3507
3508% todo
3509
3510\section{\module{distutils.command.config} --- Perform package configuration}
3511\declaremodule{standard}{distutils.command.config}
3512\modulesynopsis{Perform package configuration}
3513
3514% todo
3515
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00003516\section{\module{distutils.command.install} --- Install a package}
Fred Drake6fca7cc2004-03-23 18:43:03 +00003517\declaremodule{standard}{distutils.command.install}
3518\modulesynopsis{Install a package}
3519
3520% todo
3521
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00003522\section{\module{distutils.command.install_data}
Fred Drake6fca7cc2004-03-23 18:43:03 +00003523 --- Install data files from a package}
3524\declaremodule[distutils.command.installdata]{standard}{distutils.command.install_data}
3525\modulesynopsis{Install data files from a package}
3526
3527% todo
3528
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00003529\section{\module{distutils.command.install_headers}
Fred Drake6fca7cc2004-03-23 18:43:03 +00003530 --- Install C/\Cpp{} header files from a package}
3531\declaremodule[distutils.command.installheaders]{standard}{distutils.command.install_headers}
3532\modulesynopsis{Install C/\Cpp{} header files from a package}
3533
3534% todo
3535
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00003536\section{\module{distutils.command.install_lib}
Fred Drake6fca7cc2004-03-23 18:43:03 +00003537 --- Install library files from a package}
3538\declaremodule[distutils.command.installlib]{standard}{distutils.command.install_lib}
3539\modulesynopsis{Install library files from a package}
3540
3541% todo
3542
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00003543\section{\module{distutils.command.install_scripts}
Fred Drake6fca7cc2004-03-23 18:43:03 +00003544 --- Install script files from a package}
3545\declaremodule[distutils.command.installscripts]{standard}{distutils.command.install_scripts}
3546\modulesynopsis{Install script files from a package}
3547
3548% todo
3549
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00003550\section{\module{distutils.command.register}
Fred Drake6fca7cc2004-03-23 18:43:03 +00003551 --- Register a module with the Python Package Index}
3552\declaremodule{standard}{distutils.command.register}
3553\modulesynopsis{Register a module with the Python Package Index}
3554
3555The \code{register} command registers the package with the Python Package
3556Index. This is described in more detail in \pep{301}.
3557% todo
3558
Neal Norwitz2e56c8a2004-08-13 02:56:16 +00003559\section{Creating a new Distutils command}
Fred Drake6fca7cc2004-03-23 18:43:03 +00003560
3561This section outlines the steps to create a new Distutils command.
3562
3563A new command lives in a module in the \module{distutils.command}
3564package. There is a sample template in that directory called
3565\file{command_template}. Copy this file to a new module with the
3566same name as the new command you're implementing. This module should
3567implement a class with the same name as the module (and the command).
3568So, for instance, to create the command \code{peel_banana} (so that users
3569can run \samp{setup.py peel_banana}), you'd copy \file{command_template}
3570to \file{distutils/command/peel_banana.py}, then edit it so that it's
3571implementing the class \class{peel_banana}, a subclass of
3572\class{distutils.cmd.Command}.
3573
3574Subclasses of \class{Command} must define the following methods.
3575
3576\begin{methoddesc}{initialize_options()}
3577Set default values for all the options that this command
3578supports. Note that these defaults may be overridden by other
3579commands, by the setup script, by config files, or by the
3580command-line. Thus, this is not the place to code dependencies
3581between options; generally, \method{initialize_options()} implementations
3582are just a bunch of \samp{self.foo = None} assignments.
3583\end{methoddesc}
3584
3585\begin{methoddesc}{finalize_options}{}
3586Set final values for all the options that this command supports.
3587This is always called as late as possible, ie. after any option
3588assignments from the command-line or from other commands have been
3589done. Thus, this is the place to to code option dependencies: if
3590\var{foo} depends on \var{bar}, then it is safe to set \var{foo} from
3591\var{bar} as long as \var{foo} still has the same value it was assigned in
3592\method{initialize_options()}.
3593\end{methoddesc}
3594\begin{methoddesc}{run}{}
3595A command's raison d'etre: carry out the action it exists to
3596perform, controlled by the options initialized in
3597\method{initialize_options()}, customized by other commands, the setup
3598script, the command-line, and config files, and finalized in
3599\method{finalize_options()}. All terminal output and filesystem
3600interaction should be done by \method{run()}.
3601\end{methoddesc}
3602
3603\var{sub_commands} formalizes the notion of a ``family'' of commands,
3604eg. \code{install} as the parent with sub-commands \code{install_lib},
3605\code{install_headers}, etc. The parent of a family of commands
3606defines \var{sub_commands} as a class attribute; it's a list of
36072-tuples \samp{(command_name, predicate)}, with \var{command_name} a string
3608and \var{predicate} an unbound method, a string or None.
3609\var{predicate} is a method of the parent command that
3610determines whether the corresponding command is applicable in the
3611current situation. (Eg. we \code{install_headers} is only applicable if
3612we have any C header files to install.) If \var{predicate} is None,
3613that command is always applicable.
3614
3615\var{sub_commands} is usually defined at the *end* of a class, because
3616predicates can be unbound methods, so they must already have been
3617defined. The canonical example is the \command{install} command.
3618
Fred Drake6356fff2004-03-23 19:02:38 +00003619%
3620% The ugly "%begin{latexonly}" pseudo-environments are really just to
3621% keep LaTeX2HTML quiet during the \renewcommand{} macros; they're
3622% not really valuable.
3623%
3624
3625%begin{latexonly}
3626\renewcommand{\indexname}{Module Index}
3627%end{latexonly}
Fred Drakead622022004-03-25 16:35:10 +00003628\input{moddist.ind} % Module Index
Fred Drake6356fff2004-03-23 19:02:38 +00003629
3630%begin{latexonly}
3631\renewcommand{\indexname}{Index}
3632%end{latexonly}
Fred Drakead622022004-03-25 16:35:10 +00003633\input{dist.ind} % Index
Fred Drake6fca7cc2004-03-23 18:43:03 +00003634
Greg Wardabc52162000-02-26 00:52:48 +00003635\end{document}