@cached()
def get_system_info() -> SystemInfo:
"""Gather comprehensive system information.
Returns:
SystemInfo object with all available system details
"""
# Basic platform info
os_name = get_os_name()
arch = get_arch_name()
platform_str = get_platform_string()
os_version = get_os_version()
cpu_type = get_cpu_type()
# Python info
python_version = platform.python_version()
# System info
hostname = None
with contextlib.suppress(Exception):
hostname = platform.node()
# User info
username = os.environ.get("USER") or os.environ.get("USERNAME")
home_dir = str(Path("~").expanduser())
# Use secure temp directory - prefer environment variables over Foundation's temp dir
from provide.foundation.file.temp import system_temp_dir
temp_dir = os.environ.get("TMPDIR") or os.environ.get("TEMP") or str(system_temp_dir())
# CPU info
num_cpus = None
with contextlib.suppress(Exception):
num_cpus = os.cpu_count()
# Memory info (requires psutil for accurate values)
total_memory = None
available_memory = None
try:
import psutil
mem = psutil.virtual_memory()
total_memory = mem.total
available_memory = mem.available
except ImportError:
log.debug(
"psutil not available, memory info unavailable",
hint="Install with: pip install provide-foundation[platform]",
)
except Exception as e:
log.debug("Failed to get memory info", error=str(e))
# Disk usage
disk_usage = None
try:
disk_usage = {}
for path in ["/", home_dir, temp_dir]:
if Path(path).exists():
usage = shutil.disk_usage(path)
disk_usage[path] = {
"total": usage.total,
"used": usage.used,
"free": usage.free,
}
except Exception as e:
log.debug("Failed to get disk usage", error=str(e))
info = SystemInfo(
os_name=os_name,
arch=arch,
platform=platform_str,
os_version=os_version,
cpu_type=cpu_type,
python_version=python_version,
hostname=hostname,
username=username,
home_dir=home_dir,
temp_dir=temp_dir,
num_cpus=num_cpus,
total_memory=total_memory,
available_memory=available_memory,
disk_usage=disk_usage,
)
log.debug(
"System information gathered",
platform=platform_str,
os=os_name,
arch=arch,
python=python_version,
cpus=num_cpus,
)
return info