Files
command-assistant/edison/utils/os_utils.py
T
2025-04-09 09:34:15 +02:00

60 lines
2.0 KiB
Python

"""
Operating system utilities for the Edison application.
"""
import os
import platform
import subprocess
import distro
def get_os_friendly_name():
"""
Returns a friendly name of the user's operating system.
The function retrieves the current system platform name using the `platform.system()` function.
For Linux, it appends the distribution name retrieved from `distro.name(pretty=True)` to give a
more descriptive representation. For Darwin (Apple's macOS), it appends "macOS" to "Darwin" to
make the output clearer to the user.
Returns:
str: A friendly name for the user's operating system. It will be one of the following:
- "Linux/<distribution name>"
- "Darwin/macOS"
- The system string returned by `platform.system()` if it's not Linux or Darwin.
"""
os_name = platform.system()
if os_name == "Linux":
os_name = "Linux/" + distro.name(pretty=True)
elif os_name == "Darwin":
os_name = "Darwin/macOS"
return os_name
def missing_posix_display():
"""
Checks if the DISPLAY environment variable is set in a POSIX-compliant shell.
This function runs a shell subprocess that outputs the value of the DISPLAY environment
variable. It then checks if this value is unset (i.e., equals a newline 'b'\\n'') in the
current shell environment. If the DISPLAY variable is unset, the function returns `True`
indicating a "missing" display; otherwise, it returns `False`.
Returns:
bool: `True` if the DISPLAY environment variable is unset or empty, `False` otherwise.
"""
if os.name != "posix":
return False
display = subprocess.check_output("echo $DISPLAY", shell=True)
return display == b'\n'
def get_default_shell():
"""
Get the default shell for the current operating system.
Returns:
str: The default shell.
"""
# Unix based SHELL (/bin/bash, /bin/zsh), otherwise assuming it's Windows
return os.environ.get("SHELL", "powershell.exe")