Blame SOURCES/pyproject_convert.py

7ef706
# Copyright 2019 Gordon Messmer <gordon.messmer@gmail.com>
7ef706
#
7ef706
# Upstream: https://github.com/gordonmessmer/pyreq2rpm
7ef706
#
7ef706
# Permission is hereby granted, free of charge, to any person
7ef706
# obtaining a copy of this software and associated documentation files
7ef706
# (the "Software"), to deal in the Software without restriction,
7ef706
# including without limitation the rights to use, copy, modify, merge,
7ef706
# publish, distribute, sublicense, and/or sell copies of the Software,
7ef706
# and to permit persons to whom the Software is furnished to do so,
7ef706
# subject to the following conditions:
7ef706
#
7ef706
# The above copyright notice and this permission notice shall be
7ef706
# included in all copies or substantial portions of the Software.
7ef706
#
7ef706
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
7ef706
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
7ef706
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
7ef706
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
7ef706
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
7ef706
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
7ef706
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
7ef706
# SOFTWARE.
7ef706
7ef706
from packaging.requirements import Requirement
7ef706
from packaging.version import parse as parse_version
7ef706
7ef706
class RpmVersion():
7ef706
    def __init__(self, version_id):
7ef706
        version = parse_version(version_id)
7ef706
        if isinstance(version._version, str):
7ef706
            self.version = version._version
7ef706
        else:
7ef706
            self.epoch = version._version.epoch
7ef706
            self.version = list(version._version.release)
7ef706
            self.pre = version._version.pre
7ef706
            self.dev = version._version.dev
7ef706
            self.post = version._version.post
7ef706
            # version.local is ignored as it is not expected to appear
7ef706
            # in public releases
7ef706
            # https://www.python.org/dev/peps/pep-0440/#local-version-identifiers
7ef706
7ef706
    def is_legacy(self):
7ef706
        return isinstance(self.version, str)
7ef706
7ef706
    def increment(self):
7ef706
        self.version[-1] += 1
7ef706
        self.pre = None
7ef706
        self.dev = None
7ef706
        self.post = None
7ef706
        return self
7ef706
7ef706
    def __str__(self):
7ef706
        if self.is_legacy():
7ef706
            return self.version
7ef706
        if self.epoch:
7ef706
            rpm_epoch = str(self.epoch) + ':'
7ef706
        else:
7ef706
            rpm_epoch = ''
7ef706
        while len(self.version) > 1 and self.version[-1] == 0:
7ef706
            self.version.pop()
7ef706
        rpm_version = '.'.join(str(x) for x in self.version)
7ef706
        if self.pre:
7ef706
            rpm_suffix = '~{}'.format(''.join(str(x) for x in self.pre))
7ef706
        elif self.dev:
7ef706
            rpm_suffix = '~~{}'.format(''.join(str(x) for x in self.dev))
7ef706
        elif self.post:
7ef706
            rpm_suffix = '^post{}'.format(self.post[1])
7ef706
        else:
7ef706
            rpm_suffix = ''
7ef706
        return '{}{}{}'.format(rpm_epoch, rpm_version, rpm_suffix)
7ef706
7ef706
def convert_compatible(name, operator, version_id):
7ef706
    if version_id.endswith('.*'):
7ef706
        return 'Invalid version'
7ef706
    version = RpmVersion(version_id)
7ef706
    if version.is_legacy():
7ef706
        # LegacyVersions are not supported in this context
7ef706
        return 'Invalid version'
7ef706
    if len(version.version) == 1:
7ef706
        return 'Invalid version'
7ef706
    upper_version = RpmVersion(version_id)
7ef706
    upper_version.version.pop()
7ef706
    upper_version.increment()
7ef706
    return '({} >= {} with {} < {})'.format(
7ef706
        name, version, name, upper_version)
7ef706
7ef706
def convert_equal(name, operator, version_id):
7ef706
    if version_id.endswith('.*'):
7ef706
        version_id = version_id[:-2] + '.0'
