-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathwsl.py
More file actions
39 lines (30 loc) · 915 Bytes
/
Copy pathwsl.py
File metadata and controls
39 lines (30 loc) · 915 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os
import shutil
import subprocess
import functools
import typing as T
from pathlib import Path
Pathlike = T.Union[str, Path]
@functools.lru_cache()
def wsl_available() -> bool:
"""
heuristic to detect if Windows Subsystem for Linux is available.
Uses presence of /etc/os-release in the WSL image to say Linux is there.
This is a de facto file standard across Linux distros.
"""
has_wsl = False
if os.name == "nt" and shutil.which("wsl"):
has_wsl = wsl_file_exist("/etc/os-release")
return has_wsl
def wsl_file_exist(file: Pathlike) -> bool:
"""
path is specified as if in WSL
NOT //wsl$/Ubuntu/etc/os-release
but /etc/os-release
"""
if os.name != "nt":
return False
try:
return subprocess.run(["wsl", "test", "-f", str(file)], timeout=10).returncode == 0
except subprocess.TimeoutExpired:
return False