def parse_os_release(release_path='/etc/os-release'):
    """ Extract (distro, release, codename) from /etc/os-release if present """
    release_info = {}
    if os.path.isfile(release_path):
        for line in open(release_path, 'r').readlines():
            line = line.strip()
            if line.startswith('#'):
                continue
            parts = line.split('=')
            if len(parts) != 2:
                continue
            release_info[parts[0].strip()] = parts[1].strip("\"'\n\t ")
    # In theory, we want ID/NAME, VERSION_ID and VERSION_CODENAME (with a
    # possible fallback to VERSION on the latter), based on information at:
    # https://www.freedesktop.org/software/systemd/man/os-release.html
    # However, after reviewing several distros /etc/os-release, getting
    # the codename is a bit of a mess.  It's usually in parentheses in
    # VERSION, with some exceptions.
    distro = release_info.get('ID', '')
    release = release_info.get('VERSION_ID', '')
    codename = release_info.get('UBUNTU_CODENAME', release_info.get('VERSION', ''))
    match = re.match(r'^[^(]+ \(([^)]+)\)', codename)
    if match:
        codename = match.group(1).lower()
    if not codename and release_info.get('NAME', '') == 'openSUSE Tumbleweed':
        codename = 'tumbleweed'
    return (distro, release, codename)
Extract (distro, release, codename) from /etc/os-release if present