Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pattern match on base version in Version constructor #41

Merged
merged 5 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions dunamai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,77 @@ def serialize(
check_version(out, style)
return out

@classmethod
def parse(cls, version: Union[str, "Version"], pattern: str = _VERSION_PATTERN) -> "Version":
"""
:param version: Full version, such as 0.3.0a3+d7.gb6a9020.dirty.
:param pattern: Regular expression matched against the version.
Refer to `from_any_vcs` for more info.
"""
if isinstance(version, Version):
base = version.base
else:
base = version

try:
v_base = base if base.startswith("v") else "v" + base
matched_pattern = _match_version_pattern(pattern, [v_base], True)
except ValueError:
if isinstance(version, Version):
return version
return cls(version)

base = matched_pattern.base
tagged_metadata = matched_pattern.tagged_metadata
if isinstance(version, Version):
if version.stage is None:
stage = matched_pattern.stage_revision
else:
stage = (version.stage, version.revision)
distance = version.distance
commit = version.commit
dirty = version.dirty
if version.tagged_metadata is not None:
if tagged_metadata is None:
tagged_metadata = version.tagged_metadata
else:
tagged_metadata = "{0}.{1}".format(tagged_metadata, version.tagged_metadata)
epoch = matched_pattern.epoch if version.epoch is None else version.epoch
else:
stage = matched_pattern.stage_revision
distance = 0
commit = None
dirty = None
epoch = matched_pattern.epoch

if tagged_metadata:
if "dirty" in tagged_metadata.split("."):
dirty = True
tagged_metadata = ".".join(e for e in tagged_metadata.split(".") if e != "dirty")
if distance == 0 and tagged_metadata:
for m in tagged_metadata.split("."):
match = re.match(r"d(?P<dis>\d+)", m)
if match:
distance = int(match.group("dis"))
tagged_metadata = ".".join(e for e in tagged_metadata.split(".") if e != m)
if commit is None and tagged_metadata:
for m in tagged_metadata.split("."):
match = re.match(r"g(?P<commit>[\da-z]+)", m)
if match:
commit = match.group("commit")
tagged_metadata = ".".join(e for e in tagged_metadata.split(".") if e != m)
if tagged_metadata is not None and tagged_metadata.strip() == "":
tagged_metadata = None
return cls(
base,
stage=stage,
distance=distance,
commit=commit,
dirty=dirty,
tagged_metadata=tagged_metadata,
epoch=epoch,
)

@classmethod
def from_git(cls, pattern: str = _VERSION_PATTERN, latest_tag: bool = False) -> "Version":
r"""
Expand Down
27 changes: 26 additions & 1 deletion tests/unit/test_dunamai.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
import pkg_resources
import pkg_resources # type: ignore
import re
from contextlib import contextmanager
from pathlib import Path
Expand Down Expand Up @@ -652,3 +652,28 @@ def test__bump_version():

with pytest.raises(ValueError):
bump_version("foo", 0)


def test__parse():
assert Version.parse("1.2.3") == Version("1.2.3")
assert Version.parse("1.2.3a") == Version("1.2.3", stage=("a", None))
assert Version.parse("1.2.3a3") == Version("1.2.3", stage=("a", 3))
assert Version.parse("1.2.3+d7") == Version("1.2.3", distance=7)
assert Version.parse("1.2.3+gb6a9020") == Version("1.2.3", commit="b6a9020")
assert Version.parse("1.2.3+dirty") == Version("1.2.3", dirty=True)
assert Version.parse("1.2.3a3+d7.gb6a9020.dirty") == Version(
"1.2.3", stage=("a", 3), distance=7, commit="b6a9020", dirty=True
)
assert Version.parse("1.2.3a3+d7.gb6a9020.dirty.linux") == Version(
"1.2.3", stage=("a", 3), distance=7, commit="b6a9020", dirty=True, tagged_metadata="linux"
)
assert Version.parse("2!1.2.3") == Version("1.2.3", epoch=2)
assert Version.parse("2!1.2.3a3+d7.gb6a9020.dirty.linux") == Version(
"1.2.3",
stage=("a", 3),
distance=7,
commit="b6a9020",
dirty=True,
tagged_metadata="linux",
epoch=2,
)