feat(core): implement slice 1 tracer bullet end-to-end skeleton
This commit is contained in:
1 parent
0aed5bb003
commit
2602b66a90
13 files changed
+882
-1
No files matched your search
@@ -0,0 +1,66 @@
|
||||
"""Linux distribution and operating system detector."""
|
||||
|
||||
import os
|
||||
import platform
|
||||
from typing import Dict, Optional
|
||||
from linux_backuper.models import OsInfo
|
||||
|
||||
DEFAULT_OS_RELEASE_PATH = "/etc/os-release"
|
||||
FALLBACK_OS_RELEASE_PATH = "/usr/lib/os-release"
|
||||
|
||||
def parse_os_release(content: str) -> Dict[str, str]:
|
||||
"""Parse key-value pairs from os-release format string."""
|
||||
data = {}
|
||||
for line in content.splitlines():
|
||||
line = line.strip()
|
||||
if not line or line.startswith("#"):
|
||||
continue
|
||||
if "=" in line:
|
||||
key, val = line.split("=", 1)
|
||||
key = key.strip().lower()
|
||||
val = val.strip().strip("\"'")
|
||||
data[key] = val
|
||||
|
||||
distro_id = data.get("id", "linux").lower()
|
||||
distro_name = data.get("name", "Linux")
|
||||
version_id = data.get("version_id", data.get("build_id", "rolling"))
|
||||
|
||||
return {
|
||||
"id": distro_id,
|
||||
"name": distro_name,
|
||||
"version_id": version_id,
|
||||
"id_like": data.get("id_like", "")
|
||||
}
|
||||
|
||||
def detect_distro(os_release_path: Optional[str] = None) -> OsInfo:
|
||||
"""Detect current operating system distribution and kernel release."""
|
||||
content = ""
|
||||
candidate_paths = [os_release_path] if os_release_path else [DEFAULT_OS_RELEASE_PATH, FALLBACK_OS_RELEASE_PATH]
|
||||
|
||||
for path in candidate_paths:
|
||||
if path and os.path.isfile(path):
|
||||
try:
|
||||
with open(path, "r", encoding="utf-8", errors="replace") as f:
|
||||
content = f.read()
|
||||
break
|
||||
except (OSError, IOError):
|
||||
pass
|
||||
|
||||
if content:
|
||||
parsed = parse_os_release(content)
|
||||
distro_id = parsed["id"]
|
||||
distro_name = parsed["name"]
|
||||
version_id = parsed["version_id"]
|
||||
else:
|
||||
# Fallback for non-Linux or testing environments
|
||||
distro_id = platform.system().lower()
|
||||
distro_name = platform.system()
|
||||
version_id = platform.release()
|
||||
|
||||
return OsInfo(
|
||||
distro_id=distro_id,
|
||||
distro_name=distro_name,
|
||||
version_id=version_id,
|
||||
kernel=platform.release(),
|
||||
arch=platform.machine()
|
||||
)
|
||||
Reference in new issue
Block a user