blob: c549c908d8b7943064fb418a0101ff0960a7b50c [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +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é1b486712009-10-02 23:49:48 +000029| ``gztar`` | gzip'ed tar file | \(2) |
Georg Brandl8ec7f652007-08-15 14:28:01 +000030| | (:file:`.tar.gz`) | |
31+-----------+-------------------------+---------+
Tarek Ziadé1b486712009-10-02 23:49:48 +000032| ``bztar`` | bzip2'ed tar file | |
Georg Brandl8ec7f652007-08-15 14:28:01 +000033| | (:file:`.tar.bz2`) | |
34+-----------+-------------------------+---------+
35| ``ztar`` | compressed tar file | \(4) |
36| | (:file:`.tar.Z`) | |
37+-----------+-------------------------+---------+
Tarek Ziadé1b486712009-10-02 23:49:48 +000038| ``tar`` | tar file (:file:`.tar`) | |
Georg Brandl8ec7f652007-08-15 14:28:01 +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é1b486712009-10-02 23:49:48 +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
Andrew M. Kuchlingc7337b82010-02-22 02:08:45 +000057When using any ``tar`` format (``gztar``, ``bztar``, ``ztar`` or
58``tar``) under Unix, you can specify the ``owner`` and ``group`` names
59that will be set for each member of the archive.
Tarek Ziadé1b486712009-10-02 23:49:48 +000060
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 Brandl8ec7f652007-08-15 14:28:01 +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
Georg Brandl1882d2a2010-07-07 19:05:35 +000079 :option:`libraries` options
Georg Brandl8ec7f652007-08-15 14:28:01 +000080
Georg Brandl1882d2a2010-07-07 19:05:35 +000081 .. XXX Getting C library sources is currently broken -- no
82 :meth:`get_source_files` method in :file:`build_clib.py`!
Georg Brandl8ec7f652007-08-15 14:28:01 +000083
84* scripts identified by the :option:`scripts` option
Tarek Ziadé7dd53392009-02-16 21:38:01 +000085 See :ref:`distutils-installing-scripts`.
Georg Brandl8ec7f652007-08-15 14:28:01 +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é7dd53392009-02-16 21:38:01 +000095* all files that matches the ``package_data`` metadata.
96 See :ref:`distutils-installing-package-data`.
97
Tarek Ziadé7dd53392009-02-16 21:38:01 +000098* all files that matches the ``data_files`` metadata.
99 See :ref:`distutils-additional-files`.
100
Georg Brandl8ec7f652007-08-15 14:28:01 +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
Éric Araujoe23f1022010-08-15 00:49:35 +0000114.. versionadded:: 2.7
115 :file:`MANIFEST` files start with a comment indicating they are generated.
116 Files without this comment are not overwritten or removed.
117
Tarek Ziadé4f786b22009-12-13 23:24:13 +0000118See :ref:`manifest_template` section for a syntax reference.
119
120.. _manifest-options:
121
122Manifest-related options
123========================
124
125The normal course of operations for the :command:`sdist` command is as follows:
126
127* if the manifest file, :file:`MANIFEST` doesn't exist, read :file:`MANIFEST.in`
128 and create the manifest
129
130* if neither :file:`MANIFEST` nor :file:`MANIFEST.in` exist, create a manifest
131 with just the default file set
132
133* if either :file:`MANIFEST.in` or the setup script (:file:`setup.py`) are more
134 recent than :file:`MANIFEST`, recreate :file:`MANIFEST` by reading
135 :file:`MANIFEST.in`
136
137* use the list of files now in :file:`MANIFEST` (either just generated or read
138 in) to create the source distribution archive(s)
139
140There are a couple of options that modify this behaviour. First, use the
141:option:`--no-defaults` and :option:`--no-prune` to disable the standard
142"include" and "exclude" sets.
143
Tarek Ziadé4fc2a002010-05-17 10:54:43 +0000144Second, you might just want to (re)generate the manifest, but not create a
145source distribution::
Tarek Ziadé4f786b22009-12-13 23:24:13 +0000146
147 python setup.py sdist --manifest-only
148
Tarek Ziadé4fc2a002010-05-17 10:54:43 +0000149:option:`-o` is a sortcut for :option:`--manifest-only`.
Tarek Ziadé4f786b22009-12-13 23:24:13 +0000150
151.. _manifest_template:
152
153The MANIFEST.in template
154========================
155
156A :file:`MANIFEST.in` file can be added in a project to define the list of
157files to include in the distribution built by the :command:`sdist` command.
158
159When :command:`sdist` is run, it will look for the :file:`MANIFEST.in` file
160and interpret it to generate the :file:`MANIFEST` file that contains the
161list of files that will be included in the package.
162
163This mechanism can be used when the default list of files is not enough.
164(See :ref:`manifest`).
165
166Principle
167---------
168
Georg Brandl8ec7f652007-08-15 14:28:01 +0000169The manifest template has one command per line, where each command specifies a
170set of files to include or exclude from the source distribution. For an
Tarek Ziadé4f786b22009-12-13 23:24:13 +0000171example, let's look at the Distutils' own manifest template::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000172
173 include *.txt
174 recursive-include examples *.txt *.py
175 prune examples/sample?/build
176
177The meanings should be fairly clear: include all files in the distribution root
178matching :file:`\*.txt`, all files anywhere under the :file:`examples` directory
179matching :file:`\*.txt` or :file:`\*.py`, and exclude all directories matching
180:file:`examples/sample?/build`. All of this is done *after* the standard
181include set, so you can exclude files from the standard set with explicit
182instructions in the manifest template. (Or, you can use the
Tarek Ziadé4f786b22009-12-13 23:24:13 +0000183:option:`--no-defaults` option to disable the standard set entirely.)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000184
185The order of commands in the manifest template matters: initially, we have the
186list of default files as described above, and each command in the template adds
187to or removes from that list of files. Once we have fully processed the
188manifest template, we remove files that should not be included in the source
189distribution:
190
191* all files in the Distutils "build" tree (default :file:`build/`)
192
Georg Brandl1df03402008-03-06 06:47:18 +0000193* all files in directories named :file:`RCS`, :file:`CVS`, :file:`.svn`,
194 :file:`.hg`, :file:`.git`, :file:`.bzr` or :file:`_darcs`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000195
196Now we have our complete list of files, which is written to the manifest for
197future reference, and then used to build the source distribution archive(s).
198
199You can disable the default set of included files with the
200:option:`--no-defaults` option, and you can disable the standard exclude set
201with :option:`--no-prune`.
202
203Following the Distutils' own manifest template, let's trace how the
204:command:`sdist` command builds the list of files to include in the Distutils
205source distribution:
206
207#. include all Python source files in the :file:`distutils` and
208 :file:`distutils/command` subdirectories (because packages corresponding to
209 those two directories were mentioned in the :option:`packages` option in the
210 setup script---see section :ref:`setup-script`)
211
212#. include :file:`README.txt`, :file:`setup.py`, and :file:`setup.cfg` (standard
213 files)
214
215#. include :file:`test/test\*.py` (standard files)
216
217#. include :file:`\*.txt` in the distribution root (this will find
218 :file:`README.txt` a second time, but such redundancies are weeded out later)
219
220#. include anything matching :file:`\*.txt` or :file:`\*.py` in the sub-tree
221 under :file:`examples`,
222
223#. exclude all files in the sub-trees starting at directories matching
224 :file:`examples/sample?/build`\ ---this may exclude files included by the
225 previous two steps, so it's important that the ``prune`` command in the manifest
226 template comes after the ``recursive-include`` command
227
Georg Brandl1df03402008-03-06 06:47:18 +0000228#. exclude the entire :file:`build` tree, and any :file:`RCS`, :file:`CVS`,
229 :file:`.svn`, :file:`.hg`, :file:`.git`, :file:`.bzr` and :file:`_darcs`
230 directories
Georg Brandl8ec7f652007-08-15 14:28:01 +0000231
232Just like in the setup script, file and directory names in the manifest template
233should always be slash-separated; the Distutils will take care of converting
234them to the standard representation on your platform. That way, the manifest
235template is portable across operating systems.
236
Tarek Ziadé4f786b22009-12-13 23:24:13 +0000237Commands
238--------
Georg Brandl8ec7f652007-08-15 14:28:01 +0000239
Tarek Ziadé4f786b22009-12-13 23:24:13 +0000240The manifest template commands are:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000241
Tarek Ziadé4f786b22009-12-13 23:24:13 +0000242+-------------------------------------------+-----------------------------------------------+
243| Command | Description |
244+===========================================+===============================================+
245| :command:`include pat1 pat2 ...` | include all files matching any of the listed |
246| | patterns |
247+-------------------------------------------+-----------------------------------------------+
248| :command:`exclude pat1 pat2 ...` | exclude all files matching any of the listed |
249| | patterns |
250+-------------------------------------------+-----------------------------------------------+
251| :command:`recursive-include dir pat1 pat2 | include all files under *dir* matching any of |
252| ...` | the listed patterns |
253+-------------------------------------------+-----------------------------------------------+
254| :command:`recursive-exclude dir pat1 pat2 | exclude all files under *dir* matching any of |
255| ...` | the listed patterns |
256+-------------------------------------------+-----------------------------------------------+
257| :command:`global-include pat1 pat2 ...` | include all files anywhere in the source tree |
258| | matching --- & any of the listed patterns |
259+-------------------------------------------+-----------------------------------------------+
260| :command:`global-exclude pat1 pat2 ...` | exclude all files anywhere in the source tree |
261| | matching --- & any of the listed patterns |
262+-------------------------------------------+-----------------------------------------------+
263| :command:`prune dir` | exclude all files under *dir* |
264+-------------------------------------------+-----------------------------------------------+
265| :command:`graft dir` | include all files under *dir* |
266+-------------------------------------------+-----------------------------------------------+
Georg Brandl8ec7f652007-08-15 14:28:01 +0000267
Tarek Ziadé4f786b22009-12-13 23:24:13 +0000268The patterns here are Unix-style "glob" patterns: ``*`` matches any sequence of
269regular filename characters, ``?`` matches any single regular filename
270character, and ``[range]`` matches any of the characters in *range* (e.g.,
271``a-z``, ``a-zA-Z``, ``a-f0-9_.``). The definition of "regular filename
272character" is platform-specific: on Unix it is anything except slash; on Windows
273anything except backslash or colon.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000274
Éric Araujo0389ae62010-08-14 03:07:46 +0000275.. versionchanged:: 2.7
276 An existing generated :file:`MANIFEST` will be regenerated without
277 :command:`sdist` comparing its modification time to the one of
278 :file:`MANIFEST.in` or :file:`setup.py`.