From: Michael Tremer Date: Fri, 2 Jun 2017 17:02:32 +0000 (+0200) Subject: cli: Move exception handling into Cli base class X-Git-Tag: 0.9.28~1285^2~1336 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=17563cd015efc73b0705fd509f9f938f7f7e33fd;p=pakfire.git cli: Move exception handling into Cli base class Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/cli.py b/src/pakfire/cli.py index f4e8de59d..635d0d0bb 100644 --- a/src/pakfire/cli.py +++ b/src/pakfire/cli.py @@ -21,8 +21,10 @@ import argparse import datetime +import logging import os import shutil +import signal import sys import tempfile import time @@ -208,7 +210,23 @@ class Cli(object): args = self.parse_cli() assert args.func, "Argument function not defined" - return args.func(args) + try: + return args.func(args) + + except KeyboardInterrupt: + self.ui.message(_("Received keyboard interupt (Ctrl-C). Exiting."), + level=logging.CRITICAL) + + return 128 + signal.SIGINT + + # Catch all errors and show a user-friendly error message. + except Error as e: + self.ui.message(_("An error has occured when running Pakfire"), level=logging.CRITICAL) + + self.ui.message(_("%s: %s") % (e.__class__.__name__, e.message), + level=logging.ERROR) + + return e.exit_code def handle_info(self, ns): with self.pakfire(ns) as p: diff --git a/src/scripts/pakfire b/src/scripts/pakfire index aed9e5ad8..d582e975e 100755 --- a/src/scripts/pakfire +++ b/src/scripts/pakfire @@ -1,20 +1,32 @@ #!/usr/bin/python3 +############################################################################### +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2017 Pakfire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### import os import sys -import logging -log = logging.getLogger("pakfire") - +# Catch ImportError and show a more user-friendly message about what went wrong try: - from pakfire.cli import * - from pakfire.i18n import _ + import pakfire.cli except ImportError as e: - # Catch ImportError and show a more user-friendly message about what - # went wrong. - raise - # Try to load at least the i18n support, but when this fails as well we can # go with an English error message. try: @@ -22,7 +34,6 @@ except ImportError as e: except ImportError: _ = lambda x: x - # XXX Maybe we can make a more beautiful message here?! print(_("There has been an error when trying to import one or more of the" " modules, that are required to run Pakfire.")) print(_("Please check your installation of Pakfire.")) @@ -35,13 +46,13 @@ except ImportError as e: sys.exit(1) basename2cls = { - "pakfire" : Cli, - "pakfire-builder" : CliBuilder, - "pakfire-client" : CliClient, - "pakfire-daemon" : CliDaemon, - "pakfire-key" : CliKey, - "pakfire-server" : CliServer, - "builder" : CliBuilderIntern, + "pakfire" : pakfire.cli.Cli, + "pakfire-builder" : pakfire.cli.CliBuilder, + "pakfire-client" : pakfire.cli.CliClient, + "pakfire-daemon" : pakfire.cli.CliDaemon, + "pakfire-key" : pakfire.cli.CliKey, + "pakfire-server" : pakfire.cli.CliServer, + "builder" : pakfire.cli.CliBuilderIntern, } # Get the basename of the program @@ -52,39 +63,7 @@ basename = os.path.basename(sys.argv[0]) if basename not in basename2cls: sys.exit(127) -# Return code for the shell. -ret = 0 - -try: - # Creating command line interface - cli = basename2cls[basename]() - - cli.run() - -except KeyboardInterrupt: - log.critical("Recieved keyboard interupt (Ctrl-C). Exiting.") - ret = 1 - -# Catch all errors and show a user-friendly error message. -except Error as e: - log.critical("") - log.critical(_("An error has occured when running Pakfire.")) - log.error("") - - log.error(_("Error message:")) - log.error(" %s: %s" % (e.__class__.__name__, e.message)) - log.error("") - - # Log the traceback when in debugging mode. - log.debug("", exc_info=True) - - log.error(_("Further description:")) - msg = "%s" % e - for line in msg.splitlines(): - log.error(" %s" % line) - log.error("") - - ret = e.exit_code +cli = basename2cls[basename]() +ret = cli.run() sys.exit(ret) -