Skip to content

Commit

Permalink
Merge pull request #71 from jeronimoalbi/develop
Browse files Browse the repository at this point in the history
Version update to `1.0.2`
  • Loading branch information
jeronimoalbi authored Mar 28, 2017
2 parents 1017259 + d25b172 commit 911e857
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [1.0.2] - 2017-03-28
### Added
- Version wildcards support for single '*' to match all.

### Changed
- The wildcard ('*') in the last version part now matches any character.

## [1.0.1] - 2017-03-07
### Added
- Transport merge support for run-time calls.
Expand Down
2 changes: 1 addition & 1 deletion katana/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@

__license__ = "MIT"
__copyright__ = "Copyright (c) 2016-2017 KUSANAGI S.L. (http://kusanagi.io)"
__version__ = '1.0.1'
__version__ = '1.0.2'
28 changes: 22 additions & 6 deletions katana/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@
__license__ = "MIT"
__copyright__ = "Copyright (c) 2016-2017 KUSANAGI S.L. (http://kusanagi.io)"

# Regexp to remove duplicated '*' in versions
DUPLICATES = re.compile(r'(\*)\1+')

# Regexp to check version pattern for invalid chars
INVALID_PATTERN = re.compile(r'[^a-zA-Z0-9*.,_-]')

# Regexp to remove duplicated '*' from version
WILDCARDS = re.compile(r'\*+')

# Regexp to match version dot separators
VERSION_DOTS = re.compile(r'([^*])\.')

# Regexp to match all wildcards except the last one
VERSION_WILDCARDS = re.compile(r'\*+([^$])')


class InvalidVersionPattern(KatanaError):
"""Exception raised when a version pattern is not valid."""
Expand Down Expand Up @@ -53,12 +59,22 @@ def __init__(self, version):
if INVALID_PATTERN.search(version):
raise InvalidVersionPattern(version)

# Remove duplicated special chars from version
self.__version = DUPLICATES.sub(r'\1', version)
# Remove duplicated wildcards from version
self.__version = WILDCARDS.sub('*', version)

if '*' in self.__version:
# Create an expression for version pattern comparisons
expr = VERSION_WILDCARDS.sub(r'[^*.]+\1', self.version)
# Escape dots to work with the regular expression
expr = VERSION_DOTS.sub(r'\1\.', expr)

# If there is a final wildcard left replace it with an
# expression to match any characters after the last dot.
if expr[-1] == '*':
expr = expr[:-1] + '.*'

# Create a pattern to be use for cmparison
self.__pattern = re.compile(re.sub(r'\*+', '[^*.]+', self.version))
self.__pattern = re.compile(expr)
else:
self.__pattern = None

Expand Down
6 changes: 4 additions & 2 deletions tests/test_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_wildcards_version_match():
assert version_string.match(version)

# Version formats don't match
for version in ('A.B.C', '2.2.3', '1.2.3.alpha'):
for version in ('A.B.C', '2.2.3'):
assert not version_string.match(version)


Expand Down Expand Up @@ -112,6 +112,8 @@ def test_resolve_versions():

# Format: pattern, expected, versions
cases = (
('*', '3.4.1', ('3.4.0', '3.4.1', '3.4.a')),
('3.*', '3.4.1', ('3.4.0', '3.4.1', '3.4.a')),
('3.4.1', '3.4.1', ('3.4.0', '3.4.1', '3.4.a')),
('3.4.*', '3.4.1', ('3.4.0', '3.4.1', '3.4.a')),
('3.4.*', '3.4.1', ('3.4.a', '3.4.1', '3.4.0')),
Expand All @@ -132,7 +134,7 @@ def test_resolve_versions():

# Check for a non maching pattern
with pytest.raises(VersionNotFound):
VersionString('*.*.*').resolve(['1.0', 'A.B.C.D'])
VersionString('3.4.*.*').resolve(['1.0', 'A.B.C.D', '3.4.1'])


def test_invalid_pattern():
Expand Down

0 comments on commit 911e857

Please sign in to comment.