Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
repos:
- repo: https://github.com/psf/black
rev: 21.6b0
rev: 21.12b0
hooks:
- id: black
args: ["--target-version", "py27"]
additional_dependencies: ["click==7.1.2"]
- repo: https://github.com/PyCQA/flake8
rev: 3.9.2
hooks:
Expand Down
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pytest-cov
sphinx>=1.1
pre-commit==2.13.0; python_version > '3.5'
black; python_version > '3.5' and platform_python_implementation != 'PyPy'
click==7.1.2
mypy; python_version > '3.5' and platform_python_implementation != 'PyPy'
49 changes: 29 additions & 20 deletions distro.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python
# Copyright 2015,2016,2017 Nir Cohen
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -85,6 +86,7 @@
#: * Value: Normalized value.
NORMALIZED_OS_ID = {
"ol": "oracle", # Oracle Linux
"opensuse-leap": "opensuse", # Newer versions of OpenSuSE report as opensuse-leap
}

#: Translation table for normalizing the "Distributor ID" attribute returned by
Expand Down Expand Up @@ -151,7 +153,8 @@ def linux_distribution(full_distribution_name=True):

* ``version``: The result of :func:`distro.version`.

* ``codename``: The result of :func:`distro.codename`.
* ``codename``: The extra item (usually in parentheses) after the
os-release version number, or the result of :func:`distro.codename`.

The interface of this function is compatible with the original
:py:func:`platform.linux_distribution` function, supporting a subset of
Expand Down Expand Up @@ -198,8 +201,9 @@ def id():
"fedora" Fedora
"sles" SUSE Linux Enterprise Server
"opensuse" openSUSE
"amazon" Amazon Linux
"amzn" Amazon Linux
"arch" Arch Linux
"buildroot" Buildroot
"cloudlinux" CloudLinux OS
"exherbo" Exherbo Linux
"gentoo" GenToo Linux
Expand All @@ -219,6 +223,8 @@ def id():
"netbsd" NetBSD
"freebsd" FreeBSD
"midnightbsd" MidnightBSD
"rocky" Rocky Linux
"guix" Guix System
============== =========================================

If you have a need to get distros for reliable IDs added into this set,
Expand Down Expand Up @@ -313,6 +319,10 @@ def version(pretty=False, best=False):
sources in a fixed priority order does not always yield the most precise
version (e.g. for Debian 8.2, or CentOS 7.1).

Some other distributions may not provide this kind of information. In these
cases, an empty string would be returned. This behavior can be observed
with rolling releases distributions (e.g. Arch Linux).

The *best* parameter can be used to control the approach for the returned
version:

