blob: 94fa338c1940c339853f5bd2364fb6426a0617a0 [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é05b30342009-10-02 23:56:02 +000029| ``gztar`` | gzip'ed tar file | \(2) |
Georg Brandl116aa622007-08-15 14:28:22 +000030| | (:file:`.tar.gz`) | |
31+-----------+-------------------------+---------+
Tarek Ziadé05b30342009-10-02 23:56:02 +000032| ``bztar`` | bzip2'ed tar file | |
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é05b30342009-10-02 23:56:02 +000038| ``tar`` | tar file (:file:`.tar`) | |
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é05b30342009-10-02 23:56:02 +000054 requires the :program:`compress` program. Notice that this format is now
55 pending for deprecation and will be removed in the future versions of Python.
56
57When using any ``tar`` format (``gztar``, ``bztar``, ``ztar`` or ``tar``), you
58can specify under Unix the ``owner`` and ``group`` names that will be set for
59each member of the archive.
60
61For example, if you want all files of the archive to be owned by root::
62
63 python setup.py sdist --owner=root --group=root
Georg Brandl116aa622007-08-15 14:28:22 +000064
65
66.. _manifest:
67
68Specifying the files to distribute
69==================================
70
71If you don't supply an explicit list of files (or instructions on how to
72generate one), the :command:`sdist` command puts a minimal default set into the
73source distribution:
74
75* all Python source files implied by the :option:`py_modules` and
76 :option:`packages` options
77
78* all C source files mentioned in the :option:`ext_modules` or
79 :option:`libraries` options (
80
81 **\*\*** getting C library sources currently broken---no
82 :meth:`get_source_files` method in :file:`build_clib.py`! **\*\***)
83
84* scripts identified by the :option:`scripts` option
Tarek Ziadé0d0506e2009-02-16 21:49:12 +000085 See :ref:`distutils-installing-scripts`.
Georg Brandl116aa622007-08-15 14:28:22 +000086
87* anything that looks like a test script: :file:`test/test\*.py` (currently, the
88 Distutils don't do anything with test scripts except include them in source
89 distributions, but in the future there will be a standard for testing Python
90 module distributions)
91
92* :file:`README.txt` (or :file:`README`), :file:`setup.py` (or whatever you
93 called your setup script), and :file:`setup.cfg`
94
Tarek Ziadé0d0506e2009-02-16 21:49:12 +000095* all files that matches the ``package_data`` metadata.
96 See :ref:`distutils-installing-package-data`.
97
Tarek Ziadé0d0506e2009-02-16 21:49:12 +000098* all files that matches the ``data_files`` metadata.
99 See :ref:`distutils-additional-files`.
100
Georg Brandl116aa622007-08-15 14:28:22 +0000101Sometimes this is enough, but usually you will want to specify additional files
102to distribute. The typical way to do this is to write a *manifest template*,
103called :file:`MANIFEST.in` by default. The manifest template is just a list of
104instructions for how to generate your manifest file, :file:`MANIFEST`, which is
105the exact list of files to include in your source distribution. The
106:command:`sdist` command processes this template and generates a manifest based
107on its instructions and what it finds in the filesystem.
108
109If you prefer to roll your own manifest file, the format is simple: one filename
110per line, regular files (or symlinks to them) only. If you do supply your own
111:file:`MANIFEST`, you must specify everything: the default set of files
112described above does not apply in this case.
113
114The manifest template has one command per line, where each command specifies a
115set of files to include or exclude from the source distribution. For an
116example, again we turn to the Distutils' own manifest template::
117
118 include *.txt
119 recursive-include examples *.txt *.py
120 prune examples/sample?/build
121
122The meanings should be fairly clear: include all files in the distribution root
123matching :file:`\*.txt`, all files anywhere under the :file:`examples` directory
124matching :file:`\*.txt` or :file:`\*.py`, and exclude all directories matching
125:file:`examples/sample?/build`. All of this is done *after* the standard
126include set, so you can exclude files from the standard set with explicit
127instructions in the manifest template. (Or, you can use the
128:option:`--no-defaults` option to disable the standard set entirely.) There are
129several other commands available in the manifest template mini-language; see
130section :ref:`sdist-cmd`.
131
132The order of commands in the manifest template matters: initially, we have the
133list of default files as described above, and each command in the template adds
134to or removes from that list of files. Once we have fully processed the
135manifest template, we remove files that should not be included in the source
136distribution:
137
138* all files in the Distutils "build" tree (default :file:`build/`)
139
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000140* all files in directories named :file:`RCS`, :file:`CVS`, :file:`.svn`,
141 :file:`.hg`, :file:`.git`, :file:`.bzr` or :file:`_darcs`
Georg Brandl116aa622007-08-15 14:28:22 +0000142
143Now we have our complete list of files, which is written to the manifest for
144future reference, and then used to build the source distribution archive(s).
145
146You can disable the default set of included files with the
147:option:`--no-defaults` option, and you can disable the standard exclude set
148with :option:`--no-prune`.
149
150Following the Distutils' own manifest template, let's trace how the
151:command:`sdist` command builds the list of files to include in the Distutils
152source distribution:
153
154#. include all Python source files in the :file:`distutils` and
155 :file:`distutils/command` subdirectories (because packages corresponding to
156 those two directories were mentioned in the :option:`packages` option in the
157 setup script---see section :ref:`setup-script`)
158
159#. include :file:`README.txt`, :file:`setup.py`, and :file:`setup.cfg` (standard
160 files)
161
162#. include :file:`test/test\*.py` (standard files)
163
164#. include :file:`\*.txt` in the distribution root (this will find
165 :file:`README.txt` a second time, but such redundancies are weeded out later)
166
167#. include anything matching :file:`\*.txt` or :file:`\*.py` in the sub-tree
168 under :file:`examples`,
169
170#. exclude all files in the sub-trees starting at directories matching
171 :file:`examples/sample?/build`\ ---this may exclude files included by the
172 previous two steps, so it's important that the ``prune`` command in the manifest
173 template comes after the ``recursive-include`` command
174
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000175#. exclude the entire :file:`build` tree, and any :file:`RCS`, :file:`CVS`,
176 :file:`.svn`, :file:`.hg`, :file:`.git`, :file:`.bzr` and :file:`_darcs`
177 directories
Georg Brandl116aa622007-08-15 14:28:22 +0000178
179Just like in the setup script, file and directory names in the manifest template
180should always be slash-separated; the Distutils will take care of converting
181them to the standard representation on your platform. That way, the manifest
182template is portable across operating systems.
183
184
185.. _manifest-options:
186
187Manifest-related options
188========================
189
190The normal course of operations for the :command:`sdist` command is as follows:
191
192* if the manifest file, :file:`MANIFEST` doesn't exist, read :file:`MANIFEST.in`
193 and create the manifest
194
195* if neither :file:`MANIFEST` nor :file:`MANIFEST.in` exist, create a manifest
196 with just the default file set
197
198* if either :file:`MANIFEST.in` or the setup script (:file:`setup.py`) are more
199 recent than :file:`MANIFEST`, recreate :file:`MANIFEST` by reading
200 :file:`MANIFEST.in`
201
202* use the list of files now in :file:`MANIFEST` (either just generated or read
203 in) to create the source distribution archive(s)
204
205There are a couple of options that modify this behaviour. First, use the
206:option:`--no-defaults` and :option:`--no-prune` to disable the standard
207"include" and "exclude" sets.
208
209Second, you might want to force the manifest to be regenerated---for example, if
210you have added or removed files or directories that match an existing pattern in
211the manifest template, you should regenerate the manifest::
212
213 python setup.py sdist --force-manifest
214
215Or, 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:`--manifest-only` implies :option:`--force-manifest`. :option:`-o` is a
221shortcut for :option:`--manifest-only`, and :option:`-f` for
222:option:`--force-manifest`.
223
224