blob: 1666436be0b1d15dd74d26d04f15c0a4f1dec2c3 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001.. _source-dist:
2
3******************************
4Creating a Source Distribution
5******************************
6
7As shown in section :ref:`distutils-simple-example`, you use the :command:`sdist` command
8to create a source distribution. In the simplest case, ::
9
10 python setup.py sdist
11
12(assuming you haven't specified any :command:`sdist` options in the setup script
13or config file), :command:`sdist` creates the archive of the default format for
14the current platform. The default format is a gzip'ed tar file
15(:file:`.tar.gz`) on Unix, and ZIP file on Windows.
16
17You can specify as many formats as you like using the :option:`--formats`
18option, for example::
19
20 python setup.py sdist --formats=gztar,zip
21
22to create a gzipped tarball and a zip file. The available formats are:
23
24+-----------+-------------------------+---------+
25| Format | Description | Notes |
26+===========+=========================+=========+
27| ``zip`` | zip file (:file:`.zip`) | (1),(3) |
28+-----------+-------------------------+---------+
Tarek Ziadé96c45a92010-07-31 09:10:51 +000029| ``gztar`` | gzip'ed tar file | (2),(4) |
Georg Brandl116aa622007-08-15 14:28:22 +000030| | (:file:`.tar.gz`) | |
31+-----------+-------------------------+---------+
Tarek Ziadé96c45a92010-07-31 09:10:51 +000032| ``bztar`` | bzip2'ed tar file | \(4) |
Georg Brandl116aa622007-08-15 14:28:22 +000033| | (:file:`.tar.bz2`) | |
34+-----------+-------------------------+---------+
35| ``ztar`` | compressed tar file | \(4) |
36| | (:file:`.tar.Z`) | |
37+-----------+-------------------------+---------+
Tarek Ziadé96c45a92010-07-31 09:10:51 +000038| ``tar`` | tar file (:file:`.tar`) | \(4) |
Georg Brandl116aa622007-08-15 14:28:22 +000039+-----------+-------------------------+---------+
40
41Notes:
42
43(1)
44 default on Windows
45
46(2)
47 default on Unix
48
49(3)
50 requires either external :program:`zip` utility or :mod:`zipfile` module (part
51 of the standard Python library since Python 1.6)
52
53(4)
Tarek Ziadé96c45a92010-07-31 09:10:51 +000054 requires external utilities: :program:`tar` and possibly one of :program:`gzip`,
55 :program:`bzip2`, or :program:`compress`
Georg Brandl116aa622007-08-15 14:28:22 +000056
57
58.. _manifest:
59
60Specifying the files to distribute
61==================================
62
63If you don't supply an explicit list of files (or instructions on how to
64generate one), the :command:`sdist` command puts a minimal default set into the
65source distribution:
66
67* all Python source files implied by the :option:`py_modules` and
68 :option:`packages` options
69
70* all C source files mentioned in the :option:`ext_modules` or
Tarek Ziadé96c45a92010-07-31 09:10:51 +000071 :option:`libraries` options (
Georg Brandl116aa622007-08-15 14:28:22 +000072
Georg Brandld5f2d6e2010-07-31 09:15:10 +000073 .. XXX getting C library sources currently broken---no
74 :meth:`get_source_files` method in :file:`build_clib.py`!
Georg Brandl116aa622007-08-15 14:28:22 +000075
76* scripts identified by the :option:`scripts` option
Tarek Ziadé0d0506e2009-02-16 21:49:12 +000077 See :ref:`distutils-installing-scripts`.
Georg Brandl116aa622007-08-15 14:28:22 +000078
79* anything that looks like a test script: :file:`test/test\*.py` (currently, the
80 Distutils don't do anything with test scripts except include them in source
81 distributions, but in the future there will be a standard for testing Python
82 module distributions)
83
84* :file:`README.txt` (or :file:`README`), :file:`setup.py` (or whatever you
85 called your setup script), and :file:`setup.cfg`
86
Tarek Ziadé0d0506e2009-02-16 21:49:12 +000087* all files that matches the ``package_data`` metadata.
88 See :ref:`distutils-installing-package-data`.
89
Tarek Ziadé0d0506e2009-02-16 21:49:12 +000090* all files that matches the ``data_files`` metadata.
91 See :ref:`distutils-additional-files`.
92
Georg Brandl116aa622007-08-15 14:28:22 +000093Sometimes this is enough, but usually you will want to specify additional files
94to distribute. The typical way to do this is to write a *manifest template*,
95called :file:`MANIFEST.in` by default. The manifest template is just a list of
96instructions for how to generate your manifest file, :file:`MANIFEST`, which is
97the exact list of files to include in your source distribution. The
98:command:`sdist` command processes this template and generates a manifest based
99on its instructions and what it finds in the filesystem.
100
101If you prefer to roll your own manifest file, the format is simple: one filename
102per line, regular files (or symlinks to them) only. If you do supply your own
103:file:`MANIFEST`, you must specify everything: the default set of files
104described above does not apply in this case.
105
Éric Araujoab7c1b32011-07-31 04:06:12 +0200106.. versionchanged:: 3.1
107 An existing generated :file:`MANIFEST` will be regenerated without
108 :command:`sdist` comparing its modification time to the one of
109 :file:`MANIFEST.in` or :file:`setup.py`.
110
111.. versionchanged:: 3.1.3
Éric Araujoa8939272010-08-14 23:44:13 +0000112 :file:`MANIFEST` files start with a comment indicating they are generated.
Éric Araujoda668ff2010-08-14 02:30:34 +0000113 Files without this comment are not overwritten or removed.
114
Éric Araujoab7c1b32011-07-31 04:06:12 +0200115.. versionchanged:: 3.2.2
116 :command:`sdist` will read a :file:`MANIFEST` file if no :file:`MANIFEST.in`
117 exists, like it used to do.
118
119
Georg Brandl116aa622007-08-15 14:28:22 +0000120The manifest template has one command per line, where each command specifies a
121set of files to include or exclude from the source distribution. For an
Tarek Ziadé96c45a92010-07-31 09:10:51 +0000122example, again we turn to the Distutils' own manifest template::
Georg Brandl116aa622007-08-15 14:28:22 +0000123
124 include *.txt
125 recursive-include examples *.txt *.py
126 prune examples/sample?/build
127
128The meanings should be fairly clear: include all files in the distribution root
129matching :file:`\*.txt`, all files anywhere under the :file:`examples` directory
130matching :file:`\*.txt` or :file:`\*.py`, and exclude all directories matching
131:file:`examples/sample?/build`. All of this is done *after* the standard
132include set, so you can exclude files from the standard set with explicit
133instructions in the manifest template. (Or, you can use the
Tarek Ziadé96c45a92010-07-31 09:10:51 +0000134:option:`--no-defaults` option to disable the standard set entirely.) There are
135several other commands available in the manifest template mini-language; see
136section :ref:`sdist-cmd`.
Georg Brandl116aa622007-08-15 14:28:22 +0000137
138The order of commands in the manifest template matters: initially, we have the
139list of default files as described above, and each command in the template adds
140to or removes from that list of files. Once we have fully processed the
141manifest template, we remove files that should not be included in the source
142distribution:
143
144* all files in the Distutils "build" tree (default :file:`build/`)
145
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000146* all files in directories named :file:`RCS`, :file:`CVS`, :file:`.svn`,
147 :file:`.hg`, :file:`.git`, :file:`.bzr` or :file:`_darcs`
Georg Brandl116aa622007-08-15 14:28:22 +0000148
149Now we have our complete list of files, which is written to the manifest for
150future reference, and then used to build the source distribution archive(s).
151
152You can disable the default set of included files with the
153:option:`--no-defaults` option, and you can disable the standard exclude set
154with :option:`--no-prune`.
155
156Following the Distutils' own manifest template, let's trace how the
157:command:`sdist` command builds the list of files to include in the Distutils
158source distribution:
159
160#. include all Python source files in the :file:`distutils` and
161 :file:`distutils/command` subdirectories (because packages corresponding to
162 those two directories were mentioned in the :option:`packages` option in the
163 setup script---see section :ref:`setup-script`)
164
165#. include :file:`README.txt`, :file:`setup.py`, and :file:`setup.cfg` (standard
166 files)
167
168#. include :file:`test/test\*.py` (standard files)
169
170#. include :file:`\*.txt` in the distribution root (this will find
171 :file:`README.txt` a second time, but such redundancies are weeded out later)
172
173#. include anything matching :file:`\*.txt` or :file:`\*.py` in the sub-tree
174 under :file:`examples`,
175
176#. exclude all files in the sub-trees starting at directories matching
177 :file:`examples/sample?/build`\ ---this may exclude files included by the
178 previous two steps, so it's important that the ``prune`` command in the manifest
179 template comes after the ``recursive-include`` command
180
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000181#. exclude the entire :file:`build` tree, and any :file:`RCS`, :file:`CVS`,
182 :file:`.svn`, :file:`.hg`, :file:`.git`, :file:`.bzr` and :file:`_darcs`
183 directories
Georg Brandl116aa622007-08-15 14:28:22 +0000184
185Just like in the setup script, file and directory names in the manifest template
186should always be slash-separated; the Distutils will take care of converting
187them to the standard representation on your platform. That way, the manifest
188template is portable across operating systems.
189
190
Tarek Ziadé96c45a92010-07-31 09:10:51 +0000191.. _manifest-options:
Georg Brandl116aa622007-08-15 14:28:22 +0000192
Tarek Ziadé96c45a92010-07-31 09:10:51 +0000193Manifest-related options
194========================
Georg Brandl116aa622007-08-15 14:28:22 +0000195
Tarek Ziadé96c45a92010-07-31 09:10:51 +0000196The normal course of operations for the :command:`sdist` command is as follows:
197
Éric Araujoab7c1b32011-07-31 04:06:12 +0200198* if the manifest file (:file:`MANIFEST` by default) exists and the first line
199 does not have a comment indicating it is generated from :file:`MANIFEST.in`,
200 then it is used as is, unaltered
201
202* if the manifest file doesn't exist or has been previously automatically
203 generated, read :file:`MANIFEST.in` and create the manifest
Tarek Ziadé96c45a92010-07-31 09:10:51 +0000204
205* if neither :file:`MANIFEST` nor :file:`MANIFEST.in` exist, create a manifest
206 with just the default file set
207
Tarek Ziadé96c45a92010-07-31 09:10:51 +0000208* use the list of files now in :file:`MANIFEST` (either just generated or read
209 in) to create the source distribution archive(s)
210
211There are a couple of options that modify this behaviour. First, use the
212:option:`--no-defaults` and :option:`--no-prune` to disable the standard
213"include" and "exclude" sets.
214
215Second, you might just want to (re)generate the manifest, but not create a source
216distribution::
217
218 python setup.py sdist --manifest-only
219
220:option:`-o` is a shortcut for :option:`--manifest-only`.