Expand Down Expand Up @@ -723,10 +733,6 @@ def __init__(
* :py:exc:`IOError`: Some I/O issue with an os-release file or distro
release file.

* :py:exc:`subprocess.CalledProcessError`: The lsb_release command had
some issue (other than not being available in the program execution
path).

* :py:exc:`UnicodeError`: A data source has unexpected characters or
uses an unexpected encoding.
"""
Expand Down Expand Up @@ -784,7 +790,7 @@ def linux_distribution(self, full_distribution_name=True):
return (
self.name() if full_distribution_name else self.id(),
self.version(),
self.codename(),
self._os_release_info.get("release_codename") or self.codename(),
)

def id(self):
Expand Down Expand Up @@ -860,6 +866,13 @@ def version(self, pretty=False, best=False):
).get("version_id", ""),
self.uname_attr("release"),
]
if self.id() == "debian" or "debian" in self.like().split():
# On Debian-like, add debian_version file content to candidates list.
try:
with open(os.path.join(self.etc_dir, "debian_version")) as fp:
versions.append(fp.readline().rstrip())
except IOError:
pass
version = ""
if best:
# This algorithm uses the last version in priority order that has
Expand Down Expand Up @@ -1102,12 +1115,17 @@ def _parse_os_release_content(lines):
# stripped, etc.), so the tokens are now either:
# * variable assignments: var=value
# * commands or their arguments (not allowed in os-release)
# Ignore any tokens that are not variable assignments
if "=" in token:
k, v = token.split("=", 1)
props[k.lower()] = v
else:
# Ignore any tokens that are not variable assignments
pass

if "version" in props:
# extract release codename (if any) from version attribute
match = re.search(r"\((\D+)\)|,\s*(\D+)", props["version"])
if match:
release_codename = match.group(1) or match.group(2)
props["codename"] = props["release_codename"] = release_codename

if "version_codename" in props:
# os-release added a version_codename field. Use that in
Expand All @@ -1118,16 +1136,6 @@ def _parse_os_release_content(lines):
elif "ubuntu_codename" in props:
# Same as above but a non-standard field name used on older Ubuntus
props["codename"] = props["ubuntu_codename"]
elif "version" in props:
# If there is no version_codename, parse it from the version
match = re.search(r"(\(\D+\))|,(\s+)?\D+", props["version"])
if match:
codename = match.group()
codename = codename.strip("()")
codename = codename.strip(",")
codename = codename.strip()
# codename appears within paranthese.
props["codename"] = codename

return props

Expand Down Expand Up @@ -1276,6 +1284,7 @@ def _distro_release_info(self):
"manjaro-release",
"oracle-release",
"redhat-release",
"rocky-release",
"sl-release",
"slackware-version",
]
Expand Down
5 changes: 5 additions & 0 deletions tests/resources/distros/buildroot/etc/os-release
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
NAME=Buildroot
VERSION=2022.02
ID=buildroot
VERSION_ID=2022.02
PRETTY_NAME="Buildroot 2022.02"
21 changes: 21 additions & 0 deletions tests/resources/distros/debian10/bin/lsb_release
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash
#
# lsb_release command for testing the ld module.
# Only the -a option is supported.
#
# This version of the lsb_release command works without a corresponding
# etc/lsb-release file.
#

if [[ "$@" != "-a" ]]; then
echo "Usage: lsb_release -a"
exit 2
fi

echo "No LSB modules are available."
echo "Distributor ID: Debian"
echo "Description: Debian GNU/Linux 10 (buster)"
echo "Release: 10"
echo "Codename: buster"

exit 0
1 change: 1 addition & 0 deletions tests/resources/distros/debian10/etc/debian_version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10.11
9 changes: 9 additions & 0 deletions tests/resources/distros/debian10/etc/os-release
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
8 changes: 8 additions & 0 deletions tests/resources/distros/guix/etc/os-release
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
NAME="Guix System"
ID=guix
PRETTY_NAME="Guix System"
LOGO=guix-icon
HOME_URL="https://guix.gnu.org"
DOCUMENTATION_URL="https://guix.gnu.org/en/manual"
SUPPORT_URL="https://guix.gnu.org/en/help"
BUG_REPORT_URL="https://lists.gnu.org/mailman/listinfo/bug-guix"
10 changes: 10 additions & 0 deletions tests/resources/distros/opensuse15/etc/os-release
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
NAME="openSUSE Leap"
VERSION="15.2"
ID="opensuse-leap"
ID_LIKE="suse opensuse"
VERSION_ID="15.2"
PRETTY_NAME="openSUSE Leap 15.2"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:opensuse:leap:15.2"
BUG_REPORT_URL="https://bugs.opensuse.org"
HOME_URL="https://www.opensuse.org/"
1 change: 1 addition & 0 deletions tests/resources/distros/rocky/etc/centos-release
1 change: 1 addition & 0 deletions tests/resources/distros/rocky/etc/os-release
1 change: 1 addition & 0 deletions tests/resources/distros/rocky/etc/redhat-release
1 change: 1 addition & 0 deletions tests/resources/distros/rocky/etc/rocky-release
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Rocky Linux release 8.4 (Green Obsidian)
1 change: 1 addition & 0 deletions tests/resources/distros/rocky/etc/rocky-release-upstream
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Derived from Red Hat Enterprise Linux 8.4
1 change: 1 addition & 0 deletions tests/resources/distros/rocky/etc/system-release
1 change: 1 addition & 0 deletions tests/resources/distros/rocky/etc/system-release-cpe
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpe:/o:rocky:rocky:8.4:GA
13 changes: 13 additions & 0 deletions tests/resources/distros/rocky/usr/lib/os-release
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
NAME="Rocky Linux"
VERSION="8.4 (Green Obsidian)"
ID="rocky"
ID_LIKE="rhel centos fedora"
VERSION_ID="8.4"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Rocky Linux 8.4 (Green Obsidian)"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:rocky:rocky:8.4:GA"
HOME_URL="https://rockylinux.org/"
BUG_REPORT_URL="https://bugs.rockylinux.org/"
ROCKY_SUPPORT_PRODUCT="Rocky Linux"
ROCKY_SUPPORT_PRODUCT_VERSION="8"
Loading