Skip to content

Commit 95afb44

Browse files
committed
Improve examples of fetching package version in runtime
1 parent e154d41 commit 95afb44

File tree

10 files changed

+134
-57
lines changed

10 files changed

+134
-57
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ Changelog
1717
:tags: docs, feature
1818
:tickets: 17
1919

20-
Add small example of ``.gitignore`` file to common issues section
20+
Add small example of ``.gitignore`` file to common issues section.
21+
22+
.. change::
23+
:tags: docs, feature
24+
:tickets: 55
25+
26+
Improve examples of fetching package version in runtime.
2127

2228
.. changelog::
2329
:version: 1.13.1

docs/command.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ This script is a wrapper for ``setuptools_git_versioning`` module, you can just
7373
DE8UG: Executing 'git rev-list -n 1 "1.0.0"' at '/path/to/mypackage'
7474
INF0: Tag SHA-256: '8dc9881eacd373cb34c5d3f99a6ad9e2349a79c4'
7575
INF0: Parsing tag_formatter 'util:tag_formatter' of type 'str'
76-
DE8UG: Executing 'from mypkg.util import tag_formatter'
76+
DE8UG: Executing 'from my_module.util import tag_formatter'
7777
DE8UG: Tag after formatting: '1.0.0'
7878
DE8UG: Executing 'git status --short' at '/path/to/mypackage'
7979
INF0: Is dirty: False

docs/options/branch_formatter.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ you'll get version number which ``pip`` cannot understand.
4040
To fix that you can define a callback which will receive current branch
4141
name and return a properly formatted one:
4242

43-
- ``mypkg/util.py`` file:
43+
- ``my_module/util.py`` file:
4444

4545
.. code:: python
4646
@@ -67,7 +67,7 @@ name and return a properly formatted one:
6767

6868
.. code:: python
6969
70-
from mypkg.util import format_branch_name
70+
from my_module.util import format_branch_name
7171
7272
setuptools.setup(
7373
...,
@@ -92,7 +92,7 @@ name and return a properly formatted one:
9292
enabled = true
9393
dev_template = "{branch}.dev{ccount}"
9494
dirty_template = "{branch}.dev{ccount}"
95-
branch_formatter = "mypkg.util:format_branch_name"
95+
branch_formatter = "my_module.util:format_branch_name"
9696
9797
.. note::
9898

docs/options/starting_version.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ You can change this version by setting up ``starting_version`` option in your co
2525
)
2626
2727
- ``pyproject.toml`` file:
28-
-
28+
2929
.. code:: toml
3030
3131
[tool.setuptools-git-versioning]

docs/options/tag_formatter.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ you'll get version number which ``pip`` cannot understand.
4242
To fix that you can define a callback which will receive current tag
4343
name and return a properly formatted one:
4444

45-
- ``mypkg/util.py`` file:
45+
- ``my_module/util.py`` file:
4646

4747
.. code:: python
4848
@@ -68,7 +68,7 @@ name and return a properly formatted one:
6868

