Skip to content

Commit a08f31b

Browse files
Samuel FORESTIERHorlogeSkynet
authored andcommitted
Restores compatibility shim with platform.linux_distribution
distro#230 introduced proper support for os-release `VERSION_CODENAME` attribute but broke compatibility shim with (now defunct) `platform.linux_distribution` as the third tuple member (codename) were not taken from `VERSION` os-release attribute anymore. Closes #238.
1 parent 4d76bd1 commit a08f31b

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

src/distro/distro.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ def linux_distribution(full_distribution_name: bool = True) -> Tuple[str, str, s
151151
152152
* ``version``: The result of :func:`distro.version`.
153153
154-
* ``codename``: The result of :func:`distro.codename`.
154+
* ``codename``: The extra item (usually in parentheses) after the
155+
os-release version number, or the result of :func:`distro.codename`.
155156
156157
The interface of this function is compatible with the original
157158
:py:func:`platform.linux_distribution` function, supporting a subset of
@@ -768,7 +769,7 @@ def linux_distribution(
768769
return (
769770
self.name() if full_distribution_name else self.id(),
770771
self.version(),
771-
self.codename(),
772+
self._os_release_info.get("release_codename") or self.codename(),
772773
)
773774

774775
def id(self) -> str:
@@ -1062,6 +1063,13 @@ def _parse_os_release_content(lines: TextIO) -> Dict[str, str]:
10621063
# Ignore any tokens that are not variable assignments
10631064
pass
10641065

1066+
if "version" in props:
1067+
# extract release codename (if any) from version attribute
1068+
match = re.search(r"\((\D+)\)|,\s*(\D+)", props["version"])
1069+
if match:
1070+
release_codename = match.group(1) or match.group(2)
1071+
props["codename"] = props["release_codename"] = release_codename
1072+
10651073
if "version_codename" in props:
10661074
# os-release added a version_codename field. Use that in
10671075
# preference to anything else Note that some distros purposefully
@@ -1071,16 +1079,6 @@ def _parse_os_release_content(lines: TextIO) -> Dict[str, str]:
10711079
elif "ubuntu_codename" in props:
10721080
# Same as above but a non-standard field name used on older Ubuntus
10731081
props["codename"] = props["ubuntu_codename"]
1074-
elif "version" in props:
1075-
# If there is no version_codename, parse it from the version
1076-
match = re.search(r"(\(\D+\))|,(\s+)?\D+", props["version"])
1077-
if match:
1078-
codename = match.group()
1079-
codename = codename.strip("()")
1080-
codename = codename.strip(",")
1081-
codename = codename.strip()
1082-
# codename appears within paranthese.
1083-
props["codename"] = codename
10841082

10851083
return props
10861084

tests/test_distro.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,6 +1833,9 @@ def setup_method(self, test_method: FunctionType) -> None:
18331833
self.ubuntu14_os_release = os.path.join(
18341834
DISTROS_DIR, "ubuntu14", "etc", "os-release"
18351835
)
1836+
self.fedora30_os_release = os.path.join(
1837+
DISTROS_DIR, "fedora30", "etc", "os-release"
1838+
)
18361839

18371840
def test_info(self) -> None:
18381841
_distro = distro.LinuxDistribution(
@@ -1905,6 +1908,12 @@ def test_linux_distribution(self) -> None:
19051908
i = _distro.linux_distribution()
19061909
assert i == ("Ubuntu", "14.04", "Trusty Tahr")
19071910

1911+
_distro = distro.LinuxDistribution(
1912+
include_lsb=False, os_release_file=self.fedora30_os_release
1913+
)
1914+
i = _distro.linux_distribution()
1915+
assert i == ("Fedora", "30", "Thirty")
1916+
19081917
def test_linux_distribution_full_false(self) -> None:
19091918
_distro = distro.LinuxDistribution(
19101919
include_lsb=False, os_release_file=self.ubuntu14_os_release

0 commit comments

Comments
 (0)