Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
rt-framework / rt / misc.py
Size: Mime:
#
# Copyright (C) 2016 Rolf Neugebauer <rolf.neugebauer@docker.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#

import platform

"""Miscellaneous utility functions"""


def str2labels(labelstr):
    """Convert a comma separate list of labels to two sets.

    Plain labels are placed into the labels_set set and labels
    starting with a '!' are placed in the labels_not_set set.
    """
    labels_set = set()
    labels_not_set = set()

    if not labelstr:
        return labels_set, labels_not_set

    labels = labelstr.split(',')

    for label in labels:
        l = label.strip()
        if len(l) == 0:
            continue
        if l.startswith('!'):
            labels_not_set.add(l[1:])
        else:
            labels_set.add(l)
    return labels_set, labels_not_set


#
# OS info
#
def _get_osx_info():
    """collect information for OS X based hosts"""
    info = {}
    info["os"] = "osx"

    v = platform.mac_ver()
    info["version"] = v[0]

    if v[0].startswith("10.12"):
        info["name"] = "Sierra"
    elif v[0].startswith("10.11"):
        info["name"] = "ElCapitan"
    elif v[0].startswith("10.10"):
        info["name"] = "Yosemite"
    elif v[0].startswith("10.9"):
        info["name"] = "Mavericks"
    elif v[0].startswith("10.8"):
        info["name"] = "Lion"
    elif v[0].startswith("10.7"):
        info["name"] = "Snow Leopard"
    else:
        info["name"] = "UNKNOWN"

    info["arch"] = v[2]

    return info


def _get_win_info():
    """collect information for OS X based hosts"""
    info = {}
    info["os"] = "win"

    v = platform.win32_ver()
    info["version"] = v[1]

    # Need a way to find Edition (Pro, Home etc)
    info["name"] = "Windows" + v[0] + v[2]

    a = platform.machine()
    info["arch"] = "x86_64" if a == "AMD64" else "x86_32"

    return info


def _get_linux_info():
    """collect information for Linux based hosts"""
    platform_info = platform.uname()
    info = {}
    info["os"] = platform_info[0]
    info["version"] = platform_info[2]
    info["name"] = "Linux" + platform_info[3]
    info["arch"] = platform_info[4]

    return info


def get_os_info():
    """Collect information about the local OS and stick them into a
    canonical form"""

    t = platform.system()
    if t == "Darwin":
        return _get_osx_info()
    elif t == "Windows":
        return _get_win_info()
    elif t == "Linux":
        return _get_linux_info()
    else:
        raise Exception("Unsupported OS")