00001
00005 #include "system.h"
00006
00007 #include <rpmio.h>
00008 #include <rpmcb.h>
00009 #include <rpmlib.h>
00010
00011 #include "header-py.h"
00012 #include "rpmfi-py.h"
00013
00014 #include "debug.h"
00015
00016
00017
00018 #if Py_TPFLAGS_HAVE_ITER
00019 static PyObject *
00020 rpmfi_iter(rpmfiObject * s)
00021
00022 {
00023 Py_INCREF(s);
00024 return (PyObject *)s;
00025 }
00026 #endif
00027
00028
00029 static PyObject *
00030 rpmfi_iternext(rpmfiObject * s)
00031
00032
00033 {
00034 PyObject * result = NULL;
00035
00036
00037 if (!s->active) {
00038 s->fi = rpmfiInit(s->fi, 0);
00039 s->active = 1;
00040 }
00041
00042
00043 if (rpmfiNext(s->fi) >= 0) {
00044 const char * FN = rpmfiFN(s->fi);
00045 int FSize = rpmfiFSize(s->fi);
00046 int FMode = rpmfiFMode(s->fi);
00047 int FMtime = rpmfiFMtime(s->fi);
00048 int FFlags = rpmfiFFlags(s->fi);
00049 int FRdev = rpmfiFRdev(s->fi);
00050 int FInode = rpmfiFInode(s->fi);
00051 int FNlink = rpmfiFNlink(s->fi);
00052 int FState = rpmfiFState(s->fi);
00053 int VFlags = rpmfiVFlags(s->fi);
00054 const char * FUser = rpmfiFUser(s->fi);
00055 const char * FGroup = rpmfiFGroup(s->fi);
00056
00057 int dalgo = 0;
00058 size_t dlen = 0;
00059 const unsigned char * digest = rpmfiDigest(s->fi, &dalgo, &dlen);
00060 const unsigned char * s = digest;
00061
00062 const char * fdigest;
00063 char * t;
00064 static const char hex[] = "0123456789abcdef";
00065 int gotMD5, i;
00066
00067 fdigest = t = memset(alloca(dlen), 0, dlen);
00068 gotMD5 = 0;
00069 if (s)
00070 for (i = 0; i < 16; i++) {
00071 gotMD5 |= *s;
00072 *t++ = hex[ (*s >> 4) & 0xf ];
00073 *t++ = hex[ (*s++ ) & 0xf ];
00074 }
00075 *t = '\0';
00076
00077 result = PyTuple_New(13);
00078 if (FN == NULL) {
00079 Py_INCREF(Py_None);
00080 PyTuple_SET_ITEM(result, 0, Py_None);
00081 } else
00082 PyTuple_SET_ITEM(result, 0, Py_BuildValue("s", FN));
00083 PyTuple_SET_ITEM(result, 1, PyInt_FromLong(FSize));
00084 PyTuple_SET_ITEM(result, 2, PyInt_FromLong(FMode));
00085 PyTuple_SET_ITEM(result, 3, PyInt_FromLong(FMtime));
00086 PyTuple_SET_ITEM(result, 4, PyInt_FromLong(FFlags));
00087 PyTuple_SET_ITEM(result, 5, PyInt_FromLong(FRdev));
00088 PyTuple_SET_ITEM(result, 6, PyInt_FromLong(FInode));
00089 PyTuple_SET_ITEM(result, 7, PyInt_FromLong(FNlink));
00090 PyTuple_SET_ITEM(result, 8, PyInt_FromLong(FState));
00091 PyTuple_SET_ITEM(result, 9, PyInt_FromLong(VFlags));
00092 if (FUser == NULL) {
00093 Py_INCREF(Py_None);
00094 PyTuple_SET_ITEM(result, 10, Py_None);
00095 } else
00096 PyTuple_SET_ITEM(result, 10, Py_BuildValue("s", FUser));
00097 if (FGroup == NULL) {
00098 Py_INCREF(Py_None);
00099 PyTuple_SET_ITEM(result, 11, Py_None);
00100 } else
00101 PyTuple_SET_ITEM(result, 11, Py_BuildValue("s", FGroup));
00102 if (!gotMD5) {
00103 Py_INCREF(Py_None);
00104 PyTuple_SET_ITEM(result, 12, Py_None);
00105 } else
00106 PyTuple_SET_ITEM(result, 12, Py_BuildValue("s", fdigest));
00107
00108 } else
00109 s->active = 0;
00110
00111 return result;
00112 }
00113
00118
00119 static PyObject *
00120 rpmfi_Next(rpmfiObject * s)
00121
00122
00123 {
00124 PyObject * result = NULL;
00125
00126 result = rpmfi_iternext(s);
00127
00128 if (result == NULL) {
00129 Py_INCREF(Py_None);
00130 return Py_None;
00131 }
00132
00133 return result;
00134 }
00135
00136 #ifdef NOTYET
00137
00138 static PyObject *
00139 rpmfi_NextD(rpmfiObject * s)
00140
00141 {
00142 Py_INCREF(Py_None);
00143 return Py_None;
00144 }
00145
00146
00147 static PyObject *
00148 rpmfi_InitD(rpmfiObject * s)
00149
00150 {
00151 Py_INCREF(Py_None);
00152 return Py_None;
00153 }
00154 #endif
00155
00156
00157 static PyObject *
00158 rpmfi_Debug( rpmfiObject * s, PyObject * args, PyObject * kwds)
00159
00160
00161 {
00162 char * kwlist[] = {"debugLevel", NULL};
00163
00164 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &_rpmfi_debug))
00165 return NULL;
00166
00167 Py_INCREF(Py_None);
00168 return Py_None;
00169 }
00170
00171
00172 static PyObject *
00173 rpmfi_FC(rpmfiObject * s)
00174
00175 {
00176 return Py_BuildValue("i", rpmfiFC(s->fi));
00177 }
00178
00179
00180 static PyObject *
00181 rpmfi_FX(rpmfiObject * s)
00182
00183 {
00184 return Py_BuildValue("i", rpmfiFX(s->fi));
00185 }
00186
00187
00188 static PyObject *
00189 rpmfi_DC(rpmfiObject * s)
00190
00191 {
00192 return Py_BuildValue("i", rpmfiDC(s->fi));
00193 }
00194
00195
00196 static PyObject *
00197 rpmfi_DX(rpmfiObject * s)
00198
00199 {
00200 return Py_BuildValue("i", rpmfiDX(s->fi));
00201 }
00202
00203
00204 static PyObject *
00205 rpmfi_BN(rpmfiObject * s)
00206
00207 {
00208 return Py_BuildValue("s", xstrdup(rpmfiBN(s->fi)));
00209 }
00210
00211
00212 static PyObject *
00213 rpmfi_DN(rpmfiObject * s)
00214
00215 {
00216 return Py_BuildValue("s", xstrdup(rpmfiDN(s->fi)));
00217 }
00218
00219
00220 static PyObject *
00221 rpmfi_FN(rpmfiObject * s)
00222
00223 {
00224 return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
00225 }
00226
00227
00228 static PyObject *
00229 rpmfi_FFlags(rpmfiObject * s)
00230
00231 {
00232 return Py_BuildValue("i", rpmfiFFlags(s->fi));
00233 }
00234
00235
00236 static PyObject *
00237 rpmfi_VFlags(rpmfiObject * s)
00238
00239 {
00240 return Py_BuildValue("i", rpmfiVFlags(s->fi));
00241 }
00242
00243
00244 static PyObject *
00245 rpmfi_FMode(rpmfiObject * s)
00246
00247 {
00248 return Py_BuildValue("i", rpmfiFMode(s->fi));
00249 }
00250
00251
00252 static PyObject *
00253 rpmfi_FState(rpmfiObject * s)
00254
00255 {
00256 return Py_BuildValue("i", rpmfiFState(s->fi));
00257 }
00258
00259
00260
00261 static PyObject *
00262 rpmfi_MD5(rpmfiObject * s)
00263
00264 {
00265 int dalgo = 0;
00266 size_t dlen = 0;
00267 const unsigned char * digest;
00268 const char * fdigest;
00269 char * t;
00270 int i;
00271
00272 digest = rpmfiDigest(s->fi, &dalgo, &dlen);
00273 if (digest == NULL || dlen == 0)
00274 return NULL;
00275 fdigest = t = memset(alloca(dlen), 0, dlen);
00276 for (i = 0; i < dlen; i++, t += 2)
00277 sprintf(t, "%02x", digest[i]);
00278 *t = '\0';
00279 return Py_BuildValue("s", xstrdup(fdigest));
00280 }
00281
00282
00283 static PyObject *
00284 rpmfi_FLink(rpmfiObject * s)
00285
00286 {
00287 return Py_BuildValue("s", xstrdup(rpmfiFLink(s->fi)));
00288 }
00289
00290
00291 static PyObject *
00292 rpmfi_FSize(rpmfiObject * s)
00293
00294 {
00295 return Py_BuildValue("i", rpmfiFSize(s->fi));
00296 }
00297
00298
00299 static PyObject *
00300 rpmfi_FRdev(rpmfiObject * s)
00301
00302 {
00303 return Py_BuildValue("i", rpmfiFRdev(s->fi));
00304 }
00305
00306
00307 static PyObject *
00308 rpmfi_FMtime(rpmfiObject * s)
00309
00310 {
00311 return Py_BuildValue("i", rpmfiFMtime(s->fi));
00312 }
00313
00314
00315 static PyObject *
00316 rpmfi_FUser(rpmfiObject * s)
00317
00318 {
00319 return Py_BuildValue("s", xstrdup(rpmfiFUser(s->fi)));
00320 }
00321
00322
00323 static PyObject *
00324 rpmfi_FGroup(rpmfiObject * s)
00325
00326 {
00327 return Py_BuildValue("s", xstrdup(rpmfiFGroup(s->fi)));
00328 }
00329
00330
00331 static PyObject *
00332 rpmfi_FColor(rpmfiObject * s)
00333
00334 {
00335 return Py_BuildValue("i", rpmfiFColor(s->fi));
00336 }
00337
00338
00339 static PyObject *
00340 rpmfi_FClass(rpmfiObject * s)
00341
00342 {
00343 const char * FClass;
00344
00345 if ((FClass = rpmfiFClass(s->fi)) == NULL)
00346 FClass = "";
00347 return Py_BuildValue("s", xstrdup(FClass));
00348 }
00349
00352
00353
00354 static struct PyMethodDef rpmfi_methods[] = {
00355 {"Debug", (PyCFunction)rpmfi_Debug, METH_VARARGS|METH_KEYWORDS,
00356 NULL},
00357 {"FC", (PyCFunction)rpmfi_FC, METH_NOARGS,
00358 NULL},
00359 {"FX", (PyCFunction)rpmfi_FX, METH_NOARGS,
00360 NULL},
00361 {"DC", (PyCFunction)rpmfi_DC, METH_NOARGS,
00362 NULL},
00363 {"DX", (PyCFunction)rpmfi_DX, METH_NOARGS,
00364 NULL},
00365 {"BN", (PyCFunction)rpmfi_BN, METH_NOARGS,
00366 NULL},
00367 {"DN", (PyCFunction)rpmfi_DN, METH_NOARGS,
00368 NULL},
00369 {"FN", (PyCFunction)rpmfi_FN, METH_NOARGS,
00370 NULL},
00371 {"FFlags", (PyCFunction)rpmfi_FFlags, METH_NOARGS,
00372 NULL},
00373 {"VFlags", (PyCFunction)rpmfi_VFlags, METH_NOARGS,
00374 NULL},
00375 {"FMode", (PyCFunction)rpmfi_FMode, METH_NOARGS,
00376 NULL},
00377 {"FState", (PyCFunction)rpmfi_FState, METH_NOARGS,
00378 NULL},
00379 {"MD5", (PyCFunction)rpmfi_MD5, METH_NOARGS,
00380 NULL},
00381 {"FLink", (PyCFunction)rpmfi_FLink, METH_NOARGS,
00382 NULL},
00383 {"FSize", (PyCFunction)rpmfi_FSize, METH_NOARGS,
00384 NULL},
00385 {"FRdev", (PyCFunction)rpmfi_FRdev, METH_NOARGS,
00386 NULL},
00387 {"FMtime", (PyCFunction)rpmfi_FMtime, METH_NOARGS,
00388 NULL},
00389 {"FUser", (PyCFunction)rpmfi_FUser, METH_NOARGS,
00390 NULL},
00391 {"FGroup", (PyCFunction)rpmfi_FGroup, METH_NOARGS,
00392 NULL},
00393 {"FColor", (PyCFunction)rpmfi_FColor, METH_NOARGS,
00394 NULL},
00395 {"FClass", (PyCFunction)rpmfi_FClass, METH_NOARGS,
00396 NULL},
00397 {"next", (PyCFunction)rpmfi_Next, METH_NOARGS,
00398 "fi.next() -> (FN, FSize, FMode, FMtime, FFlags, FRdev, FInode, FNlink, FState, VFlags, FUser, FGroup, FMD5))\n\
00399 - Retrieve next file info tuple.\n" },
00400 #ifdef NOTYET
00401 {"NextD", (PyCFunction)rpmfi_NextD, METH_NOARGS,
00402 NULL},
00403 {"InitD", (PyCFunction)rpmfi_InitD, METH_NOARGS,
00404 NULL},
00405 #endif
00406 {NULL, NULL}
00407 };
00408
00409
00410
00411
00412 static void
00413 rpmfi_dealloc( rpmfiObject * s)
00414
00415 {
00416 if (s) {
00417 s->fi = rpmfiFree(s->fi);
00418 PyObject_Del(s);
00419 }
00420 }
00421
00422 static int
00423 rpmfi_print(rpmfiObject * s, FILE * fp, int flags)
00424
00425
00426 {
00427 if (!(s && s->fi))
00428 return -1;
00429
00430 s->fi = rpmfiInit(s->fi, 0);
00431 while (rpmfiNext(s->fi) >= 0)
00432 fprintf(fp, "%s\n", rpmfiFN(s->fi));
00433 return 0;
00434 }
00435
00436 static PyObject * rpmfi_getattro(PyObject * o, PyObject * n)
00437
00438 {
00439 return PyObject_GenericGetAttr(o, n);
00440 }
00441
00442 static int rpmfi_setattro(PyObject * o, PyObject * n, PyObject * v)
00443
00444 {
00445 return PyObject_GenericSetAttr(o, n, v);
00446 }
00447
00448 static int
00449 rpmfi_length(rpmfiObject * s)
00450
00451 {
00452 return rpmfiFC(s->fi);
00453 }
00454
00455
00456 static PyObject *
00457 rpmfi_subscript(rpmfiObject * s, PyObject * key)
00458
00459 {
00460 int ix;
00461
00462 if (!PyInt_Check(key)) {
00463 PyErr_SetString(PyExc_TypeError, "integer expected");
00464 return NULL;
00465 }
00466
00467 ix = (int) PyInt_AsLong(key);
00468 rpmfiSetFX(s->fi, ix);
00469 return Py_BuildValue("s", xstrdup(rpmfiFN(s->fi)));
00470 }
00471
00472
00473 static PyMappingMethods rpmfi_as_mapping = {
00474 (inquiry) rpmfi_length,
00475 (binaryfunc) rpmfi_subscript,
00476 (objobjargproc)0,
00477 };
00478
00481 static int rpmfi_init(rpmfiObject * s, PyObject *args, PyObject *kwds)
00482
00483
00484 {
00485 hdrObject * ho = NULL;
00486 PyObject * to = NULL;
00487 rpmts ts = NULL;
00488 int tagN = RPMTAG_BASENAMES;
00489 int flags = 0;
00490 char * kwlist[] = {"header", "tag", "flags", NULL};
00491
00492 if (_rpmfi_debug < 0)
00493 fprintf(stderr, "*** rpmfi_init(%p,%p,%p)\n", s, args, kwds);
00494
00495 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|Oi:rpmfi_init", kwlist,
00496 &hdr_Type, &ho, &to, &flags))
00497 return -1;
00498
00499 if (to != NULL) {
00500 tagN = tagNumFromPyObject(to);
00501 if (tagN == -1) {
00502 PyErr_SetString(PyExc_KeyError, "unknown header tag");
00503 return -1;
00504 }
00505 }
00506 s->fi = rpmfiNew(ts, hdrGetHeader(ho), tagN, flags);
00507 s->active = 0;
00508
00509 return 0;
00510 }
00511
00514 static void rpmfi_free( rpmfiObject * s)
00515
00516 {
00517 if (_rpmfi_debug)
00518 fprintf(stderr, "%p -- fi %p\n", s, s->fi);
00519 s->fi = rpmfiFree(s->fi);
00520
00521 PyObject_Del((PyObject *)s);
00522 }
00523
00526 static PyObject * rpmfi_alloc(PyTypeObject * subtype, int nitems)
00527
00528 {
00529 PyObject * s = PyType_GenericAlloc(subtype, nitems);
00530
00531 if (_rpmfi_debug < 0)
00532 fprintf(stderr, "*** rpmfi_alloc(%p,%d) ret %p\n", subtype, nitems, s);
00533 return s;
00534 }
00535
00538
00539 static PyObject * rpmfi_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds)
00540
00541
00542 {
00543 rpmfiObject * s = (void *) PyObject_New(rpmfiObject, subtype);
00544
00545
00546 if (rpmfi_init(s, args, kwds) < 0) {
00547 rpmfi_free(s);
00548 return NULL;
00549 }
00550
00551 if (_rpmfi_debug)
00552 fprintf(stderr, "%p ++ fi %p\n", s, s->fi);
00553
00554 return (PyObject *)s;
00555 }
00556
00559
00560 static char rpmfi_doc[] =
00561 "";
00562
00563
00564 PyTypeObject rpmfi_Type = {
00565 PyObject_HEAD_INIT(&PyType_Type)
00566 0,
00567 "rpm.fi",
00568 sizeof(rpmfiObject),
00569 0,
00570
00571 (destructor) rpmfi_dealloc,
00572 (printfunc) rpmfi_print,
00573 (getattrfunc)0,
00574 (setattrfunc)0,
00575 (cmpfunc)0,
00576 (reprfunc)0,
00577 0,
00578 0,
00579 &rpmfi_as_mapping,
00580 (hashfunc)0,
00581 (ternaryfunc)0,
00582 (reprfunc)0,
00583 (getattrofunc) rpmfi_getattro,
00584 (setattrofunc) rpmfi_setattro,
00585 0,
00586 Py_TPFLAGS_DEFAULT,
00587 rpmfi_doc,
00588 #if Py_TPFLAGS_HAVE_ITER
00589 0,
00590 0,
00591 0,
00592 0,
00593 (getiterfunc) rpmfi_iter,
00594 (iternextfunc) rpmfi_iternext,
00595 rpmfi_methods,
00596 0,
00597 0,
00598 0,
00599 0,
00600 0,
00601 0,
00602 0,
00603 (initproc) rpmfi_init,
00604 (allocfunc) rpmfi_alloc,
00605 (newfunc) rpmfi_new,
00606 (freefunc) rpmfi_free,
00607 0,
00608 #endif
00609 };
00610
00611
00612
00613
00614 rpmfi fiFromFi(rpmfiObject * s)
00615 {
00616 return s->fi;
00617 }
00618
00619 rpmfiObject *
00620 rpmfi_Wrap(rpmfi fi)
00621 {
00622 rpmfiObject *s = PyObject_New(rpmfiObject, &rpmfi_Type);
00623
00624 if (s == NULL)
00625 return NULL;
00626 s->fi = fi;
00627 s->active = 0;
00628 return s;
00629 }
00630
00631 rpmfiObject *
00632 hdr_fiFromHeader(PyObject * s, PyObject * args, PyObject * kwds)
00633 {
00634 hdrObject * ho = (hdrObject *)s;
00635 PyObject * to = NULL;
00636 rpmts ts = NULL;
00637 rpmTag tagN = RPMTAG_BASENAMES;
00638 int flags = 0;
00639 char * kwlist[] = {"tag", "flags", NULL};
00640
00641 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:fiFromHeader", kwlist,
00642 &to, &flags))
00643 return NULL;
00644
00645 if (to != NULL) {
00646 tagN = tagNumFromPyObject(to);
00647 if (tagN == -1) {
00648 PyErr_SetString(PyExc_KeyError, "unknown header tag");
00649 return NULL;
00650 }
00651 }
00652 return rpmfi_Wrap( rpmfiNew(ts, hdrGetHeader(ho), tagN, flags) );
00653 }