Skip to content

Commit 34e57fa

Browse files
committed
Use local git version when _version file does not exist
1 parent f4e1ace commit 34e57fa

File tree

3 files changed

+57
-18
lines changed

3 files changed

+57
-18
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PYTHONPATH:=$(shell pwd):${PYTHONPATH}
2-
PYTHON ?= python
2+
PYTHON ?= python3
33

44
all: check
55

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ source = "vcs"
4040

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

4450
[tool.hatch.build.targets.sdist]
4551
include = [

testscenarios/__init__.py

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,6 @@
2727
methods for details.
2828
"""
2929

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

0 commit comments

Comments
 (0)