os-release Documentation

os-release on PyPI

os-release is a simple Python module for reading systemd’s os-release information on modern Linux distributions.

It parses the contents of systemd’s os-release(5) files: /etc/os-release or /usr/lib/os-release.

Installation

os-release is available on PyPI.

Add it to your projects setup.py` ``install_requires:

install_requires=['os-release']

or install it directly via pip:

$ pip install os-release

Alternatively, clone the Git repository and run:

$ python setup.py install

Quick Start

The most common usage of this module is to call the os_release.current_release() method to obtain the details of the operating system on which the calling Python program is running.

For example, on a CentOS 8 system:

>>> import os_release
>>> os_release.current_release()
OsRelease(
    name='CentOS Linux',
    version='8 (Core)',
    id='centos',
    id_like=('rhel', 'fedora'),
    version_codename=None,
    version_id='8',
    pretty_name='CentOS Linux 8 (Core)',
    ansi_color='0;31',
    cpe_name='cpe:/o:centos:centos:8',
    build_id=None,
    variant=None,
    variant_id=None,
    logo=None,
    urls=Urls(
        home='https://www.centos.org/',
        documentation=None,
        support=None,
        bug_report='https://bugs.centos.org/',
        privacy_policy=None
    ),
    vendor_extra={
        'PLATFORM_ID': 'platform:el8',
        'CENTOS_MANTISBT_PROJECT': 'CentOS-8',
        'CENTOS_MANTISBT_PROJECT_VERSION': '8',
        'REDHAT_SUPPORT_PRODUCT': 'centos',
        'REDHAT_SUPPORT_PRODUCT_VERSION': '8'
    }
)

API

class os_release.OsRelease

Represents the fields of a systemd os-release file.

OsRelease is a typing.NamedTuple.

property name

Equivalent to the NAME field in os-release.

As per os-release(5), this property will default to Linux if NAME is not present in os-release.

property version

Equivalent to the VERSION field in os-release.

This property is optional and will default to None if VERSION is not present in os-release.

property id

Equivalent to the ID field in os-release.

As per os-release(5), this property will default to linux if ID is not present in os-release.

property id_like

Equivalent to the ID_LIKE field in os-release.

The value of ID_LIKE is automatically split on whitespace into a tuple.

This property is optional and will default to an empty tuple if ID_LIKE is not present in os-release.

property version_codename

Equivalent to the VERSION_CODENAME field in os-release.

This property is optional and will default to None if VERSION_CODENAME is not present in os-release.

property version_id

Equivalent to the VERSION_ID field in os-release.

This property is optional and will default to None if VERSION_ID is not present in os-release.

property pretty_name

Equivalent to the PRETTY_NAME field in os-release.

As per os-release(5), this property will default to Linux if PRETTY_NAME is not present in os-release.

property ansi_color

Equivalent to the ANSI_COLOR field in os-release.

This property is optional and will default to None if ANSI_COLOR is not present in os-release.

property cpe_name

Equivalent to the CPE_NAME field in os-release.

This property is optional and will default to None if CPE_NAME is not present in os-release.

property build_id

Equivalent to the BUILD_ID field in os-release.

This property is optional and will default to None if BUILD_ID is not present in os-release.

property variant

Equivalent to the VARIANT field in os-release.

This property is optional and will default to None if VARIANT is not present in os-release.

property variant_id

Equivalent to the VARIANT_ID field in os-release.

This property is optional and will default to None if VARIANT_ID is not present in os-release.

Equivalent to the LOGO field in os-release.

This property is optional and will default to None if LOGO is not present in os-release.

property urls

See OsRelease.Urls.

This property will always be an instance of OsRelease.Urls, even if no *_URL fields are present in os-release.

property vendor_extra

A dict of vendor-specific fields (fields that are unknown to os-release).

class Urls

Represents the various *_URL fields in os-release.

property home

Equivalent to the HOME_URL field in os-release.

property documentation

Equivalent to the DOCUMENTATION_URL field in os-release.

property support

Equivalent to the SUPPORT_URL field in os-release.

property bug_report

Equivalent to the BUG_REPORT_URL field in os-release.

property privacy_policy

Equivalent to the PRIVACY_POLICY_URL field in os-release.

is_like(query)

Returns True if the operating system represented by this OsRelease has an id or id_like that matches the passed query.

Parameters

query (str) – the id to test

Return type

bool

os_release.current_release()

Return an OsRelease tuple representing the contents of the os-release file for the current operating system.

Tries to read the following files, in order (as per os-release(5)):

  • /etc/os-release

  • /usr/lib/os-release

Raises
  • FileNotFoundError – if no os-release file can be found.

  • OsReleaseParseException – if the os-release file cannot be parsed.

Return type

OsRelease

Indices and tables