You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.0 KiB
Python

"""
host module represents our host: the machine on which the installer script is
running. On WSL, that means we're on Linux
"""
import ctypes
import os
import platform
import pathlib
import shutil
from functools import cached_property
4 months ago
class Host:
"""
hacking the python module system a little to make the module look like a
singleton object so that it can have properties
"""
4 months ago
@cached_property
def is_wsl(self):
"""
true if we're running Linux on WSL
"""
4 months ago
return "WSL2" in platform.platform()
@cached_property
def is_linux(self):
"""
true if we're on linux (including WSL), false otherwise
"""
4 months ago
return platform.system() == "Linux"
@cached_property
def is_windows(self):
"""
true if we're on Windows (and running Python from Windows)
"""
4 months ago
return platform.system() == "Windows"
@cached_property
def is_darwin(self):
"""
true if we're on MacOS
"""
4 months ago
return platform.system() == "Darwin"
@cached_property
def is_admin(self):
"""
tells us whether the running user has admin powers or not
"""
try:
return os.getuid() == 0
except AttributeError:
4 months ago
return ctypes.windll.shell32.IsUserAnAdmin() != 0 # type: ignore
@property
def dotfiles_root(self):
"""
directory containing our preferences repo
"""
here = pathlib.Path(os.path.realpath(__file__))
return here.parent.parent
@property
def home(self):
"""
the home directory
"""
return pathlib.Path.home()
4 months ago
def has(self, cmd: str):
"""
whether or not the machine has the specified command
"""
4 months ago
if self.is_darwin and cmd.endswith(".app"):
app_dir = pathlib.Path("/Applications")
app_path = app_dir / cmd
if app_path.exists():
return True
return shutil.which(cmd) is not None
4 months ago
4 months ago
host = Host()