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