Completed
Pull Request — master (#2988)
by Edward
07:10
created

complex_semver_match()   B

Complexity

Conditions 5

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
dl 0
loc 24
rs 8.1671
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
"""
17
Module containing various versioning utils.
18
"""
19
20
import semver
21
22
from st2common import __version__ as stackstorm_version
23
24
__all__ = [
25
    'get_stackstorm_version',
26
27
    'complex_semver_match'
28
]
29
30
31
def get_stackstorm_version():
32
    """
33
    Return a valid semver version string for the currently running StackStorm version.
34
    """
35
    # Special handling for dev versions which are not valid semver identifiers
36
    if 'dev' in stackstorm_version and stackstorm_version.count('.') == 1:
37
        version = stackstorm_version.replace('dev', '.0')
38
        return version
39
40
    return stackstorm_version
41
42
43
def complex_semver_match(version, version_specifier):
44
    """
45
    Custom semver match function which also supports complex semver specifiers
46
    such as >=1.6, <2.0, etc.
47
48
    :rtype: ``bool``
49
    """
50
    split_version_specifier = version_specifier.split(',')
51
52
    if len(split_version_specifier) == 1:
53
        # No comma, we can do a simple comparision
54
        return semver.match(version, version_specifier)
55
    else:
56
        # Compare part by part
57
        for version_specifier_part in split_version_specifier:
58
            version_specifier_part = version_specifier_part.strip()
59
60
            if not version_specifier_part:
61
                continue
62
63
            if not semver.match(version, version_specifier_part):
64
                return False
65
66
        return True
67