Skip to content

Commit

Permalink
Update system.py
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-migas committed Jan 3, 2025
1 parent 9f5fa95 commit daf60b1
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/koyo/system.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""System utilities."""

import contextlib
import inspect
import os
import platform
import subprocess
import sys

IS_WIN = sys.platform == "win32"
Expand Down Expand Up @@ -112,3 +114,59 @@ def who_called_me_stack() -> None:
function_name = frame_info.function
line_number = frame_info.lineno
print(f"Caller #{i}: File '{file_name}', Function '{function_name}', Line {line_number}")


def _linux_sys_name() -> str:
"""
Try to discover linux system name base on /etc/os-release file or lsb_release command output
https://www.freedesktop.org/software/systemd/man/os-release.html.
"""
os_release_path = "/etc/os-release"

if os.path.exists(os_release_path):
with open(os_release_path) as f_p:
data = {}
for line in f_p:
field, value = line.split("=")
data[field.strip()] = value.strip().strip('"')
if "PRETTY_NAME" in data:
return data["PRETTY_NAME"]
if "NAME" in data:
if "VERSION" in data:
return f'{data["NAME"]} {data["VERSION"]}'
if "VERSION_ID" in data:
return f'{data["NAME"]} {data["VERSION_ID"]}'
return f'{data["NAME"]} (no version)'
return _linux_sys_name_lsb_release()


def _linux_sys_name_lsb_release() -> str:
"""Try to discover linux system name base on lsb_release command output."""
with contextlib.suppress(subprocess.CalledProcessError):
res = subprocess.run(["lsb_release", "-d", "-r"], check=True, capture_output=True)
text = res.stdout.decode()
data = {}
for line in text.split("\n"):
key, val = line.split(":")
data[key.strip()] = val.strip()
version_str = data["Description"]
if not version_str.endswith(data["Release"]):
version_str += " " + data["Release"]
return version_str
return ""


def _sys_name() -> str:
"""Discover MacOS or Linux Human readable information. For Linux provide information about distribution."""
with contextlib.suppress(Exception):
if sys.platform == "linux":
return _linux_sys_name()
if sys.platform == "darwin":
with contextlib.suppress(subprocess.CalledProcessError):
res = subprocess.run(
["sw_vers", "-productVersion"],
check=True,
capture_output=True,
)
return f"MacOS {res.stdout.decode().strip()}"
return ""

0 comments on commit daf60b1

Please sign in to comment.