Skip to content

Commit

Permalink
Add detection of WSL1 and WSL2.
Browse files Browse the repository at this point in the history
  • Loading branch information
kdeldycke committed Dec 28, 2022
1 parent 28eeec4 commit d185669
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
36 changes: 35 additions & 1 deletion click_extra/platform.py
Expand Up @@ -32,6 +32,7 @@

from boltons.dictutils import FrozenDict


AIX = "aix"
""" Constant used to identify distributions of the AIX family. """

Expand Down Expand Up @@ -65,6 +66,12 @@
WINDOWS = "windows"
""" Constant used to identify distributions of the Windows family. """

WSL1 = "wsl1"
""" Constant used to identify Windows Subsystem for Linux v1. """

WSL2 = "wsl2"
""" Constant used to identify Windows Subsystem for Linux v2. """


def is_aix():
"""Return `True` only if current platform is of the AIX family."""
Expand Down Expand Up @@ -121,6 +128,31 @@ def is_windows():
return sys.platform.startswith("win32")


def is_wsl1():
"""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" in platform.release()


def is_wsl2():
"""Return `True` only if current platform is Windows Subsystem for Linux v2."""
return "microsoft" in platform.release()


OS_DEFINITIONS = FrozenDict(
{
AIX: ("IBM AIX", is_aix()),
Expand All @@ -134,6 +166,8 @@ def is_windows():
SOLARIS: ("Oracle Solaris", is_solaris()),
SUNOS: ("SunOS", is_sunos()),
WINDOWS: ("Windows", is_windows()),
WSL1: ("Windows Subsystem for Linux v1", is_wsl1()),
WSL2: ("Windows Subsystem for Linux v2", is_wsl2()),
}
)
"""Map OS IDs to evaluation function and OS labels."""
Expand Down Expand Up @@ -177,7 +211,7 @@ def is_windows():
`according Wikipedia <https://en.wikipedia.org/wiki/Template:Unix>`_.
"""

ANY_UNIX_COMPATIBILITY_LAYER = frozenset({CYGWIN})
ANY_UNIX_COMPATIBILITY_LAYER = frozenset({CYGWIN, WSL1, WSL2})
""" IDs of interfaces that allows UNIX binaries to run on a different host system.
.. note::
Expand Down
10 changes: 9 additions & 1 deletion click_extra/tests/test_platform.py
Expand Up @@ -46,6 +46,8 @@
is_solaris,
is_sunos,
is_windows,
is_wsl1,
is_wsl2,
os_label,
)
from .conftest import (
Expand All @@ -71,6 +73,8 @@ def test_mutual_exclusion():
assert not is_solaris()
assert not is_sunos()
assert not is_windows()
assert not is_wsl1()
assert not is_wsl2()
assert CURRENT_OS_ID == LINUX
assert CURRENT_OS_LABEL == os_label(LINUX)
if is_macos():
Expand All @@ -84,6 +88,8 @@ def test_mutual_exclusion():
assert not is_solaris()
assert not is_sunos()
assert not is_windows()
assert not is_wsl1()
assert not is_wsl2()
assert CURRENT_OS_ID == MACOS
assert CURRENT_OS_LABEL == os_label(MACOS)
if is_windows():
Expand All @@ -97,6 +103,8 @@ def test_mutual_exclusion():
assert not is_openbsd()
assert not is_solaris()
assert not is_sunos()
assert not is_wsl1()
assert not is_wsl2()
assert CURRENT_OS_ID == WINDOWS
assert CURRENT_OS_LABEL == os_label(WINDOWS)

Expand Down Expand Up @@ -137,7 +145,7 @@ def test_os_definitions():
assert isinstance(os_id, str)
assert os_id
assert os_id.isascii()
assert os_id.isalpha()
assert os_id.isalnum()
assert os_id.islower()
# Metadata.
assert isinstance(data, tuple)
Expand Down

0 comments on commit d185669

Please sign in to comment.