From: Michael Tremer Date: Wed, 9 Nov 2022 10:01:34 +0000 (+0000) Subject: UI: Move confirm callback into the CLI client X-Git-Tag: 0.9.28~114 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=142d34c4f8003d1887f56d3acd3236633ee3e760;p=pakfire.git UI: Move confirm callback into the CLI client This way, we do not have to know whether we are running in some sort of interactive mode, but simply call the callback, or continue if no callback has been configured. Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/pakfire.c b/src/_pakfire/pakfire.c index 286678988..ff66f1ef6 100644 --- a/src/_pakfire/pakfire.c +++ b/src/_pakfire/pakfire.c @@ -136,10 +136,20 @@ static int Pakfire_confirm_callback(struct pakfire* pakfire, void* data, PyObject* result = PyObject_CallObject(callback, args); - // Extract return code - if (PyLong_Check(result)) - r = PyLong_AsLong(result); + // If the callback raised an exception, we will ignore it and indicate + // that an error happened, but we cannot re-raise the exception + if (!result) { + r = -1; + goto ERROR; + } + // Set the return code + if (result == Py_True) + r = 0; + else + r = 1; + +ERROR: Py_DECREF(args); Py_DECREF(result); diff --git a/src/libpakfire/include/pakfire/transaction.h b/src/libpakfire/include/pakfire/transaction.h index 9ffb8cea9..14236ee0f 100644 --- a/src/libpakfire/include/pakfire/transaction.h +++ b/src/libpakfire/include/pakfire/transaction.h @@ -25,7 +25,6 @@ struct pakfire_transaction; enum pakfire_transaction_flags { PAKFIRE_TRANSACTION_DRY_RUN = (1 << 0), - PAKFIRE_TRANSACTION_INTERACTIVE = (1 << 1), }; struct pakfire_transaction* pakfire_transaction_ref(struct pakfire_transaction* transaction); diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index 92809d0b9..9410bd3d3 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -1544,12 +1544,6 @@ int pakfire_confirm(struct pakfire* pakfire, const char* message, const char* qu if (!pakfire->callbacks.confirm) return 0; - // Skip this, if running in non-interactive mode -#if 0 - if (!pakfire_has_flag(pakfire, PAKFIRE_FLAGS_INTERACTIVE)) - return 0; -#endif - // Run callback return pakfire->callbacks.confirm( pakfire, pakfire->callbacks.confirm_data, message, question); diff --git a/src/libpakfire/transaction.c b/src/libpakfire/transaction.c index 73a61e41d..b396a9ef6 100644 --- a/src/libpakfire/transaction.c +++ b/src/libpakfire/transaction.c @@ -1262,12 +1262,10 @@ PAKFIRE_EXPORT int pakfire_transaction_run( char* dump = pakfire_transaction_dump(transaction, 80); // Check if we should continue - if (flags & PAKFIRE_TRANSACTION_INTERACTIVE) { - r = pakfire_confirm(transaction->pakfire, dump, _("Is this okay? [y/N]")); - if (r) { - ERROR(transaction->pakfire, "Transaction aborted upon user request\n"); - goto ERROR; - } + r = pakfire_confirm(transaction->pakfire, dump, _("Is this okay?")); + if (r) { + ERROR(transaction->pakfire, "Transaction aborted upon user request\n"); + goto ERROR; } // Write transaction dump to log diff --git a/src/scripts/pakfire.in b/src/scripts/pakfire.in index 1412cc1fb..08de8840a 100644 --- a/src/scripts/pakfire.in +++ b/src/scripts/pakfire.in @@ -261,6 +261,9 @@ class Cli(object): path=args.root, offline=args.offline, logger=logger.log, + + # Callbacks + confirm_callback=self._confirm_callback, ) # Disable repositories @@ -297,6 +300,25 @@ class Cli(object): # Return with exit code sys.exit(r or 0) + def _confirm_callback(self, message, question): + """ + This is called when Pakfire needs a confirmation from the user + """ + if message: + print(message) + + while True: + # Ask the question and wait for an answer + answer = input("%s [y/N] " % question) + + # Positive response + if answer in ("y", "Y"): + return True + + # Negative response + elif answer in ("", "n", "N"): + return False + def _check(self, p, args): """ Checks if this installation is properly intact