From: Michael Tremer Date: Thu, 23 Nov 2017 17:07:04 +0000 (+0100) Subject: libpakfire: Create a Pakfire root object X-Git-Tag: 0.9.28~1285^2~1295 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6e46b18ef8614dcfcc89681bd66942c48a446563;p=pakfire.git libpakfire: Create a Pakfire root object Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index e4168bc25..62f59760e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -185,6 +185,8 @@ _pakfire_la_SOURCES = \ src/_pakfire/key.h \ src/_pakfire/package.c \ src/_pakfire/package.h \ + src/_pakfire/pakfire.c \ + src/_pakfire/pakfire.h \ src/_pakfire/pool.c \ src/_pakfire/pool.h \ src/_pakfire/problem.c \ @@ -241,6 +243,7 @@ libpakfire_la_SOURCES = \ src/libpakfire/key.c \ src/libpakfire/package.c \ src/libpakfire/packagelist.c \ + src/libpakfire/pakfire.c \ src/libpakfire/pool.c \ src/libpakfire/problem.c \ src/libpakfire/relation.c \ @@ -267,6 +270,7 @@ pkginclude_HEADERS += \ src/libpakfire/include/pakfire/package.h \ src/libpakfire/include/pakfire/packagecache.h \ src/libpakfire/include/pakfire/packagelist.h \ + src/libpakfire/include/pakfire/pakfire.h \ src/libpakfire/include/pakfire/pool.h \ src/libpakfire/include/pakfire/problem.h \ src/libpakfire/include/pakfire/relation.h \ diff --git a/src/_pakfire/_pakfiremodule.c b/src/_pakfire/_pakfiremodule.c index c226407a1..617cd0b16 100644 --- a/src/_pakfire/_pakfiremodule.c +++ b/src/_pakfire/_pakfiremodule.c @@ -31,6 +31,7 @@ #include "errors.h" #include "key.h" #include "package.h" +#include "pakfire.h" #include "pool.h" #include "problem.h" #include "relation.h" @@ -123,6 +124,13 @@ PyMODINIT_FUNC PyInit__pakfire(void) { Py_INCREF(PyExc_DependencyError); PyModule_AddObject(module, "DependencyError", PyExc_DependencyError); + // Pakfire + if (PyType_Ready(&PakfireType) < 0) + return NULL; + + Py_INCREF(&PakfireType); + PyModule_AddObject(module, "Pakfire", (PyObject *)&PakfireType); + // Archive if (PyType_Ready(&ArchiveType) < 0) return NULL; diff --git a/src/_pakfire/pakfire.c b/src/_pakfire/pakfire.c new file mode 100644 index 000000000..b3d907c7d --- /dev/null +++ b/src/_pakfire/pakfire.c @@ -0,0 +1,110 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2017 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 . # +# # +#############################################################################*/ + +#include + +#include + +#include "pakfire.h" + +static PyObject* Pakfire_new(PyTypeObject* type, PyObject* args, PyObject* kwds) { + PakfireObject* self = (PakfireObject *)type->tp_alloc(type, 0); + if (self) { + self->pakfire = NULL; + } + + return (PyObject *)self; +} + +static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) { + const char* path = NULL; + const char* arch = NULL; + + if (!PyArg_ParseTuple(args, "ss", &path, &arch)) + return -1; + + self->pakfire = pakfire_create(path, arch); + if (!self->pakfire) + return -1; + + return 0; +} + +static void Pakfire_dealloc(PakfireObject* self) { + if (self->pakfire) + pakfire_unref(self->pakfire); + + Py_TYPE(self)->tp_free((PyObject *)self); +} + +static PyObject* Pakfire_repr(PakfireObject* self) { + const char* path = pakfire_get_path(self->pakfire); + const char* arch = pakfire_get_arch(self->pakfire); + + return PyUnicode_FromFormat("<_pakfire.Pakfire %s (%s)>", path, arch); +} + +static PyObject* Pakfire_get_path(PakfireObject* self) { + const char* path = pakfire_get_path(self->pakfire); + + return PyUnicode_FromString(path); +} + +static PyObject* Pakfire_get_arch(PakfireObject* self) { + const char* arch = pakfire_get_arch(self->pakfire); + + return PyUnicode_FromString(arch); +} + +static struct PyMethodDef Pakfire_methods[] = { + { NULL }, +}; + +static struct PyGetSetDef Pakfire_getsetters[] = { + { + "arch", + (getter)Pakfire_get_arch, + NULL, + NULL, + NULL + }, + { + "path", + (getter)Pakfire_get_path, + NULL, + NULL, + NULL + }, + { NULL }, +}; + +PyTypeObject PakfireType = { + PyVarObject_HEAD_INIT(NULL, 0) + tp_name: "_pakfire.Pakfire", + tp_basicsize: sizeof(PakfireObject), + tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + tp_new: Pakfire_new, + tp_dealloc: (destructor)Pakfire_dealloc, + tp_init: (initproc)Pakfire_init, + tp_doc: "Pakfire object", + tp_methods: Pakfire_methods, + tp_getset: Pakfire_getsetters, + tp_repr: (reprfunc)Pakfire_repr, +}; diff --git a/src/_pakfire/pakfire.h b/src/_pakfire/pakfire.h new file mode 100644 index 000000000..c8f001875 --- /dev/null +++ b/src/_pakfire/pakfire.h @@ -0,0 +1,35 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2017 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 . # +# # +#############################################################################*/ + +#ifndef PYTHON_PAKFIRE_PAKFIRE_H +#define PYTHON_PAKFIRE_PAKFIRE_H + +#include + +#include + +typedef struct { + PyObject_HEAD + Pakfire pakfire; +} PakfireObject; + +extern PyTypeObject PakfireType; + +#endif /* PYTHON_PAKFIRE_PAKFIRE_H */ diff --git a/src/libpakfire/include/pakfire/pakfire.h b/src/libpakfire/include/pakfire/pakfire.h new file mode 100644 index 000000000..ce81b3fb9 --- /dev/null +++ b/src/libpakfire/include/pakfire/pakfire.h @@ -0,0 +1,44 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2017 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 . # +# # +#############################################################################*/ + +#ifndef PAKFIRE_PAKFIRE_H +#define PAKFIRE_PAKFIRE_H + +#include + +Pakfire pakfire_create(const char* path, const char* arch); + +Pakfire pakfire_ref(Pakfire pakfire); +void pakfire_unref(Pakfire pakfire); + +const char* pakfire_get_path(Pakfire pakfire); +const char* pakfire_get_arch(Pakfire pakfire); + +#ifdef PAKFIRE_PRIVATE + +struct _Pakfire { + char* path; + char* arch; + int nrefs; +}; + +#endif + +#endif /* PAKFIRE_PAKFIRE_H */ diff --git a/src/libpakfire/include/pakfire/types.h b/src/libpakfire/include/pakfire/types.h index feb69e2c3..300b8567b 100644 --- a/src/libpakfire/include/pakfire/types.h +++ b/src/libpakfire/include/pakfire/types.h @@ -21,6 +21,7 @@ #ifndef PAKFIRE_TYPES_H #define PAKFIRE_TYPES_H +typedef struct _Pakfire* Pakfire; typedef struct _PakfireArchive* PakfireArchive; typedef struct _PakfireCache* PakfireCache; typedef struct _PakfireFile* PakfireFile; diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index f6831e800..42172ad67 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -20,6 +20,13 @@ LIBPAKFIRE_0 { global: + # pakfire + pakfire_create; + pakfire_get_arch; + pakfire_get_path; + pakfire_ref; + pakfire_unref; + # archive pakfire_archive_create; pakfire_archive_extract; diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c new file mode 100644 index 000000000..f1a08ac2e --- /dev/null +++ b/src/libpakfire/pakfire.c @@ -0,0 +1,55 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2017 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 . # +# # +#############################################################################*/ + +#include +#include +#include + +Pakfire pakfire_create(const char* path, const char* arch) { + Pakfire pakfire = pakfire_calloc(1, sizeof(*pakfire)); + if (pakfire) { + pakfire->path = pakfire_strdup(path); + pakfire->arch = pakfire_strdup(arch); + } + + return pakfire; +} + +Pakfire pakfire_ref(Pakfire pakfire) { + ++pakfire->nrefs; + + return pakfire; +} + +void pakfire_unref(Pakfire pakfire) { + if (--pakfire->nrefs > 0) + return; + + pakfire_free(pakfire->path); + pakfire_free(pakfire->arch); +} + +const char* pakfire_get_path(Pakfire pakfire) { + return pakfire->path; +} + +const char* pakfire_get_arch(Pakfire pakfire) { + return pakfire->arch; +} diff --git a/src/pakfire/base.py b/src/pakfire/base.py index e2e9c09ee..33024fa2a 100644 --- a/src/pakfire/base.py +++ b/src/pakfire/base.py @@ -41,16 +41,12 @@ from .system import system from .constants import * from .i18n import _ -class Pakfire(object): +class Pakfire(_pakfire.Pakfire): __version__ = PAKFIRE_VERSION mode = None def __init__(self, path="/", config=None, arch=None, distro=None, cache_path=None): - # The path where we are operating in - self.path = path - - # Default to system architecture - self.arch = arch or system.arch + _pakfire.Pakfire.__init__(self, path, arch or system.native_arch) # Default to system distribution self.distro = distro or system.distro @@ -68,7 +64,7 @@ class Pakfire(object): # Initialize the keyring #self.keyring = keyring.Keyring(self) - self.pool = _pakfire.Pool(self.arch.name) + self.pool = _pakfire.Pool(self.arch) self.pool.cache_path = cache_path or \ os.path.join(CACHE_DIR, self.distro.sname, self.distro.release) diff --git a/src/pakfire/builder.py b/src/pakfire/builder.py index 694714458..a1eb6a8dc 100644 --- a/src/pakfire/builder.py +++ b/src/pakfire/builder.py @@ -31,6 +31,7 @@ import time import uuid from . import _pakfire +from . import arch from . import base from . import cgroup from . import config @@ -451,6 +452,9 @@ class BuilderContext(object): # Get a reference to Pakfire self.pakfire = self.builder.pakfire + # Architecture + self.arch = arches.Arch(self.pakfire.arch) + # Get a reference to the logger self.log = self.builder.log @@ -484,11 +488,11 @@ class BuilderContext(object): # Fake UTS_MACHINE, when we cannot use the personality syscall and # if the host architecture is not equal to the target architecture. - if not self.pakfire.arch.personality and \ - not system.native_arch == self.pakfire.arch.name: + if not self.arch.personality and \ + not system.native_arch == self.arch.name: env.update({ "LD_PRELOAD" : "/usr/lib/libpakfire_preload.so", - "UTS_MACHINE" : self.pakfire.arch.name, + "UTS_MACHINE" : self.arch.name, }) return env diff --git a/src/pakfire/repository/__init__.py b/src/pakfire/repository/__init__.py index 870e31f5d..3d2098ad8 100644 --- a/src/pakfire/repository/__init__.py +++ b/src/pakfire/repository/__init__.py @@ -124,7 +124,7 @@ class Repositories(object): # Handle variable expansion. replaces = { "name" : name, - "arch" : self.pakfire.arch.name, + "arch" : self.pakfire.arch, } for k, v in list(_args.items()): diff --git a/src/pakfire/repository/remote.py b/src/pakfire/repository/remote.py index f7c1b3974..4ad4b221a 100644 --- a/src/pakfire/repository/remote.py +++ b/src/pakfire/repository/remote.py @@ -184,7 +184,7 @@ class RepositoryRemote(base.RepositoryFactory): downloader = self.make_downloader() while True: - data = downloader.get("%s/repodata/repomd.json" % self.pakfire.arch.name, decode="ascii") + data = downloader.get("%s/repodata/repomd.json" % self.pakfire.arch, decode="ascii") # Parse new metadata for comparison md = metadata.Metadata(self.pakfire, metadata=data)