7ef706
        return convert_compatible(name, '~=', version_id)
7ef706
    version = RpmVersion(version_id)
7ef706
    return '{} = {}'.format(name, version)
7ef706
7ef706
def convert_arbitrary_equal(name, operator, version_id):
7ef706
    if version_id.endswith('.*'):
7ef706
        return 'Invalid version'
7ef706
    version = RpmVersion(version_id)
7ef706
    return '{} = {}'.format(name, version)
7ef706
7ef706
def convert_not_equal(name, operator, version_id):
7ef706
    if version_id.endswith('.*'):
7ef706
        version_id = version_id[:-2]
7ef706
        version = RpmVersion(version_id)
7ef706
        if version.is_legacy():
7ef706
            # LegacyVersions are not supported in this context
7ef706
            return 'Invalid version'
7ef706
        version_gt = RpmVersion(version_id).increment()
7ef706
        version_gt_operator = '>='
7ef706
        # Prevent dev and pre-releases from satisfying a < requirement
7ef706
        version = '{}~~'.format(version)
7ef706
    else:
7ef706
        version = RpmVersion(version_id)
7ef706
        version_gt = version
7ef706
        version_gt_operator = '>'
7ef706
    return '({} < {} or {} {} {})'.format(
7ef706
        name, version, name, version_gt_operator, version_gt)
7ef706
7ef706
def convert_ordered(name, operator, version_id):
7ef706
    if version_id.endswith('.*'):
7ef706
        # PEP 440 does not define semantics for prefix matching
7ef706
        # with ordered comparisons
7ef706
        # see: https://github.com/pypa/packaging/issues/320
7ef706
        # and: https://github.com/pypa/packaging/issues/321
7ef706
        # This style of specifier is officially "unsupported",
7ef706
        # even though it is processed.  Support may be removed
7ef706
        # in version 21.0.
7ef706
        version_id = version_id[:-2]
7ef706
        version = RpmVersion(version_id)
7ef706
        if operator == '>':
7ef706
            # distutils will allow a prefix match with '>'
7ef706
            operator = '>='
7ef706
        if operator == '<=':
7ef706
            # distutils will not allow a prefix match with '<='
7ef706
            operator = '<'
7ef706
    else:
7ef706
        version = RpmVersion(version_id)
7ef706
    # For backwards compatibility, fallback to previous behavior with LegacyVersions
7ef706
    if not version.is_legacy():
7ef706
        # Prevent dev and pre-releases from satisfying a < requirement
7ef706
        if operator == '<' and not version.pre and not version.dev and not version.post:
7ef706
            version = '{}~~'.format(version)
7ef706
        # Prevent post-releases from satisfying a > requirement
7ef706
        if operator == '>' and not version.pre and not version.dev and not version.post:
7ef706
            version = '{}.0'.format(version)
7ef706
    return '{} {} {}'.format(name, operator, version)
7ef706
7ef706
OPERATORS = {'~=': convert_compatible,
7ef706
             '==': convert_equal,
7ef706
             '===': convert_arbitrary_equal,
7ef706
             '!=': convert_not_equal,
7ef706
             '<=': convert_ordered,
7ef706
             '<':  convert_ordered,
7ef706
             '>=': convert_ordered,
7ef706
             '>':  convert_ordered}
7ef706
7ef706
def convert(name, operator, version_id):
7ef706
    return OPERATORS[operator](name, operator, version_id)
7ef706
7ef706
def convert_requirement(req):
7ef706
    parsed_req = Requirement.parse(req)
7ef706
    reqs = []
7ef706
    for spec in parsed_req.specs:
7ef706
        reqs.append(convert(parsed_req.project_name, spec[0], spec[1]))
7ef706
    if len(reqs) == 0:
7ef706
        return parsed_req.project_name
7ef706
    if len(reqs) == 1:
7ef706
        return reqs[0]
7ef706
    else:
7ef706
        reqs.sort()
7ef706
        return '({})'.format(' with '.join(reqs))