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