From: jbj Date: Thu, 23 May 2002 00:49:19 +0000 (+0000) Subject: Bind rpmds as class derived from header. X-Git-Tag: tznext/4.11.0.1.tizen20130304~7403 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0f0184d17f13b9cb17d6b0ba4aefb29d0ad53e10;p=tools%2Flibrpm-tizen.git Bind rpmds as class derived from header. CVS patchset: 5444 CVS date: 2002/05/23 00:49:19 --- diff --git a/python/Makefile.am b/python/Makefile.am index 461fe69..fb99ac6 100644 --- a/python/Makefile.am +++ b/python/Makefile.am @@ -33,7 +33,8 @@ poptmodule_so_SOURCES = poptmodule.c poptmodule_so_LDFLAGS = $(mylibs) $(LIBS) -shared -Wl,-soname,poptmodule.so noinst_LTLIBRARIES = librpmmodule.la -librpmmodule_la_SOURCES = rpmmodule.c hash.c upgrade.c header-py.c db-py.c +librpmmodule_la_SOURCES = rpmmodule.c hash.c upgrade.c \ + db-py.c header-py.c rpmds-py.c rpmmodule.so$(EXEEXT): $(librpmmodule_la_OBJECTS) $(LINK) -o $@ $(librpmmodule_la_OBJECTS) $(rpmmodule_so_LDFLAGS) diff --git a/python/Makefile.in b/python/Makefile.in index 6e4b9b7..019ef69 100644 --- a/python/Makefile.in +++ b/python/Makefile.in @@ -229,7 +229,9 @@ poptmodule_so_SOURCES = poptmodule.c poptmodule_so_LDFLAGS = $(mylibs) $(LIBS) -shared -Wl,-soname,poptmodule.so noinst_LTLIBRARIES = librpmmodule.la -librpmmodule_la_SOURCES = rpmmodule.c hash.c upgrade.c header-py.c db-py.c +librpmmodule_la_SOURCES = rpmmodule.c hash.c upgrade.c \ + db-py.c header-py.c rpmds-py.c + subdir = python mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs CONFIG_HEADER = $(top_builddir)/config.h @@ -238,8 +240,8 @@ LTLIBRARIES = $(noinst_LTLIBRARIES) librpmmodule_la_LDFLAGS = librpmmodule_la_LIBADD = -am_librpmmodule_la_OBJECTS = rpmmodule.lo hash.lo upgrade.lo \ - header-py.lo db-py.lo +am_librpmmodule_la_OBJECTS = rpmmodule.lo hash.lo upgrade.lo db-py.lo \ + header-py.lo rpmds-py.lo librpmmodule_la_OBJECTS = $(am_librpmmodule_la_OBJECTS) python_PROGRAMS = rpmmodule.so$(EXEEXT) poptmodule.so$(EXEEXT) PROGRAMS = $(python_PROGRAMS) @@ -262,7 +264,8 @@ depcomp = $(SHELL) $(top_srcdir)/depcomp am__depfiles_maybe = depfiles @AMDEP_TRUE@DEP_FILES = ./$(DEPDIR)/db-py.Plo ./$(DEPDIR)/hash.Plo \ @AMDEP_TRUE@ ./$(DEPDIR)/header-py.Plo ./$(DEPDIR)/poptmodule.Po \ -@AMDEP_TRUE@ ./$(DEPDIR)/rpmmodule.Plo ./$(DEPDIR)/upgrade.Plo +@AMDEP_TRUE@ ./$(DEPDIR)/rpmds-py.Plo ./$(DEPDIR)/rpmmodule.Plo \ +@AMDEP_TRUE@ ./$(DEPDIR)/upgrade.Plo COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) LTCOMPILE = $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) \ @@ -328,6 +331,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/hash.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/header-py.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/poptmodule.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rpmds-py.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rpmmodule.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/upgrade.Plo@am__quote@ diff --git a/python/header-py.c b/python/header-py.c index aeec2fe..fc8b312 100644 --- a/python/header-py.c +++ b/python/header-py.c @@ -17,6 +17,7 @@ #include "header_internal.h" #include "header-py.h" +#include "rpmds-py.h" /** \ingroup python * \class header @@ -37,28 +38,33 @@ * import os, rpm * * fd = os.open("/tmp/foo-1.0-1.i386.rpm", os.O_RDONLY) - * (header, isSource) = rpm.headerFromPackage(fd) - * fd.close() + * hdr = rpm.headerFromPackage(fd)[0] + * os.close(fd) * \endcode + * * The Python interface to the header data is quite elegant. It * presents the data in a dictionary form. We'll take the header we * just loaded and access the data within it: * \code - * print header[rpm.RPMTAG_NAME] - * print header[rpm.RPMTAG_VERSION] - * print header[rpm.RPMTAG_RELEASE] + * print hdr[rpm.RPMTAG_NAME] + * print hdr[rpm.RPMTAG_VERSION] + * print hdr[rpm.RPMTAG_RELEASE] * \endcode - * in the case of our "foor-1.0-1.i386.rpm" package, this code would + * in the case of our "foo-1.0-1.i386.rpm" package, this code would * output: \verbatim foo 1.0 1 \endverbatim + * * You make also access the header data by string name: * \code - * print header['name'] + * print hdr['name'] + * print hdr['version'] + * print hdr['release'] * \endcode + * * This method of access is a bit slower because the name must be * translated into the tag number dynamically. You also must make sure * the strings in header lookups don't get translated, or the lookups @@ -532,6 +538,9 @@ static struct PyMethodDef hdrMethods[] = { {"fullFilelist", (PyCFunction) hdrFullFilelist, 1 }, {"rhnUnload", (PyCFunction) rhnUnload, METH_VARARGS }, {"sprintf", (PyCFunction) hdrSprintf, METH_VARARGS }, + + {"dsFromHeader", (PyCFunction)hdr_dsFromHeader, METH_VARARGS, NULL}, + {NULL, NULL} /* sentinel */ }; diff --git a/python/rpmds-py.c b/python/rpmds-py.c new file mode 100644 index 0000000..1def304 --- /dev/null +++ b/python/rpmds-py.c @@ -0,0 +1,247 @@ +/** \ingroup python + * \file python/rpmds-py.c + */ + +#include "system.h" + +#include "Python.h" + +#include +#include "rpmps.h" +#include "rpmds.h" + +#include "header-py.h" +#include "rpmds-py.h" + +#include "debug.h" + +static PyObject * +rpmds_Debug(rpmdsObject * s, PyObject * args) +{ + if (!PyArg_ParseTuple(args, "i", &_rpmds_debug)) return NULL; + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject * +rpmds_Count(rpmdsObject * s, PyObject * args) +{ + if (!PyArg_ParseTuple(args, "")) return NULL; + return Py_BuildValue("i", rpmdsCount(s->ds)); +} + +static PyObject * +rpmds_Ix(rpmdsObject * s, PyObject * args) +{ + if (!PyArg_ParseTuple(args, "")) return NULL; + return Py_BuildValue("i", rpmdsIx(s->ds)); +} + +static PyObject * +rpmds_DNEVR(rpmdsObject * s, PyObject * args) +{ + if (!PyArg_ParseTuple(args, "")) return NULL; + return Py_BuildValue("s", xstrdup(rpmdsDNEVR(s->ds))); +} + +static PyObject * +rpmds_N(rpmdsObject * s, PyObject * args) +{ + if (!PyArg_ParseTuple(args, "")) return NULL; + return Py_BuildValue("s", xstrdup(rpmdsN(s->ds))); +} + +static PyObject * +rpmds_EVR(rpmdsObject * s, PyObject * args) +{ + if (!PyArg_ParseTuple(args, "")) return NULL; + return Py_BuildValue("s", xstrdup(rpmdsEVR(s->ds))); +} + +static PyObject * +rpmds_Flags(rpmdsObject * s, PyObject * args) +{ + if (!PyArg_ParseTuple(args, "")) return NULL; + return Py_BuildValue("i", rpmdsFlags(s->ds)); +} + +static PyObject * +rpmds_TagN(rpmdsObject * s, PyObject * args) +{ + if (!PyArg_ParseTuple(args, "")) return NULL; + return Py_BuildValue("i", rpmdsTagN(s->ds)); +} + +#ifdef NOTYET +static PyObject * +rpmds_Notify(rpmdsObject * s, PyObject * args) +{ + if (!PyArg_ParseTuple(args, "")) + return NULL; + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject * +rpmds_Next(rpmdsObject * s, PyObject * args) +{ + if (!PyArg_ParseTuple(args, "")) + return NULL; + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject * +rpmds_Init(rpmdsObject * s, PyObject * args) +{ + if (!PyArg_ParseTuple(args, "")) + return NULL; + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject * +rpmds_Compare(rpmdsObject * s, PyObject * args) +{ + if (!PyArg_ParseTuple(args, "")) + return NULL; + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject * +rpmds_Problem(rpmdsObject * s, PyObject * args) +{ + if (!PyArg_ParseTuple(args, "")) + return NULL; + Py_INCREF(Py_None); + return Py_None; +} +#endif + + +static struct PyMethodDef rpmds_methods[] = { + {"Debug", (PyCFunction)rpmds_Debug, METH_VARARGS, NULL}, + {"Count", (PyCFunction)rpmds_Count, METH_VARARGS, NULL}, + {"Ix", (PyCFunction)rpmds_Ix, METH_VARARGS, NULL}, + {"DNEVR", (PyCFunction)rpmds_DNEVR, METH_VARARGS, NULL}, + {"N", (PyCFunction)rpmds_N, METH_VARARGS, NULL}, + {"EVR", (PyCFunction)rpmds_EVR, METH_VARARGS, NULL}, + {"Flags", (PyCFunction)rpmds_Flags, METH_VARARGS, NULL}, + {"TagN", (PyCFunction)rpmds_TagN, METH_VARARGS, NULL}, +#ifdef NOTYET + {"Notify", (PyCFunction)rpmds_Notify, METH_VARARGS, NULL}, + {"Next", (PyCFunction)rpmds_Next, METH_VARARGS, NULL}, + {"Init", (PyCFunction)rpmds_Init, METH_VARARGS, NULL}, + {"Compare", (PyCFunction)rpmds_Compare, METH_VARARGS, NULL}, + {"Problem", (PyCFunction)rpmds_Problem, METH_VARARGS, NULL}, +#endif + {NULL, NULL} /* sentinel */ +}; + +/* ---------- */ + +static void +rpmds_dealloc(rpmdsObject * s) +{ + if (s && s->ds) + s->ds = rpmdsFree(s->ds); + PyMem_DEL(s); +} + +static int +rpmds_print(rpmdsObject * s, FILE * fp, int flags) +{ + if (!(s && s->ds)) + return -1; + + rpmdsInit(s->ds); + while (rpmdsNext(s->ds) >= 0) + fprintf(fp, "%s\n", rpmdsDNEVR(s->ds)); + return 0; +} + +static PyObject * +rpmds_getattr(rpmdsObject * s, char * name) +{ + return Py_FindMethod(rpmds_methods, (PyObject *)s, name); +} + +static int +rpmds_length(rpmdsObject * s) +{ + return rpmdsCount(s->ds); +} + +static PyObject * +rpmds_subscript(rpmdsObject * s, PyObject * key) +{ + int ix; + + if (!PyInt_Check(key)) { + PyErr_SetString(PyExc_TypeError, "integer expected"); + return NULL; + } + + ix = (int) PyInt_AsLong(key); + rpmdsSetIx(s->ds, ix); + return Py_BuildValue("s", xstrdup(rpmdsDNEVR(s->ds))); +} + +static PyMappingMethods rpmds_as_mapping = { + (inquiry) rpmds_length, /* mp_length */ + (binaryfunc) rpmds_subscript, /* mp_subscript */ + (objobjargproc)0, /* mp_ass_subscript */ +}; + +static PyTypeObject rpmds_Type = { + PyObject_HEAD_INIT(&PyType_Type) + 0, /* ob_size */ + "rpmds", /* tp_name */ + sizeof(rpmdsObject), /* tp_basicsize */ + 0, /* tp_itemsize */ + /* methods */ + (destructor)rpmds_dealloc, /* tp_dealloc */ + (printfunc)rpmds_print, /* tp_print */ + (getattrfunc)rpmds_getattr, /* tp_getattr */ + (setattrfunc)0, /* tp_setattr */ + (cmpfunc)0, /* tp_compare */ + (reprfunc)0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + &rpmds_as_mapping, /* tp_as_mapping */ + (hashfunc)0, /* tp_hash */ + (ternaryfunc)0, /* tp_call */ + (reprfunc)0, /* tp_str */ + + /* Space for future expansion */ + 0L,0L,0L,0L, + NULL /* Documentation string */ +}; + +/* ---------- */ + +rpmds dsFromDs(rpmdsObject * s) +{ + return s->ds; +} + +rpmdsObject * +rpmds_New(rpmds ds) +{ + rpmdsObject *s = PyObject_NEW(rpmdsObject, &rpmds_Type); + if (s == NULL) + return NULL; + s->ds = ds; + return s; +} + +rpmdsObject * +hdr_dsFromHeader(PyObject * s, PyObject * args) +{ + hdrObject * ho; + + if (!PyArg_ParseTuple(args, "O!", &hdrType, &ho)) + return NULL; + return rpmds_New( rpmdsNew(hdrGetHeader(ho), RPMTAG_REQUIRENAME, 0) ); +} diff --git a/python/rpmds-py.h b/python/rpmds-py.h new file mode 100644 index 0000000..5cd2749 --- /dev/null +++ b/python/rpmds-py.h @@ -0,0 +1,21 @@ +#ifndef H_RPMDS_PY +#define H_RPMDS_PY + +/** \ingroup python + * \file python/rpmds-py.h + */ + +typedef struct rpmdsObject_s { + PyObject_HEAD + rpmds ds; +} rpmdsObject; + +extern PyTypeObject rpmds_Type; + +rpmds dsFromDs(rpmdsObject * ds); + +rpmdsObject * rpmds_New(rpmds ds); + +rpmdsObject * hdr_dsFromHeader(PyObject * s, PyObject * args); + +#endif