blob: 096702b844c6f4e3ecb63dceb48ccaa1c47985bf [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
Fred Drakea09262e2001-03-01 18:35:43 +000037% The ugly "%begin{latexonly}" pseudo-environment supresses the table
38% 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
Fred Drake781380c2004-02-19 23:17:46 +0000301this document are slash-separated. (Mac OS 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
Greg Ward2afffd42000-08-06 20:37:24 +0000317\subsection{Listing whole packages}
318\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
372\subsection{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
393\subsection{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
436\subsubsection{Extension names and packages}
437
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
472\subsubsection{Extension source files}
473
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
497\subsubsection{Preprocessor options}
498
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
584\subsubsection{Library options}
585
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
615\subsubsection{Other options}
616
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
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000634\subsection{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
643current interpreter location.
Thomas Heller5f52f722001-02-19 17:48:03 +0000644
645The \option{scripts} option simply is a list of files to be handled
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000646in this way. From the PyXML setup script:
647
648\begin{verbatim}
Fred Drake630e5bd2004-03-23 18:54:12 +0000649setup(...
650 scripts=['scripts/xmlproc_parse', 'scripts/xmlproc_val']
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000651 )
652\end{verbatim}
Thomas Heller5f52f722001-02-19 17:48:03 +0000653
654
Fred Drake0eb32a62004-06-11 21:50:33 +0000655\subsection{Installing Package Data}
656
657Often, additional files need to be installed into a package. These
658files are often data that's closely related to the package's
659implementation, or text files containing documentation that might be
660of interest to programmers using the package. These files are called
661\dfn{package data}.
662
663Package data can be added to packages using the \code{package_data}
664keyword argument to the \function{setup()} function. The value must
665be a mapping from package name to a list of relative path names that
666should be copied into the package. The paths are interpreted as
667relative to the directory containing the package (information from the
668\code{package_dir} mapping is used if appropriate); that is, the files
669are expected to be part of the package in the source directories.
670They may contain glob patterns as well.
671
672The path names may contain directory portions; any necessary
673directories will be created in the installation.
674
675For example, if a package should contain a subdirectory with several
676data files, the files can be arranged like this in the source tree:
677
678\begin{verbatim}
679setup.py
680src/
681 mypkg/
682 __init__.py
683 module.py
684 data/
685 tables.dat
686 spoons.dat
687 forks.dat
688\end{verbatim}
689
690The corresponding call to \function{setup()} might be:
691
692\begin{verbatim}
693setup(...,
694 packages=['mypkg'],
695 package_dir={'mypkg': 'src/mypkg'},
Thomas Hellerdd6d2072004-06-18 17:31:23 +0000696 package_data={'mypkg': ['data/*.dat']},
Fred Drake0eb32a62004-06-11 21:50:33 +0000697 )
698\end{verbatim}
699
700
701\versionadded{2.4}
702
703
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000704\subsection{Installing Additional Files}
Fred Drakea09262e2001-03-01 18:35:43 +0000705
Thomas Heller5f52f722001-02-19 17:48:03 +0000706The \option{data\_files} option can be used to specify additional
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000707files needed by the module distribution: configuration files, message
708catalogs, data files, anything which doesn't fit in the previous
709categories.
Thomas Heller5f52f722001-02-19 17:48:03 +0000710
Fred Drake632bda32002-03-08 22:02:06 +0000711\option{data\_files} specifies a sequence of (\var{directory},
712\var{files}) pairs in the following way:
Fred Drakea09262e2001-03-01 18:35:43 +0000713
Thomas Heller5f52f722001-02-19 17:48:03 +0000714\begin{verbatim}
715setup(...
716 data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +0000717 ('config', ['cfg/data.cfg']),
718 ('/etc/init.d', ['init-script'])]
719 )
Thomas Heller5f52f722001-02-19 17:48:03 +0000720\end{verbatim}
721
722Note that you can specify the directory names where the data files
723will be installed, but you cannot rename the data files themselves.
724
Fred Drake632bda32002-03-08 22:02:06 +0000725Each (\var{directory}, \var{files}) pair in the sequence specifies the
726installation directory and the files to install there. If
727\var{directory} is a relative path, it is interpreted relative to the
728installation prefix (Python's \code{sys.prefix} for pure-Python
729packages, \code{sys.exec_prefix} for packages that contain extension
730modules). Each file name in \var{files} is interpreted relative to
731the \file{setup.py} script at the top of the package source
732distribution. No directory information from \var{files} is used to
733determine the final location of the installed file; only the name of
734the file is used.
735
Thomas Heller5f52f722001-02-19 17:48:03 +0000736You can specify the \option{data\_files} options as a simple sequence
737of files without specifying a target directory, but this is not recommended,
738and the \command{install} command will print a warning in this case.
739To install data files directly in the target directory, an empty
740string should be given as the directory.
Greg Ward16aafcd2000-04-09 04:06:44 +0000741
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000742\subsection{Additional meta-data}
743\label{meta-data}
744
745The setup script may include additional meta-data beyond the name and
746version. This information includes:
747
Fred Drakec440af52003-04-25 16:43:28 +0000748\begin{tableiv}{l|l|l|c}{code}%
749 {Meta-Data}{Description}{Value}{Notes}
750 \lineiv{name}{name of the package}
751 {short string}{(1)}
752 \lineiv{version}{version of this release}
753 {short string}{(1)(2)}
754 \lineiv{author}{package author's name}
755 {short string}{(3)}
756 \lineiv{author_email}{email address of the package author}
757 {email address}{(3)}
758 \lineiv{maintainer}{package maintainer's name}
759 {short string}{(3)}
760 \lineiv{maintainer_email}{email address of the package maintainer}
761 {email address}{(3)}
762 \lineiv{url}{home page for the package}
763 {URL}{(1)}
764 \lineiv{description}{short, summary description of the package}
765 {short string}{}
766 \lineiv{long_description}{longer description of the package}
767 {long string}{}
768 \lineiv{download_url}{location where the package may be downloaded}
769 {URL}{(4)}
770 \lineiv{classifiers}{a list of Trove classifiers}
771 {list of strings}{(4)}
772\end{tableiv}
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000773
774\noindent Notes:
775\begin{description}
Fred Drakec440af52003-04-25 16:43:28 +0000776\item[(1)] These fields are required.
777\item[(2)] It is recommended that versions take the form
778 \emph{major.minor\optional{.patch\optional{.sub}}}.
779\item[(3)] Either the author or the maintainer must be identified.
780\item[(4)] These fields should not be used if your package is to be
781 compatible with Python versions prior to 2.2.3 or 2.3. The list is
782 available from the \ulink{PyPI website}{http://www.python.org/pypi}.
783
Fred Drake630e5bd2004-03-23 18:54:12 +0000784\item['short string'] A single line of text, not more than 200 characters.
785\item['long string'] Multiple lines of plain text in reStructuredText
Fred Drakec440af52003-04-25 16:43:28 +0000786 format (see \url{http://docutils.sf.net/}).
Fred Drake630e5bd2004-03-23 18:54:12 +0000787\item['list of strings'] See below.
Fred Drakec440af52003-04-25 16:43:28 +0000788\end{description}
789
790None of the string values may be Unicode.
791
792Encoding the version information is an art in itself. Python packages
793generally adhere to the version format
794\emph{major.minor\optional{.patch}\optional{sub}}. The major number is
7950 for
796initial, experimental releases of software. It is incremented for
797releases that represent major milestones in a package. The minor
798number is incremented when important new features are added to the
799package. The patch number increments when bug-fix releases are
800made. Additional trailing version information is sometimes used to
801indicate sub-releases. These are "a1,a2,...,aN" (for alpha releases,
802where functionality and API may change), "b1,b2,...,bN" (for beta
803releases, which only fix bugs) and "pr1,pr2,...,prN" (for final
804pre-release release testing). Some examples:
805
806\begin{description}
807\item[0.1.0] the first, experimental release of a package
808\item[1.0.1a2] the second alpha release of the first patch version of 1.0
809\end{description}
810
811\option{classifiers} are specified in a python list:
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000812
813\begin{verbatim}
814setup(...
Fred Drake630e5bd2004-03-23 18:54:12 +0000815 classifiers=[
Fred Drake2a046232003-03-31 16:23:09 +0000816 'Development Status :: 4 - Beta',
817 'Environment :: Console',
818 'Environment :: Web Environment',
819 'Intended Audience :: End Users/Desktop',
820 'Intended Audience :: Developers',
821 'Intended Audience :: System Administrators',
822 'License :: OSI Approved :: Python Software Foundation License',
823 'Operating System :: MacOS :: MacOS X',
824 'Operating System :: Microsoft :: Windows',
825 'Operating System :: POSIX',
826 'Programming Language :: Python',
827 'Topic :: Communications :: Email',
828 'Topic :: Office/Business',
829 'Topic :: Software Development :: Bug Tracking',
830 ],
Fred Drake2a046232003-03-31 16:23:09 +0000831 )
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000832\end{verbatim}
833
Fred Drakec440af52003-04-25 16:43:28 +0000834If you wish to include classifiers in your \file{setup.py} file and also
835wish to remain backwards-compatible with Python releases prior to 2.2.3,
836then you can include the following code fragment in your \file{setup.py}
Fred Drake630e5bd2004-03-23 18:54:12 +0000837before the \function{setup()} call.
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000838
839\begin{verbatim}
Fred Drakec440af52003-04-25 16:43:28 +0000840# patch distutils if it can't cope with the "classifiers" or
841# "download_url" keywords
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000842if sys.version < '2.2.3':
843 from distutils.dist import DistributionMetadata
844 DistributionMetadata.classifiers = None
Fred Drake2a046232003-03-31 16:23:09 +0000845 DistributionMetadata.download_url = None
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +0000846\end{verbatim}
847
Greg Ward16aafcd2000-04-09 04:06:44 +0000848
Thomas Heller675580f2003-06-30 19:33:29 +0000849\subsection{Debugging the setup script}
Thomas Heller675580f2003-06-30 19:33:29 +0000850
851Sometimes things go wrong, and the setup script doesn't do what the
852developer wants.
853
854Distutils catches any exceptions when running the setup script, and
855print a simple error message before the script is terminated. The
856motivation for this behaviour is to not confuse administrators who
857don't know much about Python and are trying to install a package. If
858they get a big long traceback from deep inside the guts of Distutils,
859they may think the package or the Python installation is broken
860because they don't read all the way down to the bottom and see that
861it's a permission problem.
862
863On the other hand, this doesn't help the developer to find the cause
864of the failure. For this purpose, the DISTUTILS_DEBUG environment
865variable can be set to anything except an empty string, and distutils
866will now print detailed information what it is doing, and prints the
Martin v. Löwis95cf84a2003-10-19 07:32:24 +0000867full traceback in case an exception occurs.
Thomas Heller675580f2003-06-30 19:33:29 +0000868
Fred Drake211a2eb2004-03-22 21:44:43 +0000869\chapter{Writing the Setup Configuration File}
Greg Warde78298a2000-04-28 17:12:24 +0000870\label{setup-config}
Greg Ward16aafcd2000-04-09 04:06:44 +0000871
Greg Ward16aafcd2000-04-09 04:06:44 +0000872Often, it's not possible to write down everything needed to build a
Greg Ward47f99a62000-09-04 20:07:15 +0000873distribution \emph{a priori}: you may need to get some information from
874the user, or from the user's system, in order to proceed. As long as
875that information is fairly simple---a list of directories to search for
876C header files or libraries, for example---then providing a
877configuration file, \file{setup.cfg}, for users to edit is a cheap and
878easy way to solicit it. Configuration files also let you provide
879default values for any command option, which the installer can then
880override either on the command-line or by editing the config file.
Greg Ward16aafcd2000-04-09 04:06:44 +0000881
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000882% (If you have more advanced needs, such as determining which extensions
883% to build based on what capabilities are present on the target system,
884% then you need the Distutils ``auto-configuration'' facility. This
885% started to appear in Distutils 0.9 but, as of this writing, isn't mature
886% or stable enough yet for real-world use.)
Greg Ward16aafcd2000-04-09 04:06:44 +0000887
Greg Ward47f99a62000-09-04 20:07:15 +0000888The setup configuration file is a useful middle-ground between the setup
889script---which, ideally, would be opaque to installers\footnote{This
890 ideal probably won't be achieved until auto-configuration is fully
891 supported by the Distutils.}---and the command-line to the setup
892script, which is outside of your control and entirely up to the
893installer. In fact, \file{setup.cfg} (and any other Distutils
894configuration files present on the target system) are processed after
895the contents of the setup script, but before the command-line. This has
896several useful consequences:
897\begin{itemize}
898\item installers can override some of what you put in \file{setup.py} by
899 editing \file{setup.cfg}
900\item you can provide non-standard defaults for options that are not
901 easily set in \file{setup.py}
902\item installers can override anything in \file{setup.cfg} using the
903 command-line options to \file{setup.py}
904\end{itemize}
905
906The basic syntax of the configuration file is simple:
Fred Drakea09262e2001-03-01 18:35:43 +0000907
Greg Ward47f99a62000-09-04 20:07:15 +0000908\begin{verbatim}
909[command]
910option=value
911...
912\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000913
Greg Ward47f99a62000-09-04 20:07:15 +0000914where \var{command} is one of the Distutils commands (e.g.
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000915\command{build\_py}, \command{install}), and \var{option} is one of
916the options that command supports. Any number of options can be
917supplied for each command, and any number of command sections can be
918included in the file. Blank lines are ignored, as are comments, which
919run from a \character{\#} character until the end of the line. Long
920option values can be split across multiple lines simply by indenting
921the continuation lines.
Greg Ward47f99a62000-09-04 20:07:15 +0000922
923You can find out the list of options supported by a particular command
924with the universal \longprogramopt{help} option, e.g.
Fred Drakea09262e2001-03-01 18:35:43 +0000925
Greg Ward47f99a62000-09-04 20:07:15 +0000926\begin{verbatim}
927> python setup.py --help build_ext
928[...]
929Options for 'build_ext' command:
930 --build-lib (-b) directory for compiled extension modules
931 --build-temp (-t) directory for temporary files (build by-products)
932 --inplace (-i) ignore build-lib and put compiled extensions into the
933 source directory alongside your pure Python modules
934 --include-dirs (-I) list of directories to search for header files
935 --define (-D) C preprocessor macros to define
936 --undef (-U) C preprocessor macros to undefine
937[...]
938\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000939
Greg Ward47f99a62000-09-04 20:07:15 +0000940Note that an option spelled \longprogramopt{foo-bar} on the command-line
941is spelled \option{foo\_bar} in configuration files.
942
943For example, say you want your extensions to be built
944``in-place''---that is, you have an extension \module{pkg.ext}, and you
Fred Drakeeff9a872000-10-26 16:41:03 +0000945want the compiled extension file (\file{ext.so} on \UNIX, say) to be put
Greg Ward47f99a62000-09-04 20:07:15 +0000946in the same source directory as your pure Python modules
947\module{pkg.mod1} and \module{pkg.mod2}. You can always use the
948\longprogramopt{inplace} option on the command-line to ensure this:
Fred Drakea09262e2001-03-01 18:35:43 +0000949
Greg Ward47f99a62000-09-04 20:07:15 +0000950\begin{verbatim}
951python setup.py build_ext --inplace
952\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000953
Greg Ward47f99a62000-09-04 20:07:15 +0000954But this requires that you always specify the \command{build\_ext}
955command explicitly, and remember to provide \longprogramopt{inplace}.
956An easier way is to ``set and forget'' this option, by encoding it in
957\file{setup.cfg}, the configuration file for this distribution:
Fred Drakea09262e2001-03-01 18:35:43 +0000958
Greg Ward47f99a62000-09-04 20:07:15 +0000959\begin{verbatim}
960[build_ext]
961inplace=1
962\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000963
Greg Ward47f99a62000-09-04 20:07:15 +0000964This will affect all builds of this module distribution, whether or not
965you explcitly specify \command{build\_ext}. If you include
966\file{setup.cfg} in your source distribution, it will also affect
967end-user builds---which is probably a bad idea for this option, since
968always building extensions in-place would break installation of the
969module distribution. In certain peculiar cases, though, modules are
970built right in their installation directory, so this is conceivably a
971useful ability. (Distributing extensions that expect to be built in
972their installation directory is almost always a bad idea, though.)
973
974Another example: certain commands take a lot of options that don't
Andrew M. Kuchling40df7102002-05-08 13:39:03 +0000975change from run to run; for example, \command{bdist\_rpm} needs to know
Greg Ward47f99a62000-09-04 20:07:15 +0000976everything required to generate a ``spec'' file for creating an RPM
977distribution. Some of this information comes from the setup script, and
978some is automatically generated by the Distutils (such as the list of
979files installed). But some of it has to be supplied as options to
980\command{bdist\_rpm}, which would be very tedious to do on the
981command-line for every run. Hence, here is a snippet from the
982Distutils' own \file{setup.cfg}:
Fred Drakea09262e2001-03-01 18:35:43 +0000983
Greg Ward47f99a62000-09-04 20:07:15 +0000984\begin{verbatim}
985[bdist_rpm]
986release = 1
987packager = Greg Ward <gward@python.net>
988doc_files = CHANGES.txt
989 README.txt
990 USAGE.txt
991 doc/
992 examples/
993\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +0000994
Greg Ward47f99a62000-09-04 20:07:15 +0000995Note that the \option{doc\_files} option is simply a
996whitespace-separated string split across multiple lines for readability.
Greg Ward16aafcd2000-04-09 04:06:44 +0000997
998
Fred Drakea09262e2001-03-01 18:35:43 +0000999\begin{seealso}
1000 \seetitle[../inst/config-syntax.html]{Installing Python
1001 Modules}{More information on the configuration files is
1002 available in the manual for system administrators.}
1003\end{seealso}
1004
1005
Fred Drake211a2eb2004-03-22 21:44:43 +00001006\chapter{Creating a Source Distribution}
Greg Warde78298a2000-04-28 17:12:24 +00001007\label{source-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +00001008
Greg Warde78298a2000-04-28 17:12:24 +00001009As shown in section~\ref{simple-example}, you use the
Greg Ward16aafcd2000-04-09 04:06:44 +00001010\command{sdist} command to create a source distribution. In the
1011simplest case,
Fred Drakea09262e2001-03-01 18:35:43 +00001012
Greg Ward16aafcd2000-04-09 04:06:44 +00001013\begin{verbatim}
1014python setup.py sdist
1015\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001016
Greg Ward19c67f82000-06-24 01:33:16 +00001017(assuming you haven't specified any \command{sdist} options in the setup
1018script or config file), \command{sdist} creates the archive of the
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001019default format for the current platform. The default format is a gzip'ed
Fred Drakeeff9a872000-10-26 16:41:03 +00001020tar file (\file{.tar.gz}) on \UNIX, and ZIP file on Windows.
Fred Drake781380c2004-02-19 23:17:46 +00001021\XXX{no Mac OS support here}
Greg Ward54589d42000-09-06 01:37:35 +00001022
Greg Wardd5767a52000-04-19 22:48:09 +00001023You can specify as many formats as you like using the
1024\longprogramopt{formats} option, for example:
Fred Drakea09262e2001-03-01 18:35:43 +00001025
Greg Ward16aafcd2000-04-09 04:06:44 +00001026\begin{verbatim}
1027python setup.py sdist --formats=gztar,zip
1028\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001029
Greg Ward16aafcd2000-04-09 04:06:44 +00001030to create a gzipped tarball and a zip file. The available formats are:
Fred Drake781380c2004-02-19 23:17:46 +00001031
Greg Ward46b98e32000-04-14 01:53:36 +00001032\begin{tableiii}{l|l|c}{code}%
1033 {Format}{Description}{Notes}
Greg Ward54589d42000-09-06 01:37:35 +00001034 \lineiii{zip}{zip file (\file{.zip})}{(1),(3)}
1035 \lineiii{gztar}{gzip'ed tar file (\file{.tar.gz})}{(2),(4)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001036 \lineiii{bztar}{bzip2'ed tar file (\file{.tar.bz2})}{(4)}
Greg Ward47f99a62000-09-04 20:07:15 +00001037 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(4)}
Greg Ward54589d42000-09-06 01:37:35 +00001038 \lineiii{tar}{tar file (\file{.tar})}{(4)}
Greg Ward46b98e32000-04-14 01:53:36 +00001039\end{tableiii}
1040
1041\noindent Notes:
1042\begin{description}
1043\item[(1)] default on Windows
Fred Drakeeff9a872000-10-26 16:41:03 +00001044\item[(2)] default on \UNIX
Greg Wardb6528972000-09-07 02:40:37 +00001045\item[(3)] requires either external \program{zip} utility or
Greg Ward954ce8b2002-05-10 14:42:10 +00001046 \module{zipfile} module (part of the standard Python library since
1047 Python~1.6)
Greg Ward47f99a62000-09-04 20:07:15 +00001048\item[(4)] requires external utilities: \program{tar} and possibly one
1049 of \program{gzip}, \program{bzip2}, or \program{compress}
Greg Ward46b98e32000-04-14 01:53:36 +00001050\end{description}
Greg Ward16aafcd2000-04-09 04:06:44 +00001051
1052
Greg Ward54589d42000-09-06 01:37:35 +00001053
1054\subsection{Specifying the files to distribute}
Greg Warde78298a2000-04-28 17:12:24 +00001055\label{manifest}
Greg Ward16aafcd2000-04-09 04:06:44 +00001056
Greg Ward54589d42000-09-06 01:37:35 +00001057If you don't supply an explicit list of files (or instructions on how to
1058generate one), the \command{sdist} command puts a minimal default set
1059into the source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +00001060\begin{itemize}
Greg Wardfacb8db2000-04-09 04:32:40 +00001061\item all Python source files implied by the \option{py\_modules} and
Greg Ward16aafcd2000-04-09 04:06:44 +00001062 \option{packages} options
Greg Wardfacb8db2000-04-09 04:32:40 +00001063\item all C source files mentioned in the \option{ext\_modules} or
Greg Ward16aafcd2000-04-09 04:06:44 +00001064 \option{libraries} options (\XXX{getting C library sources currently
Fred Drake781380c2004-02-19 23:17:46 +00001065 broken---no \method{get_source_files()} method in \file{build_clib.py}!})
Fred Drake203b10c2004-03-31 01:50:37 +00001066\item scripts identified by the \option{scripts} option
Greg Ward16aafcd2000-04-09 04:06:44 +00001067\item anything that looks like a test script: \file{test/test*.py}
1068 (currently, the Distutils don't do anything with test scripts except
1069 include them in source distributions, but in the future there will be
1070 a standard for testing Python module distributions)
Greg Ward54589d42000-09-06 01:37:35 +00001071\item \file{README.txt} (or \file{README}), \file{setup.py} (or whatever
1072 you called your setup script), and \file{setup.cfg}
Greg Ward16aafcd2000-04-09 04:06:44 +00001073\end{itemize}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001074
Greg Ward16aafcd2000-04-09 04:06:44 +00001075Sometimes this is enough, but usually you will want to specify
1076additional files to distribute. The typical way to do this is to write
1077a \emph{manifest template}, called \file{MANIFEST.in} by default. The
Greg Ward54589d42000-09-06 01:37:35 +00001078manifest template is just a list of instructions for how to generate
1079your manifest file, \file{MANIFEST}, which is the exact list of files to
1080include in your source distribution. The \command{sdist} command
1081processes this template and generates a manifest based on its
1082instructions and what it finds in the filesystem.
1083
1084If you prefer to roll your own manifest file, the format is simple: one
1085filename per line, regular files (or symlinks to them) only. If you do
1086supply your own \file{MANIFEST}, you must specify everything: the
1087default set of files described above does not apply in this case.
Greg Ward16aafcd2000-04-09 04:06:44 +00001088
1089The manifest template has one command per line, where each command
1090specifies a set of files to include or exclude from the source
1091distribution. For an example, again we turn to the Distutils' own
1092manifest template:
Fred Drakea09262e2001-03-01 18:35:43 +00001093
Greg Ward16aafcd2000-04-09 04:06:44 +00001094\begin{verbatim}
1095include *.txt
Greg Ward87da1ea2000-04-21 04:35:25 +00001096recursive-include examples *.txt *.py
Greg Ward16aafcd2000-04-09 04:06:44 +00001097prune examples/sample?/build
1098\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001099
Greg Ward16aafcd2000-04-09 04:06:44 +00001100The meanings should be fairly clear: include all files in the
Fred Drake630e5bd2004-03-23 18:54:12 +00001101distribution root matching \file{*.txt}, all files anywhere under the
1102\file{examples} directory matching \file{*.txt} or \file{*.py}, and
1103exclude all directories matching \file{examples/sample?/build}. All of
Greg Ward54589d42000-09-06 01:37:35 +00001104this is done \emph{after} the standard include set, so you can exclude
1105files from the standard set with explicit instructions in the manifest
1106template. (Or, you can use the \longprogramopt{no-defaults} option to
1107disable the standard set entirely.) There are several other commands
1108available in the manifest template mini-language; see
1109section~\ref{sdist-cmd}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001110
Greg Ward54589d42000-09-06 01:37:35 +00001111The order of commands in the manifest template matters: initially, we
1112have the list of default files as described above, and each command in
1113the template adds to or removes from that list of files. Once we have
1114fully processed the manifest template, we remove files that should not
1115be included in the source distribution:
1116\begin{itemize}
1117\item all files in the Distutils ``build'' tree (default \file{build/})
Tim Peters2f50e902004-05-31 19:27:59 +00001118\item all files in directories named \file{RCS}, \file{CVS} or \file{.svn}
Greg Ward54589d42000-09-06 01:37:35 +00001119\end{itemize}
1120Now we have our complete list of files, which is written to the manifest
1121for future reference, and then used to build the source distribution
1122archive(s).
1123
1124You can disable the default set of included files with the
1125\longprogramopt{no-defaults} option, and you can disable the standard
1126exclude set with \longprogramopt{no-prune}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001127
Greg Ward46b98e32000-04-14 01:53:36 +00001128Following the Distutils' own manifest template, let's trace how the
Greg Ward47f99a62000-09-04 20:07:15 +00001129\command{sdist} command builds the list of files to include in the
Greg Ward46b98e32000-04-14 01:53:36 +00001130Distutils source distribution:
Greg Ward16aafcd2000-04-09 04:06:44 +00001131\begin{enumerate}
1132\item include all Python source files in the \file{distutils} and
1133 \file{distutils/command} subdirectories (because packages
1134 corresponding to those two directories were mentioned in the
Greg Ward54589d42000-09-06 01:37:35 +00001135 \option{packages} option in the setup script---see
1136 section~\ref{setup-script})
1137\item include \file{README.txt}, \file{setup.py}, and \file{setup.cfg}
1138 (standard files)
1139\item include \file{test/test*.py} (standard files)
Greg Ward16aafcd2000-04-09 04:06:44 +00001140\item include \file{*.txt} in the distribution root (this will find
1141 \file{README.txt} a second time, but such redundancies are weeded out
1142 later)
Greg Ward54589d42000-09-06 01:37:35 +00001143\item include anything matching \file{*.txt} or \file{*.py} in the
1144 sub-tree under \file{examples},
1145\item exclude all files in the sub-trees starting at directories
1146 matching \file{examples/sample?/build}---this may exclude files
1147 included by the previous two steps, so it's important that the
1148 \code{prune} command in the manifest template comes after the
1149 \code{recursive-include} command
Tim Peters2f50e902004-05-31 19:27:59 +00001150\item exclude the entire \file{build} tree, and any \file{RCS},
1151 \file{CVS} and \file{.svn} directories
Greg Wardfacb8db2000-04-09 04:32:40 +00001152\end{enumerate}
Greg Ward46b98e32000-04-14 01:53:36 +00001153Just like in the setup script, file and directory names in the manifest
1154template should always be slash-separated; the Distutils will take care
1155of converting them to the standard representation on your platform.
1156That way, the manifest template is portable across operating systems.
1157
Greg Ward16aafcd2000-04-09 04:06:44 +00001158
1159\subsection{Manifest-related options}
Greg Warde78298a2000-04-28 17:12:24 +00001160\label{manifest-options}
Greg Ward16aafcd2000-04-09 04:06:44 +00001161
1162The normal course of operations for the \command{sdist} command is as
1163follows:
1164\begin{itemize}
Greg Ward46b98e32000-04-14 01:53:36 +00001165\item if the manifest file, \file{MANIFEST} doesn't exist, read
1166 \file{MANIFEST.in} and create the manifest
Greg Ward54589d42000-09-06 01:37:35 +00001167\item if neither \file{MANIFEST} nor \file{MANIFEST.in} exist, create a
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001168 manifest with just the default file set
Greg Ward1d8f57a2000-08-05 00:43:11 +00001169\item if either \file{MANIFEST.in} or the setup script (\file{setup.py})
1170 are more recent than \file{MANIFEST}, recreate \file{MANIFEST} by
1171 reading \file{MANIFEST.in}
Greg Ward16aafcd2000-04-09 04:06:44 +00001172\item use the list of files now in \file{MANIFEST} (either just
1173 generated or read in) to create the source distribution archive(s)
1174\end{itemize}
Greg Ward54589d42000-09-06 01:37:35 +00001175There are a couple of options that modify this behaviour. First, use
1176the \longprogramopt{no-defaults} and \longprogramopt{no-prune} to
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001177disable the standard ``include'' and ``exclude'' sets.
Greg Ward16aafcd2000-04-09 04:06:44 +00001178
Greg Ward54589d42000-09-06 01:37:35 +00001179Second, you might want to force the manifest to be regenerated---for
Greg Ward16aafcd2000-04-09 04:06:44 +00001180example, if you have added or removed files or directories that match an
1181existing pattern in the manifest template, you should regenerate the
1182manifest:
Fred Drakea09262e2001-03-01 18:35:43 +00001183
Greg Ward16aafcd2000-04-09 04:06:44 +00001184\begin{verbatim}
1185python setup.py sdist --force-manifest
1186\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +00001187
1188Or, you might just want to (re)generate the manifest, but not create a
1189source distribution:
Fred Drakea09262e2001-03-01 18:35:43 +00001190
Greg Ward16aafcd2000-04-09 04:06:44 +00001191\begin{verbatim}
1192python setup.py sdist --manifest-only
1193\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001194
Greg Ward54589d42000-09-06 01:37:35 +00001195\longprogramopt{manifest-only} implies \longprogramopt{force-manifest}.
1196\programopt{-o} is a shortcut for \longprogramopt{manifest-only}, and
1197\programopt{-f} for \longprogramopt{force-manifest}.
Greg Ward16aafcd2000-04-09 04:06:44 +00001198
1199
Fred Drake211a2eb2004-03-22 21:44:43 +00001200\chapter{Creating Built Distributions}
Greg Warde78298a2000-04-28 17:12:24 +00001201\label{built-dist}
Greg Ward16aafcd2000-04-09 04:06:44 +00001202
Greg Ward46b98e32000-04-14 01:53:36 +00001203A ``built distribution'' is what you're probably used to thinking of
1204either as a ``binary package'' or an ``installer'' (depending on your
1205background). It's not necessarily binary, though, because it might
1206contain only Python source code and/or byte-code; and we don't call it a
1207package, because that word is already spoken for in Python. (And
Fred Drake2a1bc502004-02-19 23:03:29 +00001208``installer'' is a term specific to the world of mainstream desktop
1209systems.)
Greg Ward16aafcd2000-04-09 04:06:44 +00001210
Greg Ward46b98e32000-04-14 01:53:36 +00001211A built distribution is how you make life as easy as possible for
1212installers of your module distribution: for users of RPM-based Linux
1213systems, it's a binary RPM; for Windows users, it's an executable
1214installer; for Debian-based Linux users, it's a Debian package; and so
1215forth. Obviously, no one person will be able to create built
Greg Wardb6528972000-09-07 02:40:37 +00001216distributions for every platform under the sun, so the Distutils are
Greg Ward46b98e32000-04-14 01:53:36 +00001217designed to enable module developers to concentrate on their
1218specialty---writing code and creating source distributions---while an
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001219intermediary species called \emph{packagers} springs up to turn source
Greg Ward19c67f82000-06-24 01:33:16 +00001220distributions into built distributions for as many platforms as there
Greg Ward46b98e32000-04-14 01:53:36 +00001221are packagers.
1222
1223Of course, the module developer could be his own packager; or the
1224packager could be a volunteer ``out there'' somewhere who has access to
1225a platform which the original developer does not; or it could be
1226software periodically grabbing new source distributions and turning them
1227into built distributions for as many platforms as the software has
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001228access to. Regardless of who they are, a packager uses the
Greg Ward46b98e32000-04-14 01:53:36 +00001229setup script and the \command{bdist} command family to generate built
1230distributions.
1231
1232As a simple example, if I run the following command in the Distutils
1233source tree:
Fred Drakea09262e2001-03-01 18:35:43 +00001234
Greg Ward46b98e32000-04-14 01:53:36 +00001235\begin{verbatim}
1236python setup.py bdist
1237\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001238
Greg Ward46b98e32000-04-14 01:53:36 +00001239then the Distutils builds my module distribution (the Distutils itself
1240in this case), does a ``fake'' installation (also in the \file{build}
1241directory), and creates the default type of built distribution for my
Greg Wardb6528972000-09-07 02:40:37 +00001242platform. The default format for built distributions is a ``dumb'' tar
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001243file on \UNIX, and a simple executable installer on Windows. (That tar
Greg Wardb6528972000-09-07 02:40:37 +00001244file is considered ``dumb'' because it has to be unpacked in a specific
1245location to work.)
Greg Ward1d8f57a2000-08-05 00:43:11 +00001246
Fred Drakeeff9a872000-10-26 16:41:03 +00001247Thus, the above command on a \UNIX{} system creates
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001248\file{Distutils-1.0.\filevar{plat}.tar.gz}; unpacking this tarball
Greg Wardb6528972000-09-07 02:40:37 +00001249from the right place installs the Distutils just as though you had
1250downloaded the source distribution and run \code{python setup.py
1251 install}. (The ``right place'' is either the root of the filesystem or
1252Python's \filevar{prefix} directory, depending on the options given to
1253the \command{bdist\_dumb} command; the default is to make dumb
1254distributions relative to \filevar{prefix}.)
Greg Ward46b98e32000-04-14 01:53:36 +00001255
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001256Obviously, for pure Python distributions, this isn't any simpler than
1257just running \code{python setup.py install}---but for non-pure
1258distributions, which include extensions that would need to be
1259compiled, it can mean the difference between someone being able to use
1260your extensions or not. And creating ``smart'' built distributions,
1261such as an RPM package or an executable installer for Windows, is far
1262more convenient for users even if your distribution doesn't include
1263any extensions.
Greg Ward46b98e32000-04-14 01:53:36 +00001264
Greg Wardb6528972000-09-07 02:40:37 +00001265The \command{bdist} command has a \longprogramopt{formats} option,
Greg Ward1d8f57a2000-08-05 00:43:11 +00001266similar to the \command{sdist} command, which you can use to select the
1267types of built distribution to generate: for example,
Fred Drakea09262e2001-03-01 18:35:43 +00001268
Greg Ward46b98e32000-04-14 01:53:36 +00001269\begin{verbatim}
1270python setup.py bdist --format=zip
1271\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001272
Fred Drakeeff9a872000-10-26 16:41:03 +00001273would, when run on a \UNIX{} system, create
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001274\file{Distutils-1.0.\filevar{plat}.zip}---again, this archive would be
Greg Ward1d8f57a2000-08-05 00:43:11 +00001275unpacked from the root directory to install the Distutils.
Greg Ward46b98e32000-04-14 01:53:36 +00001276
1277The available formats for built distributions are:
Fred Drake781380c2004-02-19 23:17:46 +00001278
Greg Ward46b98e32000-04-14 01:53:36 +00001279\begin{tableiii}{l|l|c}{code}%
1280 {Format}{Description}{Notes}
Greg Wardb6528972000-09-07 02:40:37 +00001281 \lineiii{gztar}{gzipped tar file (\file{.tar.gz})}{(1),(3)}
1282 \lineiii{ztar}{compressed tar file (\file{.tar.Z})}{(3)}
1283 \lineiii{tar}{tar file (\file{.tar})}{(3)}
1284 \lineiii{zip}{zip file (\file{.zip})}{(4)}
1285 \lineiii{rpm}{RPM}{(5)}
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001286 \lineiii{pkgtool}{Solaris \program{pkgtool}}{}
1287 \lineiii{sdux}{HP-UX \program{swinstall}}{}
1288 \lineiii{rpm}{RPM}{(5)}
1289% \lineiii{srpm}{source RPM}{(5) \XXX{to do!}}
Thomas Heller5f52f722001-02-19 17:48:03 +00001290 \lineiii{wininst}{self-extracting ZIP file for Windows}{(2),(4)}
Greg Ward46b98e32000-04-14 01:53:36 +00001291\end{tableiii}
1292
1293\noindent Notes:
1294\begin{description}
Fred Drakeeff9a872000-10-26 16:41:03 +00001295\item[(1)] default on \UNIX
Greg Ward1d8f57a2000-08-05 00:43:11 +00001296\item[(2)] default on Windows \XXX{to-do!}
Greg Wardb6528972000-09-07 02:40:37 +00001297\item[(3)] requires external utilities: \program{tar} and possibly one
1298 of \program{gzip}, \program{bzip2}, or \program{compress}
1299\item[(4)] requires either external \program{zip} utility or
Greg Ward954ce8b2002-05-10 14:42:10 +00001300 \module{zipfile} module (part of the standard Python library since
1301 Python~1.6)
Greg Wardb6528972000-09-07 02:40:37 +00001302\item[(5)] requires external \program{rpm} utility, version 3.0.4 or
1303 better (use \code{rpm --version} to find out which version you have)
Greg Ward46b98e32000-04-14 01:53:36 +00001304\end{description}
1305
1306You don't have to use the \command{bdist} command with the
Greg Wardd5767a52000-04-19 22:48:09 +00001307\longprogramopt{formats} option; you can also use the command that
Greg Ward1d8f57a2000-08-05 00:43:11 +00001308directly implements the format you're interested in. Some of these
Greg Ward46b98e32000-04-14 01:53:36 +00001309\command{bdist} ``sub-commands'' actually generate several similar
1310formats; for instance, the \command{bdist\_dumb} command generates all
1311the ``dumb'' archive formats (\code{tar}, \code{ztar}, \code{gztar}, and
1312\code{zip}), and \command{bdist\_rpm} generates both binary and source
1313RPMs. The \command{bdist} sub-commands, and the formats generated by
1314each, are:
Fred Drake781380c2004-02-19 23:17:46 +00001315
Greg Ward46b98e32000-04-14 01:53:36 +00001316\begin{tableii}{l|l}{command}%
1317 {Command}{Formats}
1318 \lineii{bdist\_dumb}{tar, ztar, gztar, zip}
1319 \lineii{bdist\_rpm}{rpm, srpm}
Greg Ward1d8f57a2000-08-05 00:43:11 +00001320 \lineii{bdist\_wininst}{wininst}
Greg Ward46b98e32000-04-14 01:53:36 +00001321\end{tableii}
Greg Ward16aafcd2000-04-09 04:06:44 +00001322
Greg Wardb6528972000-09-07 02:40:37 +00001323The following sections give details on the individual \command{bdist\_*}
1324commands.
1325
1326
1327\subsection{Creating dumb built distributions}
1328\label{creating-dumb}
1329
1330\XXX{Need to document absolute vs. prefix-relative packages here, but
1331 first I have to implement it!}
1332
1333
1334\subsection{Creating RPM packages}
1335\label{creating-rpms}
1336
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001337The RPM format is used by many popular Linux distributions, including
Greg Wardb6528972000-09-07 02:40:37 +00001338Red Hat, SuSE, and Mandrake. If one of these (or any of the other
1339RPM-based Linux distributions) is your usual environment, creating RPM
1340packages for other users of that same distribution is trivial.
1341Depending on the complexity of your module distribution and differences
1342between Linux distributions, you may also be able to create RPMs that
1343work on different RPM-based distributions.
1344
1345The usual way to create an RPM of your module distribution is to run the
1346\command{bdist\_rpm} command:
Fred Drakea09262e2001-03-01 18:35:43 +00001347
Greg Wardb6528972000-09-07 02:40:37 +00001348\begin{verbatim}
1349python setup.py bdist_rpm
1350\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001351
Greg Wardb6528972000-09-07 02:40:37 +00001352or the \command{bdist} command with the \longprogramopt{format} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001353
Greg Wardb6528972000-09-07 02:40:37 +00001354\begin{verbatim}
1355python setup.py bdist --formats=rpm
1356\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001357
Greg Wardb6528972000-09-07 02:40:37 +00001358The former allows you to specify RPM-specific options; the latter allows
1359you to easily specify multiple formats in one run. If you need to do
1360both, you can explicitly specify multiple \command{bdist\_*} commands
1361and their options:
Fred Drakea09262e2001-03-01 18:35:43 +00001362
Greg Wardb6528972000-09-07 02:40:37 +00001363\begin{verbatim}
Fred Drake630e5bd2004-03-23 18:54:12 +00001364python setup.py bdist_rpm --packager="John Doe <jdoe@example.org>" \
Greg Wardb6528972000-09-07 02:40:37 +00001365 bdist_wininst --target_version="2.0"
1366\end{verbatim}
1367
1368Creating RPM packages is driven by a \file{.spec} file, much as using
1369the Distutils is driven by the setup script. To make your life easier,
1370the \command{bdist\_rpm} command normally creates a \file{.spec} file
1371based on the information you supply in the setup script, on the command
1372line, and in any Distutils configuration files. Various options and
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001373sections in the \file{.spec} file are derived from options in the setup
Greg Wardb6528972000-09-07 02:40:37 +00001374script as follows:
Fred Drake781380c2004-02-19 23:17:46 +00001375
Greg Wardb6528972000-09-07 02:40:37 +00001376\begin{tableii}{l|l}{textrm}%
1377 {RPM \file{.spec} file option or section}{Distutils setup script option}
1378 \lineii{Name}{\option{name}}
1379 \lineii{Summary (in preamble)}{\option{description}}
1380 \lineii{Version}{\option{version}}
1381 \lineii{Vendor}{\option{author} and \option{author\_email}, or \\&
1382 \option{maintainer} and \option{maintainer\_email}}
1383 \lineii{Copyright}{\option{licence}}
1384 \lineii{Url}{\option{url}}
1385 \lineii{\%description (section)}{\option{long\_description}}
1386\end{tableii}
1387
1388Additionally, there many options in \file{.spec} files that don't have
1389corresponding options in the setup script. Most of these are handled
1390through options to the \command{bdist\_rpm} command as follows:
Fred Drake781380c2004-02-19 23:17:46 +00001391
Greg Wardb6528972000-09-07 02:40:37 +00001392\begin{tableiii}{l|l|l}{textrm}%
1393 {RPM \file{.spec} file option or section}%
1394 {\command{bdist\_rpm} option}%
1395 {default value}
1396 \lineiii{Release}{\option{release}}{``1''}
1397 \lineiii{Group}{\option{group}}{``Development/Libraries''}
1398 \lineiii{Vendor}{\option{vendor}}{(see above)}
Andrew M. Kuchlingda23c4f2001-02-17 00:38:48 +00001399 \lineiii{Packager}{\option{packager}}{(none)}
1400 \lineiii{Provides}{\option{provides}}{(none)}
1401 \lineiii{Requires}{\option{requires}}{(none)}
1402 \lineiii{Conflicts}{\option{conflicts}}{(none)}
1403 \lineiii{Obsoletes}{\option{obsoletes}}{(none)}
Greg Wardb6528972000-09-07 02:40:37 +00001404 \lineiii{Distribution}{\option{distribution\_name}}{(none)}
1405 \lineiii{BuildRequires}{\option{build\_requires}}{(none)}
1406 \lineiii{Icon}{\option{icon}}{(none)}
1407\end{tableiii}
Fred Drake781380c2004-02-19 23:17:46 +00001408
Greg Wardb6528972000-09-07 02:40:37 +00001409Obviously, supplying even a few of these options on the command-line
1410would be tedious and error-prone, so it's usually best to put them in
1411the setup configuration file, \file{setup.cfg}---see
1412section~\ref{setup-config}. If you distribute or package many Python
1413module distributions, you might want to put options that apply to all of
1414them in your personal Distutils configuration file
1415(\file{\textasciitilde/.pydistutils.cfg}).
1416
1417There are three steps to building a binary RPM package, all of which are
1418handled automatically by the Distutils:
Fred Drake781380c2004-02-19 23:17:46 +00001419
Greg Wardb6528972000-09-07 02:40:37 +00001420\begin{enumerate}
1421\item create a \file{.spec} file, which describes the package (analogous
1422 to the Distutils setup script; in fact, much of the information in the
1423 setup script winds up in the \file{.spec} file)
1424\item create the source RPM
1425\item create the ``binary'' RPM (which may or may not contain binary
1426 code, depending on whether your module distribution contains Python
1427 extensions)
1428\end{enumerate}
Fred Drake781380c2004-02-19 23:17:46 +00001429
Greg Wardb6528972000-09-07 02:40:37 +00001430Normally, RPM bundles the last two steps together; when you use the
1431Distutils, all three steps are typically bundled together.
1432
1433If you wish, you can separate these three steps. You can use the
Fred Drake781380c2004-02-19 23:17:46 +00001434\longprogramopt{spec-only} option to make \command{bdist_rpm} just
Greg Wardb6528972000-09-07 02:40:37 +00001435create the \file{.spec} file and exit; in this case, the \file{.spec}
1436file will be written to the ``distribution directory''---normally
1437\file{dist/}, but customizable with the \longprogramopt{dist-dir}
1438option. (Normally, the \file{.spec} file winds up deep in the ``build
Fred Drake781380c2004-02-19 23:17:46 +00001439tree,'' in a temporary directory created by \command{bdist_rpm}.)
Greg Wardb6528972000-09-07 02:40:37 +00001440
Fred Drake781380c2004-02-19 23:17:46 +00001441% \XXX{this isn't implemented yet---is it needed?!}
1442% You can also specify a custom \file{.spec} file with the
1443% \longprogramopt{spec-file} option; used in conjunction with
1444% \longprogramopt{spec-only}, this gives you an opportunity to customize
1445% the \file{.spec} file manually:
1446%
1447% \begin{verbatim}
1448% > python setup.py bdist_rpm --spec-only
1449% # ...edit dist/FooBar-1.0.spec
1450% > python setup.py bdist_rpm --spec-file=dist/FooBar-1.0.spec
1451% \end{verbatim}
1452%
1453% (Although a better way to do this is probably to override the standard
1454% \command{bdist\_rpm} command with one that writes whatever else you want
1455% to the \file{.spec} file.)
Greg Wardb6528972000-09-07 02:40:37 +00001456
1457
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001458\subsection{Creating Windows Installers}
Greg Wardb6528972000-09-07 02:40:37 +00001459\label{creating-wininst}
1460
Thomas Hellere61f3652002-11-15 20:13:26 +00001461Executable installers are the natural format for binary distributions
1462on Windows. They display a nice graphical user interface, display
1463some information about the module distribution to be installed taken
Andrew M. Kuchlingd7abe2a2002-05-29 17:33:48 +00001464from the metadata in the setup script, let the user select a few
Thomas Hellere61f3652002-11-15 20:13:26 +00001465options, and start or cancel the installation.
Greg Wardb6528972000-09-07 02:40:37 +00001466
Thomas Hellere61f3652002-11-15 20:13:26 +00001467Since the metadata is taken from the setup script, creating Windows
1468installers is usually as easy as running:
Fred Drakea09262e2001-03-01 18:35:43 +00001469
Thomas Heller5f52f722001-02-19 17:48:03 +00001470\begin{verbatim}
1471python setup.py bdist_wininst
1472\end{verbatim}
Fred Drakea09262e2001-03-01 18:35:43 +00001473
Thomas Heller36343f62002-11-15 19:20:56 +00001474or the \command{bdist} command with the \longprogramopt{formats} option:
Fred Drakea09262e2001-03-01 18:35:43 +00001475
Thomas Heller5f52f722001-02-19 17:48:03 +00001476\begin{verbatim}
1477python setup.py bdist --formats=wininst
1478\end{verbatim}
1479
Thomas Hellere61f3652002-11-15 20:13:26 +00001480If you have a pure module distribution (only containing pure Python
1481modules and packages), the resulting installer will be version
1482independent and have a name like \file{foo-1.0.win32.exe}. These
Fred Drakec54d9252004-02-19 22:16:05 +00001483installers can even be created on \UNIX{} or Mac OS platforms.
Thomas Heller5f52f722001-02-19 17:48:03 +00001484
1485If you have a non-pure distribution, the extensions can only be
Andrew M. Kuchling40df7102002-05-08 13:39:03 +00001486created on a Windows platform, and will be Python version dependent.
Thomas Heller5f52f722001-02-19 17:48:03 +00001487The installer filename will reflect this and now has the form
Thomas Hellere61f3652002-11-15 20:13:26 +00001488\file{foo-1.0.win32-py2.0.exe}. You have to create a separate installer
Thomas Heller5f52f722001-02-19 17:48:03 +00001489for every Python version you want to support.
1490
1491The installer will try to compile pure modules into bytecode after
Thomas Hellere61f3652002-11-15 20:13:26 +00001492installation on the target system in normal and optimizing mode. If
1493you don't want this to happen for some reason, you can run the
Fred Drake0e9bfa32002-11-15 20:34:52 +00001494\command{bdist_wininst} command with the
1495\longprogramopt{no-target-compile} and/or the
1496\longprogramopt{no-target-optimize} option.
Thomas Hellere61f3652002-11-15 20:13:26 +00001497
Fred Drake0e9bfa32002-11-15 20:34:52 +00001498By default the installer will display the cool ``Python Powered'' logo
Thomas Hellere61f3652002-11-15 20:13:26 +00001499when it is run, but you can also supply your own bitmap which must be
Fred Drake0e9bfa32002-11-15 20:34:52 +00001500a Windows \file{.bmp} file with the \longprogramopt{bitmap} option.
Thomas Hellere61f3652002-11-15 20:13:26 +00001501
1502The installer will also display a large title on the desktop
1503background window when it is run, which is constructed from the name
1504of your distribution and the version number. This can be changed to
1505another text by using the \longprogramopt{title} option.
1506
1507The installer file will be written to the ``distribution directory''
1508--- normally \file{dist/}, but customizable with the
1509\longprogramopt{dist-dir} option.
1510
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001511\subsubsection{The Postinstallation script}
1512\label{postinstallation-script}
1513
1514Starting with Python 2.3, a postinstallation script can be specified
1515which the \longprogramopt{install-script} option. The basename of the
1516script must be specified, and the script filename must also be listed
1517in the scripts argument to the setup function.
1518
1519This script will be run at installation time on the target system
Fred Drakec54d9252004-02-19 22:16:05 +00001520after all the files have been copied, with \code{argv[1]} set to
1521\programopt{-install}, and again at uninstallation time before the
1522files are removed with \code{argv[1]} set to \programopt{-remove}.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001523
1524The installation script runs embedded in the windows installer, every
Fred Drakec54d9252004-02-19 22:16:05 +00001525output (\code{sys.stdout}, \code{sys.stderr}) is redirected into a
1526buffer and will be displayed in the GUI after the script has finished.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001527
Fred Drakea9ee0da2004-02-19 22:28:15 +00001528Some functions especially useful in this context are available as
1529additional built-in functions in the installation script.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001530
Fred Drakea9ee0da2004-02-19 22:28:15 +00001531\begin{funcdesc}{directory_created}{path}
1532\funcline{file_created}{path}
1533 These functions should be called when a directory or file is created
1534 by the postinstall script at installation time. It will register
1535 \var{path} with the uninstaller, so that it will be removed when the
1536 distribution is uninstalled. To be safe, directories are only removed
1537 if they are empty.
1538\end{funcdesc}
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001539
Fred Drakea9ee0da2004-02-19 22:28:15 +00001540\begin{funcdesc}{get_special_folder_path}{csidl_string}
1541 This function can be used to retrieve special folder locations on
1542 Windows like the Start Menu or the Desktop. It returns the full
1543 path to the folder. \var{csidl_string} must be one of the following
1544 strings:
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001545
1546\begin{verbatim}
1547"CSIDL_APPDATA"
1548
1549"CSIDL_COMMON_STARTMENU"
1550"CSIDL_STARTMENU"
1551
1552"CSIDL_COMMON_DESKTOPDIRECTORY"
1553"CSIDL_DESKTOPDIRECTORY"
1554
1555"CSIDL_COMMON_STARTUP"
1556"CSIDL_STARTUP"
1557
1558"CSIDL_COMMON_PROGRAMS"
1559"CSIDL_PROGRAMS"
1560
1561"CSIDL_FONTS"
1562\end{verbatim}
1563
Fred Drakea9ee0da2004-02-19 22:28:15 +00001564 If the folder cannot be retrieved, \exception{OSError} is raised.
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001565
Fred Drakea9ee0da2004-02-19 22:28:15 +00001566 Which folders are available depends on the exact Windows version,
1567 and probably also the configuration. For details refer to
1568 Microsoft's documentation of the
1569 \cfunction{SHGetSpecialFolderPath()} function.
1570\end{funcdesc}
Thomas Heller2c3bfc22002-12-12 18:54:19 +00001571
Fred Drakea9ee0da2004-02-19 22:28:15 +00001572\begin{funcdesc}{create_shortcut}{target, description,
1573 filename\optional{,
1574 arguments\optional{,
1575 workdir\optional{,
1576 iconpath\optional{, iconindex}}}}}
1577 This function creates a shortcut.
1578 \var{target} is the path to the program to be started by the shortcut.
1579 \var{description} is the description of the sortcut.
1580 \var{filename} is the title of the shortcut that the user will see.
1581 \var{arguments} specifies the command line arguments, if any.
1582 \var{workdir} is the working directory for the program.
1583 \var{iconpath} is the file containing the icon for the shortcut,
1584 and \var{iconindex} is the index of the icon in the file
1585 \var{iconpath}. Again, for details consult the Microsoft
1586 documentation for the \class{IShellLink} interface.
1587\end{funcdesc}
Greg Wardb6528972000-09-07 02:40:37 +00001588
Fred Drake211a2eb2004-03-22 21:44:43 +00001589\chapter{Registering with the Package Index}
Andrew M. Kuchlingd15f4e32003-01-03 15:42:14 +00001590\label{package-index}
1591
1592The Python Package Index (PyPI) holds meta-data describing distributions
1593packaged with distutils. The distutils command \command{register} is
1594used to submit your distribution's meta-data to the index. It is invoked
1595as follows:
1596
1597\begin{verbatim}
1598python setup.py register
1599\end{verbatim}
1600
1601Distutils will respond with the following prompt:
1602
1603\begin{verbatim}
1604running register
1605We need to know who you are, so please choose either:
1606 1. use your existing login,
1607 2. register as a new user,
1608 3. have the server generate a new password for you (and email it to you), or
1609 4. quit
1610Your selection [default 1]:
1611\end{verbatim}
1612
1613\noindent Note: if your username and password are saved locally, you will
1614not see this menu.
1615
1616If you have not registered with PyPI, then you will need to do so now. You
1617should choose option 2, and enter your details as required. Soon after
1618submitting your details, you will receive an email which will be used to
1619confirm your registration.
1620
1621Once you are registered, you may choose option 1 from the menu. You will
1622be prompted for your PyPI username and password, and \command{register}
1623will then submit your meta-data to the index.
1624
1625You may submit any number of versions of your distribution to the index. If
1626you alter the meta-data for a particular version, you may submit it again
1627and the index will be updated.
1628
1629PyPI holds a record for each (name, version) combination submitted. The
1630first user to submit information for a given name is designated the Owner
1631of that name. They may submit changes through the \command{register}
1632command or through the web interface. They may also designate other users
1633as Owners or Maintainers. Maintainers may edit the package information, but
1634not designate other Owners or Maintainers.
1635
1636By default PyPI will list all versions of a given package. To hide certain
1637versions, the Hidden property should be set to yes. This must be edited
1638through the web interface.
1639
1640
1641
Fred Drake211a2eb2004-03-22 21:44:43 +00001642\chapter{Examples}
Greg Ward007c04a2002-05-10 14:45:59 +00001643\label{examples}
Fred Drake40333ce2004-06-14 22:07:50 +00001644
1645This chapter provides a number of basic examples to help get started
1646with distutils. Additional information about using distutils can be
1647found in the Distutils Cookbook.
1648
1649\begin{seealso}
1650 \seelink{http://www.python.org/cgi-bin/moinmoin/DistutilsCookbook}
1651 {Distutils Cookbook}
1652 {Collection of recipes showing how to achieve more control
1653 over distutils.}
1654\end{seealso}
1655
1656
Fred Drake211a2eb2004-03-22 21:44:43 +00001657\section{Pure Python distribution (by module)}
Greg Ward007c04a2002-05-10 14:45:59 +00001658\label{pure-mod}
1659
1660If you're just distributing a couple of modules, especially if they
1661don't live in a particular package, you can specify them individually
1662using the \option{py\_modules} option in the setup script.
1663
1664In the simplest case, you'll have two files to worry about: a setup
1665script and the single module you're distributing, \file{foo.py} in this
1666example:
1667\begin{verbatim}
1668<root>/
1669 setup.py
1670 foo.py
1671\end{verbatim}
1672(In all diagrams in this section, \verb|<root>| will refer to the
1673distribution root directory.) A minimal setup script to describe this
1674situation would be:
1675\begin{verbatim}
1676from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001677setup(name='foo',
1678 version='1.0',
1679 py_modules=['foo'],
1680 )
Greg Ward007c04a2002-05-10 14:45:59 +00001681\end{verbatim}
1682Note that the name of the distribution is specified independently with
1683the \option{name} option, and there's no rule that says it has to be the
1684same as the name of the sole module in the distribution (although that's
1685probably a good convention to follow). However, the distribution name
1686is used to generate filenames, so you should stick to letters, digits,
1687underscores, and hyphens.
1688
1689Since \option{py\_modules} is a list, you can of course specify multiple
1690modules, eg. if you're distributing modules \module{foo} and
1691\module{bar}, your setup might look like this:
1692\begin{verbatim}
1693<root>/
1694 setup.py
1695 foo.py
1696 bar.py
1697\end{verbatim}
1698and the setup script might be
1699\begin{verbatim}
1700from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001701setup(name='foobar',
1702 version='1.0',
1703 py_modules=['foo', 'bar'],
1704 )
Greg Ward007c04a2002-05-10 14:45:59 +00001705\end{verbatim}
1706
1707You can put module source files into another directory, but if you have
1708enough modules to do that, it's probably easier to specify modules by
1709package rather than listing them individually.
Greg Ward16aafcd2000-04-09 04:06:44 +00001710
1711
Fred Drake211a2eb2004-03-22 21:44:43 +00001712\section{Pure Python distribution (by package)}
Greg Ward007c04a2002-05-10 14:45:59 +00001713\label{pure-pkg}
1714
1715If you have more than a couple of modules to distribute, especially if
1716they are in multiple packages, it's probably easier to specify whole
1717packages rather than individual modules. This works even if your
1718modules are not in a package; you can just tell the Distutils to process
1719modules from the root package, and that works the same as any other
1720package (except that you don't have to have an \file{\_\_init\_\_.py}
1721file).
1722
1723The setup script from the last example could also be written as
1724\begin{verbatim}
1725from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001726setup(name='foobar',
1727 version='1.0',
1728 packages=[''],
1729 )
Greg Ward007c04a2002-05-10 14:45:59 +00001730\end{verbatim}
1731(The empty string stands for the root package.)
1732
1733If those two files are moved into a subdirectory, but remain in the root
1734package, e.g.:
1735\begin{verbatim}
1736<root>/
1737 setup.py
1738 src/ foo.py
1739 bar.py
1740\end{verbatim}
1741then you would still specify the root package, but you have to tell the
1742Distutils where source files in the root package live:
1743\begin{verbatim}
1744from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001745setup(name='foobar',
1746 version='1.0',
1747 package_dir={'': 'src'},
1748 packages=[''],
1749 )
Greg Ward007c04a2002-05-10 14:45:59 +00001750\end{verbatim}
1751
1752More typically, though, you will want to distribute multiple modules in
1753the same package (or in sub-packages). For example, if the \module{foo}
1754and \module{bar} modules belong in package \module{foobar}, one way to
1755layout your source tree is
1756\begin{verbatim}
1757<root>/
1758 setup.py
1759 foobar/
1760 __init__.py
1761 foo.py
1762 bar.py
1763\end{verbatim}
1764This is in fact the default layout expected by the Distutils, and the
1765one that requires the least work to describe in your setup script:
1766\begin{verbatim}
1767from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001768setup(name='foobar',
1769 version='1.0',
1770 packages=['foobar'],
1771 )
Greg Ward007c04a2002-05-10 14:45:59 +00001772\end{verbatim}
1773
1774If you want to put modules in directories not named for their package,
1775then you need to use the \option{package\_dir} option again. For
1776example, if the \file{src} directory holds modules in the
1777\module{foobar} package:
1778\begin{verbatim}
1779<root>/
1780 setup.py
1781 src/
1782 __init__.py
1783 foo.py
1784 bar.py
1785\end{verbatim}
1786an appropriate setup script would be
1787\begin{verbatim}
1788from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001789setup(name='foobar',
1790 version='1.0',
1791 package_dir={'foobar': 'src'},
1792 packages=['foobar'],
1793 )
Greg Ward007c04a2002-05-10 14:45:59 +00001794\end{verbatim}
1795
1796Or, you might put modules from your main package right in the
1797distribution root:
1798\begin{verbatim}
1799<root>/
1800 setup.py
1801 __init__.py
1802 foo.py
1803 bar.py
1804\end{verbatim}
1805in which case your setup script would be
1806\begin{verbatim}
1807from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001808setup(name='foobar',
1809 version='1.0',
1810 package_dir={'foobar': ''},
1811 packages=['foobar'],
1812 )
Greg Ward007c04a2002-05-10 14:45:59 +00001813\end{verbatim}
1814(The empty string also stands for the current directory.)
1815
1816If you have sub-packages, they must be explicitly listed in
1817\option{packages}, but any entries in \option{package\_dir}
1818automatically extend to sub-packages. (In other words, the Distutils
1819does \emph{not} scan your source tree, trying to figure out which
1820directories correspond to Python packages by looking for
1821\file{\_\_init\_\_.py} files.) Thus, if the default layout grows a
1822sub-package:
1823\begin{verbatim}
1824<root>/
1825 setup.py
1826 foobar/
1827 __init__.py
1828 foo.py
1829 bar.py
1830 subfoo/
1831 __init__.py
1832 blah.py
1833\end{verbatim}
1834then the corresponding setup script would be
1835\begin{verbatim}
1836from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001837setup(name='foobar',
1838 version='1.0',
1839 packages=['foobar', 'foobar.subfoo'],
1840 )
Greg Ward007c04a2002-05-10 14:45:59 +00001841\end{verbatim}
1842(Again, the empty string in \option{package\_dir} stands for the current
1843directory.)
Greg Ward16aafcd2000-04-09 04:06:44 +00001844
1845
Fred Drake211a2eb2004-03-22 21:44:43 +00001846\section{Single extension module}
Greg Ward007c04a2002-05-10 14:45:59 +00001847\label{single-ext}
1848
1849Extension modules are specified using the \option{ext\_modules} option.
1850\option{package\_dir} has no effect on where extension source files are
1851found; it only affects the source for pure Python modules. The simplest
1852case, a single extension module in a single C source file, is:
1853\begin{verbatim}
1854<root>/
1855 setup.py
1856 foo.c
1857\end{verbatim}
1858If the \module{foo} extension belongs in the root package, the setup
1859script for this could be
1860\begin{verbatim}
1861from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001862setup(name='foobar',
1863 version='1.0',
1864 ext_modules=[Extension('foo', ['foo.c'])],
1865 )
Greg Ward007c04a2002-05-10 14:45:59 +00001866\end{verbatim}
1867
1868If the extension actually belongs in a package, say \module{foopkg},
1869then
1870
1871With exactly the same source tree layout, this extension can be put in
1872the \module{foopkg} package simply by changing the name of the
1873extension:
1874\begin{verbatim}
1875from distutils.core import setup
Fred Drake630e5bd2004-03-23 18:54:12 +00001876setup(name='foobar',
1877 version='1.0',
1878 ext_modules=[Extension('foopkg.foo', ['foo.c'])],
1879 )
Greg Ward007c04a2002-05-10 14:45:59 +00001880\end{verbatim}
Greg Ward16aafcd2000-04-09 04:06:44 +00001881
1882
Fred Drake211a2eb2004-03-22 21:44:43 +00001883%\section{Multiple extension modules}
Fred Drakea09262e2001-03-01 18:35:43 +00001884%\label{multiple-ext}
Greg Ward16aafcd2000-04-09 04:06:44 +00001885
1886
Fred Drake211a2eb2004-03-22 21:44:43 +00001887%\section{Putting it all together}
Greg Ward16aafcd2000-04-09 04:06:44 +00001888
1889
Fred Drake211a2eb2004-03-22 21:44:43 +00001890%\chapter{Extending the Distutils}
Fred Drakea09262e2001-03-01 18:35:43 +00001891%\label{extending}
Greg Ward4a9e7222000-04-25 02:57:36 +00001892
1893
Fred Drake211a2eb2004-03-22 21:44:43 +00001894%\section{Extending existing commands}
Fred Drakea09262e2001-03-01 18:35:43 +00001895%\label{extend-existing}
Greg Ward4a9e7222000-04-25 02:57:36 +00001896
1897
Fred Drake211a2eb2004-03-22 21:44:43 +00001898%\section{Writing new commands}
Fred Drakea09262e2001-03-01 18:35:43 +00001899%\label{new-commands}
Greg Ward4a9e7222000-04-25 02:57:36 +00001900
Fred Drakea09262e2001-03-01 18:35:43 +00001901%\XXX{Would an uninstall command be a good example here?}
Thomas Heller5f52f722001-02-19 17:48:03 +00001902
Greg Ward4a9e7222000-04-25 02:57:36 +00001903
1904
Fred Drake211a2eb2004-03-22 21:44:43 +00001905\chapter{Command Reference}
Greg Ward47f99a62000-09-04 20:07:15 +00001906\label{reference}
Greg Ward16aafcd2000-04-09 04:06:44 +00001907
1908
Fred Drakea09262e2001-03-01 18:35:43 +00001909%\subsection{Building modules: the \protect\command{build} command family}
1910%\label{build-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001911
Fred Drakea09262e2001-03-01 18:35:43 +00001912%\subsubsection{\protect\command{build}}
1913%\label{build-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001914
Fred Drakea09262e2001-03-01 18:35:43 +00001915%\subsubsection{\protect\command{build\_py}}
1916%\label{build-py-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001917
Fred Drakea09262e2001-03-01 18:35:43 +00001918%\subsubsection{\protect\command{build\_ext}}
1919%\label{build-ext-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001920
Fred Drakea09262e2001-03-01 18:35:43 +00001921%\subsubsection{\protect\command{build\_clib}}
1922%\label{build-clib-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001923
1924
Fred Drake211a2eb2004-03-22 21:44:43 +00001925\section{Installing modules: the \protect\command{install} command family}
Greg Warde78298a2000-04-28 17:12:24 +00001926\label{install-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001927
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001928The install command ensures that the build commands have been run and then
1929runs the subcommands \command{install\_lib},
1930\command{install\_data} and
1931\command{install\_scripts}.
1932
Fred Drakea09262e2001-03-01 18:35:43 +00001933%\subsubsection{\protect\command{install\_lib}}
1934%\label{install-lib-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001935
Fred Drake211a2eb2004-03-22 21:44:43 +00001936\subsection{\protect\command{install\_data}}
Greg Ward1365a302000-08-31 14:47:05 +00001937\label{install-data-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001938This command installs all data files provided with the distribution.
1939
Fred Drake211a2eb2004-03-22 21:44:43 +00001940\subsection{\protect\command{install\_scripts}}
Greg Ward1365a302000-08-31 14:47:05 +00001941\label{install-scripts-cmd}
Gregory P. Smith147e5f32000-05-12 00:58:18 +00001942This command installs all (Python) scripts in the distribution.
1943
Greg Ward16aafcd2000-04-09 04:06:44 +00001944
Fred Drakea09262e2001-03-01 18:35:43 +00001945%\subsection{Cleaning up: the \protect\command{clean} command}
1946%\label{clean-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001947
1948
Fred Drake211a2eb2004-03-22 21:44:43 +00001949\section{Creating a source distribution: the
Fred Drakeeff9a872000-10-26 16:41:03 +00001950 \protect\command{sdist} command}
Greg Warde78298a2000-04-28 17:12:24 +00001951\label{sdist-cmd}
Greg Ward16aafcd2000-04-09 04:06:44 +00001952
1953
1954\XXX{fragment moved down from above: needs context!}
Greg Wardb6528972000-09-07 02:40:37 +00001955
Greg Ward16aafcd2000-04-09 04:06:44 +00001956The manifest template commands are:
Fred Drake781380c2004-02-19 23:17:46 +00001957
Greg Ward16aafcd2000-04-09 04:06:44 +00001958\begin{tableii}{ll}{command}{Command}{Description}
Greg Ward87da1ea2000-04-21 04:35:25 +00001959 \lineii{include \var{pat1} \var{pat2} ... }
1960 {include all files matching any of the listed patterns}
1961 \lineii{exclude \var{pat1} \var{pat2} ... }
1962 {exclude all files matching any of the listed patterns}
1963 \lineii{recursive-include \var{dir} \var{pat1} \var{pat2} ... }
1964 {include all files under \var{dir} matching any of the listed patterns}
1965 \lineii{recursive-exclude \var{dir} \var{pat1} \var{pat2} ...}
1966 {exclude all files under \var{dir} matching any of the listed patterns}
1967 \lineii{global-include \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001968 {include all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001969 any of the listed patterns}
1970 \lineii{global-exclude \var{pat1} \var{pat2} ...}
Greg Ward1bbe3292000-06-25 03:14:13 +00001971 {exclude all files anywhere in the source tree matching\\&
Greg Ward87da1ea2000-04-21 04:35:25 +00001972 any of the listed patterns}
Greg Ward16aafcd2000-04-09 04:06:44 +00001973 \lineii{prune \var{dir}}{exclude all files under \var{dir}}
1974 \lineii{graft \var{dir}}{include all files under \var{dir}}
1975\end{tableii}
Fred Drake781380c2004-02-19 23:17:46 +00001976
Fred Drakeeff9a872000-10-26 16:41:03 +00001977The patterns here are \UNIX-style ``glob'' patterns: \code{*} matches any
Greg Ward16aafcd2000-04-09 04:06:44 +00001978sequence of regular filename characters, \code{?} matches any single
1979regular filename character, and \code{[\var{range}]} matches any of the
1980characters in \var{range} (e.g., \code{a-z}, \code{a-zA-Z},
Greg Wardfacb8db2000-04-09 04:32:40 +00001981\code{a-f0-9\_.}). The definition of ``regular filename character'' is
Fred Drakeeff9a872000-10-26 16:41:03 +00001982platform-specific: on \UNIX{} it is anything except slash; on Windows
Fred Drake781380c2004-02-19 23:17:46 +00001983anything except backslash or colon; on Mac OS anything except colon.
Greg Wardb6528972000-09-07 02:40:37 +00001984
Fred Drake781380c2004-02-19 23:17:46 +00001985\XXX{Windows and Mac OS support not there yet}
Greg Ward16aafcd2000-04-09 04:06:44 +00001986
1987
Fred Drake211a2eb2004-03-22 21:44:43 +00001988%\section{Creating a built distribution: the
Fred Drakea09262e2001-03-01 18:35:43 +00001989% \protect\command{bdist} command family}
1990%\label{bdist-cmds}
Greg Ward16aafcd2000-04-09 04:06:44 +00001991
1992
Fred Drake211a2eb2004-03-22 21:44:43 +00001993%\subsection{\protect\command{bdist}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001994
Fred Drake211a2eb2004-03-22 21:44:43 +00001995%\subsection{\protect\command{bdist\_dumb}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001996
Fred Drake211a2eb2004-03-22 21:44:43 +00001997%\subsection{\protect\command{bdist\_rpm}}
Greg Ward16aafcd2000-04-09 04:06:44 +00001998
Fred Drake211a2eb2004-03-22 21:44:43 +00001999%\subsection{\protect\command{bdist\_wininst}}
Fred Drakeab70b382001-08-02 15:13:15 +00002000
2001
Fred Drake6fca7cc2004-03-23 18:43:03 +00002002\chapter{API Reference \label{api-reference}}
2003
2004\section{\module{distutils.core} --- Core Distutils functionality}
2005
2006\declaremodule{standard}{distutils.core}
2007\modulesynopsis{The core Distutils functionality}
2008
2009The \module{distutils.core} module is the only module that needs to be
2010installed to use the Distutils. It provides the \function{setup()} (which
2011is called from the setup script). Indirectly provides the
2012\class{distutils.dist.Distribution} and \class{distutils.cmd.Command} class.
2013
2014\begin{funcdesc}{setup}{arguments}
2015The basic do-everything function that does most everything you could ever
2016ask for from a Distutils method. See XXXXX
2017
2018The setup function takes a large number of arguments. These
2019are laid out in the following table.
2020
2021\begin{tableiii}{c|l|l}{argument name}{argument name}{value}{type}
2022\lineiii{name}{The name of the package}{a string}
2023\lineiii{version}{The version number of the package}{See \refmodule{distutils.version}}
2024\lineiii{description}{A single line describing the package}{a string}
2025\lineiii{long_description}{Longer description of the package}{a string}
2026\lineiii{author}{The name of the package author}{a string}
2027\lineiii{author_email}{The email address of the package author}{a string}
2028\lineiii{maintainer}{The name of the current maintainer, if different from the author}{a string}
2029\lineiii{maintainer_email}{The email address of the current maintainer, if different from the author}{}
2030\lineiii{url}{A URL for the package (homepage)}{a URL}
2031\lineiii{download_url}{A URL to download the package}{a URL}
2032\lineiii{packages}{A list of Python packages that distutils will manipulate}{a list of strings}
2033\lineiii{py_modules}{A list of Python modules that distutils will manipulate}{a list of strings}
2034\lineiii{scripts}{A list of standalone script files to be built and installed}{a list of strings}
2035\lineiii{ext_modules}{A list of Python extensions to be built}{A list of
2036instances of \class{distutils.core.Extension}}
2037\lineiii{classifiers}{A list of Trove categories for the package}{XXX link to better definition}
2038\lineiii{distclass}{the \class{Distribution} class to use}{A subclass of \class{distutils.core.Distribution}}
2039% What on earth is the use case for script_name?
2040\lineiii{script_name}{The name of the setup.py script - defaults to \code{sys.argv[0]}}{a string}
2041\lineiii{script_args}{Arguments to supply to the setup script}{a list of strings}
2042\lineiii{options}{default options for the setup script}{a string}
2043\lineiii{license}{The license for the package}{}
2044\lineiii{keywords}{Descriptive meta-data. See \pep{314}}{}
2045\lineiii{platforms}{}{}
2046\lineiii{cmdclass}{A mapping of command names to \class{Command} subclasses}{a dictionary}
2047\end{tableiii}
2048
2049\end{funcdesc}
2050
2051\begin{funcdesc}{run_setup}{script_name\optional{, script_args=\code{None}, stop_after=\code{'run'}}}
2052Run a setup script in a somewhat controlled environment, and return
2053the \class{distutils.dist.Distribution} instance that drives things.
2054This is useful if you need to find out the distribution meta-data
2055(passed as keyword args from \var{script} to \function{setup()}), or
2056the contents of the config files or command-line.
2057
2058\var{script_name} is a file that will be run with \function{execfile()}
2059\var{sys.argv[0]} will be replaced with \var{script} for the duration of the
2060call. \var{script_args} is a list of strings; if supplied,
2061\var{sys.argv[1:]} will be replaced by \var{script_args} for the duration
2062of the call.
2063
2064\var{stop_after} tells \function{setup()} when to stop processing; possible
2065values:
2066
2067\begin{tableii}{c|l}{value}{value}{description}
2068\lineii{init}{Stop after the \class{Distribution} instance has been created
2069and populated with the keyword arguments to \function{setup()}}
2070\lineii{config}{Stop after config files have been parsed (and their data
2071stored in the \class{Distribution} instance)}
2072\lineii{commandline}{Stop after the command-line (\code{sys.argv[1:]} or
2073\var{script_args}) have been parsed (and the data stored in the
2074\class{Distribution} instance.)}
2075\lineii{run}{Stop after all commands have been run (the same as
2076if \function{setup()} had been called in the usual way). This is the default
2077value.}
2078\end{tableii}
2079\end{funcdesc}
2080
2081In addition, the \module{distutils.core} module exposed a number of
2082classes that live elsewhere.
2083
2084\begin{itemize}
2085\item \class{Extension} from \refmodule{distutils.extension}
2086\item \class{Command} from \refmodule{distutils.cmd}
2087\item \class{Distribution} from \refmodule{distutils.dist}
2088\end{itemize}
2089
2090A short description of each of these follows, but see the relevant
2091module for the full reference.
2092
2093\begin{classdesc*}{Extension}
2094
2095The Extension class describes a single C or \Cpp extension module in a
2096setup script. It accepts the following keyword arguments in it's
2097constructor
2098
2099\begin{tableiii}{c|l|l}{argument name}{argument name}{value}{type}
2100\lineiii{name}{the full name of the extension, including any packages
2101--- ie. \emph{not} a filename or pathname, but Python dotted name}{string}
2102\lineiii{sources}{list of source filenames, relative to the distribution
2103root (where the setup script lives), in Unix form (slash-separated) for
2104portability. Source files may be C, \Cpp, SWIG (.i), platform-specific
2105resource files, or whatever else is recognized by the \command{build_ext}
2106command as source for a Python extension.}{string}
2107\lineiii{include_dirs}{list of directories to search for C/\Cpp{} header
2108files (in \UNIX{} form for portability)}{string}
2109\lineiii{define_macros}{list of macros to define; each macro is defined
2110using a 2-tuple, where 'value' is either the string to define it to or
2111\code{None} to define it without a particular value (equivalent of
2112\code{\#define FOO} in source or \programopt{-DFOO} on \UNIX{} C
2113compiler command line) }{ (string,string)
2114tuple or (name,\code{None}) }
2115\lineiii{undef_macros}{list of macros to undefine explicitly}{string}
2116\lineiii{library_dirs}{list of directories to search for C/\Cpp{} libraries
2117at link time }{string}
2118\lineiii{libraries}{list of library names (not filenames or paths) to
2119link against }{string}
2120\lineiii{runtime_library_dirs}{list of directories to search for C/\Cpp{}
2121libraries at run time (for shared extensions, this is when the extension
2122is loaded)}{string}
2123\lineiii{extra_objects}{list of extra files to link with (eg. object
2124files not implied by 'sources', static library that must be explicitly
2125specified, binary resource files, etc.)}{string}
2126\lineiii{extra_compile_args}{any extra platform- and compiler-specific
2127information to use when compiling the source files in 'sources'. For
2128platforms and compilers where a command line makes sense, this is
2129typically a list of command-line arguments, but for other platforms it
2130could be anything.}{string}
2131\lineiii{extra_link_args}{any extra platform- and compiler-specific
2132information to use when linking object files together to create the
2133extension (or to create a new static Python interpreter). Similar
2134interpretation as for 'extra_compile_args'.}{string}
2135\lineiii{export_symbols}{list of symbols to be exported from a shared
2136extension. Not used on all platforms, and not generally necessary for
2137Python extensions, which typically export exactly one symbol: \code{init} +
2138extension_name. }{string}
2139\lineiii{depends}{list of files that the extension depends on }{string}
2140\lineiii{language}{extension language (i.e. \code{'c'}, \code{'c++'},
2141\code{'objc'}). Will be detected from the source extensions if not provided.
2142}{string}
2143\end{tableiii}
2144\end{classdesc*}
2145
2146\begin{classdesc*}{Distribution}
2147A \class{Distribution} describes how to build, install and package up a
2148Python software package.
2149
2150See the \function{setup()} function for a list of keyword arguments accepted
2151by the Distribution constructor. \function{setup()} creates a Distribution
2152instance.
2153\end{classdesc*}
2154
2155\begin{classdesc*}{Command}
2156A \class{Command} class (or rather, an instance of one of it's subclasses)
2157implement a single distutils command.
2158\end{classdesc*}
2159
2160
2161\section{\module{distutils.ccompiler} --- CCompiler base class}
2162\declaremodule{standard}{distutils.ccompiler}
2163\modulesynopsis{Abstract CCompiler class}
2164
2165This module provides the abstract base class for the \class{CCompiler}
2166classes. A \class{CCompiler} instance can be used for all the compile
2167and link steps needed to build a single project. Methods are provided to
2168set options for the compiler --- macro definitions, include directories,
2169link path, libraries and the like.
2170
2171This module provides the following functions.
2172
2173\begin{funcdesc}{gen_lib_options}{compiler, library_dirs, runtime_library_dirs, libraries}
2174Generate linker options for searching library directories and
2175linking with specific libraries. \var{libraries} and \var{library_dirs} are,
2176respectively, lists of library names (not filenames!) and search
2177directories. Returns a list of command-line options suitable for use
2178with some compiler (depending on the two format strings passed in).
2179\end{funcdesc}
2180
2181\begin{funcdesc}{gen_preprocess_options}{macros, include_dirs}
2182Generate C pre-processor options (-D, -U, -I) as used by at least
2183two types of compilers: the typical \UNIX{} compiler and Visual \Cpp.
2184\var{macros} is the usual thing, a list of 1- or 2-tuples, where \var{(name,)}
2185means undefine (-U) macro \var{name}, and \var{(name,value)} means define (-D)
2186macro \var{name} to \var{value}. \var{include_dirs} is just a list of directory
2187names to be added to the header file search path (-I). Returns a list
2188of command-line options suitable for either \UNIX{} compilers or Visual
2189\Cpp.
2190\end{funcdesc}
2191
2192\begin{funcdesc}{get_default_compiler}{osname, platform}
2193Determine the default compiler to use for the given platform.
2194
2195\var{osname} should be one of the standard Python OS names (i.e. the
2196ones returned by \var{os.name}) and \var{platform} the common value
2197returned by \var{sys.platform} for the platform in question.
2198
2199The default values are \code{os.name} and \code{sys.platform} in case the
2200parameters are not given.
2201\end{funcdesc}
2202
2203\begin{funcdesc}{new_compiler}{plat=\code{None}, compiler=\code{None}, verbose=\code{0}, dry_run=\code{0}, force=\code{0}}
2204Factory function to generate an instance of some CCompiler subclass
2205for the supplied platform/compiler combination. \var{plat} defaults
2206to \code{os.name} (eg. \code{'posix'}, \code{'nt'}), and \var{compiler}
2207defaults to the default compiler for that platform. Currently only
2208\code{'posix'} and \code{'nt'} are supported, and the default
2209compilers are ``traditional \UNIX{} interface'' (\class{UnixCCompiler}
2210class) and Visual \Cpp (\class{MSVCCompiler} class). Note that it's
2211perfectly possible to ask for a \UNIX{} compiler object under Windows,
2212and a Microsoft compiler object under \UNIX---if you supply a value
2213for \var{compiler}, \var{plat} is ignored.
2214% Is the posix/nt only thing still true? Mac OS X seems to work, and
2215% returns a UnixCCompiler instance. How to document this... hmm.
2216\end{funcdesc}
2217
2218\begin{funcdesc}{show_compilers}{}
2219Print list of available compilers (used by the
2220\longprogramopt{help-compiler} options to \command{build},
2221\command{build_ext}, \command{build_clib}).
2222\end{funcdesc}
2223
2224\begin{classdesc}{CCompiler}{\optional{verbose=\code{0}, dry_run=\code{0}, force=\code{0}}}
2225
2226The abstract base class \class{CCompiler} defines the interface that
2227must be implemented by real compiler classes. The class also has
2228some utility methods used by several compiler classes.
2229
2230The basic idea behind a compiler abstraction class is that each
2231instance can be used for all the compile/link steps in building a
2232single project. Thus, attributes common to all of those compile and
2233link steps --- include directories, macros to define, libraries to link
2234against, etc. --- are attributes of the compiler instance. To allow for
2235variability in how individual files are treated, most of those
2236attributes may be varied on a per-compilation or per-link basis.
2237
2238The constructor for each subclass creates an instance of the Compiler
2239object. Flags are \var{verbose} (show verbose output), \var{dry_run}
2240(don't actually execute the steps) and \var{force} (rebuild
2241everything, regardless of dependencies). All of these flags default to
2242\code{0} (off). Note that you probably don't want to instantiate
2243\class{CCompiler} or one of it's subclasses directly - use the
2244\function{distutils.CCompiler.new_compiler()} factory function
2245instead.
2246
2247The following methods allow you to manually alter compiler options for
2248the instance of the Compiler class.
2249
2250\begin{methoddesc}{add_include_dir}{dir}
2251Add \var{dir} to the list of directories that will be searched for
2252header files. The compiler is instructed to search directories in
2253the order in which they are supplied by successive calls to
2254\method{add_include_dir()}.
2255\end{methoddesc}
2256
2257\begin{methoddesc}{set_include_dirs}{dirs}
2258Set the list of directories that will be searched to \var{dirs} (a
2259list of strings). Overrides any preceding calls to
2260\method{add_include_dir()}; subsequent calls to
2261\method{add_include_dir()} add to the list passed to
2262\method{set_include_dirs()}. This does not affect any list of
2263standard include directories that the compiler may search by default.
2264\end{methoddesc}
2265
2266\begin{methoddesc}{add_library}{libname}
2267
2268Add \var{libname} to the list of libraries that will be included in
2269all links driven by this compiler object. Note that \var{libname}
2270should *not* be the name of a file containing a library, but the
2271name of the library itself: the actual filename will be inferred by
2272the linker, the compiler, or the compiler class (depending on the
2273platform).
2274
2275The linker will be instructed to link against libraries in the
2276order they were supplied to \method{add_library()} and/or
2277\method{set_libraries()}. It is perfectly valid to duplicate library
2278names; the linker will be instructed to link against libraries as
2279many times as they are mentioned.
2280\end{methoddesc}
2281
2282\begin{methoddesc}{set_libraries}{libnames}
2283Set the list of libraries to be included in all links driven by
2284this compiler object to \var{libnames} (a list of strings). This does
2285not affect any standard system libraries that the linker may
2286include by default.
2287\end{methoddesc}
2288
2289\begin{methoddesc}{add_library_dir}{dir}
2290Add \var{dir} to the list of directories that will be searched for
2291libraries specified to \method{add_library()} and
2292\method{set_libraries()}. The linker will be instructed to search for
2293libraries in the order they are supplied to \method{add_library_dir()}
2294and/or \method{set_library_dirs()}.
2295\end{methoddesc}
2296
2297\begin{methoddesc}{set_library_dirs}{dirs}
2298Set the list of library search directories to \var{dirs} (a list of
2299strings). This does not affect any standard library search path
2300that the linker may search by default.
2301\end{methoddesc}
2302
2303\begin{methoddesc}{add_runtime_library_dir}{dir}
2304Add \var{dir} to the list of directories that will be searched for
2305shared libraries at runtime.
2306\end{methoddesc}
2307
2308\begin{methoddesc}{set_runtime_library_dirs}{dirs}
2309Set the list of directories to search for shared libraries at
2310runtime to \var{dirs} (a list of strings). This does not affect any
2311standard search path that the runtime linker may search by
2312default.
2313\end{methoddesc}
2314
2315\begin{methoddesc}{define_macro}{name\optional{, value=\code{None}}}
2316Define a preprocessor macro for all compilations driven by this
2317compiler object. The optional parameter \var{value} should be a
2318string; if it is not supplied, then the macro will be defined
2319without an explicit value and the exact outcome depends on the
2320compiler used (XXX true? does ANSI say anything about this?)
2321\end{methoddesc}
2322
2323\begin{methoddesc}{undefine_macro}{name}
2324Undefine a preprocessor macro for all compilations driven by
2325this compiler object. If the same macro is defined by
2326\method{define_macro()} and undefined by \method{undefine_macro()}
2327the last call takes precedence (including multiple redefinitions or
2328undefinitions). If the macro is redefined/undefined on a
2329per-compilation basis (ie. in the call to \method{compile()}), then that
2330takes precedence.
2331\end{methoddesc}
2332
2333\begin{methoddesc}{add_link_object}{object}
2334Add \var{object} to the list of object files (or analogues, such as
2335explicitly named library files or the output of ``resource
2336compilers'') to be included in every link driven by this compiler
2337object.
2338\end{methoddesc}
2339
2340\begin{methoddesc}{set_link_objects}{objects}
2341Set the list of object files (or analogues) to be included in
2342every link to \var{objects}. This does not affect any standard object
2343files that the linker may include by default (such as system
2344libraries).
2345\end{methoddesc}
2346
2347The following methods implement methods for autodetection of compiler
2348options, providing some functionality similar to GNU \program{autoconf}.
2349
2350\begin{methoddesc}{detect_language}{sources}
2351Detect the language of a given file, or list of files. Uses the
2352instance attributes \member{language_map} (a dictionary), and
2353\member{language_order} (a list) to do the job.
2354\end{methoddesc}
2355
2356\begin{methoddesc}{find_library_file}{dirs, lib\optional{, debug=\code{0}}}
2357Search the specified list of directories for a static or shared
2358library file \var{lib} and return the full path to that file. If
2359\var{debug} is true, look for a debugging version (if that makes sense on
2360the current platform). Return \code{None} if \var{lib} wasn't found in any of
2361the specified directories.
2362\end{methoddesc}
2363
2364\begin{methoddesc}{has_function}{funcname \optional{, includes=\code{None}, include_dirs=\code{None}, libraries=\code{None}, library_dirs=\code{None}}}
2365Return a boolean indicating whether \var{funcname} is supported on
2366the current platform. The optional arguments can be used to
2367augment the compilation environment by providing additional include
2368files and paths and libraries and paths.
2369\end{methoddesc}
2370
2371\begin{methoddesc}{library_dir_option}{dir}
2372Return the compiler option to add \var{dir} to the list of
2373directories searched for libraries.
2374\end{methoddesc}
2375
2376\begin{methoddesc}{library_option}{lib}
2377Return the compiler option to add \var{dir} to the list of libraries
2378linked into the shared library or executable.
2379\end{methoddesc}
2380
2381\begin{methoddesc}{runtime_library_dir_option}{dir}
2382Return the compiler option to add \var{dir} to the list of
2383directories searched for runtime libraries.
2384\end{methoddesc}
2385
2386\begin{methoddesc}{set_executables}{**args}
2387Define the executables (and options for them) that will be run
2388to perform the various stages of compilation. The exact set of
2389executables that may be specified here depends on the compiler
2390class (via the 'executables' class attribute), but most will have:
2391
2392\begin{tableii}{l|l}{attribute}{attribute}{description}
2393\lineii{compiler}{the C/\Cpp{} compiler}
2394\lineii{linker_so}{linker used to create shared objects and libraries}
2395\lineii{linker_exe}{linker used to create binary executables}
2396\lineii{archiver}{static library creator}
2397\end{tableii}
2398
2399On platforms with a command-line (\UNIX, DOS/Windows), each of these
2400is a string that will be split into executable name and (optional)
2401list of arguments. (Splitting the string is done similarly to how
2402\UNIX{} shells operate: words are delimited by spaces, but quotes and
2403backslashes can override this. See
2404\function{distutils.util.split_quoted()}.)
2405\end{methoddesc}
2406
2407The following methods invoke stages in the build process.
2408
2409\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}}}
2410Compile one or more source files. Generates object files (e.g.
2411transforms a \file{.c} file to a \file{.o} file.)
2412
2413\var{sources} must be a list of filenames, most likely C/\Cpp
2414files, but in reality anything that can be handled by a
2415particular compiler and compiler class (eg. \class{MSVCCompiler} can
2416handle resource files in \var{sources}). Return a list of object
2417filenames, one per source filename in \var{sources}. Depending on
2418the implementation, not all source files will necessarily be
2419compiled, but all corresponding object filenames will be
2420returned.
2421
2422If \var{output_dir} is given, object files will be put under it, while
2423retaining their original path component. That is, \file{foo/bar.c}
2424normally compiles to \file{foo/bar.o} (for a \UNIX{} implementation); if
2425\var{output_dir} is \var{build}, then it would compile to
2426\file{build/foo/bar.o}.
2427
2428\var{macros}, if given, must be a list of macro definitions. A macro
2429definition is either a \var{(name, value)} 2-tuple or a \var{(name,)} 1-tuple.
2430The former defines a macro; if the value is \code{None}, the macro is
2431defined without an explicit value. The 1-tuple case undefines a
2432macro. Later definitions/redefinitions/undefinitions take
2433precedence.
2434
2435\var{include_dirs}, if given, must be a list of strings, the
2436directories to add to the default include file search path for this
2437compilation only.
2438
2439\var{debug} is a boolean; if true, the compiler will be instructed to
2440output debug symbols in (or alongside) the object file(s).
2441
2442\var{extra_preargs} and \var{extra_postargs} are implementation- dependent.
2443On platforms that have the notion of a command-line (e.g. \UNIX,
2444DOS/Windows), they are most likely lists of strings: extra
2445command-line arguments to prepand/append to the compiler command
2446line. On other platforms, consult the implementation class
2447documentation. In any event, they are intended as an escape hatch
2448for those occasions when the abstract compiler framework doesn't
2449cut the mustard.
2450
2451\var{depends}, if given, is a list of filenames that all targets
2452depend on. If a source file is older than any file in
2453depends, then the source file will be recompiled. This
2454supports dependency tracking, but only at a coarse
2455granularity.
2456
2457Raises \exception{CompileError} on failure.
2458\end{methoddesc}
2459
2460\begin{methoddesc}{create_static_lib}{objects, output_libname\optional{, output_dir=\code{None}, debug=\code{0}, target_lang=\code{None}}}
2461Link a bunch of stuff together to create a static library file.
2462The ``bunch of stuff'' consists of the list of object files supplied
2463as \var{objects}, the extra object files supplied to
2464\method{add_link_object()} and/or \method{set_link_objects()}, the libraries
2465supplied to \method{add_library()} and/or \method{set_libraries()}, and the
2466libraries supplied as \var{libraries} (if any).
2467
2468\var{output_libname} should be a library name, not a filename; the
2469filename will be inferred from the library name. \var{output_dir} is
2470the directory where the library file will be put. XXX defaults to what?
2471
2472\var{debug} is a boolean; if true, debugging information will be
2473included in the library (note that on most platforms, it is the
2474compile step where this matters: the \var{debug} flag is included here
2475just for consistency).
2476
2477\var{target_lang} is the target language for which the given objects
2478are being compiled. This allows specific linkage time treatment of
2479certain languages.
2480
2481Raises \exception{LibError} on failure.
2482\end{methoddesc}
2483
2484\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}}}
2485Link a bunch of stuff together to create an executable or
2486shared library file.
2487
2488The ``bunch of stuff'' consists of the list of object files supplied
2489as \var{objects}. \var{output_filename} should be a filename. If
2490\var{output_dir} is supplied, \var{output_filename} is relative to it
2491(i.e. \var{output_filename} can provide directory components if
2492needed).
2493
2494\var{libraries} is a list of libraries to link against. These are
2495library names, not filenames, since they're translated into
2496filenames in a platform-specific way (eg. \var{foo} becomes \file{libfoo.a}
2497on \UNIX{} and \file{foo.lib} on DOS/Windows). However, they can include a
2498directory component, which means the linker will look in that
2499specific directory rather than searching all the normal locations.
2500
2501\var{library_dirs}, if supplied, should be a list of directories to
2502search for libraries that were specified as bare library names
2503(ie. no directory component). These are on top of the system
2504default and those supplied to \method{add_library_dir()} and/or
2505\method{set_library_dirs()}. \var{runtime_library_dirs} is a list of
2506directories that will be embedded into the shared library and used
2507to search for other shared libraries that *it* depends on at
2508run-time. (This may only be relevant on \UNIX.)
2509
2510\var{export_symbols} is a list of symbols that the shared library will
2511export. (This appears to be relevant only on Windows.)
2512
2513\var{debug} is as for \method{compile()} and \method{create_static_lib()},
2514with the slight distinction that it actually matters on most platforms (as
2515opposed to \method{create_static_lib()}, which includes a \var{debug} flag
2516mostly for form's sake).
2517
2518\var{extra_preargs} and \var{extra_postargs} are as for \method{compile()}
2519(except of course that they supply command-line arguments for the
2520particular linker being used).
2521
2522\var{target_lang} is the target language for which the given objects
2523are being compiled. This allows specific linkage time treatment of
2524certain languages.
2525
2526Raises \exception{LinkError} on failure.
2527\end{methoddesc}
2528
2529\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}}}
2530Link an executable.
2531\var{output_progname} is the name of the file executable,
2532while \var{objects} are a list of object filenames to link in. Other arguments
2533are as for the \method{link} method.
2534\end{methoddesc}
2535
2536\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}}}
2537Link a shared library. \var{output_libname} is the name of the output
2538library, while \var{objects} is a list of object filenames to link in.
2539Other arguments are as for the \method{link} method.
2540\end{methoddesc}
2541
2542\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}}}
2543Link a shared object. \var{output_filename} is the name of the shared object
2544that will be created, while \var{objects} is a list of object filenames
2545to link in. Other arguments are as for the \method{link} method.
2546\end{methoddesc}
2547
2548\begin{methoddesc}{preprocess}{source\optional{, output_file=\code{None}, macros=\code{None}, include_dirs=\code{None}, extra_preargs=\code{None}, extra_postargs=\code{None}}}
2549Preprocess a single C/\Cpp{} source file, named in \var{source}.
2550Output will be written to file named \var{output_file}, or \var{stdout} if
2551\var{output_file} not supplied. \var{macros} is a list of macro
2552definitions as for \method{compile()}, which will augment the macros set
2553with \method{define_macro()} and \method{undefine_macro()}.
2554\var{include_dirs} is a list of directory names that will be added to the
2555default list, in the same way as \method{add_include_dir()}.
2556
2557Raises \exception{PreprocessError} on failure.
2558\end{methoddesc}
2559
2560The following utility methods are defined by the \class{CCompiler} class,
2561for use by the various concrete subclasses.
2562
2563\begin{methoddesc}{executable_filename}{basename\optional{, strip_dir=\code{0}, output_dir=\code{''}}}
2564Returns the filename of the executable for the given \var{basename}.
2565Typically for non-Windows platforms this is the same as the basename,
2566while Windows will get a \file{.exe} added.
2567\end{methoddesc}
2568
2569\begin{methoddesc}{library_filename}{libname\optional{, lib_type=\code{'static'}, strip_dir=\code{0}, output_dir=\code{''}}}
2570Returns the filename for the given library name on the current platform.
2571On \UNIX{} a library with \var{lib_type} of \code{'static'} will typically
2572be of the form \file{liblibname.a}, while a \var{lib_type} of \code{'dynamic'}
2573will be of the form \file{liblibname.so}.
2574\end{methoddesc}
2575
2576\begin{methoddesc}{object_filenames}{source_filenames\optional{, strip_dir=\code{0}, output_dir=\code{''}}}
2577Returns the name of the object files for the given source files.
2578\var{source_filenames} should be a list of filenames.
2579\end{methoddesc}
2580
2581\begin{methoddesc}{shared_object_filename}{basename\optional{, strip_dir=\code{0}, output_dir=\code{''}}}
2582Returns the name of a shared object file for the given file name \var{basename}.
2583\end{methoddesc}
2584
2585\begin{methoddesc}{execute}{func, args\optional{, msg=\code{None}, level=\code{1}}}
2586Invokes \function{distutils.util.execute()} This method invokes a
2587Python function \var{func} with the given arguments \var{args}, after
2588logging and taking into account the \var{dry_run} flag. XXX see also.
2589\end{methoddesc}
2590
2591\begin{methoddesc}{spawn}{cmd}
2592Invokes \function{distutils.util.spawn()}. This invokes an external
2593process to run the given command. XXX see also.
2594\end{methoddesc}
2595
2596\begin{methoddesc}{mkpath}{name\optional{, mode=\code{511}}}
2597
2598Invokes \function{distutils.dir_util.mkpath()}. This creates a directory
2599and any missing ancestor directories. XXX see also.
2600\end{methoddesc}
2601
2602\begin{methoddesc}{move_file}{src, dst}
2603Invokes \method{distutils.file_util.move_file()}. Renames \var{src} to
2604\var{dst}. XXX see also.
2605\end{methoddesc}
2606
2607\begin{methoddesc}{announce}{msg\optional{, level=\code{1}}}
2608Write a message using \function{distutils.log.debug()}. XXX see also.
2609\end{methoddesc}
2610
2611\begin{methoddesc}{warn}{msg}
2612Write a warning message \var{msg} to standard error.
2613\end{methoddesc}
2614
2615\begin{methoddesc}{debug_print}{msg}
2616If the \var{debug} flag is set on this \class{CCompiler} instance, print
2617\var{msg} to standard output, otherwise do nothing.
2618\end{methoddesc}
2619
2620\end{classdesc}
2621
2622%\subsection{Compiler-specific modules}
2623%
2624%The following modules implement concrete subclasses of the abstract
2625%\class{CCompiler} class. They should not be instantiated directly, but should
2626%be created using \function{distutils.ccompiler.new_compiler()} factory
2627%function.
2628
2629\section{\module{distutils.unixccompiler} --- Unix C Compiler}
2630\declaremodule{standard}{distutils.unixccompiler}
2631\modulesynopsis{UNIX C Compiler}
2632
2633This module provides the \class{UnixCCompiler} class, a subclass of
2634\class{CCompiler} that handles the typical \UNIX-style command-line
2635C compiler:
2636
2637\begin{itemize}
2638\item macros defined with \programopt{-D\var{name}\optional{=value}}
2639\item macros undefined with \programopt{-U\var{name}}
2640\item include search directories specified with
2641 \programopt{-I\var{dir}}
2642\item libraries specified with \programopt{-l\var{lib}}
2643\item library search directories specified with \programopt{-L\var{dir}}
2644\item compile handled by \program{cc} (or similar) executable with
2645 \programopt{-c} option: compiles \file{.c} to \file{.o}
2646\item link static library handled by \program{ar} command (possibly
2647 with \program{ranlib})
2648\item link shared library handled by \program{cc} \programopt{-shared}
2649\end{itemize}
2650
2651\section{\module{distutils.msvccompiler} --- Microsoft Compiler}
2652\declaremodule{standard}{distutils.msvccompiler}
2653\modulesynopsis{Microsoft Compiler}
2654
2655This module provides \class{MSVCCompiler}, an implementation of the abstract
2656\class{CCompiler} class for Microsoft Visual Studio. It should also work using
2657the freely available compiler provided as part of the .Net SDK download. XXX
2658download link.
2659
2660\section{\module{distutils.bcppcompiler} --- Borland Compiler}
2661\declaremodule{standard}{distutils.bcppcompiler}
2662This module provides \class{BorlandCCompiler}, an subclass of the abstract \class{CCompiler} class for the Borland \Cpp{} compiler.
2663
2664\section{\module{distutils.cygwincompiler} --- Cygwin Compiler}
2665\declaremodule{standard}{distutils.cygwinccompiler}
2666
2667This module provides the \class{CygwinCCompiler} class, a subclass of \class{UnixCCompiler} that
2668handles the Cygwin port of the GNU C compiler to Windows. It also contains
2669the Mingw32CCompiler class which handles the mingw32 port of GCC (same as
2670cygwin in no-cygwin mode).
2671
2672\section{\module{distutils.emxccompiler} --- OS/2 EMX Compiler}
2673\declaremodule{standard}{distutils.emxccompiler}
2674\modulesynopsis{OS/2 EMX Compiler support}
2675
2676This module provides the EMXCCompiler class, a subclass of \class{UnixCCompiler} that handles the EMX port of the GNU C compiler to OS/2.
2677
2678\section{\module{distutils.mwerkscompiler} --- Metrowerks CodeWarrior support}
2679\declaremodule{standard}{distutils.mwerkscompiler}
2680\modulesynopsis{Metrowerks CodeWarrior support}
2681
2682Contains \class{MWerksCompiler}, an implementation of the abstract
2683\class{CCompiler} class for MetroWerks CodeWarrior on the Macintosh. Needs work to support CW on Windows.
2684
2685
2686%\subsection{Utility modules}
2687%
2688%The following modules all provide general utility functions. They haven't
2689%all been documented yet.
2690
2691\section{\module{distutils.archive_util} ---
2692 Archiving utilities}
2693\declaremodule[distutils.archiveutil]{standard}{distutils.archive_util}
2694\modulesynopsis{Utility functions for creating archive files (tarballs, zip files, ...)}
2695
2696This module provides a few functions for creating archive files, such as
2697tarballs or zipfiles.
2698
2699\begin{funcdesc}{make_archive}{base_name, format\optional{, root_dir=\code{None}, base_dir=\code{None}, verbose=\code{0}, dry_run=\code{0}}}
2700Create an archive file (eg. \code{zip} or \code{tar}). \var{base_name}
2701is the name of the file to create, minus any format-specific extension;
2702\var{format} is the archive format: one of \code{zip}, \code{tar},
2703\code{ztar}, or \code{gztar}.
2704\var{root_dir} is a directory that will be the root directory of the
2705archive; ie. we typically \code{chdir} into \var{root_dir} before
2706creating the archive. \var{base_dir} is the directory where we start
2707archiving from; ie. \var{base_dir} will be the common prefix of all files and
2708directories in the archive. \var{root_dir} and \var{base_dir} both default
2709to the current directory. Returns the name of the archive file.
2710
2711\warning{This should be changed to support bz2 files}
2712\end{funcdesc}
2713
2714\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),
2715\code{'compress'}, \code{'bzip2'}, or \code{None}. Both \code{'tar'}
2716and the compression utility named by \var{'compress'} must be on the
2717default program search path, so this is probably \UNIX-specific. The
2718output tar file will be named \file{\var{base_dir}.tar}, possibly plus
2719the appropriate compression extension (\file{.gz}, \file{.bz2} or
2720\file{.Z}). Return the output filename.
2721
2722\warning{This should be replaced with calls to the \module{tarfile} module.}
2723\end{funcdesc}
2724
2725\begin{funcdesc}{make_zipfile}{base_name, base_dir\optional{, verbose=\code{0}, dry_run=\code{0}}}
2726Create a zip file from all files in and under \var{base_dir}. The output
2727zip file will be named \var{base_dir} + \file{.zip}. Uses either the
2728\module{zipfile} Python module (if available) or the InfoZIP \file{zip}
2729utility (if installed and found on the default search path). If neither
2730tool is available, raises \exception{DistutilsExecError}.
2731Returns the name of the output zip file.
2732\end{funcdesc}
2733
2734\section{\module{distutils.dep_util} --- Dependency checking}
2735\declaremodule[distutils.deputil]{standard}{distutils.dep_util}
2736\modulesynopsis{Utility functions for simple dependency checking}
2737
2738This module provides functions for performing simple, timestamp-based
2739dependency of files and groups of files; also, functions based entirely
2740on such timestamp dependency analysis.
2741
2742\begin{funcdesc}{newer}{source, target}
2743Return true if \var{source} exists and is more recently modified than
2744\var{target}, or if \var{source} exists and \var{target} doesn't.
2745Return false if both exist and \var{target} is the same age or newer
2746than \var{source}.
2747Raise \exception{DistutilsFileError} if \var{source} does not exist.
2748\end{funcdesc}
2749
2750\begin{funcdesc}{newer_pairwise}{sources, targets}
2751Walk two filename lists in parallel, testing if each source is newer
2752than its corresponding target. Return a pair of lists (\var{sources},
2753\var{targets}) where source is newer than target, according to the semantics
2754of \function{newer()}
2755%% equivalent to a listcomp...
2756\end{funcdesc}
2757
2758\begin{funcdesc}{newer_group}{sources, target\optional{, missing=\code{'error'}}}
2759Return true if \var{target} is out-of-date with respect to any file
2760listed in \var{sources} In other words, if \var{target} exists and is newer
2761than every file in \var{sources}, return false; otherwise return true.
2762\var{missing} controls what we do when a source file is missing; the
2763default (\code{'error'}) is to blow up with an \exception{OSError} from
2764inside \function{os.stat()};
2765if it is \code{'ignore'}, we silently drop any missing source files; if it is
2766\code{'newer'}, any missing source files make us assume that \var{target} is
2767out-of-date (this is handy in ``dry-run'' mode: it'll make you pretend to
2768carry out commands that wouldn't work because inputs are missing, but
2769that doesn't matter because you're not actually going to run the
2770commands).
2771\end{funcdesc}
2772
2773\section{\module{distutils.dir_util} --- Directory tree operations}
2774\declaremodule[distutils.dirutil]{standard}{distutils.dir_util}
2775\modulesynopsis{Utility functions for operating on directories and directory trees}
2776
2777This module provides functions for operating on directories and trees
2778of directories.
2779
2780\begin{funcdesc}{mkpath}{name\optional{, mode=\code{0777}, verbose=\code{0}, dry_run=\code{0}}}
2781Create a directory and any missing ancestor directories. If the
2782directory already exists (or if \var{name} is the empty string, which
2783means the current directory, which of course exists), then do
2784nothing. Raise \exception{DistutilsFileError} if unable to create some
2785directory along the way (eg. some sub-path exists, but is a file
2786rather than a directory). If \var{verbose} is true, print a one-line
2787summary of each mkdir to stdout. Return the list of directories
2788actually created.
2789\end{funcdesc}
2790
2791\begin{funcdesc}{create_tree}{base_dir, files\optional{, mode=\code{0777}, verbose=\code{0}, dry_run=\code{0}}}
2792Create all the empty directories under \var{base_dir} needed to
2793put \var{files} there. \var{base_dir} is just the a name of a directory
2794which doesn't necessarily exist yet; \var{files} is a list of filenames
2795to be interpreted relative to \var{base_dir}. \var{base_dir} + the
2796directory portion of every file in \var{files} will be created if it
2797doesn't already exist. \var{mode}, \var{verbose} and \var{dry_run} flags
2798are as for \function{mkpath()}.
2799\end{funcdesc}
2800
2801\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}}}
2802Copy an entire directory tree \var{src} to a new location \var{dst}. Both
2803\var{src} and \var{dst} must be directory names. If \var{src} is not a
2804directory, raise \exception{DistutilsFileError}. If \var{dst} does
2805not exist, it is created with \var{mkpath()}. The end result of the
2806copy is that every file in \var{src} is copied to \var{dst}, and
2807directories under \var{src} are recursively copied to \var{dst}.
2808Return the list of files that were copied or might have been copied,
2809using their output name. The return value is unaffected by \var{update}
2810or \var{dry_run}: it is simply the list of all files under \var{src},
2811with the names changed to be under \var{dst}.
2812
2813\var{preserve_mode} and \var{preserve_times} are the same as for
2814\function{copy_file} in \refmodule[distutils.fileutil]{distutils.file_util};
2815note that they only apply to regular files, not to directories. If
2816\var{preserve_symlinks} is true, symlinks will be copied as symlinks
2817(on platforms that support them!); otherwise (the default), the
2818destination of the symlink will be copied. \var{update} and
2819\var{verbose} are the same as for
2820\function{copy_file()}.
2821\end{funcdesc}
2822
2823\begin{funcdesc}{remove_tree}{directory\optional{verbose=\code{0}, dry_run=\code{0}}}
2824Recursively remove \var{directory} and all files and directories underneath
2825it. Any errors are ignored (apart from being reported to \code{stdout} if
2826\var{verbose} is true).
2827\end{funcdesc}
2828
2829\XXX{Some of this could be replaced with the shutil module?}
2830
2831\section{\module{distutils.file_util} --- Single file operations}
2832\declaremodule[distutils.fileutil]{standard}{distutils.file_util}
2833\modulesynopsis{Utility functions for operating on single files}
2834
2835This module contains some utility functions for operating on individual files.
2836
2837\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}}}
2838Copy file \var{src} to \var{dst}. If \var{dst} is a directory, then
2839\var{src} is copied there with the same name; otherwise, it must be a
2840filename. (If the file exists, it will be ruthlessly clobbered.) If
2841\var{preserve_mode} is true (the default), the file's mode (type and
2842permission bits, or whatever is analogous on the current platform) is
2843copied. If \var{preserve_times} is true (the default), the last-modified
2844and last-access times are copied as well. If \var{update} is true,
2845\var{src} will only be copied if \var{dst} does not exist, or if
2846\var{dst} does exist but is older than \var{src}.
2847
2848\var{link} allows you to make hard links (using \function{os.link}) or
2849symbolic links (using \function{os.symlink}) instead of copying: set it
2850to \code{'hard'} or \code{'sym'}; if it is \code{None} (the default),
2851files are copied. Don't set \var{link} on systems that don't support
2852it: \function{copy_file()} doesn't check if hard or symbolic linking is
2853available.
2854
2855Under Mac OS 9, uses the native file copy function in \module{macostools};
2856on other systems, uses \var{_copy_file_contents()} to copy file contents.
2857
2858Return a tuple \samp{(dest_name, copied)}: \var{dest_name} is the actual
2859name of the output file, and \var{copied} is true if the file was copied
2860(or would have been copied, if \var{dry_run} true).
2861% XXX if the destination file already exists, we clobber it if
2862% copying, but blow up if linking. Hmmm. And I don't know what
2863% macostools.copyfile() does. Should definitely be consistent, and
2864% should probably blow up if destination exists and we would be
2865% changing it (ie. it's not already a hard/soft link to src OR
2866% (not update) and (src newer than dst)).
2867\end{funcdesc}
2868
2869\begin{funcdesc}{move_file}{src, dst\optional{verbose, dry_run}}
2870Move file \var{src} to \var{dst}. If \var{dst} is a directory, the file will
2871be moved into it with the same name; otherwise, \var{src} is just renamed
2872to \var{dst}. Returns the new full name of the file.
2873\warning{Handles cross-device moves on Unix using \function{copy_file()}.
2874What about other systems???}
2875\end{funcdesc}
2876
2877\begin{funcdesc}{write_file}{filename, contents}
2878Create a file called \var{filename} and write \var{contents} (a
2879sequence of strings without line terminators) to it.
2880\end{funcdesc}
2881
Thomas Heller949f6612004-06-18 06:55:28 +00002882\section{\module{distutils.util} --- Miscellaneous other utility functions}
Fred Drake6fca7cc2004-03-23 18:43:03 +00002883\declaremodule{standard}{distutils.util}
2884\modulesynopsis{Miscellaneous other utility functions}
2885
2886This module contains other assorted bits and pieces that don't fit into
2887any other utility module.
2888
2889\begin{funcdesc}{get_platform}{}
2890Return a string that identifies the current platform. This is used
2891mainly to distinguish platform-specific build directories and
2892platform-specific built distributions. Typically includes the OS name
2893and version and the architecture (as supplied by 'os.uname()'),
2894although the exact information included depends on the OS; eg. for IRIX
2895the architecture isn't particularly important (IRIX only runs on SGI
2896hardware), but for Linux the kernel version isn't particularly
2897important.
2898
2899Examples of returned values:
2900\begin{itemize}
2901\item \code{linux-i586}
2902\item \code{linux-alpha}
2903\item \code{solaris-2.6-sun4u}
2904\item \code{irix-5.3}
2905\item \code{irix64-6.2}
2906\end{itemize}
2907
2908For non-\POSIX{} platforms, currently just returns \code{sys.platform}.
2909% XXX isn't this also provided by some other non-distutils module?
2910\end{funcdesc}
2911
2912\begin{funcdesc}{convert_path}{pathname}
2913Return 'pathname' as a name that will work on the native filesystem,
2914i.e. split it on '/' and put it back together again using the current
2915directory separator. Needed because filenames in the setup script are
2916always supplied in Unix style, and have to be converted to the local
2917convention before we can actually use them in the filesystem. Raises
2918\exception{ValueError} on non-\UNIX-ish systems if \var{pathname} either
2919starts or ends with a slash.
2920\end{funcdesc}
2921
2922\begin{funcdesc}{change_root}{new_root, pathname}
2923Return \var{pathname} with \var{new_root} prepended. If \var{pathname} is
2924relative, this is equivalent to \samp{os.path.join(new_root,pathname)}
2925Otherwise, it requires making \var{pathname} relative and then joining the
2926two, which is tricky on DOS/Windows and Mac OS.
2927\end{funcdesc}
2928
2929\begin{funcdesc}{check_environ}{}
2930Ensure that 'os.environ' has all the environment variables we
2931guarantee that users can use in config files, command-line options,
2932etc. Currently this includes:
2933\begin{itemize}
2934\item \envvar{HOME} - user's home directory (\UNIX{} only)
2935\item \envvar{PLAT} - description of the current platform, including
2936 hardware and OS (see \function{get_platform()})
2937\end{itemize}
2938\end{funcdesc}
2939
2940\begin{funcdesc}{subst_vars}{s, local_vars}
2941Perform shell/Perl-style variable substitution on \var{s}. Every
2942occurrence of \code{\$} followed by a name is considered a variable, and
2943variable is substituted by the value found in the \var{local_vars}
2944dictionary, or in \code{os.environ} if it's not in \var{local_vars}.
2945\var{os.environ} is first checked/augmented to guarantee that it contains
2946certain values: see \function{check_environ()}. Raise \exception{ValueError}
2947for any variables not found in either \var{local_vars} or \code{os.environ}.
2948
2949Note that this is not a fully-fledged string interpolation function. A
2950valid \code{\$variable} can consist only of upper and lower case letters,
2951numbers and an underscore. No \{ \} or \( \) style quoting is available.
2952\end{funcdesc}
2953
2954\begin{funcdesc}{grok_environment_error}{exc\optional{, prefix=\samp{'error: '}}}
2955Generate a useful error message from an \exception{EnvironmentError}
2956(\exception{IOError} or \exception{OSError}) exception object.
2957Handles Python 1.5.1 and later styles, and does what it can to deal with
2958exception objects that don't have a filename (which happens when the error
2959is due to a two-file operation, such as \function{rename()} or
2960\function{link()}). Returns the error message as a string prefixed
2961with \var{prefix}.
2962\end{funcdesc}
2963
2964\begin{funcdesc}{split_quoted}{s}
2965Split a string up according to Unix shell-like rules for quotes and
2966backslashes. In short: words are delimited by spaces, as long as those
2967spaces are not escaped by a backslash, or inside a quoted string.
2968Single and double quotes are equivalent, and the quote characters can
2969be backslash-escaped. The backslash is stripped from any two-character
2970escape sequence, leaving only the escaped character. The quote
2971characters are stripped from any quoted string. Returns a list of
2972words.
2973% Should probably be moved into the standard library.
2974\end{funcdesc}
2975
2976\begin{funcdesc}{execute}{func, args\optional{, msg=\code{None}, verbose=\code{0}, dry_run=\code{0}}}
2977Perform some action that affects the outside world (for instance,
2978writing to the filesystem). Such actions are special because they
2979are disabled by the \var{dry_run} flag. This method takes
2980care of all that bureaucracy for you; all you have to do is supply the
2981function to call and an argument tuple for it (to embody the
2982``external action'' being performed), and an optional message to
2983print.
2984\end{funcdesc}
2985
2986\begin{funcdesc}{strtobool}{val}
2987Convert a string representation of truth to true (1) or false (0).
2988
2989True values are \code{y}, \code{yes}, \code{t}, \code{true}, \code{on}
2990and \code{1}; false values are \code{n}, \code{no}, \code{f}, \code{false},
2991\code{off} and \code{0}. Raises \exception{ValueError} if \var{val}
2992is anything else.
2993\end{funcdesc}
2994
2995\begin{funcdesc}{byte_compile}{py_files\optional{,
2996 optimize=\code{0}, force=\code{0},
2997 prefix=\code{None}, base_dir=\code{None},
2998 verbose=\code{1}, dry_run=\code{0},
2999 direct=\code{None}}}
3000Byte-compile a collection of Python source files to either \file{.pyc}
3001or \file{.pyo} files in the same directory. \var{py_files} is a list of files
3002to compile; any files that don't end in \file{.py} are silently skipped.
3003\var{optimize} must be one of the following:
3004\begin{itemize}
3005\item \code{0} - don't optimize (generate \file{.pyc})
3006\item \code{1} - normal optimization (like \samp{python -O})
3007\item \code{2} - extra optimization (like \samp{python -OO})
3008\end{itemize}
3009
3010If \var{force} is true, all files are recompiled regardless of
3011timestamps.
3012
3013The source filename encoded in each bytecode file defaults to the
3014filenames listed in \var{py_files}; you can modify these with \var{prefix} and
3015\var{basedir}. \var{prefix} is a string that will be stripped off of each
3016source filename, and \var{base_dir} is a directory name that will be
3017prepended (after \var{prefix} is stripped). You can supply either or both
3018(or neither) of \var{prefix} and \var{base_dir}, as you wish.
3019
3020If \var{dry_run} is true, doesn't actually do anything that would
3021affect the filesystem.
3022
3023Byte-compilation is either done directly in this interpreter process
3024with the standard \module{py_compile} module, or indirectly by writing a
3025temporary script and executing it. Normally, you should let
3026\function{byte_compile()} figure out to use direct compilation or not (see
3027the source for details). The \var{direct} flag is used by the script
3028generated in indirect mode; unless you know what you're doing, leave
3029it set to \code{None}.
3030\end{funcdesc}
3031
3032\begin{funcdesc}{rfc822_escape}{header}
3033Return a version of \var{header} escaped for inclusion in an
3034\rfc{822} header, by ensuring there are 8 spaces space after each newline.
3035Note that it does no other modification of the string.
3036% this _can_ be replaced
3037\end{funcdesc}
3038
3039%\subsection{Distutils objects}
3040
3041\section{\module{distutils.dist} --- The Distribution class}
3042\declaremodule{standard}{distutils.dist}
3043\modulesynopsis{Provides the Distribution class, which represents the
3044 module distribution being built/installed/distributed}
3045
3046This module provides the \class{Distribution} class, which represents
3047the module distribution being built/installed/distributed.
3048
3049
3050\section{\module{distutils.extension} --- The Extension class}
3051\declaremodule{standard}{distutils.extension}
3052\modulesynopsis{Provides the Extension class, used to describe
3053 C/\Cpp{} extension modules in setup scripts}
3054
3055This module provides the \class{Extension} class, used to describe
3056C/\Cpp{} extension modules in setup scripts.
3057
3058%\subsection{Ungrouped modules}
3059%The following haven't been moved into a more appropriate section yet.
3060
3061\section{\module{distutils.debug} --- Distutils debug mode}
3062\declaremodule{standard}{distutils.debug}
3063\modulesynopsis{Provides the debug flag for distutils}
3064
3065This module provides the DEBUG flag.
3066
3067\section{\module{distutils.errors} --- Distutils exceptions}
3068\declaremodule{standard}{distutils.errors}
3069\modulesynopsis{Provides standard distutils exceptions}
3070
3071Provides exceptions used by the Distutils modules. Note that Distutils
3072modules may raise standard exceptions; in particular, SystemExit is
3073usually raised for errors that are obviously the end-user's fault
3074(eg. bad command-line arguments).
3075
3076This module is safe to use in \samp{from ... import *} mode; it only exports
3077symbols whose names start with \code{Distutils} and end with \code{Error}.
3078
3079\section{\module{distutils.fancy_getopt}
3080 --- Wrapper around the standard getopt module}
3081\declaremodule[distutils.fancygetopt]{standard}{distutils.fancy_getopt}
3082\modulesynopsis{Additional \module{getopt} functionality}
3083
3084This module provides a wrapper around the standard \module{getopt}
3085module that provides the following additional features:
3086
3087\begin{itemize}
3088\item short and long options are tied together
3089\item options have help strings, so \function{fancy_getopt} could potentially
3090create a complete usage summary
3091\item options set attributes of a passed-in object
3092\item boolean options can have ``negative aliases'' --- eg. if
3093\longprogramopt{quiet} is the ``negative alias'' of
3094\longprogramopt{verbose}, then \longprogramopt{quiet} on the command
3095line sets \var{verbose} to false.
3096
3097\end{itemize}
3098
3099\XXX{Should be replaced with \module{optik} (which is also now
3100known as \module{optparse} in Python 2.3 and later).}
3101
3102\begin{funcdesc}{fancy_getopt}{options, negative_opt, object, args}
3103Wrapper function. \var{options} is a list of
3104\samp{(long_option, short_option, help_string)} 3-tuples as described in the
3105constructor for \class{FancyGetopt}. \var{negative_opt} should be a dictionary
3106mapping option names to option names, both the key and value should be in the
3107\var{options} list. \var{object} is an object which will be used to store
3108values (see the \method{getopt()} method of the \class{FancyGetopt} class).
3109\var{args} is the argument list. Will use \code{sys.argv[1:]} if you
3110pass \code{None} as \var{args}.
3111\end{funcdesc}
3112
3113\begin{funcdesc}{wrap_text}{text, width}
3114Wraps \var{text} to less than \var{width} wide.
3115
3116\warning{Should be replaced with \module{textwrap} (which is available
3117in Python 2.3 and later).}
3118\end{funcdesc}
3119
3120\begin{classdesc}{FancyGetopt}{\optional{option_table=\code{None}}}
3121The option_table is a list of 3-tuples: \samp{(long_option,
3122short_option, help_string)}
3123
3124If an option takes an argument, it's \var{long_option} should have \code{'='}
3125appended; \var{short_option} should just be a single character, no \code{':'}
3126in any case. \var{short_option} should be \code{None} if a \var{long_option}
3127doesn't have a corresponding \var{short_option}. All option tuples must have
3128long options.
3129\end{classdesc}
3130
3131The \class{FancyGetopt} class provides the following methods:
3132
3133\begin{methoddesc}{getopt}{\optional{args=\code{None}, object=\code{None}}}
3134Parse command-line options in args. Store as attributes on \var{object}.
3135
3136If \var{args} is \code{None} or not supplied, uses \code{sys.argv[1:]}. If
3137\var{object} is \code{None} or not supplied, creates a new \class{OptionDummy}
3138instance, stores option values there, and returns a tuple \samp{(args,
3139object)}. If \var{object} is supplied, it is modified in place and
3140\function{getopt()} just returns \var{args}; in both cases, the returned
3141\var{args} is a modified copy of the passed-in \var{args} list, which
3142is left untouched.
3143% and args returned are?
3144\end{methoddesc}
3145
3146\begin{methoddesc}{get_option_order}{}
3147Returns the list of \samp{(option, value)} tuples processed by the
3148previous run of \method{getopt()} Raises \exception{RuntimeError} if
3149\method{getopt()} hasn't been called yet.
3150\end{methoddesc}
3151
3152\begin{methoddesc}{generate_help}{\optional{header=\code{None}}}
3153Generate help text (a list of strings, one per suggested line of
3154output) from the option table for this \class{FancyGetopt} object.
3155
3156If supplied, prints the supplied \var{header} at the top of the help.
3157\end{methoddesc}
3158
3159\section{\module{distutils.filelist} --- The FileList class}
3160\declaremodule{standard}{distutils.filelist}
3161\modulesynopsis{The \class{FileList} class, used for poking about the
3162 file system and building lists of files.}
3163
3164This module provides the \class{FileList} class, used for poking about
3165the filesystem and building lists of files.
3166
3167
3168\section{\module{distutils.log} --- Simple PEP 282-style logging}
3169\declaremodule{standard}{distutils.log}
3170\modulesynopsis{A simple logging mechanism, \pep{282}-style}
3171
3172\warning{Should be replaced with standard \module{logging} module.}
3173
3174%\subsubsection{\module{} --- }
3175%\declaremodule{standard}{distutils.magic}
3176%\modulesynopsis{ }
3177
3178
3179\section{\module{distutils.spawn} --- Spawn a sub-process}
3180\declaremodule{standard}{distutils.spawn}
3181\modulesynopsis{Provides the spawn() function}
3182
3183This module provides the \function{spawn()} function, a front-end to
3184various platform-specific functions for launching another program in a
3185sub-process.
3186Also provides \function{find_executable()} to search the path for a given
3187executable name.
3188
3189
Fred Drakeab70b382001-08-02 15:13:15 +00003190\input{sysconfig}
Greg Ward16aafcd2000-04-09 04:06:44 +00003191
3192
Fred Drake6fca7cc2004-03-23 18:43:03 +00003193\section{\module{distutils.text_file} --- The TextFile class}
3194\declaremodule[distutils.textfile]{standard}{distutils.text_file}
3195\modulesynopsis{provides the TextFile class, a simple interface to text files}
3196
3197This module provides the \class{TextFile} class, which gives an interface
3198to text files that (optionally) takes care of stripping comments, ignoring
3199blank lines, and joining lines with backslashes.
3200
3201\begin{classdesc}{TextFile}{\optional{filename=\code{None}, file=\code{None}, **options}}
3202This class provides a file-like object that takes care of all
3203the things you commonly want to do when processing a text file
3204that has some line-by-line syntax: strip comments (as long as \code{\#}
3205is your comment character), skip blank lines, join adjacent lines by
3206escaping the newline (ie. backslash at end of line), strip
3207leading and/or trailing whitespace. All of these are optional
3208and independently controllable.
3209
3210The class provides a \method{warn()} method so you can generate
3211warning messages that report physical line number, even if the
3212logical line in question spans multiple physical lines. Also
3213provides \method{unreadline()} for implementing line-at-a-time lookahead.
3214
3215\class{TextFile} instances are create with either \var{filename}, \var{file},
3216or both. \exception{RuntimeError} is raised if both are \code{None}.
3217\var{filename} should be a string, and \var{file} a file object (or
3218something that provides \method{readline()} and \method{close()}
3219methods). It is recommended that you supply at least \var{filename},
3220so that \class{TextFile} can include it in warning messages. If
3221\var{file} is not supplied, TextFile creates its own using the
3222\var{open()} builtin.
3223
3224The options are all boolean, and affect the values returned by
3225\var{readline()}
3226
3227\begin{tableiii}{c|l|l}{option name}{option name}{description}{default}
3228\lineiii{strip_comments}{
3229strip from \character{\#} to end-of-line, as well as any whitespace
3230leading up to the \character{\#}---unless it is escaped by a backslash}
3231{true}
3232\lineiii{lstrip_ws}{
3233strip leading whitespace from each line before returning it}
3234{false}
3235\lineiii{rstrip_ws}{
3236strip trailing whitespace (including line terminator!) from
3237each line before returning it.}
3238{true}
3239\lineiii{skip_blanks}{
3240skip lines that are empty *after* stripping comments and
3241whitespace. (If both lstrip_ws and rstrip_ws are false,
3242then some lines may consist of solely whitespace: these will
3243*not* be skipped, even if \var{skip_blanks} is true.)}
3244{true}
3245\lineiii{join_lines}{
3246if a backslash is the last non-newline character on a line
3247after stripping comments and whitespace, join the following line
3248to it to form one logical line; if N consecutive lines end
3249with a backslash, then N+1 physical lines will be joined to
3250form one logical line.}
3251{false}
3252\lineiii{collapse_join}{
3253strip leading whitespace from lines that are joined to their
3254predecessor; only matters if \samp{(join_lines and not lstrip_ws)}}
3255{false}
3256\end{tableiii}
3257
3258Note that since \var{rstrip_ws} can strip the trailing newline, the
3259semantics of \method{readline()} must differ from those of the builtin file
3260object's \method{readline()} method! In particular, \method{readline()}
3261returns \code{None} for end-of-file: an empty string might just be a
3262blank line (or an all-whitespace line), if \var{rstrip_ws} is true
3263but \var{skip_blanks} is not.
3264
3265\begin{methoddesc}{open}{filename}
3266Open a new file \var{filename}. This overrides any \var{file} or
3267\var{filename} constructor arguments.
3268\end{methoddesc}
3269
3270\begin{methoddesc}{close}{}
3271Close the current file and forget everything we know about it (including
3272the filename and the current line number).
3273\end{methoddesc}
3274
3275\begin{methoddesc}{warn}{msg\optional{,line=\code{None}}}
3276Print (to stderr) a warning message tied to the current logical
3277line in the current file. If the current logical line in the
3278file spans multiple physical lines, the warning refers to the
3279whole range, such as \samp{"lines 3-5"}. If \var{line} is supplied,
3280it overrides the current line number; it may be a list or tuple
3281to indicate a range of physical lines, or an integer for a
3282single physical line.
3283\end{methoddesc}
3284
3285\begin{methoddesc}{readline}{}
3286Read and return a single logical line from the current file (or
3287from an internal buffer if lines have previously been ``unread''
3288with \method{unreadline()}). If the \var{join_lines} option
3289is true, this may involve reading multiple physical lines
3290concatenated into a single string. Updates the current line number,
3291so calling \method{warn()} after \method{readline()} emits a warning
3292about the physical line(s) just read. Returns \code{None} on end-of-file,
3293since the empty string can occur if \var{rstrip_ws} is true but
3294\var{strip_blanks} is not.
3295\end{methoddesc}
3296\begin{methoddesc}{readlines}{}
3297Read and return the list of all logical lines remaining in the current file.
3298This updates the current line number to the last line of the file.
3299\end{methoddesc}
3300\begin{methoddesc}{unreadline}{line}
3301Push \var{line} (a string) onto an internal buffer that will be
3302checked by future \method{readline()} calls. Handy for implementing
3303a parser with line-at-a-time lookahead. Note that lines that are ``unread''
3304with \method{unreadline} are not subsequently re-cleansed (whitespace
3305stripped, or whatever) when read with \method{readline}. If multiple
3306calls are made to \method{unreadline} before a call to \method{readline},
3307the lines will be returned most in most recent first order.
3308\end{methoddesc}
3309
3310\end{classdesc}
3311
3312
3313\section{\module{distutils.version} --- Version number classes}
3314\declaremodule{standard}{distutils.version}
3315\modulesynopsis{implements classes that represent module version numbers. }
3316
3317% todo
3318
3319%\section{Distutils Commands}
3320%
3321%This part of Distutils implements the various Distutils commands, such
3322%as \code{build}, \code{install} \&c. Each command is implemented as a
3323%separate module, with the command name as the name of the module.
3324
3325\section{\module{distutils.cmd} --- Abstract base class for Distutils commands}
3326\declaremodule{standard}{distutils.cmd}
3327\modulesynopsis{This module provides the abstract base class Command. This
3328class is subclassed by the modules in the \refmodule{distutils.command}
3329subpackage. }
3330
3331This module supplies the abstract base class \class{Command}.
3332
3333\begin{classdesc}{Command}{dist}
3334Abstract base class for defining command classes, the ``worker bees''
3335of the Distutils. A useful analogy for command classes is to think of
3336them as subroutines with local variables called \var{options}. The
3337options are declared in \method{initialize_options()} and defined
3338(given their final values) in \method{finalize_options()}, both of
3339which must be defined by every command class. The distinction between
3340the two is necessary because option values might come from the outside
3341world (command line, config file, ...), and any options dependent on
3342other options must be computed after these outside influences have
3343been processed --- hence \method{finalize_options()}. The body of the
3344subroutine, where it does all its work based on the values of its
3345options, is the \method{run()} method, which must also be implemented
3346by every command class.
3347
3348The class constructor takes a single argument \var{dist}, a
3349\class{Distribution} instance.
3350\end{classdesc}
3351
3352
3353\section{\module{distutils.command} --- Individual Distutils commands}
3354\declaremodule{standard}{distutils.command}
3355\modulesynopsis{This subpackage contains one module for each standard Distutils command.}
3356
3357%\subsubsection{Individual Distutils commands}
3358
3359% todo
3360
3361\section{\module{distutils.command.bdist} --- Build a binary installer}
3362\declaremodule{standard}{distutils.command.bdist}
3363\modulesynopsis{Build a binary installer for a package}
3364
3365% todo
3366
3367\section{\module{distutils.command.bdist_packager} --- Abstract base class for packagers}
3368\declaremodule[distutils.command.bdistpackager]{standard}{distutils.command.bdist_packager}
3369\modulesynopsis{Abstract base class for packagers}
3370
3371% todo
3372
3373\section{\module{distutils.command.bdist_dumb} --- Build a ``dumb'' installer}
3374\declaremodule[distutils.command.bdistdumb]{standard}{distutils.command.bdist_dumb}
3375\modulesynopsis{Build a ``dumb'' installer - a simple archive of files}
3376
3377% todo
3378
3379
3380\section{\module{distutils.command.bdist_rpm} --- Build a binary distribution as a Redhat RPM and SRPM}
3381\declaremodule[distutils.command.bdistrpm]{standard}{distutils.command.bdist_rpm}
3382\modulesynopsis{Build a binary distribution as a Redhat RPM and SRPM}
3383
3384% todo
3385
3386\section{\module{distutils.command.bdist_wininst} --- Build a Windows installer}
3387\declaremodule[distutils.command.bdistwininst]{standard}{distutils.command.bdist_wininst}
3388\modulesynopsis{Build a Windows installer}
3389
3390% todo
3391
3392\section{\module{distutils.command.sdist} --- Build a source distribution}
3393\declaremodule{standard}{distutils.command.sdist}
3394\modulesynopsis{Build a source distribution}
3395
3396% todo
3397
3398\section{\module{distutils.command.build} --- Build all files of a package}
3399\declaremodule{standard}{distutils.command.build}
3400\modulesynopsis{Build all files of a package}
3401
3402% todo
3403
3404\section{\module{distutils.command.build_clib} --- Build any C libraries in a package}
3405\declaremodule[distutils.command.buildclib]{standard}{distutils.command.build_clib}
3406\modulesynopsis{Build any C libraries in a package}
3407
3408% todo
3409
3410\section{\module{distutils.command.build_ext} --- Build any extensions in a package}
3411\declaremodule[distutils.command.buildext]{standard}{distutils.command.build_ext}
3412\modulesynopsis{Build any extensions in a package}
3413
3414% todo
3415
3416\section{\module{distutils.command.build_py} --- Build the .py/.pyc files of a package}
3417\declaremodule[distutils.command.buildpy]{standard}{distutils.command.build_py}
3418\modulesynopsis{Build the .py/.pyc files of a package}
3419
3420% todo
3421
3422\section{\module{distutils.command.build_scripts} --- Build the scripts of a package}
3423\declaremodule[distutils.command.buildscripts]{standard}{distutils.command.build_scripts}
3424\modulesynopsis{Build the scripts of a package}
3425
3426% todo
3427
3428\section{\module{distutils.command.clean} --- Clean a package build area}
3429\declaremodule{standard}{distutils.command.clean}
3430\modulesynopsis{Clean a package build area}
3431
3432% todo
3433
3434\section{\module{distutils.command.config} --- Perform package configuration}
3435\declaremodule{standard}{distutils.command.config}
3436\modulesynopsis{Perform package configuration}
3437
3438% todo
3439
3440\subsubsection{\module{distutils.command.install} --- Install a package}
3441\declaremodule{standard}{distutils.command.install}
3442\modulesynopsis{Install a package}
3443
3444% todo
3445
3446\subsubsection{\module{distutils.command.install_data}
3447 --- Install data files from a package}
3448\declaremodule[distutils.command.installdata]{standard}{distutils.command.install_data}
3449\modulesynopsis{Install data files from a package}
3450
3451% todo
3452
3453\subsubsection{\module{distutils.command.install_headers}
3454 --- Install C/\Cpp{} header files from a package}
3455\declaremodule[distutils.command.installheaders]{standard}{distutils.command.install_headers}
3456\modulesynopsis{Install C/\Cpp{} header files from a package}
3457
3458% todo
3459
3460\subsubsection{\module{distutils.command.install_lib}
3461 --- Install library files from a package}
3462\declaremodule[distutils.command.installlib]{standard}{distutils.command.install_lib}
3463\modulesynopsis{Install library files from a package}
3464
3465% todo
3466
3467\subsubsection{\module{distutils.command.install_scripts}
3468 --- Install script files from a package}
3469\declaremodule[distutils.command.installscripts]{standard}{distutils.command.install_scripts}
3470\modulesynopsis{Install script files from a package}
3471
3472% todo
3473
3474\subsubsection{\module{distutils.command.register}
3475 --- Register a module with the Python Package Index}
3476\declaremodule{standard}{distutils.command.register}
3477\modulesynopsis{Register a module with the Python Package Index}
3478
3479The \code{register} command registers the package with the Python Package
3480Index. This is described in more detail in \pep{301}.
3481% todo
3482
3483\subsubsection{Creating a new Distutils command}
3484
3485This section outlines the steps to create a new Distutils command.
3486
3487A new command lives in a module in the \module{distutils.command}
3488package. There is a sample template in that directory called
3489\file{command_template}. Copy this file to a new module with the
3490same name as the new command you're implementing. This module should
3491implement a class with the same name as the module (and the command).
3492So, for instance, to create the command \code{peel_banana} (so that users
3493can run \samp{setup.py peel_banana}), you'd copy \file{command_template}
3494to \file{distutils/command/peel_banana.py}, then edit it so that it's
3495implementing the class \class{peel_banana}, a subclass of
3496\class{distutils.cmd.Command}.
3497
3498Subclasses of \class{Command} must define the following methods.
3499
3500\begin{methoddesc}{initialize_options()}
3501Set default values for all the options that this command
3502supports. Note that these defaults may be overridden by other
3503commands, by the setup script, by config files, or by the
3504command-line. Thus, this is not the place to code dependencies
3505between options; generally, \method{initialize_options()} implementations
3506are just a bunch of \samp{self.foo = None} assignments.
3507\end{methoddesc}
3508
3509\begin{methoddesc}{finalize_options}{}
3510Set final values for all the options that this command supports.
3511This is always called as late as possible, ie. after any option
3512assignments from the command-line or from other commands have been
3513done. Thus, this is the place to to code option dependencies: if
3514\var{foo} depends on \var{bar}, then it is safe to set \var{foo} from
3515\var{bar} as long as \var{foo} still has the same value it was assigned in
3516\method{initialize_options()}.
3517\end{methoddesc}
3518\begin{methoddesc}{run}{}
3519A command's raison d'etre: carry out the action it exists to
3520perform, controlled by the options initialized in
3521\method{initialize_options()}, customized by other commands, the setup
3522script, the command-line, and config files, and finalized in
3523\method{finalize_options()}. All terminal output and filesystem
3524interaction should be done by \method{run()}.
3525\end{methoddesc}
3526
3527\var{sub_commands} formalizes the notion of a ``family'' of commands,
3528eg. \code{install} as the parent with sub-commands \code{install_lib},
3529\code{install_headers}, etc. The parent of a family of commands
3530defines \var{sub_commands} as a class attribute; it's a list of
35312-tuples \samp{(command_name, predicate)}, with \var{command_name} a string
3532and \var{predicate} an unbound method, a string or None.
3533\var{predicate} is a method of the parent command that
3534determines whether the corresponding command is applicable in the
3535current situation. (Eg. we \code{install_headers} is only applicable if
3536we have any C header files to install.) If \var{predicate} is None,
3537that command is always applicable.
3538
3539\var{sub_commands} is usually defined at the *end* of a class, because
3540predicates can be unbound methods, so they must already have been
3541defined. The canonical example is the \command{install} command.
3542
Fred Drake6356fff2004-03-23 19:02:38 +00003543%
3544% The ugly "%begin{latexonly}" pseudo-environments are really just to
3545% keep LaTeX2HTML quiet during the \renewcommand{} macros; they're
3546% not really valuable.
3547%
3548
3549%begin{latexonly}
3550\renewcommand{\indexname}{Module Index}
3551%end{latexonly}
Fred Drakead622022004-03-25 16:35:10 +00003552\input{moddist.ind} % Module Index
Fred Drake6356fff2004-03-23 19:02:38 +00003553
3554%begin{latexonly}
3555\renewcommand{\indexname}{Index}
3556%end{latexonly}
Fred Drakead622022004-03-25 16:35:10 +00003557\input{dist.ind} % Index
Fred Drake6fca7cc2004-03-23 18:43:03 +00003558
Greg Wardabc52162000-02-26 00:52:48 +00003559\end{document}