From 60845a36d0fafa31611d15ccb08210caebc65aff Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sun, 31 Jul 2011 13:38:38 +0200 Subject: [PATCH] Show more user friendly error messages on Errors. --- pakfire/base.py | 4 ++ pakfire/cli.py | 5 ++ pakfire/errors.py | 13 +++-- pakfire/logger.py | 11 +++-- po/pakfire.pot | 120 ++++++++++++++++++++++++---------------------- scripts/pakfire | 43 ++++++++++++++++- 6 files changed, 131 insertions(+), 65 deletions(-) diff --git a/pakfire/base.py b/pakfire/base.py index e9544771e..40b37e16d 100644 --- a/pakfire/base.py +++ b/pakfire/base.py @@ -65,6 +65,10 @@ class Pakfire(object): self.repos = repository.Repositories(self, enable_repos=enable_repos, disable_repos=disable_repos) + def __del__(self): + # Reset logging. + logger.setup_logging() + def create_solver(self): return satsolver.Solver(self, self.pool) diff --git a/pakfire/cli.py b/pakfire/cli.py index 0fc56e74e..dd3662889 100644 --- a/pakfire/cli.py +++ b/pakfire/cli.py @@ -3,6 +3,7 @@ import argparse import sys +import logger import packages import repository import server @@ -12,6 +13,10 @@ import pakfire.api as pakfire from constants import * from i18n import _ +# Initialize a very simple logging that is removed when a Pakfire instance +# is started. +logger.setup_logging() + class Cli(object): def __init__(self): self.parser = argparse.ArgumentParser( diff --git a/pakfire/errors.py b/pakfire/errors.py index 1a8409fc2..183e8e0ee 100644 --- a/pakfire/errors.py +++ b/pakfire/errors.py @@ -1,8 +1,13 @@ #!/usr/bin/python +from i18n import _ + class Error(Exception): - pass - + exit_code = 1 + + message = _("An unhandled error occured.") + + class ActionError(Error): pass @@ -19,7 +24,9 @@ class ConfigError(Error): pass class DependencyError(Error): - pass + exit_code = 4 + + message = _("One or more dependencies could not been resolved.") class DownloadError(Error): pass diff --git a/pakfire/logger.py b/pakfire/logger.py index 1c4eb8e2e..bf29e6cda 100644 --- a/pakfire/logger.py +++ b/pakfire/logger.py @@ -3,7 +3,7 @@ import logging import time -def setup_logging(config): +def setup_logging(config=None): """ This function initialized the logger that is enabled immediately. """ @@ -25,7 +25,7 @@ def setup_logging(config): # we are running in debugging mode. handler = logging.StreamHandler() - if config.get("debug"): + if config and config.get("debug"): handler.setLevel(logging.DEBUG) else: handler.setLevel(logging.INFO) @@ -33,9 +33,10 @@ def setup_logging(config): l.addHandler(handler) # The configuration file always logs all messages. - handler = logging.FileHandler(config.get("logfile")) - handler.setLevel(logging.DEBUG) - l.addHandler(handler) + if config: + handler = logging.FileHandler(config.get("logfile")) + handler.setLevel(logging.DEBUG) + l.addHandler(handler) class BuildFormatter(logging.Formatter): diff --git a/po/pakfire.pot b/po/pakfire.pot index 7515ab7dc..88c92272f 100644 --- a/po/pakfire.pot +++ b/po/pakfire.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2011-07-30 23:11+0200\n" +"POT-Creation-Date: 2011-07-31 13:37+0200\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -38,226 +38,234 @@ msgstr "" msgid "Extracting: %s (source)" msgstr "" -#: ../pakfire/cli.py:18 +#: ../pakfire/cli.py:23 msgid "Pakfire command line interface." msgstr "" -#: ../pakfire/cli.py:25 +#: ../pakfire/cli.py:30 msgid "The path where pakfire should operate in." msgstr "" -#: ../pakfire/cli.py:76 +#: ../pakfire/cli.py:81 msgid "Enable verbose output." msgstr "" -#: ../pakfire/cli.py:79 +#: ../pakfire/cli.py:84 msgid "Path to a configuration file to load." msgstr "" -#: ../pakfire/cli.py:82 +#: ../pakfire/cli.py:87 msgid "Disable a repository temporarily." msgstr "" -#: ../pakfire/cli.py:85 +#: ../pakfire/cli.py:90 msgid "Enable a repository temporarily." msgstr "" -#: ../pakfire/cli.py:90 +#: ../pakfire/cli.py:95 msgid "Install one or more packages to the system." msgstr "" -#: ../pakfire/cli.py:92 +#: ../pakfire/cli.py:97 msgid "Give name of at least one package to install." msgstr "" -#: ../pakfire/cli.py:98 +#: ../pakfire/cli.py:103 msgid "Install one or more packages from the filesystem." msgstr "" -#: ../pakfire/cli.py:100 +#: ../pakfire/cli.py:105 msgid "Give filename of at least one package." msgstr "" -#: ../pakfire/cli.py:106 +#: ../pakfire/cli.py:111 msgid "Remove one or more packages from the system." msgstr "" -#: ../pakfire/cli.py:108 +#: ../pakfire/cli.py:113 msgid "Give name of at least one package to remove." msgstr "" -#: ../pakfire/cli.py:114 +#: ../pakfire/cli.py:119 msgid "Update the whole system or one specific package." msgstr "" -#: ../pakfire/cli.py:116 +#: ../pakfire/cli.py:121 msgid "Give a name of a package to update or leave emtpy for all." msgstr "" -#: ../pakfire/cli.py:122 +#: ../pakfire/cli.py:127 msgid "Print some information about the given package(s)." msgstr "" -#: ../pakfire/cli.py:124 +#: ../pakfire/cli.py:129 msgid "Give at least the name of one package." msgstr "" -#: ../pakfire/cli.py:130 +#: ../pakfire/cli.py:135 msgid "Search for a given pattern." msgstr "" -#: ../pakfire/cli.py:132 +#: ../pakfire/cli.py:137 msgid "A pattern to search for." msgstr "" -#: ../pakfire/cli.py:138 +#: ../pakfire/cli.py:143 msgid "Get a list of packages that provide a given file or feature." msgstr "" -#: ../pakfire/cli.py:140 +#: ../pakfire/cli.py:145 msgid "File or feature to search for." msgstr "" -#: ../pakfire/cli.py:146 +#: ../pakfire/cli.py:151 msgid "Get list of packages that belong to the given group." msgstr "" -#: ../pakfire/cli.py:148 +#: ../pakfire/cli.py:153 msgid "Group name to search for." msgstr "" -#: ../pakfire/cli.py:154 +#: ../pakfire/cli.py:159 msgid "Install all packages that belong to the given group." msgstr "" -#: ../pakfire/cli.py:156 +#: ../pakfire/cli.py:161 msgid "Group name." msgstr "" -#: ../pakfire/cli.py:162 +#: ../pakfire/cli.py:167 msgid "List all currently enabled repositories." msgstr "" -#: ../pakfire/cli.py:166 +#: ../pakfire/cli.py:171 msgid "Cleanup commands." msgstr "" -#: ../pakfire/cli.py:174 +#: ../pakfire/cli.py:179 msgid "Cleanup all temporary files." msgstr "" -#: ../pakfire/cli.py:234 ../pakfire/transaction.py:160 +#: ../pakfire/cli.py:239 ../pakfire/transaction.py:160 msgid "Repository" msgstr "" -#: ../pakfire/cli.py:234 +#: ../pakfire/cli.py:239 msgid "Enabled" msgstr "" -#: ../pakfire/cli.py:234 +#: ../pakfire/cli.py:239 msgid "Priority" msgstr "" -#: ../pakfire/cli.py:234 +#: ../pakfire/cli.py:239 msgid "Packages" msgstr "" -#: ../pakfire/cli.py:246 +#: ../pakfire/cli.py:251 msgid "Cleaning up everything..." msgstr "" -#: ../pakfire/cli.py:254 +#: ../pakfire/cli.py:259 msgid "Pakfire builder command line interface." msgstr "" -#: ../pakfire/cli.py:304 +#: ../pakfire/cli.py:309 msgid "Update the package indexes." msgstr "" -#: ../pakfire/cli.py:310 +#: ../pakfire/cli.py:315 msgid "Build one or more packages." msgstr "" -#: ../pakfire/cli.py:312 +#: ../pakfire/cli.py:317 msgid "Give name of at least one package to build." msgstr "" -#: ../pakfire/cli.py:316 +#: ../pakfire/cli.py:321 msgid "Build the package for the given architecture." msgstr "" -#: ../pakfire/cli.py:318 ../pakfire/cli.py:344 +#: ../pakfire/cli.py:323 ../pakfire/cli.py:349 msgid "Path were the output files should be copied to." msgstr "" -#: ../pakfire/cli.py:320 ../pakfire/cli.py:333 +#: ../pakfire/cli.py:325 ../pakfire/cli.py:338 msgid "Mode to run in. Is either 'release' or 'development' (default)." msgstr "" -#: ../pakfire/cli.py:325 +#: ../pakfire/cli.py:330 msgid "Go into a shell." msgstr "" -#: ../pakfire/cli.py:327 +#: ../pakfire/cli.py:332 msgid "Give name of a package." msgstr "" -#: ../pakfire/cli.py:331 +#: ../pakfire/cli.py:336 msgid "Emulated architecture in the shell." msgstr "" -#: ../pakfire/cli.py:338 +#: ../pakfire/cli.py:343 msgid "Generate a source package." msgstr "" -#: ../pakfire/cli.py:340 +#: ../pakfire/cli.py:345 msgid "Give name(s) of a package(s)." msgstr "" -#: ../pakfire/cli.py:416 +#: ../pakfire/cli.py:421 msgid "Pakfire repo command line interface." msgstr "" -#: ../pakfire/cli.py:441 +#: ../pakfire/cli.py:446 msgid "Repository management commands." msgstr "" -#: ../pakfire/cli.py:449 +#: ../pakfire/cli.py:454 msgid "Create a new repository index." msgstr "" -#: ../pakfire/cli.py:450 +#: ../pakfire/cli.py:455 msgid "Path to the packages." msgstr "" -#: ../pakfire/cli.py:451 +#: ../pakfire/cli.py:456 msgid "Path to input packages." msgstr "" -#: ../pakfire/cli.py:463 +#: ../pakfire/cli.py:468 msgid "Pakfire master command line interface." msgstr "" -#: ../pakfire/cli.py:491 +#: ../pakfire/cli.py:496 msgid "Update the sources." msgstr "" -#: ../pakfire/cli.py:501 +#: ../pakfire/cli.py:506 msgid "Pakfire server command line interface." msgstr "" -#: ../pakfire/cli.py:533 +#: ../pakfire/cli.py:538 msgid "Request a build job from the server." msgstr "" -#: ../pakfire/cli.py:539 +#: ../pakfire/cli.py:544 msgid "Send a keepalive to the server." msgstr "" -#: ../pakfire/cli.py:546 +#: ../pakfire/cli.py:551 msgid "Update all repositories." msgstr "" +#: ../pakfire/errors.py:8 +msgid "An unhandled error occured." +msgstr "" + +#: ../pakfire/errors.py:29 +msgid "One or more dependencies could not been resolved." +msgstr "" + #: ../pakfire/packages/base.py:72 msgid "Name" msgstr "" diff --git a/scripts/pakfire b/scripts/pakfire index 05b2b6e46..e9431b969 100755 --- a/scripts/pakfire +++ b/scripts/pakfire @@ -4,7 +4,32 @@ import logging import os import sys -from pakfire.cli import * +try: + from pakfire.cli import * + from pakfire.i18n import _ + +except ImportError, e: + # Catch ImportError and show a more user-friendly message about what + # went wrong. + + # Try to load at least the i18n support, but when this fails as well we can + # go with an English error message. + try: + from pakfire.i18n import _ + 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 your installation of Pakfire.") + print + print _("The error that lead to this:") + print " ", e + print + + # Exit immediately. + sys.exit(1) basename2cls = { "pakfire" : Cli, @@ -34,5 +59,21 @@ except KeyboardInterrupt: logging.critical("Recieved keyboard interupt (Ctrl-C). Exiting.") ret = 1 +# Catch all errors and show a user-friendly error message. +except Error, e: + logging.critical("") + logging.critical(_("An error has occured when running Pakfire.")) + logging.error("") + + logging.error(_("Error message:")) + logging.error(" %s: %s" % (e.__class__.__name__, e.message)) + logging.error("") + + logging.error(_("Further description:")) + logging.error(" %s" % e) + logging.error("") + + ret = e.exit_code + sys.exit(ret) -- 2.39.5