Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | .. _source-dist: |
| 2 | |
| 3 | ****************************** |
| 4 | Creating a Source Distribution |
| 5 | ****************************** |
| 6 | |
| 7 | As shown in section :ref:`distutils-simple-example`, you use the :command:`sdist` command |
| 8 | to 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 |
| 13 | or config file), :command:`sdist` creates the archive of the default format for |
| 14 | the current platform. The default format is a gzip'ed tar file |
| 15 | (:file:`.tar.gz`) on Unix, and ZIP file on Windows. |
| 16 | |
| 17 | You can specify as many formats as you like using the :option:`--formats` |
| 18 | option, for example:: |
| 19 | |
| 20 | python setup.py sdist --formats=gztar,zip |
| 21 | |
| 22 | to 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 Kuchling | 5e2d456 | 2013-11-15 13:01:52 -0500 | [diff] [blame] | 29 | | ``gztar`` | gzip'ed tar file | \(2) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 30 | | | (:file:`.tar.gz`) | | |
| 31 | +-----------+-------------------------+---------+ |
Andrew Kuchling | 5e2d456 | 2013-11-15 13:01:52 -0500 | [diff] [blame] | 32 | | ``bztar`` | bzip2'ed tar file | | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 33 | | | (:file:`.tar.bz2`) | | |
| 34 | +-----------+-------------------------+---------+ |
Serhiy Storchaka | b9cec6a | 2015-05-16 22:13:27 +0300 | [diff] [blame] | 35 | | ``xztar`` | xz'ed tar file | | |
| 36 | | | (:file:`.tar.xz`) | | |
| 37 | +-----------+-------------------------+---------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 38 | | ``ztar`` | compressed tar file | \(4) | |
| 39 | | | (:file:`.tar.Z`) | | |
| 40 | +-----------+-------------------------+---------+ |
Andrew Kuchling | 5e2d456 | 2013-11-15 13:01:52 -0500 | [diff] [blame] | 41 | | ``tar`` | tar file (:file:`.tar`) | | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 42 | +-----------+-------------------------+---------+ |
| 43 | |
Zachary Ware | 7f142c7 | 2015-07-07 00:11:36 -0500 | [diff] [blame^] | 44 | .. versionchanged:: 3.5 |
Serhiy Storchaka | b9cec6a | 2015-05-16 22:13:27 +0300 | [diff] [blame] | 45 | Added support for the ``xztar`` format. |
| 46 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 47 | Notes: |
| 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 Kuchling | 5e2d456 | 2013-11-15 13:01:52 -0500 | [diff] [blame] | 60 | 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 Storchaka | b9cec6a | 2015-05-16 22:13:27 +0300 | [diff] [blame] | 63 | When using any ``tar`` format (``gztar``, ``bztar``, ``xztar``, ``ztar`` or |
Andrew Kuchling | 5e2d456 | 2013-11-15 13:01:52 -0500 | [diff] [blame] | 64 | ``tar``), under Unix you can specify the ``owner`` and ``group`` names |
| 65 | that will be set for each member of the archive. |
| 66 | |
| 67 | For 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 70 | |
| 71 | |
| 72 | .. _manifest: |
| 73 | |
| 74 | Specifying the files to distribute |
| 75 | ================================== |
| 76 | |
| 77 | If you don't supply an explicit list of files (or instructions on how to |
| 78 | generate one), the :command:`sdist` command puts a minimal default set into the |
| 79 | source distribution: |
| 80 | |
Georg Brandl | 3f40c40 | 2014-09-21 00:35:08 +0200 | [diff] [blame] | 81 | * all Python source files implied by the ``py_modules`` and |
| 82 | ``packages`` options |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 83 | |
Georg Brandl | 3f40c40 | 2014-09-21 00:35:08 +0200 | [diff] [blame] | 84 | * all C source files mentioned in the ``ext_modules`` or |
| 85 | ``libraries`` options |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 86 | |
Georg Brandl | d5f2d6e | 2010-07-31 09:15:10 +0000 | [diff] [blame] | 87 | .. XXX getting C library sources currently broken---no |
| 88 | :meth:`get_source_files` method in :file:`build_clib.py`! |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 89 | |
Georg Brandl | 3f40c40 | 2014-09-21 00:35:08 +0200 | [diff] [blame] | 90 | * scripts identified by the ``scripts`` option |
Tarek Ziadé | 0d0506e | 2009-02-16 21:49:12 +0000 | [diff] [blame] | 91 | See :ref:`distutils-installing-scripts`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 92 | |
| 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é | 0d0506e | 2009-02-16 21:49:12 +0000 | [diff] [blame] | 101 | * all files that matches the ``package_data`` metadata. |
| 102 | See :ref:`distutils-installing-package-data`. |
| 103 | |
Tarek Ziadé | 0d0506e | 2009-02-16 21:49:12 +0000 | [diff] [blame] | 104 | * all files that matches the ``data_files`` metadata. |
| 105 | See :ref:`distutils-additional-files`. |
| 106 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 107 | Sometimes this is enough, but usually you will want to specify additional files |
| 108 | to distribute. The typical way to do this is to write a *manifest template*, |
| 109 | called :file:`MANIFEST.in` by default. The manifest template is just a list of |
| 110 | instructions for how to generate your manifest file, :file:`MANIFEST`, which is |
| 111 | the exact list of files to include in your source distribution. The |
| 112 | :command:`sdist` command processes this template and generates a manifest based |
| 113 | on its instructions and what it finds in the filesystem. |
| 114 | |
| 115 | If you prefer to roll your own manifest file, the format is simple: one filename |
| 116 | per 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 |
| 118 | described above does not apply in this case. |
| 119 | |
Éric Araujo | ab7c1b3 | 2011-07-31 04:06:12 +0200 | [diff] [blame] | 120 | .. 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 Araujo | a893927 | 2010-08-14 23:44:13 +0000 | [diff] [blame] | 126 | :file:`MANIFEST` files start with a comment indicating they are generated. |
Éric Araujo | da668ff | 2010-08-14 02:30:34 +0000 | [diff] [blame] | 127 | Files without this comment are not overwritten or removed. |
| 128 | |
Éric Araujo | ab7c1b3 | 2011-07-31 04:06:12 +0200 | [diff] [blame] | 129 | .. 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 134 | The manifest template has one command per line, where each command specifies a |
| 135 | set of files to include or exclude from the source distribution. For an |
Tarek Ziadé | 96c45a9 | 2010-07-31 09:10:51 +0000 | [diff] [blame] | 136 | example, again we turn to the Distutils' own manifest template:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 137 | |
| 138 | include *.txt |
| 139 | recursive-include examples *.txt *.py |
| 140 | prune examples/sample?/build |
| 141 | |
| 142 | The meanings should be fairly clear: include all files in the distribution root |
| 143 | matching :file:`\*.txt`, all files anywhere under the :file:`examples` directory |
| 144 | matching :file:`\*.txt` or :file:`\*.py`, and exclude all directories matching |
| 145 | :file:`examples/sample?/build`. All of this is done *after* the standard |
| 146 | include set, so you can exclude files from the standard set with explicit |
| 147 | instructions in the manifest template. (Or, you can use the |
Tarek Ziadé | 96c45a9 | 2010-07-31 09:10:51 +0000 | [diff] [blame] | 148 | :option:`--no-defaults` option to disable the standard set entirely.) There are |
| 149 | several other commands available in the manifest template mini-language; see |
| 150 | section :ref:`sdist-cmd`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 151 | |
| 152 | The order of commands in the manifest template matters: initially, we have the |
| 153 | list of default files as described above, and each command in the template adds |
| 154 | to or removes from that list of files. Once we have fully processed the |
| 155 | manifest template, we remove files that should not be included in the source |
| 156 | distribution: |
| 157 | |
| 158 | * all files in the Distutils "build" tree (default :file:`build/`) |
| 159 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 160 | * all files in directories named :file:`RCS`, :file:`CVS`, :file:`.svn`, |
| 161 | :file:`.hg`, :file:`.git`, :file:`.bzr` or :file:`_darcs` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 162 | |
| 163 | Now we have our complete list of files, which is written to the manifest for |
| 164 | future reference, and then used to build the source distribution archive(s). |
| 165 | |
| 166 | You can disable the default set of included files with the |
| 167 | :option:`--no-defaults` option, and you can disable the standard exclude set |
| 168 | with :option:`--no-prune`. |
| 169 | |
| 170 | Following the Distutils' own manifest template, let's trace how the |
| 171 | :command:`sdist` command builds the list of files to include in the Distutils |
| 172 | source distribution: |
| 173 | |
| 174 | #. include all Python source files in the :file:`distutils` and |
| 175 | :file:`distutils/command` subdirectories (because packages corresponding to |
Georg Brandl | 3f40c40 | 2014-09-21 00:35:08 +0200 | [diff] [blame] | 176 | those two directories were mentioned in the ``packages`` option in the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 177 | setup script---see section :ref:`setup-script`) |
| 178 | |
| 179 | #. include :file:`README.txt`, :file:`setup.py`, and :file:`setup.cfg` (standard |
| 180 | files) |
| 181 | |
| 182 | #. include :file:`test/test\*.py` (standard files) |
| 183 | |
| 184 | #. include :file:`\*.txt` in the distribution root (this will find |
| 185 | :file:`README.txt` a second time, but such redundancies are weeded out later) |
| 186 | |
| 187 | #. include anything matching :file:`\*.txt` or :file:`\*.py` in the sub-tree |
| 188 | under :file:`examples`, |
| 189 | |
| 190 | #. exclude all files in the sub-trees starting at directories matching |
| 191 | :file:`examples/sample?/build`\ ---this may exclude files included by the |
| 192 | previous two steps, so it's important that the ``prune`` command in the manifest |
| 193 | template comes after the ``recursive-include`` command |
| 194 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 195 | #. exclude the entire :file:`build` tree, and any :file:`RCS`, :file:`CVS`, |
| 196 | :file:`.svn`, :file:`.hg`, :file:`.git`, :file:`.bzr` and :file:`_darcs` |
| 197 | directories |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 198 | |
| 199 | Just like in the setup script, file and directory names in the manifest template |
| 200 | should always be slash-separated; the Distutils will take care of converting |
| 201 | them to the standard representation on your platform. That way, the manifest |
| 202 | template is portable across operating systems. |
| 203 | |
| 204 | |
Tarek Ziadé | 96c45a9 | 2010-07-31 09:10:51 +0000 | [diff] [blame] | 205 | .. _manifest-options: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 206 | |
Tarek Ziadé | 96c45a9 | 2010-07-31 09:10:51 +0000 | [diff] [blame] | 207 | Manifest-related options |
| 208 | ======================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 209 | |
Tarek Ziadé | 96c45a9 | 2010-07-31 09:10:51 +0000 | [diff] [blame] | 210 | The normal course of operations for the :command:`sdist` command is as follows: |
| 211 | |
Éric Araujo | ab7c1b3 | 2011-07-31 04:06:12 +0200 | [diff] [blame] | 212 | * if the manifest file (:file:`MANIFEST` by default) exists and the first line |
| 213 | does not have a comment indicating it is generated from :file:`MANIFEST.in`, |
| 214 | then it is used as is, unaltered |
| 215 | |
| 216 | * if the manifest file doesn't exist or has been previously automatically |
| 217 | generated, read :file:`MANIFEST.in` and create the manifest |
Tarek Ziadé | 96c45a9 | 2010-07-31 09:10:51 +0000 | [diff] [blame] | 218 | |
| 219 | * if neither :file:`MANIFEST` nor :file:`MANIFEST.in` exist, create a manifest |
| 220 | with just the default file set |
| 221 | |
Tarek Ziadé | 96c45a9 | 2010-07-31 09:10:51 +0000 | [diff] [blame] | 222 | * use the list of files now in :file:`MANIFEST` (either just generated or read |
| 223 | in) to create the source distribution archive(s) |
| 224 | |
| 225 | There are a couple of options that modify this behaviour. First, use the |
| 226 | :option:`--no-defaults` and :option:`--no-prune` to disable the standard |
| 227 | "include" and "exclude" sets. |
| 228 | |
| 229 | Second, you might just want to (re)generate the manifest, but not create a source |
| 230 | distribution:: |
| 231 | |
| 232 | python setup.py sdist --manifest-only |
| 233 | |
| 234 | :option:`-o` is a shortcut for :option:`--manifest-only`. |