rpm 5.2.1

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

python/header-py.c
Go to the documentation of this file.

    00001
    00005 #include "system.h"
    00006
    00007 #include "rpmio_internal.h"
    00008 #include <rpmcb.h>
    00009
    00010 #include "legacy.h"
    00011 #define _RPMTAG_INTERNAL
    00012 #include "header_internal.h"    /* XXX HEADERFLAG_ALLOCATED */
    00013 #include "rpmtypes.h"
    00014 #define _RPMEVR_INTERNAL
    00015 #include "rpmevr.h"
    00016 #include "pkgio.h"              /* XXX rpmpkgRead */
    00017
    00018 #include "rpmts.h"      /* XXX rpmtsCreate/rpmtsFree */
    00019
    00020 #include "rpmcli.h"
    00021
    00022 #include "header-py.h"
    00023 #include "rpmds-py.h"
    00024 #include "rpmfi-py.h"
    00025
    00026 #include "debug.h"
    00027
    00138 struct hdrObject_s {
    00139     PyObject_HEAD
    00140     Header h;
    00141 } ;
    00142
    00145 /*@unused@*/ static inline Header headerAllocated(Header h)
    00146         /*@modifies h @*/
    00147 {
    00148     h->flags |= HEADERFLAG_ALLOCATED;
    00149     return 0;
    00150 }
    00151
    00156
    00159 static PyObject * hdrKeyList(hdrObject * s)
    00160         /*@*/
    00161 {
    00162     HE_t he = memset(alloca(sizeof(*he)), 0, sizeof(*he));
    00163     PyObject * list, *o;
    00164     HeaderIterator hi;
    00165
    00166     list = PyList_New(0);
    00167
    00168     for (hi = headerInit(s->h);
    00169         headerNext(hi, he, 0);
    00170         he->p.ptr = _free(he->p.ptr))
    00171     {
    00172         if (he->tag == HEADER_I18NTABLE) continue;
    00173         switch (he->t) {
    00174         case RPM_I18NSTRING_TYPE:
    00175             break;
    00176         case RPM_BIN_TYPE:
    00177         case RPM_UINT64_TYPE:
    00178         case RPM_UINT32_TYPE:
    00179         case RPM_UINT16_TYPE:
    00180         case RPM_UINT8_TYPE:
    00181         case RPM_STRING_ARRAY_TYPE:
    00182         case RPM_STRING_TYPE:
    00183             PyList_Append(list, o=PyInt_FromLong(he->tag));
    00184             Py_DECREF(o);
    00185             break;
    00186         }
    00187     }
    00188     hi = headerFini(hi);
    00189
    00190     return list;
    00191 }
    00192
    00195 static PyObject * hdrUnload(hdrObject * s, PyObject * args, PyObject *keywords)
    00196         /*@*/
    00197 {
    00198     char * buf;
    00199     PyObject * rc;
    00200     int legacy = 0;
    00201     int nb;
    00202     Header h;
    00203     static char *kwlist[] = { "legacyHeader", NULL};
    00204
    00205     if (!PyArg_ParseTupleAndKeywords(args, keywords, "|i", kwlist, &legacy))
    00206         return NULL;
    00207
    00208     h = headerLink(s->h);
    00209     /* XXX this legacy switch is a hack, needs to be removed. */
    00210     if (legacy) {
    00211         h = headerCopy(s->h);   /* XXX strip region tags, etc */
    00212         (void)headerFree(s->h);
    00213         s->h = NULL;
    00214     }
    00215     {   size_t len;
    00216         buf = headerUnload(h, &len);
    00217         nb = len;
    00218         nb -= 8;        /* XXX HEADER_MAGIC_NO */
    00219     }
    00220     (void)headerFree(h);
    00221     h = NULL;
    00222
    00223     if (buf == NULL || nb == 0) {
    00224         PyErr_SetString(pyrpmError, "can't unload bad header\n");
    00225         return NULL;
    00226     }
    00227
    00228     rc = PyString_FromStringAndSize(buf, nb);
    00229     buf = _free(buf);
    00230
    00231     return rc;
    00232 }
    00233
    00236 static PyObject * hdrGetOrigin(hdrObject * s)
    00237         /*@*/
    00238 {
    00239     const char * origin = NULL;
    00240     if (s->h != NULL)
    00241
    00242         origin = headerGetOrigin(s->h);
    00243     if (origin != NULL)
    00244         return Py_BuildValue("s", origin);
    00245     Py_INCREF(Py_None);
    00246     return Py_None;
    00247 }
    00248
    00251 static PyObject * hdrSetOrigin(hdrObject * s, PyObject * args, PyObject * kwds)
    00252         /*@*/
    00253 {
    00254     char * kwlist[] = {"origin", NULL};
    00255     const char * origin = NULL;
    00256
    00257     if (!PyArg_ParseTupleAndKeywords(args, kwds, "s:SetOrigin", kwlist, &origin))
    00258         return NULL;
    00259
    00260     if (s->h != NULL && origin != NULL)
    00261         headerSetOrigin(s->h, origin);
    00262
    00263     Py_INCREF(Py_None);
    00264     return Py_None;
    00265 }
    00266
    00269 static PyObject * hdrSprintf(hdrObject * s, PyObject * args, PyObject * kwds)
    00270         /*@*/
    00271 {
    00272     char * fmt;
    00273     char * r;
    00274     errmsg_t err;
    00275     PyObject * result;
    00276     char * kwlist[] = {"format", NULL};
    00277
    00278     if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &fmt))
    00279         return NULL;
    00280
    00281     r = headerSprintf(s->h, fmt, NULL, rpmHeaderFormats, &err);
    00282     if (!r) {
    00283         PyErr_SetString(pyrpmError, err);
    00284         return NULL;
    00285     }
    00286
    00287     result = Py_BuildValue("s", r);
    00288     r = _free(r);
    00289
    00290     return result;
    00291 }
    00292
    00297 /*@unchecked@*/ /*@observer@*/
    00298 static struct PyMethodDef hdr_methods[] = {
    00299     {"keys",            (PyCFunction) hdrKeyList,       METH_NOARGS,
    00300         NULL },
    00301     {"unload",          (PyCFunction) hdrUnload,        METH_VARARGS|METH_KEYWORDS,
    00302         NULL },
    00303     {"getorigin",       (PyCFunction) hdrGetOrigin,     METH_NOARGS,
    00304         NULL },
    00305     {"setorigin",       (PyCFunction) hdrSetOrigin,     METH_NOARGS,
    00306         NULL },
    00307     {"sprintf",         (PyCFunction) hdrSprintf,       METH_VARARGS|METH_KEYWORDS,
    00308         NULL },
    00309
    00310     {"dsOfHeader",      (PyCFunction)hdr_dsOfHeader,    METH_NOARGS,
    00311         NULL},
    00312     {"dsFromHeader",    (PyCFunction)hdr_dsFromHeader,  METH_VARARGS|METH_KEYWORDS,
    00313         NULL},
    00314     {"fiFromHeader",    (PyCFunction)hdr_fiFromHeader,  METH_VARARGS|METH_KEYWORDS,
    00315         NULL},
    00316
    00317     {NULL,              NULL}           /* sentinel */
    00318 };
    00319
    00322 static int hdr_compare(hdrObject * a, hdrObject * b)
    00323         /*@*/
    00324 {
    00325     return rpmVersionCompare(a->h, b->h);
    00326 }
    00327
    00328 static long hdr_hash(PyObject * h)
    00329 {
    00330     return (long) h;
    00331 }
    00332
    00335 static void hdr_dealloc(hdrObject * s)
    00336         /*@*/
    00337 {
    00338     if (s->h) headerFree(s->h);
    00339     s->h = NULL;
    00340     PyObject_Del(s);
    00341 }
    00342
    00345 rpmTag tagNumFromPyObject (PyObject *item)
    00346 {
    00347     char * str;
    00348
    00349     if (PyInt_Check(item)) {
    00350         return (rpmTag) PyInt_AsLong(item);
    00351     } else if (PyString_Check(item) || PyUnicode_Check(item)) {
    00352         str = PyString_AsString(item);
    00353         return tagValue(str);
    00354     }
    00355     return (rpmTag)0xffffffff;
    00356 }
    00357
    00360 static PyObject * hdr_subscript(hdrObject * s, PyObject * item)
    00361         /*@*/
    00362 {
    00363     HE_t he = memset(alloca(sizeof(*he)), 0, sizeof(*he));
    00364     rpmTag tag = (rpmTag)0xffffffff;
    00365     uint32_t i;
    00366     PyObject * o, * metao;
    00367     int forceArray = 0;
    00368     const struct headerSprintfExtension_s * ext = NULL;
    00369     int xx;
    00370
    00371     if (PyCObject_Check (item))
    00372         ext = PyCObject_AsVoidPtr(item);
    00373     else
    00374         tag = tagNumFromPyObject (item);
    00375
    00376     if (tag == (rpmTag)0xffffffff && (PyString_Check(item) || PyUnicode_Check(item))) {
    00377         const struct headerSprintfExtension_s * extensions = rpmHeaderFormats;
    00378         char * str;
    00379         /* if we still don't have the tag, go looking for the header
    00380            extensions */
    00381         str = PyString_AsString(item);
    00382         while (extensions->name) {
    00383             if (extensions->type == HEADER_EXT_TAG
    00384              && !xstrcasecmp(extensions->name + 7, str)) {
    00385                 ext = extensions;
    00386             }
    00387             extensions++;
    00388             if (extensions->type == HEADER_EXT_MORE)
    00389                 extensions = *extensions->u.more;
    00390         }
    00391     }
    00392
    00393     /* Retrieve data from extension or header. */
    00394     if (ext) {
    00395         ext->u.tagFunction(s->h, he);
    00396     } else {
    00397         if (tag == (rpmTag)0xffffffff) {
    00398             PyErr_SetString(PyExc_KeyError, "unknown header tag");
    00399             return NULL;
    00400         }
    00401
    00402         he->tag = tag;
    00403         xx = headerGet(s->h, he, 0);
    00404         if (!xx) {
    00405             he->p.ptr = _free(he->p.ptr);
    00406             switch (tag) {
    00407             case RPMTAG_EPOCH:
    00408             case RPMTAG_NAME:
    00409             case RPMTAG_VERSION:
    00410             case RPMTAG_RELEASE:
    00411             case RPMTAG_DISTEPOCH:
    00412             case RPMTAG_ARCH:
    00413             case RPMTAG_OS:
    00414                 Py_INCREF(Py_None);
    00415                 return Py_None;
    00416                 break;
    00417             default:
    00418                 return PyList_New(0);
    00419                 break;
    00420             }
    00421         }
    00422     }
    00423
    00424     switch (tag) {
    00425     case RPMTAG_FILEPATHS:
    00426     case RPMTAG_ORIGPATHS:
    00427     case RPMTAG_OLDFILENAMES:
    00428     case RPMTAG_FILESIZES:
    00429     case RPMTAG_FILESTATES:
    00430     case RPMTAG_FILEMODES:
    00431     case RPMTAG_FILEUIDS:
    00432     case RPMTAG_FILEGIDS:
    00433     case RPMTAG_FILERDEVS:
    00434     case RPMTAG_FILEMTIMES:
    00435     case RPMTAG_FILEMD5S:
    00436     case RPMTAG_FILELINKTOS:
    00437     case RPMTAG_FILEFLAGS:
    00438     case RPMTAG_ROOT:
    00439     case RPMTAG_FILEUSERNAME:
    00440     case RPMTAG_FILEGROUPNAME:
    00441     case RPMTAG_REQUIRENAME:
    00442     case RPMTAG_REQUIREFLAGS:
    00443     case RPMTAG_REQUIREVERSION:
    00444     case RPMTAG_PROVIDENAME:
    00445     case RPMTAG_PROVIDEFLAGS:
    00446     case RPMTAG_PROVIDEVERSION:
    00447     case RPMTAG_OBSOLETENAME:
    00448     case RPMTAG_OBSOLETEFLAGS:
    00449     case RPMTAG_OBSOLETEVERSION:
    00450     case RPMTAG_CONFLICTNAME:
    00451     case RPMTAG_CONFLICTFLAGS:
    00452     case RPMTAG_CONFLICTVERSION:
    00453     case RPMTAG_CHANGELOGTIME:
    00454     case RPMTAG_FILEVERIFYFLAGS:
    00455         forceArray = 1;
    00456         break;
    00457     default:
    00458         break;
    00459     }
    00460
    00461     switch (he->t) {
    00462     case RPM_BIN_TYPE:
    00463         o = PyString_FromStringAndSize(he->p.str, he->c);
    00464         break;
    00465
    00466     case RPM_UINT8_TYPE:
    00467         if (he->c != 1 || forceArray) {
    00468             metao = PyList_New(0);
    00469             for (i = 0; i < he->c; i++) {
    00470                 o = PyInt_FromLong(he->p.ui8p[i]);
    00471                 PyList_Append(metao, o);
    00472                 Py_DECREF(o);
    00473             }
    00474             o = metao;
    00475         } else {
    00476             o = PyInt_FromLong(he->p.ui8p[0]);
    00477         }
    00478         break;
    00479
    00480     case RPM_UINT16_TYPE:
    00481         if (he->c != 1 || forceArray) {
    00482             metao = PyList_New(0);
    00483             for (i = 0; i < he->c; i++) {
    00484                 o = PyInt_FromLong(he->p.ui16p[i]);
    00485                 PyList_Append(metao, o);
    00486                 Py_DECREF(o);
    00487             }
    00488             o = metao;
    00489         } else {
    00490             o = PyInt_FromLong(he->p.ui16p[0]);
    00491         }
    00492         break;
    00493
    00494     case RPM_UINT32_TYPE:
    00495         if (he->c != 1 || forceArray) {
    00496             metao = PyList_New(0);
    00497             for (i = 0; i < he->c; i++) {
    00498                 o = PyInt_FromLong(he->p.ui32p[i]);
    00499                 PyList_Append(metao, o);
    00500                 Py_DECREF(o);
    00501             }
    00502             o = metao;
    00503         } else {
    00504             o = PyInt_FromLong(he->p.ui32p[0]);
    00505         }
    00506         break;
    00507
    00508     case RPM_UINT64_TYPE:
    00509         if (he->c != 1 || forceArray) {
    00510             metao = PyList_New(0);
    00511             for (i = 0; i < he->c; i++) {
    00512                 o = PyInt_FromLong(he->p.ui64p[i]);
    00513                 PyList_Append(metao, o);
    00514                 Py_DECREF(o);
    00515             }
    00516             o = metao;
    00517         } else {
    00518             o = PyInt_FromLong(he->p.ui64p[0]);
    00519         }
    00520         break;
    00521
    00522     case RPM_STRING_ARRAY_TYPE:
    00523         metao = PyList_New(0);
    00524         for (i = 0; i < he->c; i++) {
    00525             o = PyString_FromString(he->p.argv[i]);
    00526             PyList_Append(metao, o);
    00527             Py_DECREF(o);
    00528         }
    00529         o = metao;
    00530         break;
    00531
    00532     case RPM_STRING_TYPE:
    00533         if (he->p.str != NULL)
    00534             o = PyString_FromString(he->p.str);
    00535         else
    00536             o = PyString_FromString("");
    00537         break;
    00538
    00539     default:
    00540         PyErr_SetString(PyExc_TypeError, "unsupported type in header");
    00541         return NULL;
    00542     }
    00543     if (he->freeData)
    00544         he->p.ptr = _free(he->p.ptr);
    00545
    00546     return o;
    00547 }
    00548
    00551 /*@unchecked@*/ /*@observer@*/
    00552 static PyMappingMethods hdr_as_mapping = {
    00553         (lenfunc) 0,                    /* mp_length */
    00554         (binaryfunc) hdr_subscript,     /* mp_subscript */
    00555         (objobjargproc) 0,              /* mp_ass_subscript */
    00556 };
    00557
    00558 static PyObject * hdr_getattro(hdrObject * o, PyObject * n)
    00559         /*@*/
    00560 {
    00561     PyObject * res;
    00562     res = PyObject_GenericGetAttr((PyObject *)o, n);
    00563     if (res == NULL)
    00564         res = hdr_subscript(o, n);
    00565     return res;
    00566 }
    00567
    00568 static int hdr_setattro(hdrObject * o, PyObject * n, PyObject * v)
    00569         /*@*/
    00570 {
    00571     return PyObject_GenericSetAttr((PyObject *)o, n, v);
    00572 }
    00573
    00576 static char hdr_doc[] =
    00577 "";
    00578
    00581 /*@unchecked@*/ /*@observer@*/
    00582 PyTypeObject hdr_Type = {
    00583         PyObject_HEAD_INIT(&PyType_Type)
    00584         0,                              /* ob_size */
    00585         "rpm.hdr",                      /* tp_name */
    00586         sizeof(hdrObject),              /* tp_size */
    00587         0,                              /* tp_itemsize */
    00588         (destructor) hdr_dealloc,       /* tp_dealloc */
    00589         0,                              /* tp_print */
    00590         (getattrfunc) 0,                /* tp_getattr */
    00591         0,                              /* tp_setattr */
    00592         (cmpfunc) hdr_compare,          /* tp_compare */
    00593         0,                              /* tp_repr */
    00594         0,                              /* tp_as_number */
    00595         0,                              /* tp_as_sequence */
    00596         &hdr_as_mapping,                /* tp_as_mapping */
    00597         hdr_hash,                       /* tp_hash */
    00598         0,                              /* tp_call */
    00599         0,                              /* tp_str */
    00600         (getattrofunc) hdr_getattro,    /* tp_getattro */
    00601         (setattrofunc) hdr_setattro,    /* tp_setattro */
    00602         0,                              /* tp_as_buffer */
    00603         Py_TPFLAGS_DEFAULT,             /* tp_flags */
    00604         hdr_doc,                        /* tp_doc */
    00605 #if Py_TPFLAGS_HAVE_ITER
    00606         0,                              /* tp_traverse */
    00607         0,                              /* tp_clear */
    00608         0,                              /* tp_richcompare */
    00609         0,                              /* tp_weaklistoffset */
    00610         0,                              /* tp_iter */
    00611         0,                              /* tp_iternext */
    00612         hdr_methods,                    /* tp_methods */
    00613         0,                              /* tp_members */
    00614         0,                              /* tp_getset */
    00615         0,                              /* tp_base */
    00616         0,                              /* tp_dict */
    00617         0,                              /* tp_descr_get */
    00618         0,                              /* tp_descr_set */
    00619         0,                              /* tp_dictoffset */
    00620         0,                              /* tp_init */
    00621         0,                              /* tp_alloc */
    00622         0,                              /* tp_new */
    00623         0,                              /* tp_free */
    00624         0,                              /* tp_is_gc */
    00625 #endif
    00626 };
    00627
    00628 hdrObject * hdr_Wrap(Header h)
    00629 {
    00630     hdrObject * hdr = PyObject_New(hdrObject, &hdr_Type);
    00631     hdr->h = headerLink(h);
    00632     return hdr;
    00633 }
    00634
    00635 Header hdrGetHeader(hdrObject * s)
    00636 {
    00637     return s->h;
    00638 }
    00639
    00642 PyObject * hdrLoad(PyObject * self, PyObject * args, PyObject * kwds)
    00643 {
    00644     hdrObject * hdr;
    00645     char * copy = NULL;
    00646     char * obj;
    00647     Header h;
    00648     int len;
    00649     char * kwlist[] = {"headers", NULL};
    00650
    00651     if (!PyArg_ParseTupleAndKeywords(args, kwds, "s#", kwlist, &obj, &len))
    00652         return NULL;
    00653
    00654     /* malloc is needed to avoid surprises from data swab in headerLoad(). */
    00655     copy = malloc(len);
    00656     if (copy == NULL) {
    00657         PyErr_SetString(pyrpmError, "out of memory");
    00658         return NULL;
    00659     }
    00660     memcpy (copy, obj, len);
    00661
    00662     h = headerLoad(copy);
    00663     if (!h) {
    00664         PyErr_SetString(pyrpmError, "bad header");
    00665         return NULL;
    00666     }
    00667     headerAllocated(h);
    00668
    00669     hdr = hdr_Wrap(h);
    00670     (void)headerFree(h);        /* XXX ref held by hdr */
    00671     h = NULL;
    00672
    00673     return (PyObject *) hdr;
    00674 }
    00675
    00678 PyObject * rpmReadHeaders (FD_t fd)
    00679 {
    00680     PyObject * list;
    00681     Header h;
    00682     hdrObject * hdr;
    00683
    00684     if (!fd) {
    00685         PyErr_SetFromErrno(pyrpmError);
    00686         return NULL;
    00687     }
    00688
    00689     list = PyList_New(0);
    00690     Py_BEGIN_ALLOW_THREADS
    00691     {   const char item[] = "Header";
    00692         const char * msg = NULL;
    00693         rpmRC rc = rpmpkgRead(item, fd, &h, &msg);
    00694         if(rc == RPMRC_NOTFOUND) {
    00695                 Py_INCREF(Py_None);
    00696                 list = Py_None;
    00697         }
    00698         else if (rc != RPMRC_OK)
    00699             rpmlog(RPMLOG_ERR, "%s: %s: %s : error code: %d\n", "rpmpkgRead", item, msg, rc);
    00700         msg = _free(msg);
    00701     }
    00702     Py_END_ALLOW_THREADS
    00703
    00704     while (h) {
    00705         hdr = hdr_Wrap(h);
    00706         if (PyList_Append(list, (PyObject *) hdr)) {
    00707             Py_DECREF(list);
    00708             Py_DECREF(hdr);
    00709             return NULL;
    00710         }
    00711         Py_DECREF(hdr);
    00712
    00713         (void)headerFree(h);    /* XXX ref held by hdr */
    00714         h = NULL;
    00715
    00716         Py_BEGIN_ALLOW_THREADS
    00717         {   const char item[] = "Header";
    00718             const char * msg = NULL;
    00719             rpmRC rc = rpmpkgRead(item, fd, &h, &msg);
    00720             if(rc == RPMRC_NOTFOUND) {
    00721                     Py_INCREF(Py_None);
    00722                     list = Py_None;
    00723             }
    00724             else if (rc != RPMRC_OK)
    00725                 rpmlog(RPMLOG_ERR, "%s: %s: %s : error code: %d\n", "rpmpkgRead", item, msg, rc);
    00726             msg = _free(msg);
    00727         }
    00728         Py_END_ALLOW_THREADS
    00729     }
    00730
    00731     return list;
    00732 }
    00733
    00736 PyObject * rpmHeaderFromFD(PyObject * self, PyObject * args,
    00737                 PyObject * kwds)
    00738 {
    00739     FD_t fd;
    00740     int fileno;
    00741     PyObject * list;
    00742     char * kwlist[] = {"fd", NULL};
    00743
    00744     if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &fileno))
    00745         return NULL;
    00746
    00747     fd = fdDup(fileno);
    00748
    00749     list = rpmReadHeaders (fd);
    00750     Fclose(fd);
    00751
    00752     return list;
    00753 }
    00754
    00757 PyObject * rpmHeaderFromFile(PyObject * self, PyObject * args,
    00758                 PyObject *kwds)
    00759 {
    00760     char * filespec;
    00761     FD_t fd;
    00762     PyObject * list;
    00763     char * kwlist[] = {"file", NULL};
    00764
    00765     if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &filespec))
    00766         return NULL;
    00767
    00768     fd = Fopen(filespec, "r.fdio");
    00769
    00770     if (!fd) {
    00771         PyErr_SetFromErrno(pyrpmError);
    00772         return NULL;
    00773     }
    00774
    00775     list = rpmReadHeaders (fd);
    00776     Fclose(fd);
    00777
    00778     return list;
    00779 }
    00780
    00783 PyObject *
    00784 rpmSingleHeaderFromFD(PyObject * self, PyObject * args,
    00785                 PyObject * kwds)
    00786 {
    00787     FD_t fd;
    00788     int fileno;
    00789     off_t offset;
    00790     PyObject * tuple;
    00791     Header h;
    00792     char * kwlist[] = {"fd", NULL};
    00793
    00794     if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &fileno))
    00795         return NULL;
    00796
    00797     offset = lseek(fileno, 0, SEEK_CUR);
    00798
    00799     fd = fdDup(fileno);
    00800
    00801     if (!fd) {
    00802         PyErr_SetFromErrno(pyrpmError);
    00803         return NULL;
    00804     }
    00805
    00806     Py_BEGIN_ALLOW_THREADS
    00807     {   const char item[] = "Header";
    00808         const char * msg = NULL;
    00809         rpmRC rc = rpmpkgRead(item, fd, &h, &msg);
    00810         if(rc == RPMRC_NOTFOUND) {
    00811                 Py_INCREF(Py_None);
    00812                 tuple = Py_None;
    00813         }
    00814         else if (rc != RPMRC_OK)
    00815             rpmlog(RPMLOG_ERR, "%s: %s: %s : error code: %d\n", "rpmpkgRead", item, msg, rc);
    00816         msg = _free(msg);
    00817     }
    00818     Py_END_ALLOW_THREADS
    00819
    00820     Fclose(fd);
    00821
    00822     tuple = PyTuple_New(2);
    00823
    00824     if (h && tuple) {
    00825         PyTuple_SET_ITEM(tuple, 0, (PyObject *) hdr_Wrap(h));
    00826         PyTuple_SET_ITEM(tuple, 1, PyLong_FromLong(offset));
    00827         (void)headerFree(h);
    00828         h = NULL;
    00829     } else {
    00830         Py_INCREF(Py_None);
    00831         Py_INCREF(Py_None);
    00832         PyTuple_SET_ITEM(tuple, 0, Py_None);
    00833         PyTuple_SET_ITEM(tuple, 1, Py_None);
    00834     }
    00835
    00836     return tuple;
    00837 }
    00838
    00841 PyObject * rpmWriteHeaders (PyObject * list, FD_t fd)
    00842 {
    00843     int count;
    00844
    00845     if (!fd) {
    00846         PyErr_SetFromErrno(pyrpmError);
    00847         return NULL;
    00848     }
    00849
    00850     for(count = 0; count < PyList_Size(list); count++){
    00851         Py_BEGIN_ALLOW_THREADS
    00852         const char item[] = "Header";
    00853         const char * msg = NULL;
    00854         hdrObject * hdr = (hdrObject *)PyList_GetItem(list, count);
    00855         rpmRC rc = rpmpkgWrite(item, fd, hdr->h, &msg);
    00856         if (rc != RPMRC_OK)
    00857             rpmlog(RPMLOG_ERR, "%s: %s: %s : error code: %d\n", "rpmpkgWrite", item, msg, rc);
    00858         msg = _free(msg);
    00859         Py_END_ALLOW_THREADS
    00860     }
    00861
    00862     Py_RETURN_TRUE;
    00863 }
    00864
    00867 PyObject * rpmHeaderToFD(PyObject * self, PyObject * args,
    00868                 PyObject * kwds)
    00869 {
    00870     FD_t fd;
    00871     int fileno;
    00872     PyObject * list;
    00873     PyObject * ret;
    00874     char * kwlist[] = {"headers", "fd", NULL};
    00875
    00876     if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oi", kwlist, &list, &fileno))
    00877         return NULL;
    00878
    00879     fd = fdDup(fileno);
    00880
    00881     ret = rpmWriteHeaders (list, fd);
    00882     Fclose(fd);
    00883
    00884     return list;
    00885 }
    00886
    00889 PyObject * rpmHeaderToFile(PyObject * self, PyObject * args,
    00890                 PyObject *kwds)
    00891 {
    00892     char * filespec;
    00893     FD_t fd;
    00894     PyObject * list;
    00895     PyObject * ret;
    00896     char * kwlist[] = {"headers", "file", NULL};
    00897
    00898     if (!PyArg_ParseTupleAndKeywords(args, kwds, "Os", kwlist, &list, &filespec))
    00899         return NULL;
    00900
    00901     fd = Fopen(filespec, "w.fdio");
    00902     if (!fd) {
    00903         PyErr_SetFromErrno(pyrpmError);
    00904         return NULL;
    00905     }
    00906
    00907     ret = rpmWriteHeaders (list, fd);
    00908     Fclose(fd);
    00909
    00910     return ret;
    00911 }
    00912
    00915 PyObject * versionCompare (PyObject * self, PyObject * args,
    00916                 PyObject * kwds)
    00917 {
    00918     hdrObject * h1, * h2;
    00919     char * kwlist[] = {"version0", "version1", NULL};
    00920
    00921     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O!", kwlist, &hdr_Type,
    00922             &h1, &hdr_Type, &h2))
    00923         return NULL;
    00924
    00925     return Py_BuildValue("i", hdr_compare(h1, h2));
    00926 }
    00927
    00928 PyObject * labelCompare (PyObject * self, PyObject * args)
    00929 {
    00930     EVR_t a = rpmEVRnew(RPMSENSE_EQUAL, 1);
    00931     EVR_t b = rpmEVRnew(RPMSENSE_EQUAL, 1);
    00932     int rc;
    00933     PyObject *aTuple, *bTuple;
    00934
    00935     if (!PyArg_ParseTuple(args, "OO", &aTuple, &bTuple) ||
    00936             !PyArg_ParseTuple(aTuple, "zzz|z",
    00937                 &a->F[RPMEVR_E], &a->F[RPMEVR_V], &a->F[RPMEVR_R], &a->F[RPMEVR_D]) ||
    00938             !PyArg_ParseTuple(bTuple, "zzz|z",
    00939                 &b->F[RPMEVR_E], &b->F[RPMEVR_V], &b->F[RPMEVR_R], &b->F[RPMEVR_D]))
    00940     {
    00941         a = rpmEVRfree(a);
    00942         b = rpmEVRfree(b);
    00943         return NULL;
    00944     }
    00945
    00946     /* XXX HACK: postpone committing to single "missing" value for now. */
    00947     if (a->F[RPMEVR_E] == NULL) a->F[RPMEVR_E] = "0";
    00948     if (b->F[RPMEVR_E] == NULL) b->F[RPMEVR_E] = "0";
    00949     if (a->F[RPMEVR_V] == NULL) a->F[RPMEVR_V] = "";
    00950     if (b->F[RPMEVR_V] == NULL) b->F[RPMEVR_V] = "";
    00951     if (a->F[RPMEVR_R] == NULL) a->F[RPMEVR_R] = "";
    00952     if (b->F[RPMEVR_R] == NULL) b->F[RPMEVR_R] = "";
    00953     if (a->F[RPMEVR_D] == NULL) a->F[RPMEVR_D] = "";
    00954     if (b->F[RPMEVR_D] == NULL) b->F[RPMEVR_D] = "";
    00955
    00956     rc = rpmEVRcompare(a, b);
    00957
    00958     a = rpmEVRfree(a);
    00959     b = rpmEVRfree(b);
    00960
    00961     return Py_BuildValue("i", rc);
    00962 }