]> git.ipfire.org Git - pakfire.git/commitdiff
_pakfire: Add version_compare() that does not require Pakfire
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 21 Jul 2023 14:28:59 +0000 (14:28 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 21 Jul 2023 14:28:59 +0000 (14:28 +0000)
This is useful if we do not have a Pakfire instance at hand and will
save us the overhead of creating one every time.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/_pakfiremodule.c
src/libpakfire/dependencies.c
src/libpakfire/include/pakfire/dependencies.h
src/libpakfire/libpakfire.sym

index e4d5845bdc26b5adaffa2a872a4df41d52627911..c0c0ce2a9f9560083fdde33c854e32e46f0c95c8 100644 (file)
@@ -22,6 +22,7 @@
 #include <libintl.h>
 
 #include <pakfire/arch.h>
+#include <pakfire/dependencies.h>
 #include <pakfire/key.h>
 
 #include "archive.h"
@@ -92,9 +93,30 @@ ERROR:
        return NULL;
 }
 
+static PyObject* _pakfire_version_compare(PyObject* self, PyObject* args) {
+       const char* evr1 = NULL;
+       const char* evr2 = NULL;
+       int r;
+
+       if (!PyArg_ParseTuple(args, "ss", &evr1, &evr2))
+               return NULL;
+
+       // Compare versions
+       r = pakfire_static_version_compare(evr1, evr2);
+
+       // Return the return code
+       return PyLong_FromLong(r);
+}
+
 static PyMethodDef pakfireModuleMethods[] = {
        {"native_arch", (PyCFunction)_pakfire_native_arch, METH_NOARGS, NULL },
        {"supported_arches", (PyCFunction)_pakfire_supported_arches, METH_NOARGS, NULL },
+       {
+               "version_compare",
+               (PyCFunction)_pakfire_version_compare,
+               METH_VARARGS,
+               NULL,
+       },
        { NULL, NULL, 0, NULL }
 };
 
index 96350f631e2e95eac585d4754bb6c65d6dc217e3..d457e7d3d258c0a4b26bd73cbbf19eaebe986360 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 
+#include <solv/evr.h>
 #include <solv/pool.h>
 
 #include <pakfire/dependencies.h>
 #include <pakfire/logging.h>
 #include <pakfire/package.h>
 #include <pakfire/pakfire.h>
+#include <pakfire/private.h>
 #include <pakfire/string.h>
 #include <pakfire/util.h>
 
@@ -59,6 +61,27 @@ static const struct pakfire_rich_operation {
        { NULL, 0 },
 };
 
+static Pool* __pool = NULL;
+
+/*
+       This function can compare package versions without a Pakfire instance initialized.
+*/
+PAKFIRE_EXPORT int pakfire_static_version_compare(const char* evr1, const char* evr2) {
+       // Initialize the pool (unless already done)
+       if (!__pool) {
+               __pool = pool_create();
+
+               if (!__pool)
+                       return 0;
+
+               // Set to RPM mode
+               pool_setdisttype(__pool, DISTTYPE_RPM);
+       }
+
+       // Perform comparison
+       return pool_evrcmp_str(__pool, evr1, evr2, EVRCMP_COMPARE);
+}
+
 const char* pakfire_dep2str(struct pakfire* pakfire, Id id) {
        Pool* pool = pakfire_get_solv_pool(pakfire);
        if (!pool)
index baa5e91d66a82147698c0e6f5233af16144b3bef..8dd58c3392f99fb8d5a16667564d4e179c1ed16c 100644 (file)
@@ -21,6 +21,8 @@
 #ifndef PAKFIRE_DEPENDENCIES_H
 #define PAKFIRE_DEPENDENCIES_H
 
+int pakfire_static_version_compare(const char* evr1, const char* evr2);
+
 #ifdef PAKFIRE_PRIVATE
 
 #include <solv/pool.h>
index eb16a54d28022a9d41ab0ae3eeedb863191476fb..6a75f063f3c88d7a1547ae98258f1a9c25ac846b 100644 (file)
@@ -72,6 +72,9 @@ global:
        pakfire_build_unref;
        pakfire_shell;
 
+       # dependencies
+       pakfire_static_version_compare;
+
        # digest
        pakfire_digest_get_by_name;
        pakfire_digest_name;