]> git.ipfire.org Git - pakfire.git/commitdiff
dist: Add basic C implementation
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 9 Mar 2021 22:00:02 +0000 (22:00 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 9 Mar 2021 22:00:02 +0000 (22:00 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/_pakfire/pakfire.c
src/libpakfire/dist.c [new file with mode: 0644]
src/libpakfire/include/pakfire/dist.h [new file with mode: 0644]
src/libpakfire/libpakfire.sym
src/pakfire/base.py
src/pakfire/cli.py

index d11b72c8e5eec5c99bfa568e983c8a1f649aab3e..c9dd32e0267efde3cdfaecf8b1d5717b8173454d 100644 (file)
@@ -264,6 +264,7 @@ libpakfire_la_SOURCES = \
        src/libpakfire/arch.c \
        src/libpakfire/archive.c \
        src/libpakfire/db.c \
+       src/libpakfire/dist.c \
        src/libpakfire/errno.c \
        src/libpakfire/execute.c \
        src/libpakfire/file.c \
@@ -294,6 +295,7 @@ pkginclude_HEADERS += \
        src/libpakfire/include/pakfire/archive.h \
        src/libpakfire/include/pakfire/constants.h \
        src/libpakfire/include/pakfire/db.h \
+       src/libpakfire/include/pakfire/dist.h \
        src/libpakfire/include/pakfire/errno.h \
        src/libpakfire/include/pakfire/execute.h \
        src/libpakfire/include/pakfire/file.h \
index db2e0761a44570c2f15235be279e35f6203ce465..638f68c418b9a72ebba46b69b9fcb1f50040a6b4 100644 (file)
@@ -22,6 +22,7 @@
 #include <errno.h>
 
 #include <pakfire/constants.h>
+#include <pakfire/dist.h>
 #include <pakfire/execute.h>
 #include <pakfire/logging.h>
 #include <pakfire/packagelist.h>
@@ -701,7 +702,29 @@ static PyObject* Pakfire_read_makefile(PakfireObject* self, PyObject* args) {
        return ret;
 }
 
+static PyObject* Pakfire_dist(PakfireObject* self, PyObject* args) {
+       const char* path = NULL;
+       const char* target = NULL;
+
+       if (!PyArg_ParseTuple(args, "s|z", &path, &target))
+               return NULL;
+
+       int r = pakfire_dist(self->pakfire, path, target);
+       if (r) {
+               PyErr_SetFromErrno(PyExc_OSError);
+               return NULL;
+       }
+
+       Py_RETURN_NONE;
+}
+
 static struct PyMethodDef Pakfire_methods[] = {
+       {
+               "dist",
+               (PyCFunction)Pakfire_dist,
+               METH_VARARGS,
+               NULL
+       },
        {
                "execute",
                (PyCFunction)Pakfire_execute,
diff --git a/src/libpakfire/dist.c b/src/libpakfire/dist.c
new file mode 100644 (file)
index 0000000..328ed52
--- /dev/null
@@ -0,0 +1,128 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2021 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <stddef.h>
+#include <stdlib.h>
+
+#include <pakfire/dist.h>
+#include <pakfire/logging.h>
+#include <pakfire/package.h>
+#include <pakfire/packager.h>
+#include <pakfire/pakfire.h>
+#include <pakfire/parser.h>
+#include <pakfire/private.h>
+#include <pakfire/repo.h>
+#include <pakfire/types.h>
+
+PAKFIRE_EXPORT int pakfire_dist(Pakfire pakfire, const char* path, const char* target) {
+       PakfireParser makefile;
+       struct pakfire_parser_error* error = NULL;
+
+       struct pakfire_packager* packager = NULL;
+       PakfirePackage pkg = NULL;
+
+       char* filename = NULL;
+       char tempfile[PATH_MAX] = "";
+
+       // Open the target directory
+       DIR* d = opendir(target);
+       if (!d)
+               return 1;
+
+       int dfd = dirfd(d);
+
+       // Load makefile
+       int r = pakfire_read_makefile(&makefile, pakfire, path, &error);
+       if (r) {
+               if (error)
+                       pakfire_parser_error_unref(error);
+
+               return r;
+       }
+
+       PakfireRepo repo = pakfire_repo_create(pakfire, "dummy");
+       if (!repo)
+               goto ERROR;
+
+       // Get the package object
+       r = pakfire_parser_create_package(makefile, &pkg, repo, NULL);
+       if (r)
+               goto ERROR;
+
+       // Set architecture to source
+       pakfire_package_set_arch(pkg, "src");
+
+       // Create a packager
+       r = pakfire_packager_create(&packager, pkg);
+       if (r)
+               goto ERROR;
+
+       // Add the makefile
+       r = pakfire_packager_add(packager, path);
+       if (r)
+               goto ERROR;
+
+       snprintf(tempfile, PATH_MAX - 1, "%s/.pakfire-dist.XXXXXX", target);
+
+       // Create a temporary result file
+       int fd = mkostemp(tempfile, O_CLOEXEC);
+       if (fd < 0)
+               goto ERROR;
+
+       // Re-open as file handle
+       FILE* f = fdopen(fd, "w");
+
+       // Write the finished package
+       filename = pakfire_packager_finish(packager, f);
+       if (!filename) {
+               ERROR(pakfire, "pakfire_packager_finish() failed: %s\n", strerror(errno));
+               goto ERROR;
+       }
+
+       // Close the file
+       fclose(f);
+
+       // Move the temporary file to destination
+       r = renameat(AT_FDCWD, tempfile, dfd, filename);
+       if (r)
+               goto ERROR;
+
+ERROR:
+       // Remove the temporary file
+       if (*tempfile)
+               unlink(tempfile);
+
+       if (filename)
+               free(filename);
+       closedir(d);
+
+       if (packager)
+               pakfire_packager_unref(packager);
+       if (pkg)
+               pakfire_package_unref(pkg);
+       if (repo)
+               pakfire_repo_unref(repo);
+       pakfire_parser_unref(makefile);
+
+       return r;
+}
diff --git a/src/libpakfire/include/pakfire/dist.h b/src/libpakfire/include/pakfire/dist.h
new file mode 100644 (file)
index 0000000..c0582ac
--- /dev/null
@@ -0,0 +1,28 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2021 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef PAKFIRE_DIST_H
+#define PAKFIRE_DIST_H
+
+#include <pakfire/types.h>
+
+int pakfire_dist(Pakfire pakfire, const char* path, const char* target);
+
+#endif /* PAKFIRE_DIST_H */
index cbac553538f87fc5482c6c9964131db41fa73e42..99d05e9c36d7e5556bb9d5bdab3ce0c90d39c446 100644 (file)
@@ -91,6 +91,9 @@ global:
        pakfire_db_remove_package;
        pakfire_db_unref;
 
+       # dist
+       pakfire_dist;
+
        # file
        pakfire_file_cmp;
        pakfire_file_create;
index 02f19180434102b38a1c1c7a26dd5cee88316174..f9c495d574f1ff931d7d5f2348ae236261a41480 100644 (file)
@@ -149,11 +149,6 @@ class Pakfire(_pakfire.Pakfire):
                for repo in self.repos:
                        repo.clean()
 
-       def dist(self, pkg, resultdir):
-               pkg = packages.Makefile(self, pkg)
-
-               return pkg.dist(resultdir=resultdir)
-
 
 class PakfireContext(object):
        """
index c887e2ff3d12a7458fa7f3a95128638f9deb3933..e9d75c929c551069a1eb8db2ac6c157174061b77 100644 (file)
@@ -518,7 +518,7 @@ class CliBuilder(Cli):
 
                p = self.pakfire(ns)
                for pkg in pkgs:
-                       p.dist(pkg, resultdir=resultdir)
+                       p.dist(pkg, resultdir)
 
 
 class CliClient(Cli):