Skip to content

Commit

Permalink
Make VERSION_SOURCE_PATTERN public
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Mar 15, 2022
1 parent 1a90b9b commit 9545cc7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Explicitly specified `Optional[...]` typing on arguments with a default of `None`.
([Contributed by jonathangreen](https://github.com/mtkennerly/dunamai/pull/44))
* Made `VERSION_SOURCE_PATTERN` public for consumption by other tools.

## v1.10.0 (2022-03-08)

Expand Down
31 changes: 21 additions & 10 deletions dunamai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@
)
from xml.etree import ElementTree

_VERSION_PATTERN = r"""
VERSION_SOURCE_PATTERN = r"""
(?x) (?# ignore whitespace)
^v((?P<epoch>\d+)!)?(?P<base>\d+(\.\d+)*) (?# v1.2.3 or v1!2000.1.2)
([-._]?((?P<stage>[a-zA-Z]+)[-._]?(?P<revision>\d+)?))? (?# b0)
(\+(?P<tagged_metadata>.+))?$ (?# +linux)
""".strip()

# Preserve old/private name for now in case it exists in the wild
_VERSION_PATTERN = VERSION_SOURCE_PATTERN

_VALID_PEP440 = r"""
(?x)
^(\d+!)?
Expand Down Expand Up @@ -597,7 +600,7 @@ def serialize(
return out

@classmethod
def parse(cls, version: str, pattern: str = _VERSION_PATTERN) -> "Version":
def parse(cls, version: str, pattern: str = VERSION_SOURCE_PATTERN) -> "Version":
"""
Attempt to parse a string into a Version instance.
Expand Down Expand Up @@ -694,7 +697,7 @@ def bump(self, index: int = -1) -> "Version":
return bumped

@classmethod
def from_git(cls, pattern: str = _VERSION_PATTERN, latest_tag: bool = False) -> "Version":
def from_git(cls, pattern: str = VERSION_SOURCE_PATTERN, latest_tag: bool = False) -> "Version":
r"""
Determine a version based on Git tags.
Expand Down Expand Up @@ -783,7 +786,9 @@ def from_git(cls, pattern: str = _VERSION_PATTERN, latest_tag: bool = False) ->
return version

@classmethod
def from_mercurial(cls, pattern: str = _VERSION_PATTERN, latest_tag: bool = False) -> "Version":
def from_mercurial(
cls, pattern: str = VERSION_SOURCE_PATTERN, latest_tag: bool = False
) -> "Version":
r"""
Determine a version based on Mercurial tags.
Expand Down Expand Up @@ -851,7 +856,9 @@ def from_mercurial(cls, pattern: str = _VERSION_PATTERN, latest_tag: bool = Fals
return version

@classmethod
def from_darcs(cls, pattern: str = _VERSION_PATTERN, latest_tag: bool = False) -> "Version":
def from_darcs(
cls, pattern: str = VERSION_SOURCE_PATTERN, latest_tag: bool = False
) -> "Version":
r"""
Determine a version based on Darcs tags.
Expand Down Expand Up @@ -908,7 +915,7 @@ def from_darcs(cls, pattern: str = _VERSION_PATTERN, latest_tag: bool = False) -

@classmethod
def from_subversion(
cls, pattern: str = _VERSION_PATTERN, latest_tag: bool = False, tag_dir: str = "tags"
cls, pattern: str = VERSION_SOURCE_PATTERN, latest_tag: bool = False, tag_dir: str = "tags"
) -> "Version":
r"""
Determine a version based on Subversion tags.
Expand Down Expand Up @@ -985,7 +992,9 @@ def from_subversion(
return version

@classmethod
def from_bazaar(cls, pattern: str = _VERSION_PATTERN, latest_tag: bool = False) -> "Version":
def from_bazaar(
cls, pattern: str = VERSION_SOURCE_PATTERN, latest_tag: bool = False
) -> "Version":
r"""
Determine a version based on Bazaar tags.
Expand Down Expand Up @@ -1059,7 +1068,9 @@ def from_bazaar(cls, pattern: str = _VERSION_PATTERN, latest_tag: bool = False)
return version

@classmethod
def from_fossil(cls, pattern: str = _VERSION_PATTERN, latest_tag: bool = False) -> "Version":
def from_fossil(
cls, pattern: str = VERSION_SOURCE_PATTERN, latest_tag: bool = False
) -> "Version":
r"""
Determine a version based on Fossil tags.
Expand Down Expand Up @@ -1162,7 +1173,7 @@ def from_fossil(cls, pattern: str = _VERSION_PATTERN, latest_tag: bool = False)

@classmethod
def from_any_vcs(
cls, pattern: str = _VERSION_PATTERN, latest_tag: bool = False, tag_dir: str = "tags"
cls, pattern: str = VERSION_SOURCE_PATTERN, latest_tag: bool = False, tag_dir: str = "tags"
) -> "Version":
r"""
Determine a version based on a detected version control system.
Expand All @@ -1188,7 +1199,7 @@ def from_any_vcs(
def from_vcs(
cls,
vcs: Vcs,
pattern: str = _VERSION_PATTERN,
pattern: str = VERSION_SOURCE_PATTERN,
latest_tag: bool = False,
tag_dir: str = "tags",
) -> "Version":
Expand Down
4 changes: 2 additions & 2 deletions dunamai/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
from typing import Mapping, Optional

from dunamai import check_version, Version, Style, Vcs, _VERSION_PATTERN
from dunamai import check_version, Version, Style, Vcs, VERSION_SOURCE_PATTERN


common_sub_args = [
Expand Down Expand Up @@ -34,7 +34,7 @@
},
{
"triggers": ["--pattern"],
"default": _VERSION_PATTERN,
"default": VERSION_SOURCE_PATTERN,
"help": (
"Regular expression matched against the version source."
" This must contain one capture group named `base` corresponding to"
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_dunamai.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Style,
Vcs,
_run_cmd,
_VERSION_PATTERN,
VERSION_SOURCE_PATTERN,
)


Expand Down Expand Up @@ -720,7 +720,7 @@ def check_re(
tagged_metadata: Optional[str] = None,
epoch: Optional[str] = None,
) -> None:
result = re.search(_VERSION_PATTERN, tag)
result = re.search(VERSION_SOURCE_PATTERN, tag)
if result is None:
if any(x is not None for x in [base, stage, revision]):
raise ValueError("Pattern did not match, {tag}".format(tag=tag))
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import pytest

from dunamai import _run_cmd
from dunamai.__main__ import parse_args, _VERSION_PATTERN
from dunamai.__main__ import parse_args, VERSION_SOURCE_PATTERN


def test__parse_args__from():
assert parse_args(["from", "any"]) == Namespace(
command="from",
vcs="any",
pattern=_VERSION_PATTERN,
pattern=VERSION_SOURCE_PATTERN,
dirty=False,
metadata=None,
format=None,
Expand Down

0 comments on commit 9545cc7

Please sign in to comment.