]> git.ipfire.org Git - pakfire.git/commitdiff
parser: Add function to create package
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 5 Mar 2021 16:19:10 +0000 (16:19 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 5 Mar 2021 16:19:10 +0000 (16:19 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/parser.h
src/libpakfire/libpakfire.sym
src/libpakfire/parser.c
tests/libpakfire/makefile.c

index f65def9c7a0ae751ad852e58181893c6b35e588f..9e2946791bcc98b3d55ef3443cf23635675c9c55 100644 (file)
@@ -60,6 +60,9 @@ char* pakfire_parser_dump(PakfireParser parser);
 const char* pakfire_parser_get_namespace(PakfireParser parser);
 int pakfire_parser_set_namespace(PakfireParser parser, const char* namespace);
 
+int pakfire_parser_create_package(PakfireParser parser,
+       PakfirePackage* pkg, PakfireRepo repo, const char* namespace);
+
 // Errors
 int pakfire_parser_error_create(struct pakfire_parser_error** error,
                PakfireParser parser, const char* filename, int line, const char* message);
index ba24d0acf4ed50d59d90d3b007eb4da3ce1d019a..29e1d421137a458d8dc5dfa3b77603d65d427531 100644 (file)
@@ -251,6 +251,7 @@ global:
        pakfire_parser_append;
        pakfire_parser_create;
        pakfire_parser_create_child;
+       pakfire_parser_create_package;
        pakfire_parser_dump;
        pakfire_parser_expand;
        pakfire_parser_get;
index f48754d85fafe30f9b606c47739a0570efcc1f9f..5758ab9fa37838cf368ffb5b59e3f7bb5e178c59 100644 (file)
@@ -29,6 +29,7 @@
 #include <pakfire/errno.h>
 #include <pakfire/execute.h>
 #include <pakfire/logging.h>
+#include <pakfire/package.h>
 #include <pakfire/parser.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/private.h>
@@ -715,6 +716,82 @@ PAKFIRE_EXPORT int pakfire_parser_set_namespace(PakfireParser parser, const char
        return 0;
 }
 
+PAKFIRE_EXPORT int pakfire_parser_create_package(PakfireParser parser,
+               PakfirePackage* pkg, PakfireRepo repo, const char* namespace) {
+       int r = 1;
+
+       char* name = NULL;
+       char* epoch = NULL;
+       char* version = NULL;
+       char* release = NULL;
+       char* arch = NULL;
+       char* evr = NULL;
+
+       // Fetch name
+       name = pakfire_parser_get(parser, namespace, "name");
+       if (!name) {
+               ERROR(parser->pakfire, "Name is empty\n");
+               goto CLEANUP;
+       }
+
+       // Fetch epoch
+       epoch = pakfire_parser_get(parser, namespace, "epoch");
+       if (!epoch) {
+               ERROR(parser->pakfire, "Epoch is empty\n");
+               goto CLEANUP;
+       }
+
+       // Fetch version
+       version = pakfire_parser_get(parser, namespace, "version");
+       if (!version) {
+               ERROR(parser->pakfire, "Version is empty\n");
+               goto CLEANUP;
+       }
+
+       // Fetch release
+       release = pakfire_parser_get(parser, namespace, "release");
+       if (!release) {
+               ERROR(parser->pakfire, "Release is empty\n");
+               goto CLEANUP;
+       }
+
+       // Fetch arch
+       arch = pakfire_parser_get(parser, namespace, "arch");
+       if (!arch) {
+               ERROR(parser->pakfire, "Arch is empty\n");
+               goto CLEANUP;
+       }
+
+       // Compile EVR
+       evr = pakfire_package_join_evr(epoch, version, release);
+       if (!evr)
+               goto CLEANUP;
+
+       // Create a new package object
+       *pkg = pakfire_package_create(parser->pakfire, repo, name, evr, arch);
+       if (!*pkg) {
+               ERROR(parser->pakfire, "Could not create package\n");
+               goto CLEANUP;
+       }
+
+       // All okay
+       r = 0;
+
+CLEANUP:
+       if (name)
+               free(name);
+       if (epoch)
+               free(epoch);
+       if (version)
+               free(version);
+       if (release)
+               free(release);
+       if (arch)
+               free(arch);
+
+       return r;
+}
+
 // Error
 
 struct pakfire_parser_error {
index ed355103cf138072e75ff182aaf3884511afd4ee..95da58ef898410a1aee7214a8cb99282903e779c 100644 (file)
@@ -22,7 +22,9 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <pakfire/package.h>
 #include <pakfire/parser.h>
+#include <pakfire/repo.h>
 #include <pakfire/util.h>
 
 #include "../testsuite.h"
@@ -33,6 +35,24 @@ static const char* makefiles[] = {
        NULL,
 };
 
+static int load_macros(PakfireParser parser) {
+       char* macros = pakfire_path_join(TEST_SRC_PATH, "../macros/*.macro");
+       ASSERT(macros);
+
+       glob_t buffer;
+       int r = glob(macros, 0, NULL, &buffer);
+       ASSERT(r == 0);
+
+       for (unsigned int i = 0; i < buffer.gl_pathc; i++) {
+               r = pakfire_parser_read_file(parser, buffer.gl_pathv[i], NULL);
+               ASSERT(r == 0);
+       }
+
+       free(macros);
+
+       return 0;
+}
+
 static int test_parse(const struct test* t) {
        const char** makefile = makefiles;
 
@@ -60,23 +80,61 @@ static int test_parse(const struct test* t) {
 }
 
 static int test_macros(const struct test* t) {
-       char* macros = pakfire_path_join(TEST_SRC_PATH, "../macros/*.macro");
-       ASSERT(macros);
+       PakfireParser parser = pakfire_parser_create(t->pakfire, NULL, NULL, 0);
+       ASSERT(parser);
 
-       glob_t buffer;
-       int r = glob(macros, 0, NULL, &buffer);
-       ASSERT(r == 0);
+       // Load 'em all
+       int r = load_macros(parser);
+       if (r)
+               return r;
 
-       PakfireParser parser = pakfire_parser_create(t->pakfire, NULL, NULL, 0);
+       pakfire_parser_unref(parser);
+
+       return EXIT_SUCCESS;
+}
+
+static int test_packages(const struct test* t) {
+       PakfirePackage pkg = NULL;
+
+       PakfireRepo repo = pakfire_repo_create(t->pakfire, "test");
+       ASSERT(repo);
+
+       PakfireParser parser = pakfire_parser_create(t->pakfire, NULL, NULL,
+               PAKFIRE_PARSER_FLAGS_EXPAND_COMMANDS);
        ASSERT(parser);
 
-       for (unsigned int i = 0; i < buffer.gl_pathc; i++) {
-               r = pakfire_parser_read_file(parser, buffer.gl_pathv[i], NULL);
-               ASSERT(r == 0);
-       }
+       // Load macros
+       int r = load_macros(parser);
+       if (r)
+               return r;
+
+       // Read beep.nm
+       char* path = pakfire_path_join(TEST_SRC_PATH, "data/beep.nm");
+       ASSERT(path);
 
+       r = pakfire_parser_read_file(parser, path, NULL);
+       ASSERT(r == 0);
+
+       // Create package
+       r = pakfire_parser_create_package(parser, &pkg, repo, NULL);
+       ASSERT(r == 0);
+       ASSERT(pkg);
+
+       // Dump package
+       char* s = pakfire_package_dump(pkg, PAKFIRE_PKG_DUMP_LONG);
+       ASSERT(s);
+
+       printf("%s\n", s);
+
+       // Check name
+       const char* name = pakfire_package_get_name(pkg);
+       ASSERT_STRING_EQUALS(name, "beep");
+
+       // Cleanup
        pakfire_parser_unref(parser);
-       free(macros);
+       pakfire_package_unref(pkg);
+       pakfire_repo_unref(repo);
+       free(path);
 
        return EXIT_SUCCESS;
 }
@@ -84,6 +142,7 @@ static int test_macros(const struct test* t) {
 int main(int argc, char** argv) {
        testsuite_add_test(test_macros);
        testsuite_add_test(test_parse);
+       testsuite_add_test(test_packages);
 
        return testsuite_run();
 }