]> git.ipfire.org Git - pakfire.git/commitdiff
ctx: Move the confirm callback into the context
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 16 Oct 2023 14:00:28 +0000 (14:00 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 16 Oct 2023 14:00:28 +0000 (14:00 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/pakfire.c
src/cli/lib/pakfire.c
src/cli/lib/terminal.c
src/cli/lib/terminal.h
src/cli/pakfire.c
src/libpakfire/ctx.c
src/libpakfire/include/pakfire/ctx.h
src/libpakfire/include/pakfire/pakfire.h
src/libpakfire/libpakfire.sym
src/libpakfire/pakfire.c
src/libpakfire/transaction.c

index 27df756101f5a2f532213760cf597f5c9771d510..c039251db556282690e649c2d18c54feb37992b8 100644 (file)
@@ -56,50 +56,6 @@ static PyObject* Pakfire_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
        return (PyObject *)self;
 }
 
-static int Pakfire_confirm_callback(struct pakfire* pakfire, void* data,
-               const char* message, const char* question) {
-       PyObject* callback = (PyObject*)data;
-       PyObject* result = NULL;
-       int r = 0;
-
-       // Do nothing if callback isn't set
-       if (!callback)
-               return 0;
-
-       // Acquire GIL
-       PyGILState_STATE state = PyGILState_Ensure();
-
-       PyObject* args = Py_BuildValue("(ss)", message, question);
-       if (!args) {
-               r = -1;
-               goto ERROR;
-       }
-
-       result = PyObject_CallObject(callback, args);
-
-       // 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);
-
-       // Release the GIL
-       PyGILState_Release(state);
-
-       return r;
-}
-
 static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) {
        char* kwlist[] = {
                "path",
@@ -107,7 +63,6 @@ static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) {
                "logger",
                "offline",
                "conf",
-               "confirm_callback",
                NULL,
        };
        const char* path = NULL;
@@ -118,16 +73,10 @@ static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) {
 
        FILE* fconf = NULL;
 
-       if (!PyArg_ParseTupleAndKeywords(args, kwds, "|zzpOO", kwlist,
-                       &path, &arch, &offline, &conf, &self->callbacks.confirm))
+       if (!PyArg_ParseTupleAndKeywords(args, kwds, "|zzpO", kwlist,
+                       &path, &arch, &offline, &conf))
                goto ERROR;
 
-       // Check if confirm callback is callable
-       if (self->callbacks.confirm && !PyCallable_Check(self->callbacks.confirm)) {
-               PyErr_SetString(PyExc_TypeError, "Confirm callback is not callable");
-               goto ERROR;
-       }
-
        // Map the configuration
        if (conf != Py_None) {
                fconf = PyObject_AsFileHandle(conf, "r");
@@ -166,14 +115,6 @@ static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) {
                goto ERROR;
     }
 
-       // Configure confirm callback
-       if (self->callbacks.confirm) {
-               pakfire_set_confirm_callback(self->pakfire,
-                       Pakfire_confirm_callback, self->callbacks.confirm);
-
-               Py_INCREF(self->callbacks.confirm);
-       }
-
 ERROR:
        if (fconf)
                fclose(fconf);
@@ -183,12 +124,6 @@ ERROR:
 
 static void Pakfire_dealloc(PakfireObject* self) {
        if (self->pakfire) {
-               // Reset confirm callback
-               if (self->callbacks.confirm) {
-                       pakfire_set_confirm_callback(self->pakfire, NULL, NULL);
-                       Py_DECREF(self->callbacks.confirm);
-               }
-
                Py_BEGIN_ALLOW_THREADS
 
                pakfire_unref(self->pakfire);
index 6d9b048a8ec095b02c7503fad9463d472c7a27f4..9582da5f1518f73bf24dea69b70283dd06c04e1e 100644 (file)
@@ -101,14 +101,7 @@ int cli_setup_pakfire(struct pakfire** pakfire, struct cli_config* config) {
        // Setup progress callback
        pakfire_set_setup_progress_callback(p, cli_setup_progressbar, NULL);
 
-       // If the user wants to answer yes to everything we only configure that
-       if (config->yes) {
-               pakfire_set_confirm_callback(p, cli_term_confirm_yes, NULL);
-
-       } else if (!pakfire_has_flag(p, PAKFIRE_FLAGS_BUILD)) {
-               // Configure confirm callback
-               pakfire_set_confirm_callback(p, cli_term_confirm, NULL);
-
+       if (!pakfire_has_flag(p, PAKFIRE_FLAGS_BUILD)) {
                // Configure pick solution callback
                pakfire_set_pick_solution_callback(p, cli_term_pick_solution, NULL);
 
index 45b78c35eb3f26371933e56fd22e766c69460d34..3822b80fd4edc374b1a0e0a4ff18026db03826c0 100644 (file)
@@ -85,7 +85,7 @@ int cli_term_is_interactive(void) {
        return isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) && isatty(STDERR_FILENO);
 }
 
-int cli_term_confirm(struct pakfire* pakfire,
+int cli_term_confirm(struct pakfire_ctx* ctx, struct pakfire* pakfire,
                void* data, const char* message, const char* question) {
        char* line = NULL;
        size_t length = 0;
@@ -140,7 +140,7 @@ END:
        return r;
 }
 
-int cli_term_confirm_yes(struct pakfire* pakfire,
+int cli_term_confirm_yes(struct pakfire_ctx* ctx, struct pakfire* pakfire,
                void* data, const char* message, const char* question) {
        return 0;
 }
index f48b778067e3517b0ba3579e06c346c32898ca66..88b62266731e929efda5c826f48a1bc710235067 100644 (file)
@@ -21,6 +21,7 @@
 #ifndef PAKFIRE_CLI_TERMINAL_H
 #define PAKFIRE_CLI_TERMINAL_H
 
+#include <pakfire/ctx.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/transaction.h>
 
@@ -28,9 +29,9 @@ int cli_term_get_dimensions(int* rows, int* cols);
 
 int cli_term_is_interactive(void);
 
-int cli_term_confirm(struct pakfire* pakfire,
+int cli_term_confirm(struct pakfire_ctx* ctx, struct pakfire* pakfire,
        void* data, const char* message, const char* question);
-int cli_term_confirm_yes(struct pakfire* pakfire,
+int cli_term_confirm_yes(struct pakfire_ctx* ctx, struct pakfire* pakfire,
        void* data, const char* message, const char* question);
 
 int cli_term_pick_solution(
index 7d0c41021d1fd0d7ec4ac5caecec4967d46b8bb1..6940ad5361583f8f4c16c9b78f3ec8c64aec7eae 100644 (file)
@@ -32,6 +32,7 @@
 #include "lib/requires.h"
 #include "lib/search.h"
 #include "lib/sync.h"
+#include "lib/terminal.h"
 #include "lib/update.h"
 #include "lib/version.h"
 
@@ -55,9 +56,10 @@ enum {
        OPT_DEBUG        = 2,
        OPT_OFFLINE      = 3,
        OPT_ROOT         = 4,
+       OPT_YES          = 5,
 
-       OPT_ENABLE_REPO  = 5,
-       OPT_DISABLE_REPO = 6,
+       OPT_ENABLE_REPO  = 6,
+       OPT_DISABLE_REPO = 7,
 };
 
 static struct argp_option options[] = {
@@ -67,6 +69,7 @@ static struct argp_option options[] = {
        { "root",         OPT_ROOT,         "PATH", 0, "The path to operate in", 0 },
        { "enable-repo",  OPT_ENABLE_REPO,  "REPO", 0, "Enable a repository", 0 },
        { "disable-repo", OPT_DISABLE_REPO, "REPO", 0, "Disable a repository", 0 },
+       { "yes",          OPT_YES,            NULL, 0, "Answer all questions with yes", 0 },
        { NULL },
 };
 
@@ -104,6 +107,10 @@ static error_t parse(int key, char* arg, void* data) {
                        config->root = arg;
                        break;
 
+               case OPT_YES:
+                       pakfire_ctx_set_confirm_callback(config->ctx, cli_term_confirm_yes, NULL);
+                       break;
+
                // Enable/Disable Repositories
 
                case OPT_ENABLE_REPO:
@@ -137,6 +144,9 @@ int main(int argc, char* argv[]) {
        if (r)
                goto ERROR;
 
+       // Make this all interactive
+       pakfire_ctx_set_confirm_callback(ctx, cli_term_confirm, NULL);
+
        struct cli_config config = {
                .ctx      = ctx,
                // XXX hard-coded path
index d22af95f715aff4a40b08c8d5e092c6a083f0861..84d511b111c1616ff87fcd4919f17437d25bec1f 100644 (file)
@@ -41,6 +41,12 @@ struct pakfire_ctx {
                pakfire_log_callback callback;
                void* data;
        } log;
+
+       // Confirm
+       struct pakfire_ctx_confirm {
+               pakfire_confirm_callback callback;
+               void* data;
+       } confirm;
 };
 
 static int parse_log_level(const char* level) {
@@ -83,6 +89,14 @@ static int pakfire_ctx_setup_logging(struct pakfire_ctx* ctx) {
        return 0;
 }
 
+static int pakfire_ctx_default_confirm_callback(struct pakfire_ctx* ctx,
+               struct pakfire* pakfire, void* data, const char* message, const char* question) {
+       // Just log the message
+       CTX_INFO(ctx, "%s\n", message);
+
+       return 0;
+}
+
 static void pakfire_ctx_free(struct pakfire_ctx* ctx) {
        free(ctx);
 }
@@ -104,6 +118,9 @@ PAKFIRE_EXPORT int pakfire_ctx_create(struct pakfire_ctx** ctx) {
        if (r)
                goto ERROR;
 
+       // Set the default confirm callback
+       pakfire_ctx_set_confirm_callback(c, pakfire_ctx_default_confirm_callback, NULL);
+
        // Return the pointer
        *ctx = c;
 
@@ -158,3 +175,20 @@ void pakfire_ctx_log(struct pakfire_ctx* ctx, int level, const char* file, int l
        ctx->log.callback(ctx->log.data, level, file, line, fn, format, args);
        va_end(args);
 }
+
+// Confirm
+
+PAKFIRE_EXPORT void pakfire_ctx_set_confirm_callback(struct pakfire_ctx* ctx,
+               pakfire_confirm_callback callback, void* data) {
+       ctx->confirm.callback = callback;
+       ctx->confirm.data     = data;
+}
+
+int pakfire_ctx_confirm(struct pakfire_ctx* ctx, struct pakfire* pakfire,
+               const char* message, const char* question) {
+       // Run callback
+       if (!ctx->confirm.callback)
+               return 0;
+
+       return ctx->confirm.callback(ctx, pakfire, ctx->confirm.data, message, question);
+}
index e5c4980f36efc2b4c900b3ebd91ee87a110544da..74dc33ce02f7e37a562a41f68c30409f780a24df 100644 (file)
@@ -24,6 +24,7 @@
 struct pakfire_ctx;
 
 #include <pakfire/logging.h>
+#include <pakfire/pakfire.h>
 
 int pakfire_ctx_create(struct pakfire_ctx** ctx);
 
@@ -38,11 +39,24 @@ void pakfire_ctx_set_log_level(struct pakfire_ctx* ctx, int level);
 void pakfire_ctx_set_log_callback(struct pakfire_ctx* ctx,
        pakfire_log_callback callback, void* data);
 
+// Confirm
+
+typedef int (*pakfire_confirm_callback)(struct pakfire_ctx* ctx, struct pakfire* pakfire,
+       void* data, const char* message, const char* question);
+
+void pakfire_ctx_set_confirm_callback(struct pakfire_ctx* ctx,
+       pakfire_confirm_callback callback, void* data);
+
 #ifdef PAKFIRE_PRIVATE
 
 void pakfire_ctx_log(struct pakfire_ctx* ctx, int level, const char* file, int line,
        const char* fn, const char* format, ...) __attribute__((format(printf, 6, 7)));
 
+// Confirm
+
+int pakfire_ctx_confirm(struct pakfire_ctx* ctx, struct pakfire* pakfire,
+       const char* message, const char* question);
+
 #endif /* PAKFIRE_PRIVATE */
 
 #endif /* PAKFIRE_CTX_H */
index cd3ab2941edb53aa8e95ae711b731fedc7577195..08584df07332f76ee5bf6e8be42ffd9dc67dd1ef 100644 (file)
@@ -46,10 +46,6 @@ enum pakfire_flags {
 };
 
 // Callbacks
-typedef int (*pakfire_confirm_callback)(struct pakfire* pakfire, void* data,
-       const char* message, const char* question);
-void pakfire_set_confirm_callback(struct pakfire* pakfire,
-       pakfire_confirm_callback callback, void* data);
 typedef void (*pakfire_status_callback)(struct pakfire* pakfire, void* data,
        int progress, const char* status);
 
index a57458d3aa3cafdad628a80e0a4ff7d51bbaeff9..cbc234601278c2e5f25b8b45833882536b7a600f 100644 (file)
@@ -27,6 +27,7 @@ global:
        pakfire_ctx_get_log_level;
        pakfire_ctx_set_log_level;
        pakfire_ctx_set_log_callback;
+       pakfire_ctx_set_confirm_callback;
 
        # pakfire
        pakfire_check;
index 9a7caf8aeab56514ee9f2ad038d102028cc47f66..ac9992f4d9f303b2d96de084521ac13dcd6e9475 100644 (file)
@@ -1653,26 +1653,6 @@ void pakfire_log(struct pakfire* pakfire, int priority, int r,
        pakfire_ctx_log(pakfire->ctx, priority, file, line, fn, "%s", buffer);
 }
 
-// UI
-
-PAKFIRE_EXPORT void pakfire_set_confirm_callback(struct pakfire* pakfire,
-               pakfire_confirm_callback callback, void* data) {
-       pakfire->callbacks.confirm = callback;
-       pakfire->callbacks.confirm_data = data;
-}
-
-int pakfire_confirm(struct pakfire* pakfire, const char* message, const char* question) {
-       // Run callback
-       if (pakfire->callbacks.confirm)
-               return pakfire->callbacks.confirm(
-                       pakfire, pakfire->callbacks.confirm_data, message, question);
-
-       // If no callback is set, we just log the message
-       INFO(pakfire, "%s\n", message);
-
-       return 0;
-}
-
 static const char* pakfire_user_lookup(void* data, la_int64_t uid) {
        struct pakfire* pakfire = (struct pakfire*)data;
 
index 2274c04beb4b87046d9267440b36470e29703a25..7c112e1cf3523a44507ed4a4085b5d85d6c3f35c 100644 (file)
@@ -27,6 +27,7 @@
 #include <solv/solverdebug.h>
 
 #include <pakfire/archive.h>
+#include <pakfire/ctx.h>
 #include <pakfire/db.h>
 #include <pakfire/dependencies.h>
 #include <pakfire/digest.h>
@@ -45,6 +46,7 @@
 #include <pakfire/util.h>
 
 struct pakfire_transaction {
+       struct pakfire_ctx* ctx;
        struct pakfire* pakfire;
        int nrefs;
 
@@ -173,7 +175,10 @@ static void pakfire_transaction_free(struct pakfire_transaction* transaction) {
                transaction_free(transaction->transaction);
        if (transaction->solver)
                solver_free(transaction->solver);
-       pakfire_unref(transaction->pakfire);
+       if (transaction->pakfire)
+               pakfire_unref(transaction->pakfire);
+       if (transaction->ctx)
+               pakfire_ctx_unref(transaction->ctx);
        free(transaction);
 }
 
@@ -321,6 +326,9 @@ PAKFIRE_EXPORT int pakfire_transaction_create(struct pakfire_transaction** trans
        if (!t)
                return -errno;
 
+       // Store a reference to the context
+       t->ctx = pakfire_ctx(pakfire);
+
        // Store reference to Pakfire
        t->pakfire = pakfire_ref(pakfire);
 
@@ -1940,7 +1948,7 @@ PAKFIRE_EXPORT int pakfire_transaction_run(struct pakfire_transaction* transacti
        dump = pakfire_transaction_dump(transaction, 80);
 
        // Check if we should continue
-       r = pakfire_confirm(transaction->pakfire, dump, _("Is this okay?"));
+       r = pakfire_ctx_confirm(transaction->ctx, transaction->pakfire, dump, _("Is this okay?"));
        if (r) {
                ERROR(transaction->pakfire, "Transaction aborted upon user request\n");
                goto ERROR;