# Copyright Kevin Deldycke <kevin@deldycke.com> and contributors.## This program is Free Software; you can redistribute it and/or# modify it under the terms of the GNU General Public License# as published by the Free Software Foundation; either version 2# of the License, or (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."""Heuristics to detect platforms.This collection of heuristics is designed as a set of separate function with minimallogic and dependencies.All these heuristics can be hard-cached as the underlying system is not changingbetween code execution. They are still allowed to depends on each others, as long asyou're careful of not implementing circular dependencies... warning:: Even if highly unlikely, it is possible to have multiple platforms detected for the same environment. Typical example is `Ubuntu WSL <https://documentation.ubuntu.com/wsl/>`_, which will make both the ``is_wsl2()`` and ``is_ubuntu()`` functions return ``True`` at the same time. That's because of the environment metadata, where: .. code-block:: shell-session $ uname -a Linux 5.15.167.4-microsoft-standard-WSL2 $ cat /etc/os-release PRETTY_NAME="Ubuntu 22.04.5 LTS" That way we have the possibility elsewhere in ``extra-platforms`` to either decide if we only allow one, and only one, heuristic to match the current system, or allow for considering multiple systems at the same time.Detection of Linux distribution rely on `distro<https://github.com/python-distro/distro>`_ to gather as much details as possible.And also because it is the recommended replacement for Python's original``platform.linux_distribution`` function (which was removed in Python 3.8).For all other platforms, we either rely on:- `sys.platform <https://docs.python.org/3/library/sys.html#sys.platform>`_- `platform.platform <https://docs.python.org/3/library/platform.html#platform.platform>`_- `platform.release <https://docs.python.org/3/library/platform.html#platform.release>`_- environment variables.. seealso:: Other source of inspiration for platform detection: - `Rust's sysinfo crate <https://github.com/stanislav-tkach/os_info/tree/master/os_info/src>`_."""from__future__importannotationsimportloggingimportplatformimportsysfromfunctoolsimportcachefromosimportenvironfrom.import_report_msgimportdistro
[docs]@cachedefis_aix()->bool:"""Return `True` if current platform is AIX."""returnsys.platform.startswith("aix")ordistro.id()=="aix"
[docs]@cachedefis_altlinux()->bool:"""Return `True` if current platform is ALT Linux."""returndistro.id()=="altlinux"
[docs]@cachedefis_amzn()->bool:"""Return `True` if current platform is Amazon Linux."""returndistro.id()=="amzn"
[docs]@cachedefis_android()->bool:"""Return `True` if current platform is Android. Source: https://github.com/kivy/kivy/blob/master/kivy/utils.py#L429 """return"ANDROID_ROOT"inenvironor"P4A_BOOTSTRAP"inenviron
[docs]@cachedefis_arch()->bool:"""Return `True` if current platform is Arch Linux."""returndistro.id()=="arch"
[docs]@cachedefis_buildroot()->bool:"""Return `True` if current platform is Buildroot."""returndistro.id()=="buildroot"
[docs]@cachedefis_centos()->bool:"""Return `True` if current platform is CentOS."""returndistro.id()=="centos"
[docs]@cachedefis_cloudlinux()->bool:"""Return `True` if current platform is CloudLinux OS."""returndistro.id()=="cloudlinux"
[docs]@cachedefis_cygwin()->bool:"""Return `True` if current platform is Cygwin."""returnsys.platform.startswith("cygwin")
[docs]@cachedefis_debian()->bool:"""Return `True` if current platform is Debian."""returndistro.id()=="debian"
[docs]@cachedefis_exherbo()->bool:"""Return `True` if current platform is Exherbo Linux."""returndistro.id()=="exherbo"
[docs]@cachedefis_fedora()->bool:"""Return `True` if current platform is Fedora."""returndistro.id()=="fedora"
[docs]@cachedefis_freebsd()->bool:"""Return `True` if current platform is FreeBSD."""returnsys.platform.startswith("freebsd")ordistro.id()=="freebsd"
[docs]@cachedefis_gentoo()->bool:"""Return `True` if current platform is GenToo Linux."""returndistro.id()=="gentoo"
[docs]@cachedefis_guix()->bool:"""Return `True` if current platform is Guix System."""returndistro.id()=="guix"
[docs]@cachedefis_hurd()->bool:"""Return `True` if current platform is GNU/Hurd."""returnsys.platform.startswith("GNU")
[docs]@cachedefis_ibm_powerkvm()->bool:"""Return `True` if current platform is IBM PowerKVM."""returndistro.id()=="ibm_powerkvm"
[docs]@cachedefis_kvmibm()->bool:"""Return `True` if current platform is KVM for IBM z Systems."""returndistro.id()=="kvmibm"
[docs]@cachedefis_linuxmint()->bool:"""Return `True` if current platform is Linux Mint."""returndistro.id()=="linuxmint"
[docs]@cachedefis_macos()->bool:"""Return `True` if current platform is macOS."""returnplatform.platform(terse=True).startswith(("macOS","Darwin"))
[docs]@cachedefis_mageia()->bool:"""Return `True` if current platform is Mageia."""returndistro.id()=="mageia"
[docs]@cachedefis_mandriva()->bool:"""Return `True` if current platform is Mandriva Linux."""returndistro.id()=="mandriva"
[docs]@cachedefis_midnightbsd()->bool:"""Return `True` if current platform is MidnightBSD."""returnsys.platform.startswith("midnightbsd")ordistro.id()=="midnightbsd"
[docs]@cachedefis_netbsd()->bool:"""Return `True` if current platform is NetBSD."""returnsys.platform.startswith("netbsd")ordistro.id()=="netbsd"
[docs]@cachedefis_nobara()->bool:"""Return `True` if current platform is Nobara Linux."""returndistro.id()=="nobara"
[docs]@cachedefis_openbsd()->bool:"""Return `True` if current platform is OpenBSD."""returnsys.platform.startswith("openbsd")ordistro.id()=="openbsd"
[docs]@cachedefis_opensuse()->bool:"""Return `True` if current platform is openSUSE."""returndistro.id()=="opensuse"
[docs]@cachedefis_oracle()->bool:"""Return `True` if current platform is Oracle Linux (and Oracle Enterprise Linux)."""returndistro.id()=="oracle"
[docs]@cachedefis_parallels()->bool:"""Return `True` if current platform is Parallels."""returndistro.id()=="parallels"
[docs]@cachedefis_pidora()->bool:"""Return `True` if current platform is Pidora."""returndistro.id()=="pidora"
[docs]@cachedefis_raspbian()->bool:"""Return `True` if current platform is Raspbian."""returndistro.id()=="raspbian"
[docs]@cachedefis_rhel()->bool:"""Return `True` if current platform is RedHat Enterprise Linux."""returndistro.id()=="rhel"
[docs]@cachedefis_rocky()->bool:"""Return `True` if current platform is Rocky Linux."""returndistro.id()=="rocky"
[docs]@cachedefis_scientific()->bool:"""Return `True` if current platform is Scientific Linux."""returndistro.id()=="scientific"
[docs]@cachedefis_slackware()->bool:"""Return `True` if current platform is Slackware."""returndistro.id()=="slackware"
[docs]@cachedefis_sles()->bool:"""Return `True` if current platform is SUSE Linux Enterprise Server."""returndistro.id()=="sles"
[docs]@cachedefis_solaris()->bool:"""Return `True` if current platform is Solaris."""returnplatform.platform(aliased=True,terse=True).startswith("Solaris")
[docs]@cachedefis_sunos()->bool:"""Return `True` if current platform is SunOS."""returnplatform.platform(aliased=True,terse=True).startswith("SunOS")
[docs]@cachedefis_tumbleweed()->bool:"""Return `True` if current platform is openSUSE Tumbleweed."""returndistro.id()=="opensuse-tumbleweed"
[docs]@cachedefis_tuxedo()->bool:"""Return `True` if current platform is Tuxedo OS."""returndistro.id()=="tuxedo"
[docs]@cachedefis_ubuntu()->bool:"""Return `True` if current platform is Ubuntu."""returndistro.id()=="ubuntu"
[docs]@cachedefis_unknown_linux()->bool:"""Return `True` if current platform is an unknown Linux."""unknown_linux=sys.platform.startswith("linux")andnot(is_altlinux()oris_amzn()oris_android()oris_arch()oris_buildroot()oris_centos()oris_cloudlinux()oris_debian()oris_exherbo()oris_fedora()oris_gentoo()oris_guix()oris_ibm_powerkvm()oris_kvmibm()oris_linuxmint()oris_mageia()oris_mandriva()oris_nobara()oris_opensuse()oris_oracle()oris_parallels()oris_pidora()oris_raspbian()oris_rhel()oris_rocky()oris_scientific()oris_slackware()oris_sles()oris_tumbleweed()oris_tuxedo()oris_ubuntu()oris_xenserver())ifunknown_linux:logging.warning(f"Unknow Linux detected: {distro.info()!r}. {_report_msg}")returnunknown_linux
[docs]@cachedefis_windows()->bool:"""Return `True` if current platform is Windows."""returnsys.platform.startswith("win32")
[docs]@cachedefis_wsl1()->bool:"""Return `True` if current platform is running over Windows Subsystem for Linux v1. .. caution:: The only difference between WSL1 and WSL2 is `the case of the kernel release version <https://github.com/andweeb/presence.nvim/pull/64#issue-1174430662>`_: - WSL 1: .. code-block:: shell-session $ uname -r 4.4.0-22572-Microsoft - WSL 2: .. code-block:: shell-session $ uname -r 5.10.102.1-microsoft-standard-WSL2 """return"Microsoft"inplatform.release()
[docs]@cachedefis_wsl2()->bool:"""Return `True` if current platform is running over Windows Subsystem for Linux v2."""return"microsoft"inplatform.release()
[docs]@cachedefis_xenserver()->bool:"""Return `True` if current platform is XenServer."""returndistro.id()=="xenserver"