6969
.. code:: python
7070
71-
from mypkg.util import format_tag_name
71+
from my_module.util import format_tag_name
7272
7373
setuptools.setup(
7474
...,
@@ -93,7 +93,7 @@ name and return a properly formatted one:
9393
enabled = true
9494
dev_template = "{tag}.dev{ccount}"
9595
dirty_template = "{tag}.dev{ccount}"
96-
tag_formatter = "mypkg.util:format_tag_name"
96+
tag_formatter = "my_module.util:format_tag_name"
9797
9898
.. note::
9999

docs/runtime_version.rst

Lines changed: 81 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,83 @@
33
Retrieving package version at runtime
44
-------------------------------------
55

6-
Using ``version_file`` or ``version_callback`` options
7-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6+
Using ``version_file`` option (recommended)
7+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
88

9-
The preferred way to set version number inside a package is
10-
to simply store it in some file or variable/function, and
11-
then use it in ``setup.py`` / ``pyproject.toml`` as version source.
9+
In case of using :ref:`version_file <version-file>` option you can directly read the ``VERSION`` file content,
10+
and use at as version number.
1211

13-
It this case you can get current version without any access to ``.git`` folder
14-
(which is required by ``setuptools-git-versioning``).
12+
To resolve version number in runtime, you should move ``VERSION`` file to your module subfolder:
1513

16-
See:
14+
- ``setup.py``:
15+
16+
Create ``MANIFEST.in`` file in the project root:
17+
18+
.. code::
19+
20+
include my_module/VERSION
21+
22+
Then make few changes in ``setup.py``:
23+
24+
.. code:: python
25+
26+
...
27+
28+
# change VERSION file path
29+
version_file = root_path / "my_module" / "VERSION"
30+
31+
setuptools.setup(
32+
...,
33+
setuptools_git_versioning={
34+
"enabled": True,
35+
"version_file": version_file,
36+
},
37+
# read MANIFEST.in and include files mentioned here to the package
38+
include_package_data=True,
39+
# this package will read some included files in runtime, avoid installing it as .zip
40+
zip_safe=False,
41+
)
42+
43+
- ``pyproject.toml``:
44+
45+
.. code:: toml
46+
47+
[tool.setuptools.package-data]
48+
# include VERSION file to a package
49+
my_module = ["VERSION"]
50+
# this package will read some included files in runtime, avoid installing it as .zip
51+
zip-safe = false
52+
53+
[tool.setuptools-git-versioning]
54+
enabled = true
55+
# change the file path
56+
version_file = "my_module/VERSION"
57+
58+
And then read this file:
59+
60+
.. code:: python
61+
62+
# content of my_module/__init__.py
63+
64+
from pathlib import Path
65+
66+
# you can use os.path and open() as well
67+
__version__ = Path(__file__).parent.joinpath("VERSION").read_text()
68+
69+
70+
Using ``version_callback`` option
71+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72+
73+
In case of using :ref:`version_callback <version-callback>` option you can directly call this callback inside a module:
74+
75+
.. code:: python
76+
77+
# content of my_module/__init__.py
78+
79+
from my_module.version import get_version
80+
81+
__version__ = get_version()
1782
18-
* :ref:`version-file`
19-
* :ref:`version-callback`
2083
2184
Using ``importlib``
2285
~~~~~~~~~~~~~~~~~~~
@@ -66,19 +129,19 @@ Calling internals of ``setuptools_git_versioning`` module
66129

67130
.. warning::
68131

69-
This way is STRONGLY DISCOURAGED. Functions in the module
70-
are not a part of public API, and could be changed in the future without
71-
maintaining backward compatibility.
132+
This way is STRONGLY DISCOURAGED. Functions in the module
133+
are not a part of public API, and could be changed in the future without
134+
maintaining backward compatibility.
72135

73136
.. warning::
74137

75-
Use this ONLY in CI/CD tools.
138+
Use this ONLY in CI/CD tools.
76139

77-
NEVER use ``setuptools_git_versioning`` inside your package, because ``.git``
78-
folder is not being included into it, and target OS can lack of ``git`` executable.
140+
NEVER use ``setuptools_git_versioning`` inside your package, because ``.git``
141+
folder is not being included into it, and target OS can lack of ``git`` executable.
79142

80-
``.git`` folder and ``git`` executable presence is crucial
81-
for ``setuptools-git-versioning`` to work properly.
143+
``.git`` folder and ``git`` executable presence is crucial
144+
for ``setuptools-git-versioning`` to work properly.
82145

83146
.. code:: python
84147

docs/schemas/callback/version_callback.rst

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ because there are no tags in the ``dev`` branch.
2525

2626
If you want to get synchronized version numbers in both ``master`` and ``dev`` branches,
2727
you can create a function in some file (for example, in the
28-
``mypkg/version.py`` file):
28+
``my_module/version.py`` file):
2929

3030
.. code:: python
3131
@@ -36,7 +36,7 @@ Then place it in both the branches and update your ``setup.py`` or ``pyproject.t
3636

