Files
archey4/archey/entries/distro.py
HorlogeSkynet f9e98a281b [CORE] Prevents inconsistent PATH from breaking subprocess executions
It appears "broken" `PATH` environment variable (e.g. ending with a
file instead of a directory) may lead to `NotADirectoryError` Python
exception. Let's get bulletproof by extending most of subprocess calls
expected exceptions to parent `OSError` (or at least by actually
catching `NotADirectoryError` explicitly).

> closes #164

Co-Authored-By: Michael Bromilow <developer@bromilow.uk>
2025-03-02 20:05:06 +01:00

55 lines
1.7 KiB
Python

"""Distribution and architecture detection class"""
import platform
from subprocess import check_output
from typing import Optional
from archey.distributions import Distributions
from archey.entry import Entry
class Distro(Entry):
"""Uses `distro` and `platform` modules to retrieve distribution and architecture information"""
_ICON = "\uf17c" # fa_linux
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if platform.system() == "Darwin":
distro_name = self._fetch_darwin_release()
else:
distro_name = Distributions.get_distro_name() or self._fetch_android_release()
self.value = {"name": distro_name, "arch": platform.machine()}
@staticmethod
def _fetch_android_release() -> Optional[str]:
"""Simple method to fetch current release on Android systems"""
try:
release = check_output(
["getprop", "ro.build.version.release"], universal_newlines=True
).rstrip()
except OSError:
return None
return f"Android {release}"
@staticmethod
def _fetch_darwin_release() -> Optional[str]:
"""Simple method to fetch current release on Darwin systems"""
# For macOS, let's mimic Python's `platform.platform` internal behavior here.
macos_release = platform.mac_ver()[0]
if macos_release:
return f"macOS {macos_release}"
return f"Darwin {platform.release()}"
def output(self, output) -> None:
output.append(
self.name,
f"{{}} {self.value['arch']}".format(
self.value["name"] or self._default_strings.get("not_detected")
),
)