8000 bpo-28468: Add platform.freedesktop_osrelease by tiran · Pull Request #23492 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-28468: Add platform.freedesktop_osrelease #23492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Victor prefers to keep ID_LIKE untouched
Documentation now shows how to use ID_LIKE correctly.
  • Loading branch information
tiran committed Nov 25, 2020
commit 679af65ca3798dc36dedabfdc66303b4cee49373
28 changes: 20 additions & 8 deletions Doc/library/platform.rst
Raises :exc:`OSError` or subclass when neither ``/etc/os-release`` nor
Original file line number Diff line number Diff line change
Expand Up @@ -266,16 +266,28 @@ Linux Platforms
is available in most Linux distributions. A noticeable exception is
Android and Android-based distributions.

All fields except ``NAME``, ``ID``, and ``PRETTY_NAME`` are optional.
If present, the ``ID_LIKE`` parsed and presented as a tuple of strings.
Comments, empty lines, and invalid lines are silently omitted.
``/usr/lib/os-release`` can be read.

On success, the function returns a dictionary where keys and values are
strings. Values have their special characters like ``"`` and ``$``
unquoted. The fields ``NAME``, ``ID``, and ``PRETTY_NAME`` are always
defined according to the standard. All other fields are optional. Vendors
may include additional fields.

Note that fields like ``NAME``, ``VERSION``, and ``VARIANT`` are strings
suitable for presentation to users. Programs should use fields like
``ID`` + ``ID_LIKE``, ``VERSION_ID``, or ``VARIANT_ID`` to identify
Linux distributions. Vendors may include additional fields.

Raises :exc:`OSError` or subclass when neither ``/etc/os-release`` nor
``/usr/lib/os-release`` can be read.
``ID``, ``ID_LIKE``, ``VERSION_ID``, or ``VARIANT_ID`` to identify
Linux distributions.

Example::

def get_like_distro():
info = platform.freedesktop_os_release()
ids = [info["ID"]]
if "ID_LIKE" in info:
# ids are space separated and ordered by precedence
ids.extend(info["ID_LIKE"].split())
return ids

.. versionadded:: 3.10
6 changes: 0 additions & 6 deletions Lib/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -1261,12 +1261,6 @@ def _parse_os_release(lines):
r"\1", mo.group('value')
)

# ID_LIKE is a space separated field of ids
if 'ID_LIKE' in info:
info['ID_LIKE'] = tuple(
id_like for id_like in info['ID_LIKE'].split(' ') if id_like
)

return info


Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,15 +468,15 @@ def test_parse_os_release(self):
info = platform._parse_os_release(UBUNTU_OS_RELEASE.splitlines())
self.assertEqual(info["NAME"], "Ubuntu")
self.assertEqual(info["ID"], "ubuntu")
self.assertEqual(info["ID_LIKE"], ("debian",))
self.assertEqual(info["ID_LIKE"], "debian")
self.assertEqual(info["VERSION_CODENAME"], "focal")

info = platform._parse_os_release(TEST_OS_RELEASE.splitlines())
expected = {
"ID": "linux",
"NAME": "Linux",
"PRETTY_NAME": "Linux",
"ID_LIKE": (" 4B83 ;egg", "spam", "viking"),
"ID_LIKE": "egg spam viking",
"EMPTY": "",
"DOUBLE_QUOTE": "double",
"EMPTY_DOUBLE": "",
Expand Down
0