man: recommend built-in platform.freedesktop_os_release() in our page

Python gained support for reading os-release, let's advertise it a bit more.
Our open-coded example is still useful, but let's not suggest it as the
default implementation.

I added quotes around the printed string because it looks a bit better
this way.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2022-02-28 09:47:28 +01:00 committed by Luca Boccassi
parent ba119ffcf9
commit ee6fd6a509
3 changed files with 28 additions and 2 deletions

View file

@ -0,0 +1,12 @@
#!/usr/bin/python
# SPDX-License-Identifier: CC0-1.0
import platform
os_release = platform.freedesktop_os_release()
pretty_name = os_release.get('PRETTY_NAME', 'Linux')
print(f'Running on {pretty_name!r}')
if 'fedora' in [os_release.get('ID', 'linux'),
*os_release.get('ID_LIKE', '').split()]:
print('Looks like Fedora!')

View file

@ -29,7 +29,7 @@ def read_os_release():
os_release = dict(read_os_release())
pretty_name = os_release.get('PRETTY_NAME', 'Linux')
print(f'Running on {pretty_name}')
print(f'Running on {pretty_name!r}')
if 'debian' in [os_release.get('ID', 'linux'),
*os_release.get('ID_LIKE', '').split()]:

View file

@ -502,9 +502,23 @@ VERSION_ID=32</programlisting>
<example>
<title>Reading <filename>os-release</filename> in
<citerefentry><refentrytitle>python</refentrytitle><manvolnum>1</manvolnum></citerefentry></title>
<citerefentry><refentrytitle>python</refentrytitle><manvolnum>1</manvolnum></citerefentry> (versions &gt;= 3.10)</title>
<programlisting><xi:include href="check-os-release-simple.py" parse="text" /></programlisting>
<para>See docs for <ulink url="https://docs.python.org/3/library/platform.html#platform.freedesktop_os_release">
<function>platform.freedesktop_os_release</function></ulink> for more details.
</para>
</example>
<example>
<title>Reading <filename>os-release</filename> in
<citerefentry><refentrytitle>python</refentrytitle><manvolnum>1</manvolnum></citerefentry> (any version)</title>
<programlisting><xi:include href="check-os-release.py" parse="text" /></programlisting>
<para>Note that the above version that uses the built-in implementation is preferred
in most cases, and the open-coded version here is provided for reference.</para>
</example>
</refsect1>