1Tagging and "Daily Build" or "Snapshot" Releases
2------------------------------------------------
3
4When a set of related projects are under development, it may be important to
5track finer-grained version increments than you would normally use for e.g.
6"stable" releases.  While stable releases might be measured in dotted numbers
7with alpha/beta/etc. status codes, development versions of a project often
8need to be tracked by revision or build number or even build date.  This is
9especially true when projects in development need to refer to one another, and
10therefore may literally need an up-to-the-minute version of something!
11
12To support these scenarios, ``setuptools`` allows you to "tag" your source and
13egg distributions by adding one or more of the following to the project's
14"official" version identifier:
15
16* A manually-specified pre-release tag, such as "build" or "dev", or a
17  manually-specified post-release tag, such as a build or revision number
18  (``--tag-build=STRING, -bSTRING``)
19
20* An 8-character representation of the build date (``--tag-date, -d``), as
21  a postrelease tag
22
23You can add these tags by adding ``egg_info`` and the desired options to
24the command line ahead of the ``sdist`` or ``bdist`` commands that you want
25to generate a daily build or snapshot for.  See the section below on the
26:ref:`egg_info <egg_info>` command for more details.
27
28(Also, before you release your project, be sure to see the section on
29:ref:`Specifying Your Project's Version` for more information about how pre- and
30post-release tags affect how version numbers are interpreted.  This is
31important in order to make sure that dependency processing tools will know
32which versions of your project are newer than others.)
33
34Finally, if you are creating builds frequently, and either building them in a
35downloadable location or are copying them to a distribution server, you should
36probably also check out the :ref:`rotate <rotate>` command, which lets you automatically
37delete all but the N most-recently-modified distributions matching a glob
38pattern.  So, you can use a command line like::
39
40    setup.py egg_info -rbDEV bdist_egg rotate -m.egg -k3
41
42to build an egg whose version info includes "DEV-rNNNN" (where NNNN is the
43most recent Subversion revision that affected the source tree), and then
44delete any egg files from the distribution directory except for the three
45that were built most recently.
46
47If you have to manage automated builds for multiple packages, each with
48different tagging and rotation policies, you may also want to check out the
49:ref:`alias <alias>` command, which would let each package define an alias like ``daily``
50that would perform the necessary tag, build, and rotate commands.  Then, a
51simpler script or cron job could just run ``setup.py daily`` in each project
52directory.  (And, you could also define sitewide or per-user default versions
53of the ``daily`` alias, so that projects that didn't define their own would
54use the appropriate defaults.)
55
56Generating Source Distributions
57-------------------------------
58
59``setuptools`` enhances the distutils' default algorithm for source file
60selection with pluggable endpoints for looking up files to include. If you are
61using a revision control system, and your source distributions only need to
62include files that you're tracking in revision control, use a corresponding
63plugin instead of writing a ``MANIFEST.in`` file. See the section below on
64:ref:`Adding Support for Revision Control Systems` for information on plugins.
65
66If you need to include automatically generated files, or files that are kept in
67an unsupported revision control system, you'll need to create a ``MANIFEST.in``
68file to specify any files that the default file location algorithm doesn't
69catch.  See the distutils documentation for more information on the format of
70the ``MANIFEST.in`` file.
71
72But, be sure to ignore any part of the distutils documentation that deals with
73``MANIFEST`` or how it's generated from ``MANIFEST.in``; setuptools shields you
74from these issues and doesn't work the same way in any case.  Unlike the
75distutils, setuptools regenerates the source distribution manifest file
76every time you build a source distribution, and it builds it inside the
77project's ``.egg-info`` directory, out of the way of your main project
78directory.  You therefore need not worry about whether it is up-to-date or not.
79
80Indeed, because setuptools' approach to determining the contents of a source
81distribution is so much simpler, its ``sdist`` command omits nearly all of
82the options that the distutils' more complex ``sdist`` process requires.  For
83all practical purposes, you'll probably use only the ``--formats`` option, if
84you use any option at all.
85
86
87Making "Official" (Non-Snapshot) Releases
88~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89
90When you make an official release, creating source or binary distributions,
91you will need to override the tag settings from ``setup.cfg``, so that you
92don't end up registering versions like ``foobar-0.7a1.dev-r34832``.  This is
93easy to do if you are developing on the trunk and using tags or branches for
94your releases - just make the change to ``setup.cfg`` after branching or
95tagging the release, so the trunk will still produce development snapshots.
96
97Alternately, if you are not branching for releases, you can override the
98default version options on the command line, using something like::
99
100    setup.py egg_info -Db "" sdist bdist_egg
101
102The first part of this command (``egg_info -Db ""``) will override the
103configured tag information, before creating source and binary eggs. Thus, these
104commands will use the plain version from your ``setup.py``, without adding the
105build designation string.
106
107Of course, if you will be doing this a lot, you may wish to create a personal
108alias for this operation, e.g.::
109
110    setup.py alias -u release egg_info -Db ""
111
112You can then use it like this::
113
114    setup.py release sdist bdist_egg
115
116Or of course you can create more elaborate aliases that do all of the above.
117See the sections below on the :ref:`egg_info <egg_info>` and
118:ref:`alias <alias>` commands for more ideas.
119
120Distributing Extensions compiled with Cython
121--------------------------------------------
122
123``setuptools`` will detect at build time whether Cython is installed or not.
124If Cython is not found ``setuptools`` will ignore pyx files.
125
126To ensure Cython is available, include Cython in the build-requires section
127of your pyproject.toml::
128
129    [build-system]
130    requires=[..., "cython"]
131
132Built with pip 10 or later, that declaration is sufficient to include Cython
133in the build. For broader compatibility, declare the dependency in your
134setup-requires of setup.cfg::
135
136    [options]
137    setup_requires =
138        ...
139        cython
140
141As long as Cython is present in the build environment, ``setuptools`` includes
142transparent support for building Cython extensions, as
143long as extensions are defined using ``setuptools.Extension``.
144
145If you follow these rules, you can safely list ``.pyx`` files as the source
146of your ``Extension`` objects in the setup script.  If it is, then ``setuptools``
147will use it.
148
149Of course, for this to work, your source distributions must include the C
150code generated by Cython, as well as your original ``.pyx`` files.  This means
151that you will probably want to include current ``.c`` files in your revision
152control system, rebuilding them whenever you check changes in for the ``.pyx``
153source files.  This will ensure that people tracking your project in a revision
154control system will be able to build it even if they don't have Cython
155installed, and that your source releases will be similarly usable with or
156without Cython.
157
158
159.. _Specifying Your Project's Version:
160
161Specifying Your Project's Version
162---------------------------------
163
164Setuptools can work well with most versioning schemes. Over the years,
165setuptools has tried to closely follow the
166`PEP 440 <https://www.python.org/dev/peps/pep-0440/>`_ scheme, but it
167also supports legacy versions. There are, however, a
168few special things to watch out for, in order to ensure that setuptools and
169other tools can always tell what version of your package is newer than another
170version.  Knowing these things will also help you correctly specify what
171versions of other projects your project depends on.
172
173A version consists of an alternating series of release numbers and pre-release
174or post-release tags.  A release number is a series of digits punctuated by
175dots, such as ``2.4`` or ``0.5``.  Each series of digits is treated
176numerically, so releases ``2.1`` and ``2.1.0`` are different ways to spell the
177same release number, denoting the first subrelease of release 2.  But  ``2.10``
178is the *tenth* subrelease of release 2, and so is a different and newer release
179from ``2.1`` or ``2.1.0``.  Leading zeros within a series of digits are also
180ignored, so ``2.01`` is the same as ``2.1``, and different from ``2.0.1``.
181
182Following a release number, you can have either a pre-release or post-release
183tag.  Pre-release tags make a version be considered *older* than the version
184they are appended to.  So, revision ``2.4`` is *newer* than revision ``2.4c1``,
185which in turn is newer than ``2.4b1`` or ``2.4a1``.  Postrelease tags make
186a version be considered *newer* than the version they are appended to.  So,
187revisions like ``2.4-1`` are newer than ``2.4``, but *older*
188than ``2.4.1`` (which has a higher release number).
189
190In the case of legacy versions (for example, ``2.4pl1``), they are considered
191older than non-legacy versions. Taking that in count, a revision ``2.4pl1``
192is *older* than ``2.4``
193
194A pre-release tag is a series of letters that are alphabetically before
195"final".  Some examples of prerelease tags would include ``alpha``, ``beta``,
196``a``, ``c``, ``dev``, and so on.  You do not have to place a dot or dash
197before the prerelease tag if it's immediately after a number, but it's okay to
198do so if you prefer.  Thus, ``2.4c1`` and ``2.4.c1`` and ``2.4-c1`` all
199represent release candidate 1 of version ``2.4``, and are treated as identical
200by setuptools.
201
202In addition, there are three special prerelease tags that are treated as if
203they were the letter ``c``: ``pre``, ``preview``, and ``rc``.  So, version
204``2.4rc1``, ``2.4pre1`` and ``2.4preview1`` are all the exact same version as
205``2.4c1``, and are treated as identical by setuptools.
206
207A post-release tag is either a series of letters that are alphabetically
208greater than or equal to "final", or a dash (``-``).  Post-release tags are
209generally used to separate patch numbers, port numbers, build numbers, revision
210numbers, or date stamps from the release number.  For example, the version
211``2.4-r1263`` might denote Subversion revision 1263 of a post-release patch of
212version ``2.4``.  Or you might use ``2.4-20051127`` to denote a date-stamped
213post-release.
214
215Notice that after each pre or post-release tag, you are free to place another
216release number, followed again by more pre- or post-release tags.  For example,
217``0.6a9.dev-r41475`` could denote Subversion revision 41475 of the in-
218development version of the ninth alpha of release 0.6.  Notice that ``dev`` is
219a pre-release tag, so this version is a *lower* version number than ``0.6a9``,
220which would be the actual ninth alpha of release 0.6.  But the ``-r41475`` is
221a post-release tag, so this version is *newer* than ``0.6a9.dev``.
222
223For the most part, setuptools' interpretation of version numbers is intuitive,
224but here are a few tips that will keep you out of trouble in the corner cases:
225
226* Don't stick adjoining pre-release tags together without a dot or number
227  between them.  Version ``1.9adev`` is the ``adev`` prerelease of ``1.9``,
228  *not* a development pre-release of ``1.9a``.  Use ``.dev`` instead, as in
229  ``1.9a.dev``, or separate the prerelease tags with a number, as in
230  ``1.9a0dev``.  ``1.9a.dev``, ``1.9a0dev``, and even ``1.9.a.dev`` are
231  identical versions from setuptools' point of view, so you can use whatever
232  scheme you prefer.
233
234* If you want to be certain that your chosen numbering scheme works the way
235  you think it will, you can use the ``pkg_resources.parse_version()`` function
236  to compare different version numbers::
237
238    >>> from pkg_resources import parse_version
239    >>> parse_version("1.9.a.dev") == parse_version("1.9a0dev")
240    True
241    >>> parse_version("2.1-rc2") < parse_version("2.1")
242    True
243    >>> parse_version("0.6a9dev-r41475") < parse_version("0.6a9")
244    True
245
246Once you've decided on a version numbering scheme for your project, you can
247have setuptools automatically tag your in-development releases with various
248pre- or post-release tags.  See the following sections for more details:
249
250* `Tagging and "Daily Build" or "Snapshot" Releases`_
251* The :ref:`egg_info <egg_info>` command
252