コード例 #1
0
def determine_osinfo(tarfh: tarfile.TarFile) -> um.OperatingSystemId:
    '''
    tries to determine the operating system identification, roughly as specified by
        https://www.freedesktop.org/software/systemd/man/os-release.html
    and otherwise following some conventions believed to be common.

    The argument (an opened tarfile) is being read from its initial position, possibly (but
    not necessarily) to the end. The underlying stream does not need to be seekable.
    It is the caller's responsibility to close the tarfile handle after this function returns.

    The tarfile is expected to contain a directory tree from a "well-known" unix-style operating
    system distribution. In particular, the following (GNU/) Linux distributions are well-supported:
    - alpine
    - debian
    - centos

    In case nothing was recognised within the given tarfile, the returned OperatingSystemId's
    attributes will all be `None`.
    '''
    known_fnames = (
        'debian_version',
        'centos-release',
        'os-release',
    )

    os_info = {}

    for info in tarfh:
        fname = info.name.split('/')[-1]

        if not fname in known_fnames:
            continue

        if info.issym():
            # we assume fnames are the same (this assumption might not always be correct)
            continue

        if not info.isfile():
            continue

        # found an "interesting" file
        contents = tarfh.extractfile(info).read().decode('utf-8')

        if fname == 'os-release':
            for k, v in _parse_os_release(contents):
                if k in os_info:
                    if k == 'VERSION_ID' and version.is_semver_parseable(v) and \
                        not version.is_semver_parseable(os_info[k]):
                        pass
                    else:
                        continue  # os-release has lesser precedence
                os_info[k] = v
            if os_info.get('ID') == 'ubuntu' and (ver :=
                                                  os_info.get('VERSION')):
                # of _course_ ubuntu requires a special hack
                os_info['VERSION_ID'] = ver.split(' ', 1)[0]
        elif fname == 'centos-release':
            for k, v in _parse_centos_release(contents):
                os_info[k] = v
コード例 #2
0
ファイル: util.py プロジェクト: ik-automation/cc-utils
def outdated_draft_releases(
    draft_releases: [github3.repos.release.Release],
    greatest_release_version: str,
):
    '''Find outdated draft releases from a list of draft releases and return them. This is achieved
    by partitioning the release versions according to their joined major and minor version.
    Partitions are then checked:
        - if there is only a single release in a partition it is either a hotfix release
            (keep corresponding release) or it is not (delete if it is not the greatest release
            according to semver)
        - if there are multiple releases versions in a partition, keep only the release
            corresponding to greatest (according to semver)
    '''

    greatest_release_version_info = semver.parse_version_info(greatest_release_version)

    def _has_semver_draft_prerelease_label(release_name):
        version_info = semver.parse_version_info(release_name)
        if version_info.prerelease != 'draft':
            return False
        return True

    autogenerated_draft_releases = [
        release for release in draft_releases
        if release.name
        and version.is_semver_parseable(release.name)
        and _has_semver_draft_prerelease_label(release.name)
    ]

    draft_release_version_infos = [
        semver.parse_version_info(release.name)
        for release in autogenerated_draft_releases
    ]

    def _yield_outdated_version_infos_from_partition(partition):
        if len(partition) == 1:
            version_info = partition.pop()
            if version_info < greatest_release_version_info and version_info.patch == 0:
                yield version_info
        else:
            yield from [
                version_info
                for version_info in partition[1:]
            ]

    outdated_version_infos = list()
    for partition in version.partition_by_major_and_minor(draft_release_version_infos):
        outdated_version_infos.extend(_yield_outdated_version_infos_from_partition(partition))

    outdated_draft_releases = [
        release
        for release in autogenerated_draft_releases
        if semver.parse_version_info(release.name) in outdated_version_infos
    ]

    return outdated_draft_releases
コード例 #3
0
        if fname == 'os-release':
            for k, v in _parse_os_release(contents):
                if k in os_info:
                    if k == 'VERSION_ID' and version.is_semver_parseable(v) and \
                        not version.is_semver_parseable(os_info[k]):
                        pass
                    else:
                        continue  # os-release has lesser precedence
                os_info[k] = v
            if os_info.get('ID') == 'ubuntu' and (ver :=
                                                  os_info.get('VERSION')):
                # of _course_ ubuntu requires a special hack
                os_info['VERSION_ID'] = ver.split(' ', 1)[0]
        elif fname == 'centos-release':
            for k, v in _parse_centos_release(contents):
                os_info[k] = v
        elif fname == 'debian_version':
            for k, v in _parse_debian_version(contents):
                if k in os_info:
                    if not version.is_semver_parseable(v):
                        continue  # e.g. ubuntu has "misleading" debian_version
                os_info[k] = v
        else:
            raise NotImplementedError(fname)

    return dacite.from_dict(
        data_class=um.OperatingSystemId,
        data=os_info,
    )