3737
.. code:: python
3838
39-
from mypkg.version import get_version
39+
from my_module.version import get_version
4040
4141
setuptools.setup(
4242
...,
@@ -55,15 +55,15 @@ Then place it in both the branches and update your ``setup.py`` or ``pyproject.t
5555
5656
[tool.setuptools-git-versioning]
5757
enabled = true
58-
version_callback = "mypkg.version:get_version"
58+
version_callback = "my_module.version:get_version"
5959
6060
When you'll try to get current version in **any** branch, the result
6161
of executing this function will be returned instead of latest tag
6262
number.
6363

6464
If a value of this option is not a function but just str, it also could be used:
6565

66-
- ``mypkg/__init__.py`` file:
66+
- ``my_module/__init__.py`` file:
6767

6868
.. code:: python
6969
@@ -73,13 +73,13 @@ If a value of this option is not a function but just str, it also could be used:
7373

7474
.. code:: python
7575
76-
import mypkg
76+
import my_module
7777
7878
setuptools.setup(
7979
...,
8080
setuptools_git_versioning={
8181
"enabled": True,
82-
"version_callback": mypkg.__version__,
82+
"version_callback": my_module.__version__,
8383
},
8484
)
8585
@@ -92,7 +92,7 @@ If a value of this option is not a function but just str, it also could be used:
9292
9393
[tool.setuptools-git-versioning]
9494
enabled = true
95-
version_callback = "mypkg:__version__"
95+
version_callback = "my_module:__version__"
9696
9797
**Please take into account that any tag in the branch is completely ignored if version_callback
9898
is set**.
@@ -105,3 +105,4 @@ You should explicitly call ``setuptools_git_versioning.version_from_git`` functi
105105
See also
106106
""""""""
107107
- :ref:`version-callback-option` option
108+
- :ref:`runtime-version`

docs/schemas/file/index.rst

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@
33
File-based release (recommended)
44
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55

6-
.. note::
6+
By default, ``setuptools-git-versioning`` can be used only within:
77

8-
Prefer this schema because it allows to get a version number in the following cases:
9-
* Downloading source tarball without ``.git`` folder (:issue:`77`).
10-
* Shallow repo clone without tags (:issue:`75`).
11-
* Getting version number from a branch which does not contain any tags (`Git-flow <https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow>`__ and its derivatives)
8+
* git repo, which means a ``.git`` subfolder should exist in the repo root folder
9+
* branch with at least one tag
10+
11+
Otherwise it will be impossible to get project version based on the git repo commits,
12+
and ``setuptools-git-versioning`` will return version number ``0.0.1`` (or other value set up by :ref:`starting-version-option`).
13+
14+
But one or all of these requirements cannot be satisfied in the following cases:
15+
16+
* Downloading source tarball without ``.git`` folder (:issue:`77`).
17+
* Shallow repo clone without tags (:issue:`75`).
18+
* Getting version number from a branch which does not contain any tags (`Git-flow <https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow>`__ and its derivatives)
19+
20+
To avoid getting meaningless version number prefer using versioning schema described below.
1221

1322
.. toctree::
1423
:maxdepth: 1

docs/schemas/file/version_file.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,17 @@ Then place it in both the branches and update your config file:
3939

4040
.. code:: python
4141
42-
import os
42+
# you can use os.path instead of pathlib
43+
from pathlib import Path
4344
44-
HERE = os.path.dirname(__file__)
45-
VERSION_FILE = os.path.join(HERE, "VERSION")
45+
root_path = Path(__file__).parent
46+
version_file = root_path / "VERSION"
4647
4748
setuptools.setup(
4849
...,
4950
setuptools_git_versioning={
5051
"enabled": True,
51-
"version_file": VERSION_FILE,
52+
"version_file": version_file,
5253
},
5354
)
5455
@@ -63,10 +64,11 @@ Then place it in both the branches and update your config file:
6364
When you'll try to get current version in non-master branch, the content
6465
of this file (``1.0.0``) will be returned instead default version number.
6566

66-
**Please take into account that version_file is ignored if tag
67-
is present**
67+
**Please take into account that version_file is ignored if any tag
68+
is present in the current branch.**
6869

6970
See also
7071
"""""""""
7172
- :ref:`version-callback`
7273
- :ref:`version-file-option` option
74+
- :ref:`runtime-version`

setup.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
1-
import os
1+
from pathlib import Path
22
from setuptools import setup, find_packages
33
from setuptools_git_versioning import version_from_git
44

5-
HERE = os.path.dirname(os.path.abspath(__file__))
65

6+
def parse_requirements(file: Path) -> list[str]:
7+
lines = file.read_text().splitlines()
8+
return [line.rstrip() for line in lines if line and not line.startswith("#")]
79

8-
def parse_requirements(file_content):
9-
lines = file_content.splitlines()
10-
return [line.strip() for line in lines if line and not line.startswith("#")]
1110

12-
13-
with open(os.path.join(HERE, "README.rst")) as f:
14-
long_description = f.read()
15-
16-
with open(os.path.join(HERE, "requirements.txt")) as f:
17-
requirements = parse_requirements(f.read())
11+
here = Path(__file__).parent.resolve()
12+
requirements = parse_requirements(here / "requirements.txt")
13+
long_description = here.joinpath("README.rst").read_text()
1814

1915
setup(
2016
name="setuptools-git-versioning",
2117
# +local version is not allowed in PyPI
2218
# https://github.com/pypa/pypi-legacy/issues/731#issuecomment-345461596
23-
version=version_from_git(root=HERE, dev_template="{tag}.post{ccount}"),
19+
version=version_from_git(root=here, dev_template="{tag}.post{ccount}"),
2420
author="dolfinus",
2521
author_email="[email protected]",
2622
description="Use git repo data for building a version number according PEP-440",

0 commit comments

Comments
 (0)