You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ariatosca.apache.org by AviaE <gi...@git.apache.org> on 2017/05/07 15:19:10 UTC

[GitHub] incubator-ariatosca pull request #100: ARIA-140 Version utils

Github user AviaE commented on a diff in the pull request:

    https://github.com/apache/incubator-ariatosca/pull/100#discussion_r115142886
  
    --- Diff: aria/utils/versions.py ---
    @@ -0,0 +1,162 @@
    +# Licensed to the Apache Software Foundation (ASF) under one or more
    +# contributor license agreements.  See the NOTICE file distributed with
    +# this work for additional information regarding copyright ownership.
    +# The ASF licenses this file to You under the Apache License, Version 2.0
    +# (the "License"); you may not use this file except in compliance with
    +# the License.  You may obtain a copy of the License at
    +#
    +#     http://www.apache.org/licenses/LICENSE-2.0
    +#
    +# Unless required by applicable law or agreed to in writing, software
    +# distributed under the License is distributed on an "AS IS" BASIS,
    +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    +# See the License for the specific language governing permissions and
    +# limitations under the License.
    +
    +"""
    +General-purpose version string handling
    +"""
    +
    +import re
    +
    +
    +_INF = float('inf')
    +
    +_NULL = (), _INF
    +
    +_DIGITS_RE = re.compile(r'^\d+$')
    +
    +_PREFIXES = {
    +    'dev':   0.0001,
    +    'alpha': 0.001,
    +    'beta':  0.01,
    +    'rc':    0.1
    +}
    +
    +
    +class VersionString(unicode):
    +    """
    +    Version string that can be compared, sorted, made unique in a set, and used as a unique dict
    +    key.
    +
    +    The primary part of the string is one or more dot-separated natural numbers. Trailing zeroes
    +    are treated as redundant, e.g. "1.0.0" == "1.0" == "1".
    +
    +    An optional qualifier can be added after a "-". The qualifier can be a natural number or a
    +    specially treated prefixed natural number, e.g. "1.1-beta1" > "1.1-alpha2". The case of the
    +    prefix is ignored.
    +
    +    Numeric qualifiers will always be greater than prefixed integer qualifiers, e.g. "1.1-1" >
    +    "1.1-beta1".
    +
    +    Versions without a qualifier will always be greater than their equivalents with a qualifier,
    +    e.g. e.g. "1.1" > "1.1-1".
    +
    +    Any value that does not conform to this format will be treated as a zero version, which would
    +    be lesser than any non-zero version.
    +
    +    For efficient list sorts use the ``key`` property, e.g.:
    +    ``sorted(versions, key=lambda x: x.key)``
    +    """
    +
    +    NULL = None # initialized below
    +
    +    def __init__(self, value=None):
    +        if value is not None:
    +            super(VersionString, self).__init__(value)
    +        self.key = parse_version_string(self)
    +
    +    def __eq__(self, version):
    +        if not isinstance(version, VersionString):
    +            version = VersionString(version)
    +        return self.key == version.key
    +
    +    def __lt__(self, version):
    +        if not isinstance(version, VersionString):
    +            version = VersionString(version)
    +        return self.key < version.key
    +
    +    def __hash__(self):
    +        return self.key.__hash__()
    +
    +
    +def parse_version_string(version): # pylint: disable=too-many-branches
    +    """
    +    Parses a version string.
    +
    +    :param version: The version string
    +    :returns: The primary tuple and qualifier float
    +    :rtype: ((int), float)
    +    """
    +
    +    if version is None:
    +        return _NULL
    +    version = unicode(version)
    +
    +    # Split to primary and qualifier on '-'
    +    split = version.split('-', 2)
    +    if len(split) == 2:
    +        primary, qualifier = split
    +    else:
    --- End diff --
    
    The `2` in `split` means you include the part to the right of the second separator as the third element in the result.
    Which means that if you get passed `1.2-3-beta4`, you will treat it as a valid version string with the value `1.2`.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---