Skip to content

Commit cc58b3c

Browse files
committed
Use local git version when _version file does not exist
1 parent 0c20508 commit cc58b3c

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

pyproject.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ source = "vcs"
4141

4242
[tool.hatch.build.hooks.vcs]
4343
version-file = "testscenarios/_version.py"
44+
tag-pattern = "^(testtools-)?(?P<version>[0-9]+(\\.[0-9]+)*(-[0-9]+)?)(\\.post(?P<post>[0-9]+))?$"
45+
template = """\
46+
# This file is automatically generated by hatch.
47+
version = {version!r}
48+
__version__ = {version_tuple!r}
49+
"""
4450

4551
[tool.hatch.build.targets.sdist]
4652
include = [
@@ -57,4 +63,4 @@ include = [
5763
"README.rst",
5864
"tox.ini",
5965
"doc/*.py",
60-
]
66+
]

testscenarios/__init__.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,53 @@ def test_suite():
6161
def load_tests(standard_tests, module, loader):
6262
standard_tests.addTests(loader.loadTestsFromNames(["testscenarios.tests"]))
6363
return standard_tests
64+
65+
66+
def __get_git_version():
67+
import os
68+
import subprocess
69+
70+
cwd = os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir)
71+
72+
try:
73+
out = subprocess.check_output(
74+
["git", "describe"], stderr=subprocess.STDOUT, cwd=cwd
75+
)
76+
except (OSError, subprocess.CalledProcessError):
77+
return None
78+
79+
try:
80+
version = out.strip().decode("utf-8")
81+
except UnicodeDecodeError:
82+
return None
83+
84+
if "-" in version: # after tag
85+
# convert version-N-githash to version.postN+githash
86+
return version.replace("-", ".post", 1).replace("-g", "+git", 1)
87+
else:
88+
return version
89+
90+
91+
# same format as sys.version_info: "A tuple containing the five components of
92+
# the version number: major, minor, micro, releaselevel, and serial. All
93+
# values except releaselevel are integers; the release level is 'alpha',
94+
# 'beta', 'candidate', or 'final'. The version_info value corresponding to the
95+
# Python version 2.0 is (2, 0, 0, 'final', 0)." Additionally we use a
96+
# releaselevel of 'dev' for unreleased under-development code.
97+
#
98+
# If the releaselevel is 'alpha' then the major/minor/micro components are not
99+
# established at this point, and setup.py will use a version of next-$(revno).
100+
# If the releaselevel is 'final', then the tarball will be major.minor.micro.
101+
# Otherwise it is major.minor.micro~$(revno).
102+
103+
try:
104+
from ._version import __version__, version
105+
except ModuleNotFoundError:
106+
# package is not installed
107+
if version := __get_git_version():
108+
# we're in a git repo
109+
__version__ = tuple([int(v) if v.isdigit() else v for v in version.split(".")])
110+
else:
111+
# we're working with a tarball or similar
112+
version = "0.0.0"
113+
__version__ = (0, 0, 0)

0 commit comments

Comments
 (0)