Adjust python callback for amount+total type change
[platform/upstream/rpm.git] / python / rpmts-py.c
1 /** \ingroup py_c
2  * \file python/rpmts-py.c
3  */
4
5 #include "system.h"
6
7 #include <rpm/rpmlib.h> /* rpmReadPackageFile, headerCheck */
8 #include <rpm/rpmtag.h>
9 #include <rpm/rpmpgp.h>
10 #include <rpm/rpmdb.h>
11 #include <rpm/rpmbuild.h>
12
13 #include "header-py.h"
14 #include "rpmds-py.h"   /* XXX for rpmdsNew */
15 #include "rpmfi-py.h"   /* XXX for rpmfiNew */
16 #include "rpmmi-py.h"
17 #include "rpmps-py.h"
18 #include "rpmte-py.h"
19 #include "spec-py.h"
20
21 #include "rpmts-py.h"
22
23 #include "debug.h"
24
25 extern int _rpmts_debug;
26
27
28 /** \ingroup python
29  * \name Class: Rpmts
30  * \class Rpmts
31  * \brief A python rpm.ts object represents an RPM transaction set.
32  *
33  * The transaction set is the workhorse of RPM.  It performs the
34  * installation and upgrade of packages.  The rpm.ts object is
35  * instantiated by the TransactionSet function in the rpm module.
36  *
37  * The TransactionSet function takes two optional arguments. The first
38  * argument is the root path. The second is the verify signature disable flags,
39  * a set of the following bits:
40  *
41  * -    rpm.RPMVSF_NOHDRCHK     if set, don't check rpmdb headers
42  * -    rpm.RPMVSF_NEEDPAYLOAD  if not set, check header+payload (if possible)
43  * -    rpm.RPMVSF_NOSHA1HEADER if set, don't check header SHA1 digest
44  * -    rpm.RPMVSF_NODSAHEADER  if set, don't check header DSA signature
45  * -    rpm.RPMVSF_NOMD5        if set, don't check header+payload MD5 digest
46  * -    rpm.RPMVSF_NODSA        if set, don't check header+payload DSA signature
47  * -    rpm.RPMVSF_NORSA        if set, don't check header+payload RSA signature
48  *
49  * For convenience, there are the following masks:
50  * -    rpm._RPMVSF_NODIGESTS           if set, don't check digest(s).
51  * -    rpm._RPMVSF_NOSIGNATURES        if set, don't check signature(s).
52  *
53  * A rpm.ts object has the following methods:
54  *
55  * - addInstall(hdr,data,mode)  Add an install element to a transaction set.
56  * @param hdr   the header to be added
57  * @param data  user data that will be passed to the transaction callback
58  *              during transaction execution
59  * @param mode  optional argument that specifies if this package should
60  *              be installed ('i'), upgraded ('u'), or if it is just
61  *              available to the transaction when computing
62  *              dependencies but no action should be performed with it
63  *              ('a').
64  *
65  * - addErase(name) Add an erase element to a transaction set.
66  * @param name  the package name to be erased
67  *
68  * - check()    Perform a dependency check on the transaction set. After
69  *              headers have been added to a transaction set, a dependency
70  *              check can be performed to make sure that all package
71  *              dependencies are satisfied.
72  * @return      None If there are no unresolved dependencies
73  *              Otherwise a list of complex tuples is returned, one tuple per
74  *              unresolved dependency, with
75  * The format of the dependency tuple is:
76  *     ((packageName, packageVersion, packageRelease),
77  *      (reqName, reqVersion),
78  *      needsFlags,
79  *      suggestedPackage,
80  *      sense)
81  *     packageName, packageVersion, packageRelease are the name,
82  *     version, and release of the package that has the unresolved
83  *     dependency or conflict.
84  *     The reqName and reqVersion are the name and version of the
85  *     requirement or conflict.
86  *     The needsFlags is a bitfield that describes the versioned
87  *     nature of a requirement or conflict.  The constants
88  *     rpm.RPMSENSE_LESS, rpm.RPMSENSE_GREATER, and
89  *     rpm.RPMSENSE_EQUAL can be logical ANDed with the needsFlags
90  *     to get versioned dependency information.
91  *     suggestedPackage is a tuple if the dependency check was aware
92  *     of a package that solves this dependency problem when the
93  *     dependency check was run.  Packages that are added to the
94  *     transaction set as "available" are examined during the
95  *     dependency check as possible dependency solvers. The tuple
96  *     contains two values, (header, suggestedName).  These are set to
97  *     the header of the suggested package and its name, respectively.
98  *     If there is no known package to solve the dependency problem,
99  *     suggestedPackage is None.
100  *     The constants rpm.RPMDEP_SENSE_CONFLICTS and
101  *     rpm.RPMDEP_SENSE_REQUIRES are set to show a dependency as a
102  *     requirement or a conflict.
103  *
104  * - ts.order() Do a topological sort of added element relations.
105  * @return      None
106  *
107  * - ts.setFlags(transFlags) Set transaction set flags.
108  * @param transFlags - bit(s) to controll transaction operations. The
109  *              following values can be logically OR'ed together:
110  *      - rpm.RPMTRANS_FLAG_TEST - test mode, do not modify the RPM
111  *              database, change any files, or run any package scripts
112  *      - rpm.RPMTRANS_FLAG_BUILD_PROBS - only build a list of
113  *              problems encountered when attempting to run this transaction
114  *              set
115  *      - rpm.RPMTRANS_FLAG_NOSCRIPTS - do not execute package scripts
116  *      - rpm.RPMTRANS_FLAG_JUSTDB - only make changes to the rpm
117  *              database, do not modify files.
118  *      - rpm.RPMTRANS_FLAG_NOTRIGGERS - do not run trigger scripts
119  *      - rpm.RPMTRANS_FLAG_NODOCS - do not install files marked as %doc
120  *      - rpm.RPMTRANS_FLAG_ALLFILES - create all files, even if a
121  *              file is marked %config(missingok) and an upgrade is
122  *              being performed.
123  *      - rpm.RPMTRANS_FLAG_KEEPOBSOLETE - do not remove obsoleted
124  *              packages.
125  * @return      previous transFlags
126  *
127  * - ts.setProbFilter(ignoreSet) Set transaction set problem filter.
128  * @param problemSetFilter - control bit(s) to ignore classes of problems,
129  *              a logical or of one or more of the following bit(s):
130  *      - rpm.RPMPROB_FILTER_IGNOREOS -
131  *      - rpm.RPMPROB_FILTER_IGNOREARCH -
132  *      - rpm.RPMPROB_FILTER_REPLACEPKG -
133  *      - rpm.RPMPROB_FILTER_FORCERELOCATE -
134  *      - rpm.RPMPROB_FILTER_REPLACENEWFILES -
135  *      - rpm.RPMPROB_FILTER_REPLACEOLDFILES -
136  *      - rpm.RPMPROB_FILTER_OLDPACKAGE -
137  *      - rpm.RPMPROB_FILTER_DISKSPACE -
138  * @return      previous ignoreSet
139  *
140  * - ts.run(callback,data) Attempt to execute a transaction set.
141  *      After the transaction set has been populated with install/upgrade or
142  *      erase actions, the transaction set can be executed by invoking
143  *      the ts.run() method.
144  */
145
146 /** \ingroup py_c
147  */
148 struct rpmtsCallbackType_s {
149     PyObject * cb;
150     PyObject * data;
151     rpmtsObject * tso;
152     int pythonError;
153     PyThreadState *_save;
154 };
155
156 /** \ingroup py_c
157  */
158 static PyObject *
159 rpmts_Debug(rpmtsObject * s, PyObject * args, PyObject * kwds)
160 {
161     char * kwlist[] = {"debugLevel", NULL};
162
163     if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:Debug", kwlist,
164             &_rpmts_debug))
165         return NULL;
166
167 if (_rpmts_debug < 0)
168 fprintf(stderr, "*** rpmts_Debug(%p) ts %p\n", s, s->ts);
169
170     Py_INCREF(Py_None);
171     return Py_None;
172 }
173
174 #if 0
175 /** \ingroup py_c
176  * Add package to universe of possible packages to install in transaction set.
177  * @param ts            transaction set
178  * @param h             header
179  * @param key           package private data
180  */
181 static void rpmtsAddAvailableElement(rpmts ts, Header h,
182                 fnpyKey key)
183 {
184     int scareMem = 0;
185     rpmds provides = rpmdsNew(h, RPMTAG_PROVIDENAME, scareMem);
186     rpmfi fi = rpmfiNew(ts, h, RPMTAG_BASENAMES, scareMem);
187
188     /* XXX FIXME: return code RPMAL_NOMATCH is error */
189     (void) rpmalAdd(&ts->availablePackages, RPMAL_NOMATCH, key,
190                 provides, fi, rpmtsColor(ts));
191     fi = rpmfiFree(fi);
192     provides = rpmdsFree(provides);
193
194 if (_rpmts_debug < 0)
195 fprintf(stderr, "\tAddAvailable(%p) list %p\n", ts, ts->availablePackages);
196
197 }
198 #endif
199
200 /** \ingroup py_c
201  */
202 static PyObject *
203 rpmts_AddInstall(rpmtsObject * s, PyObject * args, PyObject * kwds)
204 {
205     hdrObject * h;
206     PyObject * key;
207     char * how = "u";   /* XXX default to upgrade element if missing */
208     int isUpgrade = 0;
209     char * kwlist[] = {"header", "key", "how", NULL};
210     int rc = 0;
211
212     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O|s:AddInstall", kwlist,
213             &hdr_Type, &h, &key, &how))
214         return NULL;
215
216     {   PyObject * hObj = (PyObject *) h;
217         if (hObj->ob_type != &hdr_Type) {
218             PyErr_SetString(PyExc_TypeError, "bad type for header argument");
219             return NULL;
220         }
221     }
222
223 if (_rpmts_debug < 0 || (_rpmts_debug > 0 && *how != 'a'))
224 fprintf(stderr, "*** rpmts_AddInstall(%p,%p,%p,%s) ts %p\n", s, h, key, how, s->ts);
225
226     if (how && strcmp(how, "a") && strcmp(how, "u") && strcmp(how, "i")) {
227         PyErr_SetString(PyExc_TypeError, "how argument must be \"u\", \"a\", or \"i\"");
228         return NULL;
229     } else if (how && !strcmp(how, "u"))
230         isUpgrade = 1;
231
232     /*
233      * XXX resurrect when better available mechanism is, well, available.
234      * OTOH nothing appears to use it these days...
235      * Raise exception to catch out any callers while broken.
236      */
237     if (how && !strcmp(how, "a")) {
238 #ifdef DYING
239         rpmtsAddAvailableElement(s->ts, hdrGetHeader(h), key); 
240 #else
241         PyErr_SetString(pyrpmError, "available package mechanism currently broken");
242         return NULL;
243 #endif
244     } else
245         rc = rpmtsAddInstallElement(s->ts, hdrGetHeader(h), key, isUpgrade, NULL);
246     if (rc) {
247         PyErr_SetString(pyrpmError, "adding package to transaction failed");
248         return NULL;
249     }
250         
251
252     /* This should increment the usage count for me */
253     if (key)
254         PyList_Append(s->keyList, key);
255
256     Py_INCREF(Py_None);
257     return Py_None;
258 }
259
260 /** \ingroup py_c
261  * @todo Permit finer control (i.e. not just --allmatches) of deleted elments.
262  */
263 static PyObject *
264 rpmts_AddErase(rpmtsObject * s, PyObject * args, PyObject * kwds)
265 {
266     PyObject * o;
267     int count;
268     rpmdbMatchIterator mi;
269     char * kwlist[] = {"name", NULL};
270
271 if (_rpmts_debug)
272 fprintf(stderr, "*** rpmts_AddErase(%p) ts %p\n", s, s->ts);
273
274     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:AddErase", kwlist, &o))
275         return NULL;
276
277     if (PyString_Check(o)) {
278         char * name = PyString_AsString(o);
279
280         mi = rpmtsInitIterator(s->ts, RPMDBI_LABEL, name, 0);
281         count = rpmdbGetIteratorCount(mi);
282         if (count <= 0) {
283             mi = rpmdbFreeIterator(mi);
284             PyErr_SetString(pyrpmError, "package not installed");
285             return NULL;
286         } else { /* XXX: Note that we automatically choose to remove all matches */
287             Header h;
288             while ((h = rpmdbNextIterator(mi)) != NULL) {
289                 unsigned int recOffset = rpmdbGetIteratorOffset(mi);
290                 if (recOffset)
291                     rpmtsAddEraseElement(s->ts, h, recOffset);
292             }
293         }
294         mi = rpmdbFreeIterator(mi);
295     } else
296     if (PyInt_Check(o)) {
297         uint32_t instance = PyInt_AsLong(o);
298
299         mi = rpmtsInitIterator(s->ts, RPMDBI_PACKAGES, &instance, sizeof(instance));
300         if (instance == 0 || mi == NULL) {
301             mi = rpmdbFreeIterator(mi);
302             PyErr_SetString(pyrpmError, "package not installed");
303             return NULL;
304         } else {
305             Header h;
306             while ((h = rpmdbNextIterator(mi)) != NULL) {
307                 uint32_t recOffset = rpmdbGetIteratorOffset(mi);
308                 if (recOffset)
309                     rpmtsAddEraseElement(s->ts, h, recOffset);
310                 break;
311             }
312         }
313         mi = rpmdbFreeIterator(mi);
314     }
315
316     Py_INCREF(Py_None);
317     return Py_None;
318 }
319
320 /** \ingroup py_c
321  */
322 static int
323 rpmts_SolveCallback(rpmts ts, rpmds ds, const void * data)
324 {
325     struct rpmtsCallbackType_s * cbInfo = (struct rpmtsCallbackType_s *) data;
326     PyObject * args, * result;
327     int res = 1;
328
329 if (_rpmts_debug)
330 fprintf(stderr, "*** rpmts_SolveCallback(%p,%p,%p) \"%s\"\n", ts, ds, data, rpmdsDNEVR(ds));
331
332     if (cbInfo->tso == NULL) return res;
333     if (cbInfo->pythonError) return res;
334     if (cbInfo->cb == Py_None) return res;
335
336     PyEval_RestoreThread(cbInfo->_save);
337
338     args = Py_BuildValue("(Oissi)", cbInfo->tso,
339                 rpmdsTagN(ds), rpmdsN(ds), rpmdsEVR(ds), rpmdsFlags(ds));
340     result = PyEval_CallObject(cbInfo->cb, args);
341     Py_DECREF(args);
342
343     if (!result) {
344         cbInfo->pythonError = 1;
345     } else {
346         if (PyInt_Check(result))
347             res = PyInt_AsLong(result);
348         Py_DECREF(result);
349     }
350
351     cbInfo->_save = PyEval_SaveThread();
352
353     return res;
354 }
355
356 /** \ingroup py_c
357  */
358 static PyObject *
359 rpmts_Check(rpmtsObject * s, PyObject * args, PyObject * kwds)
360 {
361     rpmps ps;
362     rpmProblem p;
363     PyObject * list, * cf;
364     struct rpmtsCallbackType_s cbInfo;
365     int i;
366     int xx;
367     char * kwlist[] = {"callback", NULL};
368
369     memset(&cbInfo, 0, sizeof(cbInfo));
370     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:Check", kwlist,
371             &cbInfo.cb))
372         return NULL;
373
374     if (cbInfo.cb != NULL) {
375         if (!PyCallable_Check(cbInfo.cb)) {
376             PyErr_SetString(PyExc_TypeError, "expected a callable");
377             return NULL;
378         }
379         xx = rpmtsSetSolveCallback(s->ts, rpmts_SolveCallback, (void *)&cbInfo);
380     }
381
382 if (_rpmts_debug)
383 fprintf(stderr, "*** rpmts_Check(%p) ts %p cb %p\n", s, s->ts, cbInfo.cb);
384
385     cbInfo.tso = s;
386     cbInfo.pythonError = 0;
387     cbInfo._save = PyEval_SaveThread();
388
389 #ifdef DYING
390     /* XXX resurrect availablePackages one more time ... */
391     rpmalMakeIndex(s->ts->availablePackages);
392 #endif
393
394     xx = rpmtsCheck(s->ts);
395     ps = rpmtsProblems(s->ts);
396
397     if (cbInfo.cb)
398         xx = rpmtsSetSolveCallback(s->ts, rpmtsSolve, NULL);
399
400     PyEval_RestoreThread(cbInfo._save);
401
402     if (ps != NULL) {
403         list = PyList_New(0);
404         rpmpsi psi = rpmpsInitIterator(ps);
405
406         /* XXX TODO: rpmlib >= 4.0.3 can return multiple suggested keys. */
407         while ((i = rpmpsNextIterator(psi)) >= 0) {
408             const char * needsName;
409             char * byName, * byVersion, * byRelease, *byArch;
410             char * needsOP, * needsVersion;
411             rpmsenseFlags needsFlags, sense;
412             fnpyKey key;
413
414             p = rpmpsGetProblem(psi);
415
416             byName = strdup(rpmProblemGetPkgNEVR(p));
417             if ((byArch= strrchr(byName, '.')) != NULL)
418                 *byArch++ = '\0';
419             if ((byRelease = strrchr(byName, '-')) != NULL)
420                 *byRelease++ = '\0';
421             if ((byVersion = strrchr(byName, '-')) != NULL)
422                 *byVersion++ = '\0';
423
424             key = rpmProblemGetKey(p);
425
426             needsName = rpmProblemGetAltNEVR(p);
427             if (needsName[1] == ' ') {
428                 sense = (needsName[0] == 'C')
429                         ? RPMDEP_SENSE_CONFLICTS : RPMDEP_SENSE_REQUIRES;
430                 needsName += 2;
431             } else
432                 sense = RPMDEP_SENSE_REQUIRES;
433             if ((needsVersion = strrchr(needsName, ' ')) != NULL)
434                 *needsVersion++ = '\0';
435
436             needsFlags = 0;
437             if ((needsOP = strrchr(needsName, ' ')) != NULL) {
438                 for (*needsOP++ = '\0'; *needsOP != '\0'; needsOP++) {
439                     if (*needsOP == '<')        needsFlags |= RPMSENSE_LESS;
440                     else if (*needsOP == '>')   needsFlags |= RPMSENSE_GREATER;
441                     else if (*needsOP == '=')   needsFlags |= RPMSENSE_EQUAL;
442                 }
443             }
444
445             cf = Py_BuildValue("((sss)(ss)iOi)", byName, byVersion, byRelease,
446                                needsName, needsVersion, needsFlags,
447                                (key != NULL ? key : Py_None),
448                                sense);
449             PyList_Append(list, (PyObject *) cf);
450             Py_DECREF(cf);
451             free(byName);
452         }
453
454         psi = rpmpsFreeIterator(psi);
455         ps = rpmpsFree(ps);
456
457         return list;
458     }
459
460     Py_INCREF(Py_None);
461     return Py_None;
462 }
463
464 /** \ingroup py_c
465  */
466 static PyObject *
467 rpmts_Order(rpmtsObject * s)
468 {
469     int rc;
470
471 if (_rpmts_debug)
472 fprintf(stderr, "*** rpmts_Order(%p) ts %p\n", s, s->ts);
473
474     Py_BEGIN_ALLOW_THREADS
475     rc = rpmtsOrder(s->ts);
476     Py_END_ALLOW_THREADS
477
478     return Py_BuildValue("i", rc);
479 }
480
481 /** \ingroup py_c
482  */
483 static PyObject *
484 rpmts_Clean(rpmtsObject * s)
485 {
486 if (_rpmts_debug)
487 fprintf(stderr, "*** rpmts_Clean(%p) ts %p\n", s, s->ts);
488
489     rpmtsClean(s->ts);
490
491     Py_INCREF(Py_None);
492     return Py_None;
493 }
494
495 /** \ingroup py_c
496  */
497 static PyObject *
498 rpmts_OpenDB(rpmtsObject * s)
499 {
500     int dbmode;
501
502 if (_rpmts_debug)
503 fprintf(stderr, "*** rpmts_OpenDB(%p) ts %p\n", s, s->ts);
504
505     dbmode = rpmtsGetDBMode(s->ts);
506     if (dbmode == -1)
507         dbmode = O_RDONLY;
508
509     return Py_BuildValue("i", rpmtsOpenDB(s->ts, dbmode));
510 }
511
512 /** \ingroup py_c
513  */
514 static PyObject *
515 rpmts_CloseDB(rpmtsObject * s)
516 {
517     int rc;
518
519 if (_rpmts_debug)
520 fprintf(stderr, "*** rpmts_CloseDB(%p) ts %p\n", s, s->ts);
521
522     rc = rpmtsCloseDB(s->ts);
523     rpmtsSetDBMode(s->ts, -1);  /* XXX disable lazy opens */
524
525     return Py_BuildValue("i", rc);
526 }
527
528 /** \ingroup py_c
529  */
530 static PyObject *
531 rpmts_InitDB(rpmtsObject * s)
532 {
533     int rc;
534
535 if (_rpmts_debug)
536 fprintf(stderr, "*** rpmts_InitDB(%p) ts %p\n", s, s->ts);
537
538     rc = rpmtsInitDB(s->ts, O_RDONLY);
539     if (rc == 0)
540         rc = rpmtsCloseDB(s->ts);
541
542     return Py_BuildValue("i", rc);
543 }
544
545 /** \ingroup py_c
546  */
547 static PyObject *
548 rpmts_RebuildDB(rpmtsObject * s)
549 {
550     int rc;
551
552 if (_rpmts_debug)
553 fprintf(stderr, "*** rpmts_RebuildDB(%p) ts %p\n", s, s->ts);
554
555     Py_BEGIN_ALLOW_THREADS
556     rc = rpmtsRebuildDB(s->ts);
557     Py_END_ALLOW_THREADS
558
559     return Py_BuildValue("i", rc);
560 }
561
562 /** \ingroup py_c
563  */
564 static PyObject *
565 rpmts_VerifyDB(rpmtsObject * s)
566 {
567     int rc;
568
569 if (_rpmts_debug)
570 fprintf(stderr, "*** rpmts_VerifyDB(%p) ts %p\n", s, s->ts);
571
572     Py_BEGIN_ALLOW_THREADS
573     rc = rpmtsVerifyDB(s->ts);
574     Py_END_ALLOW_THREADS
575
576     return Py_BuildValue("i", rc);
577 }
578
579 /** \ingroup py_c
580  */
581 static PyObject *
582 rpmts_HdrFromFdno(rpmtsObject * s, PyObject * args, PyObject * kwds)
583 {
584     PyObject * result = NULL;
585     Header h;
586     FD_t fd;
587     int fdno;
588     rpmRC rpmrc;
589     char * kwlist[] = {"fd", NULL};
590
591     if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:HdrFromFdno", kwlist,
592             &fdno))
593         return NULL;
594
595     fd = fdDup(fdno);
596     rpmrc = rpmReadPackageFile(s->ts, fd, "rpmts_HdrFromFdno", &h);
597     Fclose(fd);
598
599 if (_rpmts_debug)
600 fprintf(stderr, "*** rpmts_HdrFromFdno(%p) ts %p rc %d\n", s, s->ts, rpmrc);
601
602     switch (rpmrc) {
603     case RPMRC_OK:
604         if (h)
605             result = Py_BuildValue("N", hdr_Wrap(h));
606         h = headerFree(h);      /* XXX ref held by result */
607         break;
608
609     case RPMRC_NOKEY:
610         PyErr_SetString(pyrpmError, "public key not available");
611         break;
612
613     case RPMRC_NOTTRUSTED:
614         PyErr_SetString(pyrpmError, "public key not trusted");
615         break;
616
617     case RPMRC_NOTFOUND:
618     case RPMRC_FAIL:
619     default:
620         PyErr_SetString(pyrpmError, "error reading package header");
621         break;
622     }
623
624     return result;
625 }
626
627 /** \ingroup py_c
628  */
629 static PyObject *
630 rpmts_HdrCheck(rpmtsObject * s, PyObject * args, PyObject * kwds)
631 {
632     PyObject * blob;
633     PyObject * result = NULL;
634     char * msg = NULL;
635     const void * uh;
636     int uc;
637     rpmRC rpmrc;
638     char * kwlist[] = {"headers", NULL};
639
640 if (_rpmts_debug)
641 fprintf(stderr, "*** rpmts_HdrCheck(%p) ts %p\n", s, s->ts);
642
643     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:HdrCheck", kwlist, &blob))
644         return NULL;
645
646     if (blob == Py_None) {
647         Py_INCREF(Py_None);
648         return Py_None;
649     }
650     if (!PyString_Check(blob)) {
651         PyErr_SetString(pyrpmError, "hdrCheck takes a string of octets");
652         return result;
653     }
654     uh = PyString_AsString(blob);
655     uc = PyString_Size(blob);
656
657     rpmrc = headerCheck(s->ts, uh, uc, &msg);
658
659     switch (rpmrc) {
660     case RPMRC_OK:
661         Py_INCREF(Py_None);
662         result = Py_None;
663         break;
664
665     case RPMRC_NOKEY:
666         PyErr_SetString(pyrpmError, "public key not availaiable");
667         break;
668
669     case RPMRC_NOTTRUSTED:
670         PyErr_SetString(pyrpmError, "public key not trusted");
671         break;
672
673     case RPMRC_FAIL:
674     default:
675         PyErr_SetString(pyrpmError, msg);
676         break;
677     }
678     msg = _free(msg);
679
680     return result;
681 }
682
683 /** \ingroup py_c
684  */
685 static PyObject *
686 rpmts_SetVSFlags(rpmtsObject * s, PyObject * args, PyObject * kwds)
687 {
688     rpmVSFlags vsflags;
689     char * kwlist[] = {"flags", NULL};
690
691 if (_rpmts_debug)
692 fprintf(stderr, "*** rpmts_SetVSFlags(%p) ts %p\n", s, s->ts);
693
694     if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:SetVSFlags", kwlist,
695             &vsflags))
696         return NULL;
697
698     /* XXX FIXME: value check on vsflags, or build pure python object 
699      * for it, and require an object of that type */
700
701     return Py_BuildValue("i", rpmtsSetVSFlags(s->ts, vsflags));
702 }
703
704 /** \ingroup py_c
705  */
706 static PyObject *
707 rpmts_GetVSFlags(rpmtsObject * s)
708 {
709     return Py_BuildValue("i", rpmtsVSFlags(s->ts));
710 }
711
712 /** \ingroup py_c
713  */
714 static PyObject *
715 rpmts_SetColor(rpmtsObject * s, PyObject * args, PyObject * kwds)
716 {
717     rpm_color_t tscolor;
718     char * kwlist[] = {"color", NULL};
719
720 if (_rpmts_debug)
721 fprintf(stderr, "*** rpmts_SetColor(%p) ts %p\n", s, s->ts);
722
723     if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:Color", kwlist, &tscolor))
724         return NULL;
725
726     /* XXX FIXME: value check on tscolor, or build pure python object
727      * for it, and require an object of that type */
728
729     return Py_BuildValue("i", rpmtsSetColor(s->ts, tscolor));
730 }
731
732 /** \ingroup py_c
733  */
734 static PyObject *
735 rpmts_PgpPrtPkts(rpmtsObject * s, PyObject * args, PyObject * kwds)
736 {
737     PyObject * blob;
738     unsigned char * pkt;
739     unsigned int pktlen;
740     int rc;
741     char * kwlist[] = {"octets", NULL};
742
743 if (_rpmts_debug)
744 fprintf(stderr, "*** rpmts_PgpPrtPkts(%p) ts %p\n", s, s->ts);
745
746     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:PgpPrtPkts", kwlist, &blob))
747         return NULL;
748
749     if (blob == Py_None) {
750         Py_INCREF(Py_None);
751         return Py_None;
752     }
753     if (!PyString_Check(blob)) {
754         PyErr_SetString(pyrpmError, "pgpPrtPkts takes a string of octets");
755         return NULL;
756     }
757     pkt = (unsigned char *)PyString_AsString(blob);
758     pktlen = PyString_Size(blob);
759
760     rc = pgpPrtPkts(pkt, pktlen, NULL, 1);
761
762     return Py_BuildValue("i", rc);
763 }
764
765 /** \ingroup py_c
766  */
767 static PyObject *
768 rpmts_PgpImportPubkey(rpmtsObject * s, PyObject * args, PyObject * kwds)
769 {
770     PyObject * blob;
771     unsigned char * pkt;
772     unsigned int pktlen;
773     int rc;
774     char * kwlist[] = {"pubkey", NULL};
775
776 if (_rpmts_debug)
777 fprintf(stderr, "*** rpmts_PgpImportPubkey(%p) ts %p\n", s, s->ts);
778
779     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:PgpImportPubkey",
780             kwlist, &blob))
781         return NULL;
782
783     if (blob == Py_None) {
784         Py_INCREF(Py_None);
785         return Py_None;
786     }
787     if (!PyString_Check(blob)) {
788         PyErr_SetString(pyrpmError, "PgpImportPubkey takes a string of octets");
789         return NULL;
790     }
791     pkt = (unsigned char *)PyString_AsString(blob);
792     pktlen = PyString_Size(blob);
793
794     rc = rpmtsImportPubkey(s->ts, pkt, pktlen);
795
796     return Py_BuildValue("i", rc);
797 }
798
799 /** \ingroup py_c
800  */
801 static PyObject *
802 rpmts_GetKeys(rpmtsObject * s)
803 {
804     const void **data = NULL;
805     int num, i;
806     PyObject *tuple;
807
808 if (_rpmts_debug)
809 fprintf(stderr, "*** rpmts_GetKeys(%p) ts %p\n", s, s->ts);
810
811     rpmtsGetKeys(s->ts, &data, &num);
812     if (data == NULL || num <= 0) {
813         data = _free(data);
814         Py_INCREF(Py_None);
815         return Py_None;
816     }
817
818     tuple = PyTuple_New(num);
819
820     for (i = 0; i < num; i++) {
821         PyObject *obj;
822         obj = (data[i] ? (PyObject *) data[i] : Py_None);
823         Py_INCREF(obj);
824         PyTuple_SetItem(tuple, i, obj);
825     }
826
827     data = _free(data);
828
829     return tuple;
830 }
831
832 /** \ingroup py_c
833  */
834 static void *
835 rpmtsCallback(const void * hd, const rpmCallbackType what,
836                          const rpm_loff_t amount, const rpm_loff_t total,
837                          const void * pkgKey, rpmCallbackData data)
838 {
839     Header h = (Header) hd;
840     struct rpmtsCallbackType_s * cbInfo = data;
841     PyObject * pkgObj = (PyObject *) pkgKey;
842     PyObject * args, * result;
843     static FD_t fd;
844
845     if (cbInfo->pythonError) return NULL;
846     if (cbInfo->cb == Py_None) return NULL;
847
848     /* Synthesize a python object for callback (if necessary). */
849     if (pkgObj == NULL) {
850         if (h) {
851             const char * n = NULL;
852             (void) headerNVR(h, &n, NULL, NULL);
853             pkgObj = Py_BuildValue("s", n);
854         } else {
855             pkgObj = Py_None;
856             Py_INCREF(pkgObj);
857         }
858     } else
859         Py_INCREF(pkgObj);
860
861     PyEval_RestoreThread(cbInfo->_save);
862
863     args = Py_BuildValue("(iLLOO)", what, amount, total, pkgObj, cbInfo->data);
864     result = PyEval_CallObject(cbInfo->cb, args);
865     Py_DECREF(args);
866     Py_DECREF(pkgObj);
867
868     if (!result) {
869         cbInfo->pythonError = 1;
870         cbInfo->_save = PyEval_SaveThread();
871         return NULL;
872     }
873
874     if (what == RPMCALLBACK_INST_OPEN_FILE) {
875         int fdno;
876
877         if (!PyArg_Parse(result, "i", &fdno)) {
878             cbInfo->pythonError = 1;
879             cbInfo->_save = PyEval_SaveThread();
880             return NULL;
881         }
882         Py_DECREF(result);
883         cbInfo->_save = PyEval_SaveThread();
884
885         fd = fdDup(fdno);
886 if (_rpmts_debug)
887 fprintf(stderr, "\t%p = fdDup(%d)\n", fd, fdno);
888
889         fcntl(Fileno(fd), F_SETFD, FD_CLOEXEC);
890
891         return fd;
892     } else
893     if (what == RPMCALLBACK_INST_CLOSE_FILE) {
894 if (_rpmts_debug)
895 fprintf(stderr, "\tFclose(%p)\n", fd);
896         Fclose (fd);
897     } else {
898 if (_rpmts_debug)
899 fprintf(stderr, "\t%llu:%llu key %p\n", (long long) amount, (long long) total, pkgKey);
900     }
901
902     Py_DECREF(result);
903     cbInfo->_save = PyEval_SaveThread();
904
905     return NULL;
906 }
907
908 /** \ingroup py_c
909  */
910 static PyObject *
911 rpmts_SetFlags(rpmtsObject * s, PyObject * args, PyObject * kwds)
912 {
913     rpmtransFlags transFlags = 0;
914     char * kwlist[] = {"flags", NULL};
915
916     if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:SetFlags", kwlist,
917             &transFlags))
918         return NULL;
919
920 if (_rpmts_debug)
921 fprintf(stderr, "*** rpmts_SetFlags(%p) ts %p transFlags %x\n", s, s->ts, transFlags);
922
923     /* XXX FIXME: value check on flags, or build pure python object 
924      * for it, and require an object of that type */
925
926     return Py_BuildValue("i", rpmtsSetFlags(s->ts, transFlags));
927 }
928
929 /** \ingroup py_c
930  */
931 static PyObject *
932 rpmts_SetProbFilter(rpmtsObject * s, PyObject * args, PyObject * kwds)
933 {
934     rpmprobFilterFlags ignoreSet = 0;
935     rpmprobFilterFlags oignoreSet;
936     char * kwlist[] = {"ignoreSet", NULL};
937
938     if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:ProbFilter", kwlist,
939             &ignoreSet))
940         return NULL;
941
942 if (_rpmts_debug)
943 fprintf(stderr, "*** rpmts_SetProbFilter(%p) ts %p ignoreSet %x\n", s, s->ts, ignoreSet);
944
945     oignoreSet = s->ignoreSet;
946     s->ignoreSet = ignoreSet;
947
948     return Py_BuildValue("i", oignoreSet);
949 }
950
951 /** \ingroup py_c
952  */
953 static rpmpsObject *
954 rpmts_Problems(rpmtsObject * s)
955 {
956
957 if (_rpmts_debug)
958 fprintf(stderr, "*** rpmts_Problems(%p) ts %p\n", s, s->ts);
959
960     return rpmps_Wrap( rpmtsProblems(s->ts) );
961 }
962
963 /** \ingroup py_c
964  */
965 static PyObject *
966 rpmts_Run(rpmtsObject * s, PyObject * args, PyObject * kwds)
967 {
968     int rc;
969     PyObject * list;
970     rpmps ps;
971     rpmpsi psi;
972     struct rpmtsCallbackType_s cbInfo;
973     char * kwlist[] = {"callback", "data", NULL};
974
975     if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO:Run", kwlist,
976             &cbInfo.cb, &cbInfo.data))
977         return NULL;
978
979     cbInfo.tso = s;
980     cbInfo.pythonError = 0;
981     cbInfo._save = PyEval_SaveThread();
982
983     if (cbInfo.cb != NULL) {
984         if (!PyCallable_Check(cbInfo.cb)) {
985             PyErr_SetString(PyExc_TypeError, "expected a callable");
986             return NULL;
987         }
988         (void) rpmtsSetNotifyCallback(s->ts, rpmtsCallback, (void *) &cbInfo);
989     }
990
991 if (_rpmts_debug)
992 fprintf(stderr, "*** rpmts_Run(%p) ts %p ignore %x\n", s, s->ts, s->ignoreSet);
993
994     rc = rpmtsRun(s->ts, NULL, s->ignoreSet);
995     ps = rpmtsProblems(s->ts);
996
997     if (cbInfo.cb)
998         (void) rpmtsSetNotifyCallback(s->ts, NULL, NULL);
999
1000     PyEval_RestoreThread(cbInfo._save);
1001
1002     if (cbInfo.pythonError) {
1003         ps = rpmpsFree(ps);
1004         return NULL;
1005     }
1006
1007     if (rc < 0) {
1008         list = PyList_New(0);
1009         return list;
1010     } else if (!rc) {
1011         Py_INCREF(Py_None);
1012         return Py_None;
1013     }
1014
1015     list = PyList_New(0);
1016     psi = rpmpsInitIterator(ps);
1017     while (rpmpsNextIterator(psi) >= 0) {
1018         rpmProblem p = rpmpsGetProblem(psi);
1019         char * ps = rpmProblemString(p);
1020         PyObject * prob = Py_BuildValue("s(isN)", ps,
1021                              rpmProblemGetType(p),
1022                              rpmProblemGetStr(p),
1023                              PyLong_FromLongLong(rpmProblemGetDiskNeed(p)));
1024         PyList_Append(list, prob);
1025         free(ps);
1026         Py_DECREF(prob);
1027     }
1028
1029     psi = rpmpsFreeIterator(psi);
1030     ps = rpmpsFree(ps);
1031
1032     return list;
1033 }
1034
1035 #if Py_TPFLAGS_HAVE_ITER
1036 static PyObject *
1037 rpmts_iter(rpmtsObject * s)
1038 {
1039 if (_rpmts_debug)
1040 fprintf(stderr, "*** rpmts_iter(%p) ts %p\n", s, s->ts);
1041
1042     Py_INCREF(s);
1043     return (PyObject *)s;
1044 }
1045 #endif
1046
1047 /**
1048  * @todo Add TR_ADDED filter to iterator.
1049  */
1050 static PyObject *
1051 rpmts_iternext(rpmtsObject * s)
1052 {
1053     PyObject * result = NULL;
1054     rpmte te;
1055
1056 if (_rpmts_debug)
1057 fprintf(stderr, "*** rpmts_iternext(%p) ts %p tsi %p %d\n", s, s->ts, s->tsi, s->tsiFilter);
1058
1059     /* Reset iterator on 1st entry. */
1060     if (s->tsi == NULL) {
1061         s->tsi = rpmtsiInit(s->ts);
1062         if (s->tsi == NULL)
1063             return NULL;
1064         s->tsiFilter = 0;
1065     }
1066
1067     te = rpmtsiNext(s->tsi, s->tsiFilter);
1068     if (te != NULL) {
1069         result = (PyObject *) rpmte_Wrap(te);
1070     } else {
1071         s->tsi = rpmtsiFree(s->tsi);
1072         s->tsiFilter = 0;
1073     }
1074
1075     return result;
1076 }
1077
1078 /**
1079  * @todo Add TR_ADDED filter to iterator.
1080  */
1081 static PyObject *
1082 rpmts_Next(rpmtsObject * s)
1083 {
1084     PyObject * result;
1085
1086 if (_rpmts_debug)
1087 fprintf(stderr, "*** rpmts_Next(%p) ts %p\n", s, s->ts);
1088
1089     result = rpmts_iternext(s);
1090
1091     if (result == NULL) {
1092         Py_INCREF(Py_None);
1093         return Py_None;
1094     }
1095
1096     return result;
1097 }
1098
1099 /**
1100  */
1101 static specObject *
1102 spec_Parse(rpmtsObject * s, PyObject * args, PyObject * kwds)
1103 {
1104     const char * specfile;
1105     rpmSpec spec;
1106     char * buildRoot = NULL;
1107     int recursing = 0;
1108     char * passPhrase = "";
1109     char *cookie = NULL;
1110     int anyarch = 1;
1111     int force = 1;
1112     char * kwlist[] = {"specfile", NULL};
1113
1114     if (!PyArg_ParseTupleAndKeywords(args, kwds, "s:Parse", kwlist, &specfile))
1115         return NULL;
1116
1117     if (parseSpec(s->ts, specfile,"/", buildRoot,recursing, passPhrase,
1118              cookie, anyarch, force)!=0) {
1119              PyErr_SetString(pyrpmError, "can't parse specfile\n");
1120                      return NULL;
1121    }
1122
1123     spec = rpmtsSpec(s->ts);
1124     return spec_Wrap(spec);
1125 }
1126
1127 /**
1128  */
1129 static rpmmiObject *
1130 rpmts_Match(rpmtsObject * s, PyObject * args, PyObject * kwds)
1131 {
1132     PyObject *TagN = NULL;
1133     PyObject *Key = NULL;
1134     char *key = NULL;
1135 /* XXX lkey *must* be a 32 bit integer, int "works" on all known platforms. */
1136     int lkey = 0;
1137     int len = 0;
1138     rpmTag tag = RPMDBI_PACKAGES;
1139     char * kwlist[] = {"tagNumber", "key", NULL};
1140
1141 if (_rpmts_debug)
1142 fprintf(stderr, "*** rpmts_Match(%p) ts %p\n", s, s->ts);
1143
1144     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:Match", kwlist,
1145             &TagN, &Key))
1146         return NULL;
1147
1148     if (TagN && (tag = tagNumFromPyObject (TagN)) == -1) {
1149         PyErr_SetString(PyExc_TypeError, "unknown tag type");
1150         return NULL;
1151     }
1152
1153     if (Key) {
1154         if (PyString_Check(Key) || PyUnicode_Check(Key)) {
1155             key = PyString_AsString(Key);
1156             len = PyString_Size(Key);
1157         } else if (PyInt_Check(Key)) {
1158             lkey = PyInt_AsLong(Key);
1159             key = (char *)&lkey;
1160             len = sizeof(lkey);
1161         } else {
1162             PyErr_SetString(PyExc_TypeError, "unknown key type");
1163             return NULL;
1164         }
1165     }
1166
1167     /* XXX If not already opened, open the database O_RDONLY now. */
1168     /* XXX FIXME: lazy default rdonly open also done by rpmtsInitIterator(). */
1169     if (rpmtsGetRdb(s->ts) == NULL) {
1170         int rc = rpmtsOpenDB(s->ts, O_RDONLY);
1171         if (rc || rpmtsGetRdb(s->ts) == NULL) {
1172             PyErr_SetString(PyExc_TypeError, "rpmdb open failed");
1173             return NULL;
1174         }
1175     }
1176
1177     return rpmmi_Wrap( rpmtsInitIterator(s->ts, tag, key, len), (PyObject*)s);
1178 }
1179
1180 /** \ingroup py_c
1181  */
1182 static struct PyMethodDef rpmts_methods[] = {
1183  {"Debug",      (PyCFunction)rpmts_Debug,       METH_VARARGS|METH_KEYWORDS,
1184         NULL},
1185
1186  {"addInstall", (PyCFunction) rpmts_AddInstall, METH_VARARGS|METH_KEYWORDS,
1187         NULL },
1188  {"addErase",   (PyCFunction) rpmts_AddErase,   METH_VARARGS|METH_KEYWORDS,
1189         NULL },
1190  {"check",      (PyCFunction) rpmts_Check,      METH_VARARGS|METH_KEYWORDS,
1191         NULL },
1192  {"order",      (PyCFunction) rpmts_Order,      METH_NOARGS,
1193         NULL },
1194  {"setFlags",   (PyCFunction) rpmts_SetFlags,   METH_VARARGS|METH_KEYWORDS,
1195 "ts.setFlags(transFlags) -> previous transFlags\n\
1196 - Set control bit(s) for executing ts.run().\n\
1197   Note: This method replaces the 1st argument to the old ts.run()\n" },
1198  {"setProbFilter",      (PyCFunction) rpmts_SetProbFilter,      METH_VARARGS|METH_KEYWORDS,
1199 "ts.setProbFilter(ignoreSet) -> previous ignoreSet\n\
1200 - Set control bit(s) for ignoring problems found by ts.run().\n\
1201   Note: This method replaces the 2nd argument to the old ts.run()\n" },
1202  {"problems",   (PyCFunction) rpmts_Problems,   METH_NOARGS,
1203 "ts.problems() -> ps\n\
1204 - Return current problem set.\n" },
1205  {"run",        (PyCFunction) rpmts_Run,        METH_VARARGS|METH_KEYWORDS,
1206 "ts.run(callback, data) -> (problems)\n\
1207 - Run a transaction set, returning list of problems found.\n\
1208   Note: The callback may not be None.\n" },
1209  {"clean",      (PyCFunction) rpmts_Clean,      METH_NOARGS,
1210         NULL },
1211  {"openDB",     (PyCFunction) rpmts_OpenDB,     METH_NOARGS,
1212 "ts.openDB() -> None\n\
1213 - Open the default transaction rpmdb.\n\
1214   Note: The transaction rpmdb is lazily opened, so ts.openDB() is seldom needed.\n" },
1215  {"closeDB",    (PyCFunction) rpmts_CloseDB,    METH_NOARGS,
1216 "ts.closeDB() -> None\n\
1217 - Close the default transaction rpmdb.\n\
1218   Note: ts.closeDB() disables lazy opens, and should hardly ever be used.\n" },
1219  {"initDB",     (PyCFunction) rpmts_InitDB,     METH_NOARGS,
1220 "ts.initDB() -> None\n\
1221 - Initialize the default transaction rpmdb.\n\
1222  Note: ts.initDB() is seldom needed anymore.\n" },
1223  {"rebuildDB",  (PyCFunction) rpmts_RebuildDB,  METH_NOARGS,
1224 "ts.rebuildDB() -> None\n\
1225 - Rebuild the default transaction rpmdb.\n" },
1226  {"verifyDB",   (PyCFunction) rpmts_VerifyDB,   METH_NOARGS,
1227 "ts.verifyDB() -> None\n\
1228 - Verify the default transaction rpmdb.\n" },
1229  {"hdrFromFdno",(PyCFunction) rpmts_HdrFromFdno,METH_VARARGS|METH_KEYWORDS,
1230 "ts.hdrFromFdno(fdno) -> hdr\n\
1231 - Read a package header from a file descriptor.\n" },
1232  {"hdrCheck",   (PyCFunction) rpmts_HdrCheck,   METH_VARARGS|METH_KEYWORDS,
1233         NULL },
1234  {"setVSFlags",(PyCFunction) rpmts_SetVSFlags,  METH_VARARGS|METH_KEYWORDS,
1235 "ts.setVSFlags(vsflags) -> ovsflags\n\
1236 - Set signature verification flags. Values for vsflags are:\n\
1237     rpm.RPMVSF_NOHDRCHK      if set, don't check rpmdb headers\n\
1238     rpm.RPMVSF_NEEDPAYLOAD   if not set, check header+payload (if possible)\n\
1239     rpm.RPMVSF_NOSHA1HEADER  if set, don't check header SHA1 digest\n\
1240     rpm.RPMVSF_NODSAHEADER   if set, don't check header DSA signature\n\
1241     rpm.RPMVSF_NOMD5         if set, don't check header+payload MD5 digest\n\
1242     rpm.RPMVSF_NODSA         if set, don't check header+payload DSA signature\n\
1243     rpm.RPMVSF_NORSA         if set, don't check header+payload RSA signature\n\
1244     rpm._RPMVSF_NODIGESTS    if set, don't check digest(s)\n\
1245     rpm._RPMVSF_NOSIGNATURES if set, don't check signature(s)\n" },
1246  {"getVSFlags",(PyCFunction) rpmts_GetVSFlags,  METH_NOARGS,
1247 "ts.getVSFlags() -> vsflags\n\
1248 - Retrieve current signature verification flags from transaction\n" },
1249  {"setColor",(PyCFunction) rpmts_SetColor,      METH_VARARGS|METH_KEYWORDS,
1250         NULL },
1251  {"pgpPrtPkts", (PyCFunction) rpmts_PgpPrtPkts, METH_VARARGS|METH_KEYWORDS,
1252         NULL },
1253  {"pgpImportPubkey",    (PyCFunction) rpmts_PgpImportPubkey,    METH_VARARGS|METH_KEYWORDS,
1254         NULL },
1255  {"getKeys",    (PyCFunction) rpmts_GetKeys,    METH_NOARGS,
1256         NULL },
1257  {"parseSpec",  (PyCFunction) spec_Parse,       METH_VARARGS|METH_KEYWORDS,
1258 "ts.parseSpec(\"/path/to/foo.spec\") -> spec\n\
1259 - Parse a spec file.\n" },
1260  {"dbMatch",    (PyCFunction) rpmts_Match,      METH_VARARGS|METH_KEYWORDS,
1261 "ts.dbMatch([TagN, [key, [len]]]) -> mi\n\
1262 - Create a match iterator for the default transaction rpmdb.\n" },
1263  {"next",               (PyCFunction)rpmts_Next,        METH_NOARGS,
1264 "ts.next() -> te\n\
1265 - Retrieve next transaction set element.\n" },
1266     {NULL,              NULL}           /* sentinel */
1267 };
1268
1269 /** \ingroup py_c
1270  */
1271 static void rpmts_dealloc(rpmtsObject * s)
1272 {
1273
1274 if (_rpmts_debug)
1275 fprintf(stderr, "%p -- ts %p db %p\n", s, s->ts, rpmtsGetRdb(s->ts));
1276     s->ts = rpmtsFree(s->ts);
1277
1278     if (s->scriptFd) Fclose(s->scriptFd);
1279     /* this will free the keyList, and decrement the ref count of all
1280        the items on the list as well :-) */
1281     Py_DECREF(s->keyList);
1282     PyObject_Del((PyObject *)s);
1283 }
1284
1285 static PyObject * rpmts_getattro(PyObject * o, PyObject * n)
1286 {
1287     return PyObject_GenericGetAttr(o, n);
1288 }
1289
1290 /** \ingroup py_c
1291  */
1292 static int rpmts_setattro(PyObject * o, PyObject * n, PyObject * v)
1293 {
1294     rpmtsObject *s = (rpmtsObject *)o;
1295     char * name = PyString_AsString(n);
1296     int fdno;
1297
1298     if (!strcmp(name, "scriptFd")) {
1299         if (!PyArg_Parse(v, "i", &fdno)) return 0;
1300         if (fdno < 0) {
1301             PyErr_SetString(PyExc_TypeError, "bad file descriptor");
1302             return -1;
1303         } else {
1304             s->scriptFd = fdDup(fdno);
1305             rpmtsSetScriptFd(s->ts, s->scriptFd);
1306         }
1307     } else {
1308         PyErr_SetString(PyExc_AttributeError, name);
1309         return -1;
1310     }
1311
1312     return 0;
1313 }
1314
1315 /** \ingroup py_c
1316  */
1317 static int rpmts_init(rpmtsObject * s, PyObject *args, PyObject *kwds)
1318 {
1319     /* nothing to do atm... */
1320     return 0;
1321 }
1322
1323 /** \ingroup py_c
1324  */
1325 static void rpmts_free(rpmtsObject * s)
1326 {
1327 if (_rpmts_debug)
1328 fprintf(stderr, "%p -- ts %p db %p\n", s, s->ts, rpmtsGetRdb(s->ts));
1329     s->ts = rpmtsFree(s->ts);
1330
1331     if (s->scriptFd)
1332         Fclose(s->scriptFd);
1333
1334     /* this will free the keyList, and decrement the ref count of all
1335        the items on the list as well :-) */
1336     Py_DECREF(s->keyList);
1337
1338     PyObject_Del((PyObject *)s);
1339 }
1340
1341 /** \ingroup py_c
1342  */
1343 static PyObject * rpmts_alloc(PyTypeObject * subtype, int nitems)
1344 {
1345     PyObject * s = PyType_GenericAlloc(subtype, nitems);
1346
1347 if (_rpmts_debug < 0)
1348 fprintf(stderr, "*** rpmts_alloc(%p,%d) ret %p\n", subtype, nitems, s);
1349     return s;
1350 }
1351
1352 /** \ingroup py_c
1353  */
1354 static PyObject * rpmts_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds)
1355 {
1356     rpmtsObject * s = (void *) PyObject_New(rpmtsObject, subtype);
1357
1358     char * rootDir = "/";
1359     rpmVSFlags vsflags = rpmExpandNumeric("%{?__vsflags}");
1360     char * kwlist[] = {"rootdir", "vsflags", 0};
1361
1362     if (_rpmts_debug < 0)
1363         fprintf(stderr, "*** rpmts_new(%p,%p,%p)\n", s, args, kwds);
1364
1365     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|si:rpmts_init", kwlist,
1366             &rootDir, &vsflags))
1367         return NULL;
1368
1369     s->ts = rpmtsCreate();
1370     /* XXX: Why is there no rpmts_SetRootDir() ? */
1371     (void) rpmtsSetRootDir(s->ts, rootDir);
1372     /* XXX: make this use common code with rpmts_SetVSFlags() to check the
1373      *      python objects */
1374     (void) rpmtsSetVSFlags(s->ts, vsflags);
1375     s->keyList = PyList_New(0);
1376     s->scriptFd = NULL;
1377     s->tsi = NULL;
1378     s->tsiFilter = 0;
1379
1380     if (_rpmts_debug)
1381         fprintf(stderr, "%p ++ ts %p db %p\n", s, s->ts, rpmtsGetRdb(s->ts));
1382
1383     return (PyObject *)s;
1384 }
1385
1386 /**
1387  */
1388 static char rpmts_doc[] =
1389 "";
1390
1391 /** \ingroup py_c
1392  */
1393 PyTypeObject rpmts_Type = {
1394         PyObject_HEAD_INIT(&PyType_Type)
1395         0,                              /* ob_size */
1396         "rpm.ts",                       /* tp_name */
1397         sizeof(rpmtsObject),            /* tp_size */
1398         0,                              /* tp_itemsize */
1399         (destructor) rpmts_dealloc,     /* tp_dealloc */
1400         0,                              /* tp_print */
1401         (getattrfunc)0,                 /* tp_getattr */
1402         (setattrfunc)0,                 /* tp_setattr */
1403         0,                              /* tp_compare */
1404         0,                              /* tp_repr */
1405         0,                              /* tp_as_number */
1406         0,                              /* tp_as_sequence */
1407         0,                              /* tp_as_mapping */
1408         0,                              /* tp_hash */
1409         0,                              /* tp_call */
1410         0,                              /* tp_str */
1411         (getattrofunc) rpmts_getattro,  /* tp_getattro */
1412         (setattrofunc) rpmts_setattro,  /* tp_setattro */
1413         0,                              /* tp_as_buffer */
1414         Py_TPFLAGS_DEFAULT,             /* tp_flags */
1415         rpmts_doc,                      /* tp_doc */
1416 #if Py_TPFLAGS_HAVE_ITER
1417         0,                              /* tp_traverse */
1418         0,                              /* tp_clear */
1419         0,                              /* tp_richcompare */
1420         0,                              /* tp_weaklistoffset */
1421         (getiterfunc) rpmts_iter,       /* tp_iter */
1422         (iternextfunc) rpmts_iternext,  /* tp_iternext */
1423         rpmts_methods,                  /* tp_methods */
1424         0,                              /* tp_members */
1425         0,                              /* tp_getset */
1426         0,                              /* tp_base */
1427         0,                              /* tp_dict */
1428         0,                              /* tp_descr_get */
1429         0,                              /* tp_descr_set */
1430         0,                              /* tp_dictoffset */
1431         (initproc) rpmts_init,          /* tp_init */
1432         (allocfunc) rpmts_alloc,        /* tp_alloc */
1433         (newfunc) rpmts_new,            /* tp_new */
1434         (freefunc) rpmts_free,          /* tp_free */
1435         0,                              /* tp_is_gc */
1436 #endif
1437 };
1438
1439 /**
1440  */
1441 PyObject *
1442 rpmts_Create(PyObject * self, PyObject * args, PyObject * kwds)
1443 {
1444     return PyObject_Call((PyObject *) &rpmts_Type, args, kwds);
1445 }