sourcedist.rst 9.51 KB
Newer Older
1 2 3 4 5 6
.. _source-dist:

******************************
Creating a Source Distribution
******************************

7 8
.. include:: ./_setuptools_disclaimer.rst

9 10 11 12 13 14 15 16 17 18
As shown in section :ref:`distutils-simple-example`, you use the :command:`sdist` command
to create a source distribution.  In the simplest case, ::

   python setup.py sdist

(assuming you haven't specified any :command:`sdist` options in the setup script
or config file), :command:`sdist` creates the archive of the default format for
the current platform.  The default format is a gzip'ed tar file
(:file:`.tar.gz`) on Unix, and ZIP file on Windows.

19
You can specify as many formats as you like using the :option:`!--formats`
20 21 22 23 24 25 26 27 28 29 30
option, for example::

   python setup.py sdist --formats=gztar,zip

to create a gzipped tarball and a zip file.  The available formats are:

+-----------+-------------------------+---------+
| Format    | Description             | Notes   |
+===========+=========================+=========+
| ``zip``   | zip file (:file:`.zip`) | (1),(3) |
+-----------+-------------------------+---------+
31
| ``gztar`` | gzip'ed tar file        | \(2)    |
32 33
|           | (:file:`.tar.gz`)       |         |
+-----------+-------------------------+---------+
34
| ``bztar`` | bzip2'ed tar file       |         |
35 36
|           | (:file:`.tar.bz2`)      |         |
+-----------+-------------------------+---------+
37 38 39
| ``xztar`` | xz'ed tar file          |         |
|           | (:file:`.tar.xz`)       |         |
+-----------+-------------------------+---------+
40 41 42
| ``ztar``  | compressed tar file     | \(4)    |
|           | (:file:`.tar.Z`)        |         |
+-----------+-------------------------+---------+
43
| ``tar``   | tar file (:file:`.tar`) |         |
44 45
+-----------+-------------------------+---------+

46
.. versionchanged:: 3.5
47 48
   Added support for the ``xztar`` format.

49 50 51 52 53 54 55 56 57 58 59 60 61
Notes:

(1)
   default on Windows

(2)
   default on Unix

(3)
   requires either external :program:`zip` utility or :mod:`zipfile` module (part
   of the standard Python library since Python 1.6)

(4)
62 63 64
   requires the :program:`compress` program. Notice that this format is now
   pending for deprecation and will be removed in the future versions of Python.

65
When using any ``tar`` format (``gztar``, ``bztar``, ``xztar``, ``ztar`` or
66 67 68 69 70 71
``tar``), under Unix you can specify the ``owner`` and ``group`` names
that will be set for each member of the archive.

For example, if you want all files of the archive to be owned by root::

    python setup.py sdist --owner=root --group=root
72 73 74 75 76 77 78 79 80 81 82


.. _manifest:

Specifying the files to distribute
==================================

If you don't supply an explicit list of files (or instructions on how to
generate one), the :command:`sdist` command puts a minimal default set into the
source distribution:

83 84
* all Python source files implied by the ``py_modules`` and
  ``packages`` options
85

86 87
* all C source files mentioned in the ``ext_modules`` or
  ``libraries`` options
88

89 90
  .. XXX getting C library sources currently broken---no
         :meth:`get_source_files` method in :file:`build_clib.py`!
91

92
* scripts identified by the ``scripts`` option
93
  See :ref:`distutils-installing-scripts`.
94 95 96 97 98 99

* anything that looks like a test script: :file:`test/test\*.py` (currently, the
  Distutils don't do anything with test scripts except include them in source
  distributions, but in the future there will be a standard for testing Python
  module distributions)

100 101 102
* Any of the standard README files (:file:`README`, :file:`README.txt`,
  or :file:`README.rst`), :file:`setup.py` (or whatever you called your setup
  script), and :file:`setup.cfg`.
103

104 105 106 107 108 109
* all files that matches the ``package_data`` metadata.
  See :ref:`distutils-installing-package-data`.

* all files that matches the ``data_files`` metadata.
  See :ref:`distutils-additional-files`.

110 111 112 113 114 115 116 117 118 119 120 121 122
Sometimes this is enough, but usually you will want to specify additional files
to distribute.  The typical way to do this is to write a *manifest template*,
called :file:`MANIFEST.in` by default.  The manifest template is just a list of
instructions for how to generate your manifest file, :file:`MANIFEST`, which is
the exact list of files to include in your source distribution.  The
:command:`sdist` command processes this template and generates a manifest based
on its instructions and what it finds in the filesystem.

If you prefer to roll your own manifest file, the format is simple: one filename
per line, regular files (or symlinks to them) only.  If you do supply your own
:file:`MANIFEST`, you must specify everything: the default set of files
described above does not apply in this case.

123 124 125 126 127 128
.. versionchanged:: 3.1
   An existing generated :file:`MANIFEST` will be regenerated without
   :command:`sdist` comparing its modification time to the one of
   :file:`MANIFEST.in` or :file:`setup.py`.

