[modules/arch-update] Gracefully handle exit code 2

fixes #624
This commit is contained in:
tobi-wan-kenobi 2020-05-11 15:10:20 +02:00
parent f2aa006130
commit 3fdd2d2be6
2 changed files with 17 additions and 12 deletions

View file

@ -33,13 +33,16 @@ class Module(core.module.Module):
return self.__packages == 0 and not self.__error return self.__packages == 0 and not self.__error
def update(self): def update(self):
try:
result = util.cli.execute("checkupdates")
self.__packages = len(result.split("\n")) - 1
self.__error = False self.__error = False
except Exception as e: code, result = util.cli.execute("checkupdates", ignore_errors=True, return_exitcode=True)
logging.exception(e)
if code == 0:
self.__packages = len(result.split("\n"))
elif code == 2:
self.__packages = 0
else:
self.__error = True self.__error = True
log.error("checkupdates exited with {}: {}".format(code, result))
def state(self, widget): def state(self, widget):
if self.__error: if self.__error:

View file

@ -4,7 +4,7 @@ import subprocess
import logging import logging
def execute(cmd, wait=True, ignore_errors=False, include_stderr=False, env=None): def execute(cmd, wait=True, ignore_errors=False, include_stderr=False, env=None, return_exitcode=False):
"""Executes a commandline utility and returns its output """Executes a commandline utility and returns its output
:param cmd: the command (as string) to execute, returns the program's output :param cmd: the command (as string) to execute, returns the program's output
@ -12,11 +12,12 @@ def execute(cmd, wait=True, ignore_errors=False, include_stderr=False, env=None)
:param ignore_errors: set to True to return a string when an exception is thrown, otherwise might throw, defaults to False :param ignore_errors: set to True to return a string when an exception is thrown, otherwise might throw, defaults to False
:param include_stderr: set to True to include stderr output in the return value, defaults to False :param include_stderr: set to True to include stderr output in the return value, defaults to False
:param env: provide a dict here to specify a custom execution environment, defaults to None :param env: provide a dict here to specify a custom execution environment, defaults to None
:param return_exitcode: set to True to return a pair, where the first member is the exit code and the message the second, defaults to False
:raises RuntimeError: the command either didn't exist or didn't exit cleanly, and ignore_errors was set to False :raises RuntimeError: the command either didn't exist or didn't exit cleanly, and ignore_errors was set to False
:return: output of cmd, or stderr, if ignore_errors is True and the command failed :return: output of cmd, or stderr, if ignore_errors is True and the command failed; or a tuple of exitcode and the previous, if return_exitcode is set to True
:rtype: string :rtype: string or tuple (if return_exitcode is set to True)
""" """
args = shlex.split(cmd) args = shlex.split(cmd)
logging.debug(cmd) logging.debug(cmd)
@ -35,10 +36,11 @@ def execute(cmd, wait=True, ignore_errors=False, include_stderr=False, env=None)
if proc.returncode != 0: if proc.returncode != 0:
err = "{} exited with code {}".format(cmd, proc.returncode) err = "{} exited with code {}".format(cmd, proc.returncode)
if ignore_errors: if ignore_errors:
return err return (proc.returncode, err) if return_exitcode else err
raise RuntimeError(err) raise RuntimeError(err)
return out.decode("utf-8") res = out.decode("utf-8")
return "" return (proc.returncode, res) if return_exitcode else res
return (0, "") if return_exitcode else ""
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4