def semver_max_ver(*v): l = len(v) if l > 2: return semver.max_ver(v[0], semver_max_ver(*v[1:])) elif l == 2: return semver.max_ver(*v) else: return v[0]
def _get_latest_version_gcs(self): """ Retrieve the latest git tag version using gsutil ls on GCS. self: self object type: URIGenerator return latest version on GCS type: str """ if self.latest_version is not None: return self.latest_version prefix = self._get_prefix() # get versions from the specified prefix versions = list(self._get_versions_from_gcs()) if len(versions) == 0: if self.major_version: raise ValueError( "No matching versions for major version {} under the {} directory. It's possible that a _SUCCESS file is missing under these subdirectory." .format(self.major_version, prefix)) raise ValueError( "No valid versions under the {} directory. It's possible that a _SUCCESS file is missing for versions under these subdirectories." .format(prefix)) if len(versions) == 1: return versions[0] # retrieves the latest version and caches the value for any subsequent calls self.latest_version = functools.reduce( lambda x, y: semver.max_ver(x, y), versions) return self.latest_version
def main(): if len(sys.argv) != 3: print("Usage: python {} input_folder output_folder".format( sys.argv[0])) sys.exit(-1) input_folder = sys.argv[1] output_folder = sys.argv[2] if not os.path.exists(output_folder): os.makedirs(output_folder) latest_versions = [] for base, _, files in os.walk(input_folder): if len(files) != 0: latest_version = "0.0.0" for f in files: latest_version = semver.max_ver(latest_version, os.path.splitext(f)[0]) latest_versions.append(os.path.join(base, latest_version + ".json")) for f in latest_versions: new_name = os.path.split(os.path.dirname(f))[1] + ".json" output_file = os.path.join(output_folder, new_name) copyfile(f, output_file) print("{} => {}".format(f, output_file))
def get_latest_semver(versions): '''Scan all versions in list to find the latest one.''' latest = '0.0.0' for version in versions: logging.warning(f'{latest} {version}') latest = max_ver(latest, version) return latest
def get_latest_version(version_list: list) -> str: if not bool(version_list): return '1.0.0' list_len = len(version_list) if list_len == 1: return version_list[0] max_ver = version_list[0] for ver in version_list: max_ver = semver.max_ver(ver, max_ver) return max_ver
def current_version(): file = str(pathlib.Path() / 'messages.json') with open(file) as open_file: messages_index = json.load(open_file) current_version = '0.1.0' for version in messages_index: if version == 'install': continue current_version = semver.max_ver(current_version, version) return current_version
def __init__(self, builds: Set[ImageBuild]): self.builds = builds latest = {} self.latest = {} # Find latest versions for build in builds: major = semver.format_major_version(build.version) minor = semver.format_minor_version(build.version) version = build.get_formatted_version() latest[major] = semver.max_ver(latest.get(major, "0.0.0"), version) latest[minor] = semver.max_ver(latest.get(minor, "0.0.0"), version) # Rehash latest versions for l, version in latest.items(): if version not in self.latest: self.latest[version] = set() self.latest[version].add(l)
def get_release_version(cls, version_file, next_git_tag_version): file_version = cls.read_version_file(version_file) if not file_version: return next_git_tag_version finalized_file_version = semver.finalize_version(file_version) result = semver.max_ver(finalized_file_version, next_git_tag_version) logging.info( 'Release version is %s (max of git-version `%s` and ' 'file-version `%s`)', result, next_git_tag_version, finalized_file_version) return result
def get_tools_release(): """Retrieve the latest FreeDicttools release as a tuple with containing (version, date, downloadlink).""" if not 'FREEDICT_TOOLS' in os.environ or not shutil.which('git'): raise ReleaseError(("Unable to retrieve list of rleases of " "FreeDict tools. Either FREEDICT_TOOLS is unset or git not " "installed.")) releases = git(['tag']).split('\n') max_ver = '0.0.0' for tag in releases: max_ver = semver.max_ver(max_ver, tag) if max_ver == '0.0.0': raise ReleaseError("No tools releases found.") date = re.search('^([0-9]{4}-[0-9]{2}-[0-9]{2})', git(['show', '-s', '--pretty=format:%ci'])).groups()[0] return (max_ver, date, '{}/freedict-tools-{}.tar.xz'.format( RELEASE_HTTP_TOOL_BASE, max_ver))
def get_latest_semver_hashicorp(versions): '''Scan all versions in list to find the latest one for HashiCorp products.''' latest = '0.0.0' for version in versions: # Drop anything that's closed-source (Enterprise), alpha, beta, rc, # etc. if '+ent' not in version and '-alpha' not in version \ and '-beta' not in version and '-connect' not in version \ and '-oci' not in version and '-rc' not in version: logging.warning(f'{latest} {version}') latest = max_ver(latest, version) else: logging.warning(f'SKIPPING {version}') return latest
def find_semantic_version(releases, versions): matching_releases = [] for release in releases: if release.count('.') == 2: try: if semver.match(release, versions[0]) \ and semver.match(release, versions[1]): matching_releases.append(release) except: pass if len(matching_releases) == 1: out = matching_releases[0] elif len(matching_releases) == 2: out = semver.max_ver(matching_releases[0], matching_releases[1]) elif len(matching_releases) > 2: out = sorted(matching_releases)[-1] else: out = False return out
def version_tags(versions): tags = {} max_versions = OrderedDict() for v in sorted(versions, key=semver.VersionInfo.parse, reverse=True): tags[v] = [v] parent = v while parent: parent, _, _ = parent.rpartition('.') if not parent or parent == '0': continue if parent in max_versions: max_versions[parent] = semver.max_ver(max_versions[parent], v) else: max_versions[parent] = v for key, value in max_versions.items(): tags[value].append(key) return tags
def _latest_stable_tag(tags): version_revs = dict() for tag in tags: parts = tag.split('-') if len(parts) != 2: continue if parts[0] != 'android': continue full_version = parts[1] version, rrev = full_version.split('_') rev = int(rrev[1:]) if version not in version_revs: version_revs[version] = list() version_revs[version].append(rev) max_version = "0.0.0" for version in version_revs.iterkeys(): max_version = semver.max_ver(max_version, version) max_rev = 0 for rev in version_revs[max_version]: max_rev = max(max_rev, rev) return ''.join(["android-", max_version, "_r", str(max_rev)])
def test_should_get_max(): assert max_ver('3.4.5', '4.0.2') == '4.0.2'
def test_should_get_max_same(): assert max_ver('3.4.5', '3.4.5') == '3.4.5'
def test_should_get_max(self): self.assertEqual(max_ver('3.4.5', '4.0.2'), '4.0.2')
def test_should_get_max(): assert max_ver("3.4.5", "4.0.2") == "4.0.2"
def test_should_get_max_same(): assert max_ver("3.4.5", "3.4.5") == "3.4.5"
def get_max_version(versions): max = '0.0.0' for v in versions: max = semver.max_ver(max, v) return max
def get(request): try: request_path_segments = request.path_info.lower().split('/') # remove '/api/docs/' requested_file = '/'.join(request_path_segments[3:]) path = os.path.join(*( settings.MEDIA_ROOT, 'docs', requested_file)) logger.debug('Docs get on path %s', path) requested_file_segments = requested_file.split('/') library_name = requested_file_segments[0] if len(requested_file_segments) > 0 else '' version = requested_file_segments[1] if len(requested_file_segments) > 1 else '' file_path = '/'.join(requested_file_segments[2:]) library_path = os.path.join(*( settings.MEDIA_ROOT, 'docs', library_name)) logger.debug('Docs get version %s', version) if version == 'latest': all_versions = next(os.walk(library_path))[1] latest_version = '0.0.0' if len(all_versions) > 1: for a, b in itertools.combinations(all_versions, 2): highest_version = semver.max_ver(a, b) if highest_version > latest_version: latest_version = highest_version else: latest_version = all_versions[0] latest_file_pointer = os.path.join(*( settings.MEDIA_ROOT, 'docs', library_path, latest_version, file_path)) path = latest_file_pointer logger.debug('Docs get latest on path %s', path) if re.match(r'[\w,\s\S]+\.[A-Za-z]{2,4}$', path): content = open(path, 'rb').read() if path.endswith('.json'): try: content = json.loads(content.decode('utf-8')) return Response(content) except ValueError: return Response( {'error': 'JSON file cannot be decoded'}, status=status.HTTP_400_BAD_REQUEST) elif path.endswith('.md'): content = markdown.markdown( content.decode('utf-8'), output_format='html5') return HttpResponse(content=content) elif path.endswith('.png'): return HttpResponse(content=content, content_type='image/png') elif path.endswith('.jpeg'): return HttpResponse(content=content, content_type='image/jpeg') else: return HttpResponse(content=content) else: result = [] for item in os.listdir(path): item_path = os.path.join(*(path, item)) if os.path.isdir(item_path): item_path += '/' item_path = item_path.replace(settings.MEDIA_ROOT, '') result.append(item_path[1:]) return Response({'files': result}) return Response() except IOError as exc: return Response( { 'error': { 'code': exc.errno, 'message': exc.strerror } }, status=status.HTTP_400_BAD_REQUEST )