App.os_cascade: Don't use platform.system()

Python's platform.system() outputs 'Linux', and to use it is
tempting. Sadly, that's wrong, because it reflects the host's idea of
the target system, not the execution context's, so replace it with
straight 'linux' if the distro is known, or, failing that, the output
of uname -s.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-03-23 09:11:15 +01:00
commit 4393ca21fc

View file

@ -300,15 +300,31 @@ class App(Base):
@property @property
def os_release(self) -> str: def os_release(self) -> str:
if self.__os_release is None: if self.__os_release is None:
result = self.call_async( release_file = '/etc/os-release'
self.exec_context.run( try:
['/usr/bin/cat', '/etc/os-release'], result = self.call_async(
throw=True, self.exec_context.run(
cmd_input=InputMode.NonInteractive ['/usr/bin/cat', release_file],
throw=True,
cmd_input=InputMode.NonInteractive
)
) )
) self.__os_release = result.decode().stdout.strip()
assert result.status == 0 except Exception as e:
self.__os_release = result.decode().stdout.strip() result = self.call_async(
self.exec_context.run(
['uname', '-s'],
throw=False,
cmd_input=InputMode.NonInteractive
)
)
if result.status != 0:
log(ERR, f'/etc/os-release and uname both failed, the latter with exit status {result.status}')
raise
log(INFO, f'Failed to read {release_file} ({str(e)}), falling back to uname')
uname = result.decode().stdout.strip().lower()
self.__os_release = f'ID={uname}\nVERSION_CODENAME=unknown'
return self.__os_release return self.__os_release
def os_release_field(self, key: str, throw: bool=False) -> str: def os_release_field(self, key: str, throw: bool=False) -> str:
@ -422,32 +438,38 @@ class App(Base):
def __append(entry: str): def __append(entry: str):
if not entry in ret: if not entry in ret:
ret.append(entry) ret.append(entry)
import platform
if self.__os_cascade is None: if self.__os_cascade is None:
ret = [ 'os', platform.system().lower() ] ret = [ 'os' ]
match self.distro_id: match self.distro_id:
case 'centos': case 'centos':
__append('linux')
__append('pkg-rpm') __append('pkg-rpm')
__append('pm-yum') __append('pm-yum')
__append('redhat') __append('redhat')
__append('rhel') __append('rhel')
case 'fedora' | 'rhel': case 'fedora' | 'rhel':
__append('linux')
__append('pkg-rpm') __append('pkg-rpm')
__append('pm-yum') __append('pm-yum')
__append('redhat') __append('redhat')
case 'suse': case 'suse':
__append('linux')
__append('pkg-rpm') __append('pkg-rpm')
__append('pm-zypper') __append('pm-zypper')
case 'kali' | 'raspbian': case 'kali' | 'raspbian':
__append('linux')
__append('pkg-debian') __append('pkg-debian')
__append('pm-apt') __append('pm-apt')
__append('debian') __append('debian')
case 'ubuntu': case 'ubuntu':
__append('linux')
__append('pkg-debian') __append('pkg-debian')
__append('pm-apt') __append('pm-apt')
case 'archlinux': case 'archlinux':
__append('linux')
__append('pkg-pm') __append('pkg-pm')
__append('pm-pacman') __append('pm-pacman')
os = self.__opt_os if self.__opt_os is not None else self.get_os() os = self.__opt_os if self.__opt_os is not None else self.get_os()
name = re.sub(r'-.*', '', os) name = re.sub(r'-.*', '', os)
series = os series = os