]> git.ipfire.org Git - pakfire.git/commitdiff
libpakfire: Add function to import an archive to a package
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 Jun 2019 11:54:00 +0000 (12:54 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 Jun 2019 11:54:00 +0000 (12:54 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
12 files changed:
src/_pakfire/repo.c
src/libpakfire/archive.c
src/libpakfire/include/pakfire/archive.h
src/libpakfire/include/pakfire/package.h
src/libpakfire/include/pakfire/repo.h
src/libpakfire/include/pakfire/util.h
src/libpakfire/libpakfire.sym
src/libpakfire/package.c
src/libpakfire/repo.c
src/libpakfire/util.c
src/pakfire/base.py
src/pakfire/cli.py

index 6ddc75005b8f7dba188985bc6e2355fb0d327633..bbf3e195891a5e63635fdabb17a9a05f58fcb761 100644 (file)
@@ -25,6 +25,7 @@
 #include <pakfire/repo.h>
 #include <pakfire/util.h>
 
+#include "archive.h"
 #include "package.h"
 #include "repo.h"
 
@@ -310,6 +311,23 @@ static PyObject* Repo__add_package(RepoObject* self, PyObject* args) {
        return obj;
 }
 
+static PyObject* Repo_add_archive(RepoObject* self, PyObject* args) {
+       ArchiveObject* archive = NULL;
+
+       if (!PyArg_ParseTuple(args, "O!", &ArchiveType, &archive))
+               return NULL;
+
+       // Add package
+       PakfirePackage pkg = pakfire_repo_add_archive(self->repo, archive->archive);
+       assert(pkg);
+
+       // Create Python object
+       PyObject* obj = new_package(&PackageType, pkg);
+       pakfire_package_unref(pkg);
+
+       return obj;
+}
+
 static PyObject* Repo_cache_age(RepoObject* self, PyObject* args) {
        const char* path = NULL;
 
@@ -472,6 +490,12 @@ static struct PyMethodDef Repo_methods[] = {
                METH_VARARGS,
                NULL
        },
+       {
+               "add_archive",
+               (PyCFunction)Repo_add_archive,
+               METH_VARARGS,
+               NULL
+       },
        { NULL }
 };
 
index b5b6ae2e43cc5c712c4ddd586e79d482e468e0cf..e66bf9bfe685db54eeaeb21fedc9c924b5e74db9 100644 (file)
@@ -40,6 +40,7 @@
 #include <pakfire/i18n.h>
 #include <pakfire/key.h>
 #include <pakfire/logging.h>
+#include <pakfire/package.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/parser.h>
 #include <pakfire/private.h>
@@ -530,6 +531,10 @@ static int pakfire_archive_read_metadata(PakfireArchive archive, struct archive*
        return pakfire_archive_walk(archive, pakfire_archive_read_metadata_entry);
 }
 
+PAKFIRE_EXPORT char* pakfire_archive_get(PakfireArchive archive, const char* key) {
+       return pakfire_parser_get(archive->parser, key);
+}
+
 static int archive_copy_data(struct archive* in, struct archive* out) {
        int r;
        const void* buff;
@@ -1076,3 +1081,101 @@ PAKFIRE_EXPORT const char* pakfire_archive_verify_strerror(pakfire_archive_verif
 
        return NULL;
 }
+
+/*
+       Copy all metadata from this archive to the package object
+*/
+PAKFIRE_EXPORT PakfirePackage pakfire_archive_make_package(PakfireArchive archive, PakfireRepo repo) {
+       char* name  = pakfire_archive_get(archive, "package.name");
+       char* arch = pakfire_archive_get(archive, "package.arch");
+
+       char* e = pakfire_archive_get(archive, "package.epoch");
+       char* v = pakfire_archive_get(archive, "package.version");
+       char* r = pakfire_archive_get(archive, "package.release");
+       char* evr = pakfire_package_join_evr(e, v, r);
+
+       PakfirePackage pkg = pakfire_package_create2(
+               archive->pakfire, repo, name, evr, arch
+       );
+
+       pakfire_free(name);
+       pakfire_free(arch);
+       pakfire_free(e);
+       pakfire_free(v);
+       pakfire_free(r);
+       pakfire_free(evr);
+
+#ifdef ENABLE_DEBUG
+       char* nevra = pakfire_package_get_nevra(pkg);
+       DEBUG(archive->pakfire, "Created package %s (%p) from archive %p\n",
+               nevra, pkg, archive);
+       pakfire_free(nevra);
+#endif
+
+       // Set UUID
+       char* uuid = pakfire_archive_get(archive, "package.uuid");
+       if (uuid) {
+               pakfire_package_set_uuid(pkg, uuid);
+               pakfire_free(uuid);
+       }
+
+       // Set groups
+#warning broken
+#if 0
+       char* groups = pakfire_archive_get(archive, "package.groups");
+       if (groups) {
+               pakfire_package_set_groups(pkg, groups);
+               pakfire_free(groups);
+       }
+#endif
+
+       // Set maintainer
+       char* maintainer = pakfire_archive_get(archive, "package.maintainer");
+       if (maintainer) {
+               pakfire_package_set_maintainer(pkg, maintainer);
+               pakfire_free(maintainer);
+       }
+
+       // Set URL
+       char* url = pakfire_archive_get(archive, "package.url");
+       if (url) {
+               pakfire_package_set_url(pkg, url);
+               pakfire_free(url);
+       }
+
+       // Set license
+       char* license = pakfire_archive_get(archive, "package.license");
+       if (license) {
+               pakfire_package_set_license(pkg, license);
+               pakfire_free(license);
+       }
+
+       // Set summary
+       char* summary = pakfire_archive_get(archive, "package.summary");
+       if (summary) {
+               pakfire_package_set_summary(pkg, summary);
+               pakfire_free(summary);
+       }
+
+       // Set description
+       char* description = pakfire_archive_get(archive, "package.description");
+       if (description) {
+               pakfire_package_set_description(pkg, description);
+               pakfire_free(description);
+       }
+
+       // XXX Get package size
+
+       // Get install size
+       char* size = pakfire_archive_get(archive, "package.size");
+       if (size) {
+               size_t s = pakfire_string_to_size(size);
+               pakfire_free(size);
+
+               pakfire_package_set_installsize(pkg, s);
+       }
+
+       // XXX more data to follow
+
+       return pkg;
+}
index a60432da75140422182006cc19bbba8d2012a1f6..600ce2f13032b3f0269ceb38abe9706ca67152e9 100644 (file)
@@ -43,6 +43,8 @@ PakfireArchive pakfire_archive_create(Pakfire pakfire);
 PakfireArchive pakfire_archive_ref(PakfireArchive archive);
 PakfireArchive pakfire_archive_unref(PakfireArchive archive);
 
+char* pakfire_archive_get(PakfireArchive archive, const char* key);
+
 PakfireArchive pakfire_archive_open(Pakfire pakfire, const char* path);
 
 int pakfire_archive_read(PakfireArchive archive, const char* filename,
@@ -65,6 +67,8 @@ PakfireArchiveSignature pakfire_archive_signature_ref(PakfireArchiveSignature si
 void pakfire_archive_signature_unref(PakfireArchiveSignature signature);
 const char* pakfire_archive_signature_get_data(PakfireArchiveSignature signature);
 
+PakfirePackage pakfire_archive_make_package(PakfireArchive archive, PakfireRepo repo);
+
 #define PAKFIRE_ARCHIVE_FN_CHECKSUMS           "chksums"
 #define PAKFIRE_ARCHIVE_FN_FILELIST                    "filelist"
 #define PAKFIRE_ARCHIVE_FN_FORMAT                      "pakfire-format"
index d7995cf8db5595b6fe993ed51d08497a86ff52b5..da7035309755c458c09b0984a855e46e88978e35 100644 (file)
@@ -45,6 +45,7 @@ const char* pakfire_package_get_name(PakfirePackage pkg);
 void pakfire_package_set_name(PakfirePackage pkg, const char* name);
 const char* pakfire_package_get_evr(PakfirePackage pkg);
 void pakfire_package_set_evr(PakfirePackage pkg, const char* evr);
+char* pakfire_package_join_evr(const char* e, const char* v, const char* r);
 unsigned long pakfire_package_get_epoch(PakfirePackage pkg);
 const char* pakfire_package_get_version(PakfirePackage pkg);
 const char* pakfire_package_get_release(PakfirePackage pkg);
index 14baf158b0c6a4a107f3a0155b240a844c567c22..49588202799cf2dd9dea5928a87a5dae415eb02f 100644 (file)
@@ -69,6 +69,8 @@ int pakfire_repo_read_solv_fp(PakfireRepo repo, FILE *f, int flags);
 int pakfire_repo_write_solv(PakfireRepo repo, const char* filename, int flags);
 int pakfire_repo_write_solv_fp(PakfireRepo repo, FILE *f, int flags);
 
+PakfirePackage pakfire_repo_add_archive(PakfireRepo repo, PakfireArchive archive);
+
 // Cache
 
 int pakfire_repo_clean(PakfireRepo repo);
index e43510d73e27d2ace7d9950013c56384dd1809bb..c2e9faa418aa9448dc966e7ce8e2cf7f900e2677 100644 (file)
@@ -58,4 +58,6 @@ void init_libgcrypt();
 
 int pakfire_read_file_into_buffer(FILE* f, char** buffer, size_t* len);
 
+size_t pakfire_string_to_size(const char* s);
+
 #endif /* PAKFIRE_UTIL_H */
index 48694c17b12110d9d923b6a2dcec90997b7d4c93..04ae7437cc0fed05031778f53d35ea9231faeb08 100644 (file)
@@ -171,6 +171,7 @@ global:
        pakfire_package_id;
        pakfire_package_is_installed;
        pakfire_package_is_in_group;
+       pakfire_package_join_evr;
        pakfire_package_ref;
        pakfire_package_set_arch;
        pakfire_package_set_buildhost;
@@ -234,6 +235,7 @@ global:
        pakfire_problem_unref;
 
        # repo
+       pakfire_repo_add_archive;
        pakfire_repo_cache_access;
        pakfire_repo_cache_age;
        pakfire_repo_cache_get_path;
@@ -353,6 +355,7 @@ global:
        pakfire_get_errno;
        pakfire_path_join;
        pakfire_read_file_into_buffer;
+       pakfire_string_to_size;
 
 local:
        *;
index 6ebf28380f78a88d768b183ffe27dd4d50e2b1d1..209193f56b0321c0ba28c4f31fcf8cbb077d4966 100644 (file)
@@ -232,6 +232,25 @@ PAKFIRE_EXPORT void pakfire_package_set_evr(PakfirePackage pkg, const char* evr)
        s->evr = pool_str2id(pool, evr, 1);
 }
 
+PAKFIRE_EXPORT char* pakfire_package_join_evr(const char* e, const char* v, const char* r) {
+       size_t l = strlen(v) + strlen(r) + 2;
+
+       // Do not include epoch when it is zero
+       if (strncmp(e, "0", strlen("0")) == 0) {
+               e = NULL;
+       } else {
+               l += strlen(e);
+       }
+
+       char* buffer = pakfire_malloc(l + 1);
+       if (e)
+               snprintf(buffer, l + 1, "%s:%s.%s", e, v, r);
+       else
+               snprintf(buffer, l + 1, "%s.%s", v, r);
+
+       return buffer;
+}
+
 static void split_evr(Pool* pool, const char* evr_c, char** epoch, char** version, char** release) {
     char *e, *v, *r;
 
index 329e2d531583831219b6a04debc53d0e9cdeec41..05a80087448b04f33d6cf692178614e9491e31e8 100644 (file)
@@ -29,6 +29,7 @@
 
 #include <lzma.h>
 
+#include <pakfire/archive.h>
 #include <pakfire/constants.h>
 #include <pakfire/errno.h>
 #include <pakfire/logging.h>
@@ -557,6 +558,10 @@ PAKFIRE_EXPORT PakfirePackage pakfire_repo_add_package(PakfireRepo repo) {
        return pakfire_package_create(repo->pakfire, id);
 }
 
+PAKFIRE_EXPORT PakfirePackage pakfire_repo_add_archive(PakfireRepo repo, PakfireArchive archive) {
+       return pakfire_archive_make_package(archive, repo);
+}
+
 // Cache
 
 static char* pakfire_repo_get_cache_prefix(PakfireRepo repo) {
index 092f117735560a7c4621917cec519bbff7af56a5..bfe36a11161b5a466002141be94f6a817c87535d 100644 (file)
@@ -318,3 +318,13 @@ PAKFIRE_EXPORT int pakfire_read_file_into_buffer(FILE* f, char** buffer, size_t*
 
        return 0;
 }
+
+PAKFIRE_EXPORT size_t pakfire_string_to_size(const char* s) {
+       size_t size;
+
+       int r = sscanf(s, "%zu", &size);
+       if (r == 1)
+               return size;
+
+       return 0;
+}
index d89aa230b162699d0e0ea78a0be204586c0fd626..09dc2da47f2faa51a8b64513fa181a1f7059eb8c 100644 (file)
@@ -201,19 +201,20 @@ class PakfireContext(object):
 
                return request.solve(**kwargs)
 
-       def info(self, patterns):
+       def info(self, args):
                pkgs = []
 
-               # For all patterns we run a single search which returns us a bunch
-               # of solvables which are transformed into Package objects.
-               for pattern in patterns:
-                       if os.path.exists(pattern) and not os.path.isdir(pattern):
-                               pkg = packages.open(self.pakfire, self.pakfire.repos.dummy, pattern)
-                               if pkg:
+               with _pakfire.Repo(self.pakfire, "tmp", clean=True) as r:
+                       for arg in args:
+                               if os.path.exists(arg) and not os.path.isdir(arg):
+                                       archive = _pakfire.Archive(self.pakfire, arg)
+
+                                       # Add the archive to the repository
+                                       pkg = r.add_archive(archive)
                                        pkgs.append(pkg)
 
-                       else:
-                               pkgs += self.pakfire.whatprovides(pattern, name_only=True)
+                               else:
+                                       pkgs += self.pakfire.whatprovides(arg, name_only=True)
 
                return sorted(pkgs)
 
index 630af9ab355619e20b8f362cbfa458eae461af6e..7fb3614408bddef9806b1e0049f4ffd2eeb3509a 100644 (file)
@@ -288,7 +288,7 @@ class Cli(object):
                with self.pakfire(ns) as p:
                        for pkg in p.info(ns.package):
                                s = pkg.dump(long=ns.verbose)
-                               print(s)
+                               self.ui.message(s)
 
        def handle_search(self, ns):
                with self.pakfire(ns) as p: