diff --git a/CHANGES b/CHANGES index 7614e7c..39c463b 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,7 @@ +**Unreleased** + +* Added support for OpenBSD, FreeBSD, and NetBSD + **1.2.0 (2017.12.24)** * Lazily load parsers to speed up import time. diff --git a/README.md b/README.md index 0bfc6a9..f810c96 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -Distro - a Linux OS platform information API -============================================ +Distro - an OS platform information API +======================================= [![Build Status](https://travis-ci.org/nir0s/distro.svg?branch=master)](https://travis-ci.org/nir0s/distro) [![Build status](https://ci.appveyor.com/api/projects/status/e812qjk1gf0f74r5/branch/master?svg=true)](https://ci.appveyor.com/project/nir0s/distro/branch/master) diff --git a/README.rst b/README.rst index ee3a8db..7aac266 100644 --- a/README.rst +++ b/README.rst @@ -1,4 +1,4 @@ -Distro - a Linux OS platform information API -============================================ +Distro - an OS platform information API +======================================= See `Official GitHub repo `_. diff --git a/distro.py b/distro.py index 39bfce7..aa4defc 100755 --- a/distro.py +++ b/distro.py @@ -1,4 +1,4 @@ -# Copyright 2015,2016 Nir Cohen +# Copyright 2015,2016,2017 Nir Cohen # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -23,7 +23,7 @@ 3.5 deprecated this function, and Python 3.7 is expected to remove it altogether. Its predecessor function :py:func:`platform.dist` was already deprecated since Python 2.6 and is also expected to be removed in Python 3.7. -Still, there are many cases in which access to Linux distribution information +Still, there are many cases in which access to OS distribution information is needed. See `Python issue 1322 `_ for more information. """ @@ -94,7 +94,7 @@ def linux_distribution(full_distribution_name=True): """ - Return information about the current Linux distribution as a tuple + Return information about the current OS distribution as a tuple ``(id_name, version, codename)`` with items as follows: * ``id_name``: If *full_distribution_name* is false, the result of @@ -110,22 +110,22 @@ def linux_distribution(full_distribution_name=True): The data it returns may not exactly be the same, because it uses more data sources than the original function, and that may lead to different data if - the Linux distribution is not consistent across multiple data sources it + the OS distribution is not consistent across multiple data sources it provides (there are indeed such distributions ...). Another reason for differences is the fact that the :func:`distro.id` method normalizes the distro ID string to a reliable machine-readable value - for a number of popular Linux distributions. + for a number of popular OS distributions. """ return _distro.linux_distribution(full_distribution_name) def id(): """ - Return the distro ID of the current Linux distribution, as a + Return the distro ID of the current distribution, as a machine-readable string. - For a number of Linux distributions, the returned distro ID value is + For a number of OS distributions, the returned distro ID value is *reliable*, in the sense that it is documented and that it does not change across releases of the distribution. @@ -158,6 +158,9 @@ def id(): "scientific" Scientific Linux "slackware" Slackware "xenserver" XenServer + "openbsd" OpenBSD + "netbsd" NetBSD + "freebsd" FreeBSD ============== ========================================= If you have a need to get distros for reliable IDs added into this set, @@ -187,7 +190,7 @@ def id(): * a normalization of the ID is performed, based upon `normalization tables`_. The purpose of this normalization is to ensure that the ID is as reliable as possible, even across incompatible changes - in the Linux distributions. A common reason for an incompatible change is + in the OS distributions. A common reason for an incompatible change is the addition of an os-release file, or the addition of the lsb_release command, with ID values that differ from what was previously determined from the distro release file name. @@ -197,7 +200,7 @@ def id(): def name(pretty=False): """ - Return the name of the current Linux distribution, as a human-readable + Return the name of the current OS distribution, as a human-readable string. If *pretty* is false, the name is returned without version or codename. @@ -236,7 +239,7 @@ def name(pretty=False): def version(pretty=False, best=False): """ - Return the version of the current Linux distribution, as a human-readable + Return the version of the current OS distribution, as a human-readable string. If *pretty* is false, the version is returned without codename (e.g. @@ -280,7 +283,7 @@ def version(pretty=False, best=False): def version_parts(best=False): """ - Return the version of the current Linux distribution as a tuple + Return the version of the current OS distribution as a tuple ``(major, minor, build_number)`` with items as follows: * ``major``: The result of :func:`distro.major_version`. @@ -297,7 +300,7 @@ def version_parts(best=False): def major_version(best=False): """ - Return the major version of the current Linux distribution, as a string, + Return the major version of the current OS distribution, as a string, if provided. Otherwise, the empty string is returned. The major version is the first part of the dot-separated version string. @@ -310,7 +313,7 @@ def major_version(best=False): def minor_version(best=False): """ - Return the minor version of the current Linux distribution, as a string, + Return the minor version of the current OS distribution, as a string, if provided. Otherwise, the empty string is returned. The minor version is the second part of the dot-separated version string. @@ -323,7 +326,7 @@ def minor_version(best=False): def build_number(best=False): """ - Return the build number of the current Linux distribution, as a string, + Return the build number of the current OS distribution, as a string, if provided. Otherwise, the empty string is returned. The build number is the third part of the dot-separated version string. @@ -337,7 +340,7 @@ def build_number(best=False): def like(): """ Return a space-separated list of distro IDs of distributions that are - closely related to the current Linux distribution in regards to packaging + closely related to the current OS distribution in regards to packaging and programming interfaces, for example distributions the current distribution is a derivative from. @@ -353,7 +356,7 @@ def like(): def codename(): """ - Return the codename for the release of the current Linux distribution, + Return the codename for the release of the current OS distribution, as a string. If the distribution does not have a codename, an empty string is returned. @@ -377,7 +380,7 @@ def codename(): def info(pretty=False, best=False): """ - Return certain machine-readable information items about the current Linux + Return certain machine-readable information items about the current OS distribution in a dictionary, as shown in the following example: .. sourcecode:: python @@ -422,7 +425,7 @@ def info(pretty=False, best=False): def os_release_info(): """ Return a dictionary containing key-value pairs for the information items - from the os-release file data source of the current Linux distribution. + from the os-release file data source of the current OS distribution. See `os-release file`_ for details about these information items. """ @@ -432,7 +435,7 @@ def os_release_info(): def lsb_release_info(): """ Return a dictionary containing key-value pairs for the information items - from the lsb_release command data source of the current Linux distribution. + from the lsb_release command data source of the current OS distribution. See `lsb_release command output`_ for details about these information items. @@ -443,17 +446,25 @@ def lsb_release_info(): def distro_release_info(): """ Return a dictionary containing key-value pairs for the information items - from the distro release file data source of the current Linux distribution. + from the distro release file data source of the current OS distribution. See `distro release file`_ for details about these information items. """ return _distro.distro_release_info() +def uname_info(): + """ + Return a dictionary containing key-value pairs for the information items + from the distro release file data source of the current OS distribution. + """ + return _distro.uname_info() + + def os_release_attr(attribute): """ Return a single named information item from the os-release file data source - of the current Linux distribution. + of the current OS distribution. Parameters: @@ -472,7 +483,7 @@ def os_release_attr(attribute): def lsb_release_attr(attribute): """ Return a single named information item from the lsb_release command output - data source of the current Linux distribution. + data source of the current OS distribution. Parameters: @@ -492,7 +503,7 @@ def lsb_release_attr(attribute): def distro_release_attr(attribute): """ Return a single named information item from the distro release file - data source of the current Linux distribution. + data source of the current OS distribution. Parameters: @@ -508,6 +519,23 @@ def distro_release_attr(attribute): return _distro.distro_release_attr(attribute) +def uname_attr(attribute): + """ + Return a single named information item from the distro release file + data source of the current OS distribution. + + Parameters: + + * ``attribute`` (string): Key of the information item. + + Returns: + + * (string): Value of the information item, if the item exists. + The empty string, if the item does not exist. + """ + return _distro.uname_attr(attribute) + + class cached_property(object): """A version of @property which caches the value. On access, it calls the underlying function and sets the value in `__dict__` so future accesses @@ -525,13 +553,13 @@ def __get__(self, obj, owner): class LinuxDistribution(object): """ - Provides information about a Linux distribution. + Provides information about a OS distribution. This package creates a private module-global instance of this class with default initialization arguments, that is used by the `consolidated accessor functions`_ and `single source accessor functions`_. By using default initialization arguments, that module-global instance - returns data about the current Linux distribution (i.e. the distro this + returns data about the current OS distribution (i.e. the distro this package runs on). Normally, it is not necessary to create additional instances of this class. @@ -544,7 +572,8 @@ class LinuxDistribution(object): def __init__(self, include_lsb=True, os_release_file='', - distro_release_file=''): + distro_release_file='', + include_uname=True): """ The initialization method of this class gathers information from the available data sources, and stores that in private instance attributes. @@ -578,6 +607,11 @@ def __init__(self, distro release file can be found, the data source for the distro release file will be empty. + * ``include_name`` (bool): Controls whether uname command output is + included as a data source. If the uname command is not available in + the program execution path the data source for the uname command will + be empty. + Public instance attributes: * ``os_release_file`` (string): The path name of the @@ -591,6 +625,10 @@ def __init__(self, * ``include_lsb`` (bool): The result of the ``include_lsb`` parameter. This controls whether the lsb information will be loaded. + * ``include_uname`` (bool): The result of the ``include_uname`` + parameter. This controls whether the uname information will + be loaded. + Raises: * :py:exc:`IOError`: Some I/O issue with an os-release file or distro @@ -607,6 +645,7 @@ def __init__(self, os.path.join(_UNIXCONFDIR, _OS_RELEASE_BASENAME) self.distro_release_file = distro_release_file or '' # updated later self.include_lsb = include_lsb + self.include_uname = include_uname def __repr__(self): """Return repr of all info @@ -616,14 +655,16 @@ def __repr__(self): "os_release_file={self.os_release_file!r}, " \ "distro_release_file={self.distro_release_file!r}, " \ "include_lsb={self.include_lsb!r}, " \ + "include_uname={self.include_uname!r}, " \ "_os_release_info={self._os_release_info!r}, " \ "_lsb_release_info={self._lsb_release_info!r}, " \ - "_distro_release_info={self._distro_release_info!r})".format( + "_distro_release_info={self._distro_release_info!r}, " \ + "_uname_info={self._uname_info!r})".format( self=self) def linux_distribution(self, full_distribution_name=True): """ - Return information about the Linux distribution that is compatible + Return information about the OS distribution that is compatible with Python's :func:`platform.linux_distribution`, supporting a subset of its parameters. @@ -636,7 +677,7 @@ def linux_distribution(self, full_distribution_name=True): ) def id(self): - """Return the distro ID of the Linux distribution, as a string. + """Return the distro ID of the OS distribution, as a string. For details, see :func:`distro.id`. """ @@ -656,22 +697,28 @@ def normalize(distro_id, table): if distro_id: return normalize(distro_id, NORMALIZED_DISTRO_ID) + distro_id = self.uname_attr('id') + if distro_id: + return normalize(distro_id, NORMALIZED_DISTRO_ID) + return '' def name(self, pretty=False): """ - Return the name of the Linux distribution, as a string. + Return the name of the OS distribution, as a string. For details, see :func:`distro.name`. """ name = self.os_release_attr('name') \ or self.lsb_release_attr('distributor_id') \ - or self.distro_release_attr('name') + or self.distro_release_attr('name') \ + or self.uname_attr('name') if pretty: name = self.os_release_attr('pretty_name') \ or self.lsb_release_attr('description') if not name: - name = self.distro_release_attr('name') + name = self.distro_release_attr('name') \ + or self.uname_attr('name') version = self.version(pretty=True) if version: name = name + ' ' + version @@ -679,7 +726,7 @@ def name(self, pretty=False): def version(self, pretty=False, best=False): """ - Return the version of the Linux distribution, as a string. + Return the version of the OS distribution, as a string. For details, see :func:`distro.version`. """ @@ -690,7 +737,8 @@ def version(self, pretty=False, best=False): self._parse_distro_release_content( self.os_release_attr('pretty_name')).get('version_id', ''), self._parse_distro_release_content( - self.lsb_release_attr('description')).get('version_id', '') + self.lsb_release_attr('description')).get('version_id', ''), + self.uname_attr('release') ] version = '' if best: @@ -712,7 +760,7 @@ def version(self, pretty=False, best=False): def version_parts(self, best=False): """ - Return the version of the Linux distribution, as a tuple of version + Return the version of the OS distribution, as a tuple of version numbers. For details, see :func:`distro.version_parts`. @@ -736,7 +784,7 @@ def major_version(self, best=False): def minor_version(self, best=False): """ - Return the minor version number of the Linux distribution. + Return the minor version number of the current distribution. For details, see :func:`distro.minor_version`. """ @@ -744,7 +792,7 @@ def minor_version(self, best=False): def build_number(self, best=False): """ - Return the build number of the Linux distribution. + Return the build number of the current distribution. For details, see :func:`distro.build_number`. """ @@ -752,7 +800,7 @@ def build_number(self, best=False): def like(self): """ - Return the IDs of distributions that are like the Linux distribution. + Return the IDs of distributions that are like the OS distribution. For details, see :func:`distro.like`. """ @@ -760,7 +808,7 @@ def like(self): def codename(self): """ - Return the codename of the Linux distribution. + Return the codename of the OS distribution. For details, see :func:`distro.codename`. """ @@ -771,7 +819,7 @@ def codename(self): def info(self, pretty=False, best=False): """ - Return certain machine-readable information about the Linux + Return certain machine-readable information about the OS distribution. For details, see :func:`distro.info`. @@ -791,7 +839,7 @@ def info(self, pretty=False, best=False): def os_release_info(self): """ Return a dictionary containing key-value pairs for the information - items from the os-release file data source of the Linux distribution. + items from the os-release file data source of the OS distribution. For details, see :func:`distro.os_release_info`. """ @@ -800,7 +848,7 @@ def os_release_info(self): def lsb_release_info(self): """ Return a dictionary containing key-value pairs for the information - items from the lsb_release command data source of the Linux + items from the lsb_release command data source of the OS distribution. For details, see :func:`distro.lsb_release_info`. @@ -810,17 +858,25 @@ def lsb_release_info(self): def distro_release_info(self): """ Return a dictionary containing key-value pairs for the information - items from the distro release file data source of the Linux + items from the distro release file data source of the OS distribution. For details, see :func:`distro.distro_release_info`. """ return self._distro_release_info + def uname_info(self): + """ + Return a dictionary containing key-value pairs for the information + items from the uname command data source of the OS distribution. + + For details, see :func:`distro.uname_info`. + """ + def os_release_attr(self, attribute): """ Return a single named information item from the os-release file data - source of the Linux distribution. + source of the OS distribution. For details, see :func:`distro.os_release_attr`. """ @@ -829,7 +885,7 @@ def os_release_attr(self, attribute): def lsb_release_attr(self, attribute): """ Return a single named information item from the lsb_release command - output data source of the Linux distribution. + output data source of the OS distribution. For details, see :func:`distro.lsb_release_attr`. """ @@ -838,12 +894,21 @@ def lsb_release_attr(self, attribute): def distro_release_attr(self, attribute): """ Return a single named information item from the distro release file - data source of the Linux distribution. + data source of the OS distribution. For details, see :func:`distro.distro_release_attr`. """ return self._distro_release_info.get(attribute, '') + def uname_attr(self, attribute): + """ + Return a single named information item from the uname command + output data source of the OS distribution. + + For details, see :func:`distro.uname_release_attr`. + """ + return self._uname_info.get(attribute, '') + @cached_property def _os_release_info(self): """ @@ -960,6 +1025,34 @@ def _parse_lsb_release_content(lines): props.update({k.replace(' ', '_').lower(): v.strip()}) return props + @cached_property + def _uname_info(self): + with open(os.devnull, 'w') as devnull: + try: + cmd = ('uname', '-rs') + stdout = subprocess.check_output(cmd, stderr=devnull) + except OSError: + return {} + content = stdout.decode(sys.getfilesystemencoding()).splitlines() + return self._parse_uname_content(content) + + @staticmethod + def _parse_uname_content(lines): + props = {} + match = re.search(r'^([^\s]+)\s+([\d\.]+)', lines[0].strip()) + if match: + name, version = match.groups() + + # This is to prevent the Linux kernel version from + # appearing as the 'best' version on otherwise + # identifiable distributions. + if name == 'Linux': + return {} + props['id'] = name.lower() + props['name'] = name + props['release'] = version + return props + @cached_property def _distro_release_info(self): """ @@ -1082,7 +1175,7 @@ def main(): logger.setLevel(logging.DEBUG) logger.addHandler(logging.StreamHandler(sys.stdout)) - parser = argparse.ArgumentParser(description="Linux distro info tool") + parser = argparse.ArgumentParser(description="OS distro info tool") parser.add_argument( '--json', '-j', diff --git a/docs/index.rst b/docs/index.rst index e9b6253..72d0fe4 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -21,7 +21,7 @@ Compatibility ============= The ``distro`` package is supported on Python 2.7, 3.4+ and PyPy, and on -any Linux distribution that provides one or more of the `data sources`_ +any Linux or *BSD distribution that provides one or more of the `data sources`_ used by this package. This package is tested on Python 2.7, 3.4+ and PyPy, with test data that @@ -54,6 +54,8 @@ namely from these data sources: * The `distro release file`_, if present. +* The uname command output, if present. + Access to the information ========================= diff --git a/setup.py b/setup.py index c140bc3..d39ec92 100644 --- a/setup.py +++ b/setup.py @@ -36,7 +36,7 @@ def read(*parts): author_email='nir36g@gmail.com', license='Apache License, Version 2.0', platforms='All', - description='Linux Distribution - a Linux OS platform information API', + description='Distro - an OS platform information API', long_description=read('README.rst'), py_modules=['distro'], entry_points={ @@ -50,6 +50,10 @@ def read(*parts): 'Intended Audience :: System Administrators', 'License :: OSI Approved :: Apache Software License', 'Operating System :: POSIX :: Linux', + 'Operating System :: POSIX :: BSD', + 'Operating System :: POSIX :: BSD :: FreeBSD', + 'Operating System :: POSIX :: BSD :: NetBSD', + 'Operating System :: POSIX :: BSD :: OpenBSD', 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', diff --git a/tests/resources/distros/freebsd111/bin/uname b/tests/resources/distros/freebsd111/bin/uname new file mode 100755 index 0000000..96854bd --- /dev/null +++ b/tests/resources/distros/freebsd111/bin/uname @@ -0,0 +1,4 @@ +#!/bin/sh + +echo "FreeBSD 11.1-RELEASE" + diff --git a/tests/resources/distros/netbsd711/bin/uname b/tests/resources/distros/netbsd711/bin/uname new file mode 100755 index 0000000..a7d60f5 --- /dev/null +++ b/tests/resources/distros/netbsd711/bin/uname @@ -0,0 +1,4 @@ +#!/bin/sh + +echo "NetBSD 7.1.1" + diff --git a/tests/resources/distros/openbsd62/bin/uname b/tests/resources/distros/openbsd62/bin/uname new file mode 100755 index 0000000..9440121 --- /dev/null +++ b/tests/resources/distros/openbsd62/bin/uname @@ -0,0 +1,4 @@ +#!/bin/sh + +echo "OpenBSD 6.2" + diff --git a/tests/resources/testdistros/distro/baduname/bin/uname b/tests/resources/testdistros/distro/baduname/bin/uname new file mode 100644 index 0000000..7ac9df1 --- /dev/null +++ b/tests/resources/testdistros/distro/baduname/bin/uname @@ -0,0 +1,2 @@ +#!/bin/sh +echo "I'm a bad uname file!" diff --git a/tests/test_distro.py b/tests/test_distro.py index ab33de6..0bfdbd2 100644 --- a/tests/test_distro.py +++ b/tests/test_distro.py @@ -450,6 +450,36 @@ def test_manjaro1512_lsb_release(self): # } # self._test_outcome(desired_outcome) + def test_openbsd62_uname(self): + self._test_outcome({ + 'id': 'openbsd', + 'name': 'OpenBSD', + 'version': '6.2', + 'pretty_name': 'OpenBSD 6.2', + 'pretty_version': '6.2', + 'best_version': '6.2' + }) + + def test_netbsd711_uname(self): + self._test_outcome({ + 'id': 'netbsd', + 'name': 'NetBSD', + 'version': '7.1.1', + 'pretty_name': 'NetBSD 7.1.1', + 'pretty_version': '7.1.1', + 'best_version': '7.1.1' + }) + + def test_freebsd111_uname(self): + self._test_outcome({ + 'id': 'freebsd', + 'name': 'FreeBSD', + 'version': '11.1', + 'pretty_name': 'FreeBSD 11.1', + 'pretty_version': '11.1', + 'best_version': '11.1' + }) + def test_ubuntu14normal_lsb_release(self): self._setup_for_distro(os.path.join(TESTDISTROS, 'lsb', 'ubuntu14_normal')) @@ -554,6 +584,15 @@ def test_unknowndistro_release(self): 'minor_version': '0' } self._test_outcome(desired_outcome) + + def test_bad_uname(self): + self._setup_for_distro(os.path.join(TESTDISTROS, 'distro', + 'baduname')) + self.distro = distro.LinuxDistribution() + + assert self.distro.uname_attr('id') == '' + assert self.distro.uname_attr('name') == '' + assert self.distro.uname_attr('release') == '' @pytest.mark.skipif(not IS_LINUX, reason='Irrelevant on non-linux') @@ -1874,6 +1913,7 @@ def _test_consistency(function, kwargs=None): _test_consistency('os_release_info') _test_consistency('lsb_release_info') _test_consistency('distro_release_info') + _test_consistency('uname_info') os_release_keys = [ 'name', @@ -1904,6 +1944,14 @@ def _test_consistency(function, kwargs=None): ] for key in distro_release_keys: _test_consistency('distro_release_attr', {'attribute': key}) + + uname_keys = [ + 'id', + 'name', + 'release' + ] + for key in uname_keys: + _test_consistency('uname_attr', {'attribute': key}) @pytest.mark.skipif(not IS_LINUX, reason='Irrelevant on non-linux')