From 4393ca21fc4b05f4a4bb1f6568698381b85fed6c Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Mon, 23 Mar 2026 09:11:15 +0100 Subject: [PATCH] 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 --- src/python/jw/pkg/App.py | 42 ++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/src/python/jw/pkg/App.py b/src/python/jw/pkg/App.py index 5241b066..f4a0c633 100644 --- a/src/python/jw/pkg/App.py +++ b/src/python/jw/pkg/App.py @@ -300,15 +300,31 @@ class App(Base): @property def os_release(self) -> str: if self.__os_release is None: - result = self.call_async( - self.exec_context.run( - ['/usr/bin/cat', '/etc/os-release'], - throw=True, - cmd_input=InputMode.NonInteractive + release_file = '/etc/os-release' + try: + result = self.call_async( + self.exec_context.run( + ['/usr/bin/cat', release_file], + throw=True, + cmd_input=InputMode.NonInteractive + ) ) - ) - assert result.status == 0 - self.__os_release = result.decode().stdout.strip() + self.__os_release = result.decode().stdout.strip() + except Exception as e: + 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 def os_release_field(self, key: str, throw: bool=False) -> str: @@ -422,32 +438,38 @@ class App(Base): def __append(entry: str): if not entry in ret: ret.append(entry) - import platform if self.__os_cascade is None: - ret = [ 'os', platform.system().lower() ] + ret = [ 'os' ] match self.distro_id: case 'centos': + __append('linux') __append('pkg-rpm') __append('pm-yum') __append('redhat') __append('rhel') case 'fedora' | 'rhel': + __append('linux') __append('pkg-rpm') __append('pm-yum') __append('redhat') case 'suse': + __append('linux') __append('pkg-rpm') __append('pm-zypper') case 'kali' | 'raspbian': + __append('linux') __append('pkg-debian') __append('pm-apt') __append('debian') case 'ubuntu': + __append('linux') __append('pkg-debian') __append('pm-apt') case 'archlinux': + __append('linux') __append('pkg-pm') __append('pm-pacman') + os = self.__opt_os if self.__opt_os is not None else self.get_os() name = re.sub(r'-.*', '', os) series = os