.. versionchanged:: 3.1.3
Éric Araujo's avatar
Éric Araujo committed
129
   :file:`MANIFEST` files start with a comment indicating they are generated.
130 131
   Files without this comment are not overwritten or removed.

132 133 134 135
.. versionchanged:: 3.2.2
   :command:`sdist` will read a :file:`MANIFEST` file if no :file:`MANIFEST.in`
   exists, like it used to do.

136 137 138
.. versionchanged:: 3.7
   :file:`README.rst` is now included in the list of distutils standard READMEs.

139

140 141
The manifest template has one command per line, where each command specifies a
set of files to include or exclude from the source distribution.  For an
142 143 144
example, again we turn to the Distutils' own manifest template:

.. code-block:: none
145 146 147 148 149 150 151 152 153 154 155

   include *.txt
   recursive-include examples *.txt *.py
   prune examples/sample?/build

The meanings should be fairly clear: include all files in the distribution root
matching :file:`\*.txt`, all files anywhere under the :file:`examples` directory
matching :file:`\*.txt` or :file:`\*.py`, and exclude all directories matching
:file:`examples/sample?/build`.  All of this is done *after* the standard
include set, so you can exclude files from the standard set with explicit
instructions in the manifest template.  (Or, you can use the
156
:option:`!--no-defaults` option to disable the standard set entirely.)  There are
157 158
several other commands available in the manifest template mini-language; see
section :ref:`sdist-cmd`.
159 160 161 162 163 164 165 166 167

The order of commands in the manifest template matters: initially, we have the
list of default files as described above, and each command in the template adds
to or removes from that list of files.  Once we have fully processed the
manifest template, we remove files that should not be included in the source
distribution:

* all files in the Distutils "build" tree (default :file:`build/`)

Christian Heimes's avatar
Christian Heimes committed
168 169
* all files in directories named :file:`RCS`, :file:`CVS`, :file:`.svn`,
  :file:`.hg`, :file:`.git`, :file:`.bzr` or :file:`_darcs`
170 171 172 173 174

Now we have our complete list of files, which is written to the manifest for
future reference, and then used to build the source distribution archive(s).

You can disable the default set of included files with the
175 176
:option:`!--no-defaults` option, and you can disable the standard exclude set
with :option:`!--no-prune`.
177 178 179 180 181 182 183

Following the Distutils' own manifest template, let's trace how the
:command:`sdist` command builds the list of files to include in the Distutils
source distribution:

#. include all Python source files in the :file:`distutils` and
   :file:`distutils/command` subdirectories (because packages corresponding to
184
   those two directories were mentioned in the ``packages`` option in the
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
   setup script---see section :ref:`setup-script`)

#. include :file:`README.txt`, :file:`setup.py`, and :file:`setup.cfg` (standard
   files)

#. include :file:`test/test\*.py` (standard files)

#. include :file:`\*.txt` in the distribution root (this will find
   :file:`README.txt` a second time, but such redundancies are weeded out later)

#. include anything matching :file:`\*.txt` or :file:`\*.py` in the sub-tree
   under :file:`examples`,

#. exclude all files in the sub-trees starting at directories matching
   :file:`examples/sample?/build`\ ---this may exclude files included by the
   previous two steps, so it's important that the ``prune`` command in the manifest
   template comes after the ``recursive-include`` command

Christian Heimes's avatar
Christian Heimes committed
203 204 205
#. exclude the entire :file:`build` tree, and any :file:`RCS`, :file:`CVS`,
   :file:`.svn`, :file:`.hg`, :file:`.git`, :file:`.bzr` and :file:`_darcs`
   directories
206 207 208 209 210 211

Just like in the setup script, file and directory names in the manifest template
should always be slash-separated; the Distutils will take care of converting
them to the standard representation on your platform. That way, the manifest
template is portable across operating systems.

212 213 214 215 216 217 218 219

.. _manifest-options:

Manifest-related options
========================

The normal course of operations for the :command:`sdist` command is as follows:

220 221 222 223 224 225
* if the manifest file (:file:`MANIFEST` by default) exists and the first line
  does not have a comment indicating it is generated from :file:`MANIFEST.in`,
  then it is used as is, unaltered

* if the manifest file doesn't exist or has been previously automatically
  generated, read :file:`MANIFEST.in` and create the manifest
226 227 228 229 230 231 232 233

* if neither :file:`MANIFEST` nor :file:`MANIFEST.in` exist, create a manifest
  with just the default file set

* use the list of files now in :file:`MANIFEST` (either just generated or read
  in) to create the source distribution archive(s)

There are a couple of options that modify this behaviour.  First, use the
234
:option:`!--no-defaults` and :option:`!--no-prune` to disable the standard
235 236 237 238 239 240 241
"include" and "exclude" sets.

Second, you might just want to (re)generate the manifest, but not create a source
distribution::

   python setup.py sdist --manifest-only

242
:option:`!-o` is a shortcut for :option:`!--manifest-only`.