def version_compare(version1, version2, operator ): # type: (str, str, str) -> bool from poetry.semver.helpers import normalize_version if operator in _trans_op: operator = _trans_op[operator] elif operator in _trans_op.values(): pass else: raise ValueError('Invalid operator') version1 = parse(version1) version2 = parse(version2) try: version1 = parse(normalize_version(str(version1))) except ValueError: pass try: version2 = parse(normalize_version(str(version2))) except ValueError: pass return operator(version1, version2)
def package(self, name, version): name = name.lower() version = normalize_version(version) for package in self.packages: if name == package.name and package.version == version: return package
def _transform_version(self, version, pretty_version): # attempt to transform 2.1.1 to 2.1 # this allows you to upgrade through minor versions try: parts = normalize_version(version).split('.') except ValueError: return pretty_version # check to see if we have a semver-looking version if len(parts) == 4 and re.match('^0\D?', parts[3]): # remove the last parts (the patch version number and any extra) if parts[0] == '0': del parts[3] else: del parts[3] del parts[2] version = '.'.join(parts) else: return pretty_version return f'^{version}'
def get_package(name, version): return Package(name, normalize_version(version), version)
def test_normalize_fail(version): with pytest.raises(ValueError): normalize_version(version)
def test_normalize(version, expected): assert normalize_version(version) == expected