# 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 each platform.All these heuristics can be hard-cached as the underlying system is not changingbetween code execution.We mostly rely on `distro <https://github.com/python-distro/distro>`_ to detect theLinux distribution, as it is the recommended replacement for Python's original``platform.linux_distribution`` function (which was removed in Python 3.8).We then fill in the gaps with ``sys.platform`` and environment variables to detectsystems not covered by ``distro``.This collection of heuristics is designed as a set of separate function with minimallogic and dependencies. That way we can double-check during execution that noheuristics are conflicting or matching multiple systems at the same time... note:: Heuristics for unrecognized platforms can be transplanted from `Rust's sysinfo crate <https://github.com/stanislav-tkach/os_info/tree/master/os_info/src>`_."""from__future__importannotationsimportloggingimportplatformimportsysfromosimportenvironimportdistrofrom.importcache
[docs]@cachedefis_aix()->bool:"""Return `True` only if current platform is AIX."""returnsys.platform.startswith("aix")ordistro.id()=="aix"
[docs]@cachedefis_altlinux()->bool:"""Return `True` only if current platform is ALT Linux."""returndistro.id()=="altlinux"
[docs]@cachedefis_amzn()->bool:"""Return `True` only if current platform is Amazon Linux."""returndistro.id()=="amzn"
[docs]@cachedefis_android()->bool:"""Return `True` only 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` only if current platform is Arch Linux."""returndistro.id()=="arch"
[docs]@cachedefis_buildroot()->bool:"""Return `True` only if current platform is Buildroot."""returndistro.id()=="buildroot"
[docs]@cachedefis_centos()->bool:"""Return `True` only if current platform is CentOS."""returndistro.id()=="centos"
[docs]@cachedefis_cloudlinux()->bool:"""Return `True` only if current platform is CloudLinux OS."""returndistro.id()=="cloudlinux"
[docs]@cachedefis_cygwin()->bool:"""Return `True` only if current platform is Cygwin."""returnsys.platform.startswith("cygwin")
[docs]@cachedefis_debian()->bool:"""Return `True` only if current platform is Debian."""returndistro.id()=="debian"
[docs]@cachedefis_exherbo()->bool:"""Return `True` only if current platform is Exherbo Linux."""returndistro.id()=="exherbo"
[docs]@cachedefis_fedora()->bool:"""Return `True` only if current platform is Fedora."""returndistro.id()=="fedora"
[docs]@cachedefis_freebsd()->bool:"""Return `True` only if current platform is FreeBSD."""returnsys.platform.startswith("freebsd")ordistro.id()=="freebsd"
[docs]@cachedefis_gentoo()->bool:"""Return `True` only if current platform is GenToo Linux."""returndistro.id()=="gentoo"
[docs]@cachedefis_guix()->bool:"""Return `True` only if current platform is Guix System."""returndistro.id()=="guix"
[docs]@cachedefis_hurd()->bool:"""Return `True` only if current platform is GNU/Hurd."""returnsys.platform.startswith("GNU")
[docs]@cachedefis_ibm_powerkvm()->bool:"""Return `True` only if current platform is IBM PowerKVM."""returndistro.id()=="ibm_powerkvm"
[docs]@cachedefis_kvmibm()->bool:"""Return `True` only if current platform is KVM for IBM z Systems."""returndistro.id()=="kvmibm"
[docs]@cachedefis_linuxmint()->bool:"""Return `True` only if current platform is Linux Mint."""returndistro.id()=="linuxmint"
[docs]@cachedefis_macos()->bool:"""Return `True` only if current platform is macOS."""returnplatform.platform(terse=True).startswith(("macOS","Darwin"))
[docs]@cachedefis_mageia()->bool:"""Return `True` only if current platform is Mageia."""returndistro.id()=="mageia"
[docs]@cachedefis_mandriva()->bool:"""Return `True` only if current platform is Mandriva Linux."""returndistro.id()=="mandriva"
[docs]@cachedefis_midnightbsd()->bool:"""Return `True` only if current platform is MidnightBSD."""returnsys.platform.startswith("midnightbsd")ordistro.id()=="midnightbsd"
[docs]@cachedefis_netbsd()->bool:"""Return `True` only if current platform is NetBSD."""returnsys.platform.startswith("netbsd")ordistro.id()=="netbsd"
[docs]@cachedefis_openbsd()->bool:"""Return `True` only if current platform is OpenBSD."""returnsys.platform.startswith("openbsd")ordistro.id()=="openbsd"
[docs]@cachedefis_opensuse()->bool:"""Return `True` only if current platform is openSUSE."""returndistro.id()=="opensuse"
[docs]@cachedefis_oracle()->bool:"""Return `True` only if current platform is Oracle Linux (and Oracle Enterprise Linux)."""returndistro.id()=="oracle"
[docs]@cachedefis_parallels()->bool:"""Return `True` only if current platform is Parallels."""returndistro.id()=="parallels"
[docs]@cachedefis_pidora()->bool:"""Return `True` only if current platform is Pidora."""returndistro.id()=="pidora"
[docs]@cachedefis_raspbian()->bool:"""Return `True` only if current platform is Raspbian."""returndistro.id()=="raspbian"
[docs]@cachedefis_rhel()->bool:"""Return `True` only if current platform is RedHat Enterprise Linux."""returndistro.id()=="rhel"
[docs]@cachedefis_rocky()->bool:"""Return `True` only if current platform is Rocky Linux."""returndistro.id()=="rocky"
[docs]@cachedefis_scientific()->bool:"""Return `True` only if current platform is Scientific Linux."""returndistro.id()=="scientific"
[docs]@cachedefis_slackware()->bool:"""Return `True` only if current platform is Slackware."""returndistro.id()=="slackware"
[docs]@cachedefis_sles()->bool:"""Return `True` only if current platform is SUSE Linux Enterprise Server."""returndistro.id()=="sles"
[docs]@cachedefis_solaris()->bool:"""Return `True` only if current platform is Solaris."""returnplatform.platform(aliased=True,terse=True).startswith("Solaris")
[docs]@cachedefis_sunos()->bool:"""Return `True` only if current platform is SunOS."""returnplatform.platform(aliased=True,terse=True).startswith("SunOS")
[docs]@cachedefis_tuxedo()->bool:"""Return `True` only if current platform is Tuxedo OS."""returndistro.id()=="tuxedo"
[docs]@cachedefis_ubuntu()->bool:"""Return `True` only if current platform is Ubuntu."""returndistro.id()=="ubuntu"
[docs]@cachedefis_unknown_linux()->bool:"""Return `True` only if current platform is an unknown Linux. Excludes WSL1 and WSL2 from this check to `avoid false positives <https://github.com/kdeldycke/meta-package-manager/issues/944>`_. """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_opensuse()oris_oracle()oris_parallels()oris_pidora()oris_raspbian()oris_rhel()oris_rocky()oris_scientific()oris_slackware()oris_sles()oris_tuxedo()oris_ubuntu()oris_xenserver()oris_wsl1()oris_wsl2())ifunknown_linux:logging.warning(f"Unknow Linux detected: {distro.info()!r}. You can report this warning to"" the extra-platforms project to improve detection heuristics.")returnunknown_linux
[docs]@cachedefis_windows()->bool:"""Return `True` only if current platform is Windows."""returnsys.platform.startswith("win32")
[docs]@cachedefis_wsl1()->bool:"""Return `True` only if current platform is 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` only if current platform is Windows Subsystem for Linux v2."""return"microsoft"inplatform.release()
[docs]@cachedefis_xenserver()->bool:"""Return `True` only if current platform is XenServer."""returndistro.id()=="xenserver"