def satisfying(list_versions, versionexpr, result): """ returns the maximum version that satisfies the expression if some version cannot be converted to loose SemVer, it is discarded with a msg This provides some workaround for failing comparisons like "2.1" not matching "<=2.1" """ from semver import SemVer, Range, max_satisfying version_range, loose, include_prerelease = _parse_versionexpr(versionexpr, result) # Check version range expression try: act_range = Range(version_range, loose) except ValueError: raise ConanException("version range expression '%s' is not valid" % version_range) # Validate all versions candidates = {} for v in list_versions: try: ver = SemVer(v, loose=loose) candidates[ver] = v except (ValueError, AttributeError): result.append("WARN: Version '%s' is not semver, cannot be compared with a range" % str(v)) # Search best matching version in range result = max_satisfying(candidates, act_range, loose=loose, include_prerelease=include_prerelease) return candidates.get(result)
def get_version(build=0, version_format=None, dot=False): version = get_tag_version() # Get the commit hash of the version v_hash = subprocess.Popen(['git', 'rev-list', '-n', '1', version], stdout=subprocess.PIPE, stderr=DEVNULL, cwd='.').stdout.read().decode('utf-8').rstrip() # Get the current commit hash c_hash = subprocess.Popen(['git', 'rev-parse', 'HEAD'], stdout=subprocess.PIPE, stderr=DEVNULL, cwd='.').stdout.read().decode('utf-8').rstrip() # If the version commit hash and current commit hash # do not match return the branch name else return the version if v_hash != c_hash: logger.debug("v_hash and c_hash do not match!") branch = subprocess.Popen( ['git', 'rev-parse', '--abbrev-ref', 'HEAD'], stdout=subprocess.PIPE, stderr=DEVNULL, cwd='.').stdout.read().decode('utf-8').rstrip() semver = SemVer() semver.merged_branch = branch logger.debug("merged branch is: {}".format(semver.merged_branch)) version_type = semver.get_version_type() logger.debug("version type is: {}".format(version_type)) if version_type: next_version = bump_version(get_tag_version(), version_type, False, False) if version_format in ('npm', 'docker'): return "{}-{}.{}".format(next_version, re.sub(r'[/_]', '-', branch), build) if version_format == 'maven': qualifier = 'SNAPSHOT' if build == 0 else build return "{}-{}-{}".format(next_version, re.sub(r'[/_]', '-', branch), qualifier) if dot: branch = branch.replace('/', '.') return branch return version
def satisfying(list_versions, versionexpr, output): """ returns the maximum version that satisfies the expression if some version cannot be converted to loose SemVer, it is discarded with a msg This provides some woraround for failing comparisons like "2.1" not matching "<=2.1" """ from semver import SemVer, max_satisfying version_range = versionexpr.replace(",", " ") candidates = {} for v in list_versions: try: ver = SemVer(v, loose=True) if not ver.prerelease: # Hack to allow version "2.1" match expr "<=2.1" ver.prerelease = [0] candidates[ver] = v except (ValueError, AttributeError): output.warn("Version '%s' is not semver, cannot be compared with a range" % str(v)) result = max_satisfying(candidates, version_range, loose=True) return candidates.get(result)
class Version(object): _semver = None loose = True # Allow incomplete version strings like '1.2' or '1-dev0' def __init__(self, value): v = str(value).strip() try: self._semver = SemVer(v, loose=self.loose) except ValueError: raise ConanException("Invalid version '{}'".format(value)) def __str__(self): return str(self._semver) @property def major(self): return str(self._semver.major) @property def minor(self): return str(self._semver.minor) @property def patch(self): return str(self._semver.patch) @property def prerelease(self): return str(".".join(map(str, self._semver.prerelease))) @property def build(self): return str(".".join(map(str, self._semver.build))) def __eq__(self, other): if not isinstance(other, Version): other = Version(other) return self._semver.compare(other._semver) == 0 def __lt__(self, other): if not isinstance(other, Version): other = Version(other) return self._semver.compare(other._semver) < 0
from semver import SemVer SemVer("1.a.1", loose=True)
def __init__(self, value): v = str(value).strip() try: self._semver = SemVer(v, loose=self.loose) except ValueError: raise ConanException("Invalid version '{}'".format(value))