]> git.ipfire.org Git - pakfire.git/commitdiff
UI: Move confirm callback into the CLI client
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 9 Nov 2022 10:01:34 +0000 (10:01 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 9 Nov 2022 10:01:34 +0000 (10:01 +0000)
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 <michael.tremer@ipfire.org>
src/_pakfire/pakfire.c
src/libpakfire/include/pakfire/transaction.h
src/libpakfire/pakfire.c
src/libpakfire/transaction.c
src/scripts/pakfire.in

index 286678988b13e2dd156e932cb7ef77718aa2f0dd..ff66f1ef6105e1d48f976812580e3d96a8f4280b 100644 (file)
@@ -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);
 
index 9ffb8cea97c8ef83c59cdc7916ee4e1d3e7544c5..14236ee0fa9f673ed4ec4082d0b60f69c1d95de0 100644 (file)
@@ -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);
index 92809d0b9f75a13823d93399451921397b86877d..9410bd3d3095a4b985cab75e0cf26e17c7ce60a2 100644 (file)
@@ -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);
index 73a61e41da60f3eedd8cd1be5649f98b4342de5f..b396a9ef6da9d2cd5f281bcea256f614f5ad5eb8 100644 (file)
@@ -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
index 1412cc1fb8e506c86e04cc33cdaa56b84f8e307b..08de8840a59e2a2ad29ff41efac9201176f60e7c 100644 (file)
@@ -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