Skip to content

Commit

Permalink
Merge pull request #4 from chrisburr/extract-version-funcs
Browse files Browse the repository at this point in the history
Expose version parsing function
  • Loading branch information
chrisburr authored Sep 14, 2020
2 parents da78cf2 + ff74571 commit ef06220
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/diraccfg/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import absolute_import

__all__ = [
'CFG',
"CFG",
"parseVersion",
]

from .cfg import CFG
from .versions import parseVersion
22 changes: 11 additions & 11 deletions src/diraccfg/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import json
import os
import sys
import re

from .cfg import CFG
from .versions import parseVersion


def parseArgs():
Expand Down Expand Up @@ -48,17 +48,17 @@ def sortVersions(allow_pre_releases=False):

parsedVersions = {}
for obj in objs:
match = re.match(r"^v(?P<major>\d+)r(?P<minor>\d+)(?:p(?P<patch>\d+))?(?:-pre(?P<pre>\d+))?$", obj)
if match:
v = match.groupdict()
if not allow_pre_releases and v['pre']:
try:
major, minor, patch, pre = parseVersion(obj)
except ValueError:
if obj not in ('integration', 'devel', 'master'):
sys.stderr.write('WARN: Unexpected version string %r\n' % obj)
else:
if pre is None:
pre = sys.maxsize
elif not allow_pre_releases:
continue
if v['pre'] is None:
v['pre'] = sys.maxsize
v = {k: 0 if v is None else int(v) for k, v in v.items()}
parsedVersions[obj] = (v['major'], v['minor'], v['patch'], v['pre'])
elif obj not in ('integration', 'devel', 'master'):
sys.stderr.write('WARN: Unexpected version string %r\n' % obj)
parsedVersions[obj] = (major, minor, patch, pre)

print(json.dumps(sorted(parsedVersions, key=parsedVersions.get, reverse=True)))

Expand Down
30 changes: 30 additions & 0 deletions src/diraccfg/versions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import re


def parseVersion(versionString):
"""Parse a DIRAC-style version sting
:param versionString: Version identifier to parse
:returns: `tuple` of 4 values (major, minor, patch, pre). All values will be
`int` except "pre" which is `None` for released versions.
:raises: ValueError if the versionString is invalid
"""
match = re.match(
r"^v(?P<major>\d+)r(?P<minor>\d+)(?:p(?P<patch>\d+))?(?:-pre(?P<pre>\d+))?$",
versionString,
)
if not match:
raise ValueError("%s is not a valid version" % versionString)

segments = match.groupdict()
for k, v in segments.items():
if k != "pre" and v is None:
segments[k] = 0
if v is not None:
segments[k] = int(v)

return (segments["major"], segments["minor"], segments["patch"], segments["pre"])
68 changes: 68 additions & 0 deletions tests/test_versions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import pytest

import diraccfg


@pytest.mark.parametrize(
"string,expected",
[
("v7r0p30", (7, 0, 30, None)),
("v7r1", (7, 1, 0, None)),
("v7r1-pre1", (7, 1, 0, 1)),
("v7r1-pre9", (7, 1, 0, 9)),
("v7r1p10", (7, 1, 10, None)),
("v7r1p9", (7, 1, 9, None)),
("v7r2-pre10", (7, 2, 0, 10)),
("v7r2-pre9", (7, 2, 0, 9)),
("v7r2p8-pre9", (7, 2, 8, 9)),
("v10r0p30", (10, 0, 30, None)),
("v10r1", (10, 1, 0, None)),
("v10r1-pre1", (10, 1, 0, 1)),
("v10r1-pre9", (10, 1, 0, 9)),
("v10r1p10", (10, 1, 10, None)),
("v10r1p9", (10, 1, 9, None)),
("v10r2-pre10", (10, 2, 0, 10)),
("v10r2-pre9", (10, 2, 0, 9)),
("v10r2p8-pre9", (10, 2, 8, 9)),
("v100r0p30", (100, 0, 30, None)),
("v100r1", (100, 1, 0, None)),
("v100r1-pre1", (100, 1, 0, 1)),
("v100r1-pre9", (100, 1, 0, 9)),
("v100r1p10", (100, 1, 10, None)),
("v100r1p9", (100, 1, 9, None)),
("v100r2-pre10", (100, 2, 0, 10)),
("v100r2-pre9", (100, 2, 0, 9)),
("v100r2p8-pre9", (100, 2, 8, 9)),
],
)
def test_parseValid(string, expected):
assert expected == diraccfg.parseVersion(string)


@pytest.mark.parametrize(
"string",
[
"v7r0p30-pre30-pre30",
"master",
"dev",
"integration",
"7r0p30",
"v7r2p30p2",
"1.0.0",
"v7p30",
"v7p30-pre2",
"v10p30",
"v10p30-pre2",
"v100p30",
"v100p30-pre2",
],
)
def test_parseInvalid(string):
with pytest.raises(ValueError):
diraccfg.parseVersion(string)

0 comments on commit ef06220

Please sign in to comment.