From: Michael Tremer Date: Fri, 5 Mar 2021 16:19:10 +0000 (+0000) Subject: parser: Add function to create package X-Git-Tag: 0.9.28~1285^2~627 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f59d680a311132b5415d1bfc41c7bcb1a1f8f076;p=pakfire.git parser: Add function to create package Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/include/pakfire/parser.h b/src/libpakfire/include/pakfire/parser.h index f65def9c7..9e2946791 100644 --- a/src/libpakfire/include/pakfire/parser.h +++ b/src/libpakfire/include/pakfire/parser.h @@ -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); diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index ba24d0acf..29e1d4211 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -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; diff --git a/src/libpakfire/parser.c b/src/libpakfire/parser.c index f48754d85..5758ab9fa 100644 --- a/src/libpakfire/parser.c +++ b/src/libpakfire/parser.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -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 { diff --git a/tests/libpakfire/makefile.c b/tests/libpakfire/makefile.c index ed355103c..95da58ef8 100644 --- a/tests/libpakfire/makefile.c +++ b/tests/libpakfire/makefile.c @@ -22,7 +22,9 @@ #include #include +#include #include +#include #include #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(); }