Skip to content

Commit

Permalink
Merge pull request #201 from epics-containers/semver-compare
Browse files Browse the repository at this point in the history
Add semver comparrison
  • Loading branch information
gilesknap authored Apr 3, 2024
2 parents d3ac14b + 9026476 commit 4505f85
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ dependencies = [
"ruamel.yaml",
"jinja2",
"GitPython",
"pvi~=0.8", # pvi currently tracks breaking changes with minor version
"semantic-version",
"pvi~=0.8", # pvi currently tracks breaking changes with minor version
] # Add project dependencies here, e.g. ["click", "numpy"]
dynamic = ["version"]
license.file = "LICENSE"
Expand Down
19 changes: 19 additions & 0 deletions src/ibek/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from ruamel.yaml import YAML

from ibek._version import __version__
from ibek.commands import semver_compare
from ibek.dev_cmds.commands import dev_cli
from ibek.globals import NaturalOrderGroup
from ibek.ioc_cmds.commands import ioc_cli
Expand Down Expand Up @@ -59,6 +60,24 @@ def main(
"""


@cli.command()
def compare(
base: str = typer.Argument(
help='SemVer string e.g. "1.2.0"',
),
target: str = typer.Argument(
help='An operator (<=,>=,==,<,>) followed by a SemVer string e.g.">=1.2.0"',
),
):
"""
Compare two SemVer strings similarly to pip's requirements specifier syntax
"""
if semver_compare(base, target):
raise typer.Exit(code=0)
else:
raise typer.Exit(code=1)


# test with:
# pipenv run python -m ibek
if __name__ == "__main__":
Expand Down
26 changes: 26 additions & 0 deletions src/ibek/commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import operator

from semantic_version import Version


def semver_compare(base: str, target: str) -> bool:
"""
Compare two semVer strings similarly to pip's requirements specifier syntax
"""
ops = {
">=": operator.ge,
"<=": operator.le,
"==": operator.eq,
">": operator.gt,
"<": operator.lt,
}
for op in ops.keys():
if op in target:
in_op = op
target = target.strip(op)
break

if ops[in_op](Version.coerce(base), Version.coerce(target)):
return True
else:
return False
10 changes: 10 additions & 0 deletions tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Some unit tests for ibek.
"""

from ibek.commands import semver_compare
from ibek.ioc import (
clear_entity_model_ids,
id_to_entity,
Expand Down Expand Up @@ -53,3 +54,12 @@ def test_object_references():
assert device.type == "mymodule.device"
assert device.port is port
assert id_to_entity == {"PORT": port}


def test_compare():
"""
Verify the SemVer comparrisons work
"""
assert semver_compare("1.1.1", "==1.1.1")
assert semver_compare("1.1.1", ">=1.1.0")
assert not semver_compare("1.1.1", ">=1.1.2")

0 comments on commit 4505f85

Please sign in to comment.