blob: cc289c9b2551cbdf256a6b29f4db7b0db170456e [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
Martin Panter5c679332016-10-30 04:20:17 +000017You can specify as many formats as you like using the :option:`!--formats`
Georg Brandl116aa622007-08-15 14:28:22 +000018option, 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+-----------+-------------------------+---------+
Andrew Kuchling5e2d4562013-11-15 13:01:52 -050029| ``gztar`` | gzip'ed tar file | \(2) |
Georg Brandl116aa622007-08-15 14:28:22 +000030| | (:file:`.tar.gz`) | |
31+-----------+-------------------------+---------+
Andrew Kuchling5e2d4562013-11-15 13:01:52 -050032| ``bztar`` | bzip2'ed tar file | |
Georg Brandl116aa622007-08-15 14:28:22 +000033| | (:file:`.tar.bz2`) | |
34+-----------+-------------------------+---------+
Serhiy Storchakab9cec6a2015-05-16 22:13:27 +030035| ``xztar`` | xz'ed tar file | |
36| | (:file:`.tar.xz`) | |
37+-----------+-------------------------+---------+
Georg Brandl116aa622007-08-15 14:28:22 +000038| ``ztar`` | compressed tar file | \(4) |
39| | (:file:`.tar.Z`) | |
40+-----------+-------------------------+---------+
Andrew Kuchling5e2d4562013-11-15 13:01:52 -050041| ``tar`` | tar file (:file:`.tar`) | |
Georg Brandl116aa622007-08-15 14:28:22 +000042+-----------+-------------------------+---------+
43
Zachary Ware7f142c72015-07-07 00:11:36 -050044.. versionchanged:: 3.5
Serhiy Storchakab9cec6a2015-05-16 22:13:27 +030045 Added support for the ``xztar`` format.
46
Georg Brandl116aa622007-08-15 14:28:22 +000047Notes:
48
49(1)
50 default on Windows
51
52(2)
53 default on Unix
54
55(3)
56 requires either external :program:`zip` utility or :mod:`zipfile` module (part
57 of the standard Python library since Python 1.6)
58
59(4)
Andrew Kuchling5e2d4562013-11-15 13:01:52 -050060 requires the :program:`compress` program. Notice that this format is now
61 pending for deprecation and will be removed in the future versions of Python.
62
Serhiy Storchakab9cec6a2015-05-16 22:13:27 +030063When using any ``tar`` format (``gztar``, ``bztar``, ``xztar``, ``ztar`` or
Andrew Kuchling5e2d4562013-11-15 13:01:52 -050064``tar``), under Unix you can specify the ``owner`` and ``group`` names
65that will be set for each member of the archive.
66
67For example, if you want all files of the archive to be owned by root::
68
69 python setup.py sdist --owner=root --group=root
Georg Brandl116aa622007-08-15 14:28:22 +000070
71
72.. _manifest:
73
74Specifying the files to distribute
75==================================
76
77If you don't supply an explicit list of files (or instructions on how to
78generate one), the :command:`sdist` command puts a minimal default set into the
79source distribution:
80
Georg Brandl3f40c402014-09-21 00:35:08 +020081* all Python source files implied by the ``py_modules`` and
82 ``packages`` options
Georg Brandl116aa622007-08-15 14:28:22 +000083
Georg Brandl3f40c402014-09-21 00:35:08 +020084* all C source files mentioned in the ``ext_modules`` or
85 ``libraries`` options
Georg Brandl116aa622007-08-15 14:28:22 +000086
Georg Brandld5f2d6e2010-07-31 09:15:10 +000087 .. XXX getting C library sources currently broken---no
88 :meth:`get_source_files` method in :file:`build_clib.py`!
Georg Brandl116aa622007-08-15 14:28:22 +000089
Georg Brandl3f40c402014-09-21 00:35:08 +020090* scripts identified by the ``scripts`` option
Tarek Ziadé0d0506e2009-02-16 21:49:12 +000091 See :ref:`distutils-installing-scripts`.
Georg Brandl116aa622007-08-15 14:28:22 +000092
93* anything that looks like a test script: :file:`test/test\*.py` (currently, the
94 Distutils don't do anything with test scripts except include them in source
95 distributions, but in the future there will be a standard for testing Python
96 module distributions)
97
98* :file:`README.txt` (or :file:`README`), :file:`setup.py` (or whatever you
99 called your setup script), and :file:`setup.cfg`
100
Tarek Ziadé0d0506e2009-02-16 21:49:12 +0000101* all files that matches the ``package_data`` metadata.
102 See :ref:`distutils-installing-package-data`.
103
Tarek Ziadé0d0506e2009-02-16 21:49:12 +0000104* all files that matches the ``data_files`` metadata.
105 See :ref:`distutils-additional-files`.
106
Georg Brandl116aa622007-08-15 14:28:22 +0000107Sometimes this is enough, but usually you will want to specify additional files
108to distribute. The typical way to do this is to write a *manifest template*,
109called :file:`MANIFEST.in` by default. The manifest template is just a list of
110instructions for how to generate your manifest file, :file:`MANIFEST`, which is
111the exact list of files to include in your source distribution. The
112:command:`sdist` command processes this template and generates a manifest based
113on its instructions and what it finds in the filesystem.
114
115If you prefer to roll your own manifest file, the format is simple: one filename
116per line, regular files (or symlinks to them) only. If you do supply your own
117:file:`MANIFEST`, you must specify everything: the default set of files
118described above does not apply in this case.
119
Éric Araujoab7c1b32011-07-31 04:06:12 +0200120.. versionchanged:: 3.1
121 An existing generated :file:`MANIFEST` will be regenerated without
122 :command:`sdist` comparing its modification time to the one of
123 :file:`MANIFEST.in` or :file:`setup.py`.
124
125.. versionchanged:: 3.1.3
Éric Araujoa8939272010-08-14 23:44:13 +0000126 :file:`MANIFEST` files start with a comment indicating they are generated.
Éric Araujoda668ff2010-08-14 02:30:34 +0000127 Files without this comment are not overwritten or removed.
128
Éric Araujoab7c1b32011-07-31 04:06:12 +0200129.. versionchanged:: 3.2.2
130 :command:`sdist` will read a :file:`MANIFEST` file if no :file:`MANIFEST.in`
131 exists, like it used to do.
132
133
Georg Brandl116aa622007-08-15 14:28:22 +0000134The manifest template has one command per line, where each command specifies a
135set of files to include or exclude from the source distribution. For an
Martin Panter1050d2d2016-07-26 11:18:21 +0200136example, again we turn to the Distutils' own manifest template:
137
138.. code-block:: none
Georg Brandl116aa622007-08-15 14:28:22 +0000139
140 include *.txt
141 recursive-include examples *.txt *.py
142 prune examples/sample?/build
143
144The meanings should be fairly clear: include all files in the distribution root
145matching :file:`\*.txt`, all files anywhere under the :file:`examples` directory
146matching :file:`\*.txt` or :file:`\*.py`, and exclude all directories matching
147:file:`examples/sample?/build`. All of this is done *after* the standard
148include set, so you can exclude files from the standard set with explicit
149instructions in the manifest template. (Or, you can use the
Martin Panter5c679332016-10-30 04:20:17 +0000150:option:`!--no-defaults` option to disable the standard set entirely.) There are
Tarek Ziadé96c45a92010-07-31 09:10:51 +0000151several other commands available in the manifest template mini-language; see
152section :ref:`sdist-cmd`.
Georg Brandl116aa622007-08-15 14:28:22 +0000153
154The order of commands in the manifest template matters: initially, we have the
155list of default files as described above, and each command in the template adds
156to or removes from that list of files. Once we have fully processed the
157manifest template, we remove files that should not be included in the source
158distribution:
159
160* all files in the Distutils "build" tree (default :file:`build/`)
161
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000162* all files in directories named :file:`RCS`, :file:`CVS`, :file:`.svn`,
163 :file:`.hg`, :file:`.git`, :file:`.bzr` or :file:`_darcs`
Georg Brandl116aa622007-08-15 14:28:22 +0000164
165Now we have our complete list of files, which is written to the manifest for
166future reference, and then used to build the source distribution archive(s).
167
168You can disable the default set of included files with the
Martin Panter5c679332016-10-30 04:20:17 +0000169:option:`!--no-defaults` option, and you can disable the standard exclude set
170with :option:`!--no-prune`.
Georg Brandl116aa622007-08-15 14:28:22 +0000171
172Following the Distutils' own manifest template, let's trace how the
173:command:`sdist` command builds the list of files to include in the Distutils
174source distribution:
175
176#. include all Python source files in the :file:`distutils` and
177 :file:`distutils/command` subdirectories (because packages corresponding to
Georg Brandl3f40c402014-09-21 00:35:08 +0200178 those two directories were mentioned in the ``packages`` option in the
Georg Brandl116aa622007-08-15 14:28:22 +0000179 setup script---see section :ref:`setup-script`)
180
181#. include :file:`README.txt`, :file:`setup.py`, and :file:`setup.cfg` (standard
182 files)
183
184#. include :file:`test/test\*.py` (standard files)
185
186#. include :file:`\*.txt` in the distribution root (this will find
187 :file:`README.txt` a second time, but such redundancies are weeded out later)
188
189#. include anything matching :file:`\*.txt` or :file:`\*.py` in the sub-tree
190 under :file:`examples`,
191
192#. exclude all files in the sub-trees starting at directories matching
193 :file:`examples/sample?/build`\ ---this may exclude files included by the
194 previous two steps, so it's important that the ``prune`` command in the manifest
195 template comes after the ``recursive-include`` command
196
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000197#. exclude the entire :file:`build` tree, and any :file:`RCS`, :file:`CVS`,
198 :file:`.svn`, :file:`.hg`, :file:`.git`, :file:`.bzr` and :file:`_darcs`
199 directories
Georg Brandl116aa622007-08-15 14:28:22 +0000200
201Just like in the setup script, file and directory names in the manifest template
202should always be slash-separated; the Distutils will take care of converting
203them to the standard representation on your platform. That way, the manifest
204template is portable across operating systems.
205
206
Tarek Ziadé96c45a92010-07-31 09:10:51 +0000207.. _manifest-options:
Georg Brandl116aa622007-08-15 14:28:22 +0000208
Tarek Ziadé96c45a92010-07-31 09:10:51 +0000209Manifest-related options
210========================
Georg Brandl116aa622007-08-15 14:28:22 +0000211
Tarek Ziadé96c45a92010-07-31 09:10:51 +0000212The normal course of operations for the :command:`sdist` command is as follows:
213
Éric Araujoab7c1b32011-07-31 04:06:12 +0200214* if the manifest file (:file:`MANIFEST` by default) exists and the first line
215 does not have a comment indicating it is generated from :file:`MANIFEST.in`,
216 then it is used as is, unaltered
217
218* if the manifest file doesn't exist or has been previously automatically
219 generated, read :file:`MANIFEST.in` and create the manifest
Tarek Ziadé96c45a92010-07-31 09:10:51 +0000220
221* if neither :file:`MANIFEST` nor :file:`MANIFEST.in` exist, create a manifest
222 with just the default file set
223
Tarek Ziadé96c45a92010-07-31 09:10:51 +0000224* use the list of files now in :file:`MANIFEST` (either just generated or read
225 in) to create the source distribution archive(s)
226
227There are a couple of options that modify this behaviour. First, use the
Martin Panter5c679332016-10-30 04:20:17 +0000228:option:`!--no-defaults` and :option:`!--no-prune` to disable the standard
Tarek Ziadé96c45a92010-07-31 09:10:51 +0000229"include" and "exclude" sets.
230
231Second, you might just want to (re)generate the manifest, but not create a source
232distribution::
233
234 python setup.py sdist --manifest-only
235
Martin Panter5c679332016-10-30 04:20:17 +0000236:option:`!-o` is a shortcut for :option:`!--manifest-only`.