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