2 * \file python/rpmts-py.c
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>
13 #include "header-py.h"
14 #include "rpmds-py.h" /* XXX for rpmdsNew */
15 #include "rpmfi-py.h" /* XXX for rpmfiNew */
25 extern int _rpmts_debug;
31 * \brief A python rpm.ts object represents an RPM transaction set.
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.
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:
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
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).
53 * A rpm.ts object has the following methods:
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').
62 * - addErase(name) Add an erase element to a transaction set.
63 * @param name the package name to be erased
65 * - check() Perform a dependency check on the transaction set. After
66 * headers have been added to a transaction set, a dependency
67 * check can be performed to make sure that all package
68 * dependencies are satisfied.
69 * @return None If there are no unresolved dependencies
70 * Otherwise a list of complex tuples is returned, one tuple per
71 * unresolved dependency, with
72 * The format of the dependency tuple is:
73 * ((packageName, packageVersion, packageRelease),
74 * (reqName, reqVersion),
78 * packageName, packageVersion, packageRelease are the name,
79 * version, and release of the package that has the unresolved
80 * dependency or conflict.
81 * The reqName and reqVersion are the name and version of the
82 * requirement or conflict.
83 * The needsFlags is a bitfield that describes the versioned
84 * nature of a requirement or conflict. The constants
85 * rpm.RPMSENSE_LESS, rpm.RPMSENSE_GREATER, and
86 * rpm.RPMSENSE_EQUAL can be logical ANDed with the needsFlags
87 * to get versioned dependency information.
88 * suggestedPackage is a tuple if the dependency check was aware
89 * of a package that solves this dependency problem when the
90 * dependency check was run. Packages that are added to the
91 * transaction set as "available" are examined during the
92 * dependency check as possible dependency solvers. The tuple
93 * contains two values, (header, suggestedName). These are set to
94 * the header of the suggested package and its name, respectively.
95 * If there is no known package to solve the dependency problem,
96 * suggestedPackage is None.
97 * The constants rpm.RPMDEP_SENSE_CONFLICTS and
98 * rpm.RPMDEP_SENSE_REQUIRES are set to show a dependency as a
99 * requirement or a conflict.
101 * - ts.order() Do a topological sort of added element relations.
104 * - ts.setFlags(transFlags) Set transaction set flags.
105 * @param transFlags - bit(s) to controll transaction operations. The
106 * following values can be logically OR'ed together:
107 * - rpm.RPMTRANS_FLAG_TEST - test mode, do not modify the RPM
108 * database, change any files, or run any package scripts
109 * - rpm.RPMTRANS_FLAG_BUILD_PROBS - only build a list of
110 * problems encountered when attempting to run this transaction
112 * - rpm.RPMTRANS_FLAG_NOSCRIPTS - do not execute package scripts
113 * - rpm.RPMTRANS_FLAG_JUSTDB - only make changes to the rpm
114 * database, do not modify files.
115 * - rpm.RPMTRANS_FLAG_NOTRIGGERS - do not run trigger scripts
116 * - rpm.RPMTRANS_FLAG_NODOCS - do not install files marked as %doc
117 * - rpm.RPMTRANS_FLAG_ALLFILES - create all files, even if a
118 * file is marked %config(missingok) and an upgrade is
120 * - rpm.RPMTRANS_FLAG_KEEPOBSOLETE - do not remove obsoleted
122 * @return previous transFlags
124 * - ts.setProbFilter(ignoreSet) Set transaction set problem filter.
125 * @param problemSetFilter - control bit(s) to ignore classes of problems,
126 * a logical or of one or more of the following bit(s):
127 * - rpm.RPMPROB_FILTER_IGNOREOS -
128 * - rpm.RPMPROB_FILTER_IGNOREARCH -
129 * - rpm.RPMPROB_FILTER_REPLACEPKG -
130 * - rpm.RPMPROB_FILTER_FORCERELOCATE -
131 * - rpm.RPMPROB_FILTER_REPLACENEWFILES -
132 * - rpm.RPMPROB_FILTER_REPLACEOLDFILES -
133 * - rpm.RPMPROB_FILTER_OLDPACKAGE -
134 * - rpm.RPMPROB_FILTER_DISKSPACE -
135 * @return previous ignoreSet
137 * - ts.run(callback,data) Attempt to execute a transaction set.
138 * After the transaction set has been populated with install/upgrade or
139 * erase actions, the transaction set can be executed by invoking
140 * the ts.run() method.
145 struct rpmtsCallbackType_s {
149 PyThreadState *_save;
155 rpmts_Debug(rpmtsObject * s, PyObject * args, PyObject * kwds)
157 char * kwlist[] = {"debugLevel", NULL};
159 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:Debug", kwlist,
163 if (_rpmts_debug < 0)
164 fprintf(stderr, "*** rpmts_Debug(%p) ts %p\n", s, s->ts);
171 static void die(PyObject *cb)
176 if (PyErr_Occurred()) {
179 if ((r = PyObject_Repr(cb)) != NULL) {
180 pyfn = PyString_AsString(r);
182 fprintf(stderr, _("error: python callback %s failed, aborting!\n"),
183 pyfn ? pyfn : "???");
184 rpmdbCheckTerminate(1);
191 rpmts_AddInstall(rpmtsObject * s, PyObject * args, PyObject * kwds)
195 char * how = "u"; /* XXX default to upgrade element if missing */
197 char * kwlist[] = {"header", "key", "how", NULL};
200 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O|s:AddInstall", kwlist,
201 &hdr_Type, &h, &key, &how))
204 { PyObject * hObj = (PyObject *) h;
205 if (hObj->ob_type != &hdr_Type) {
206 PyErr_SetString(PyExc_TypeError, "bad type for header argument");
211 if (_rpmts_debug < 0 || (_rpmts_debug > 0))
212 fprintf(stderr, "*** rpmts_AddInstall(%p,%p,%p,%s) ts %p\n", s, h, key, how, s->ts);
214 if (how && !rstreq(how, "u") && !rstreq(how, "i")) {
215 PyErr_SetString(PyExc_TypeError, "how argument must be \"u\" or \"i\"");
217 } else if (how && rstreq(how, "u"))
220 rc = rpmtsAddInstallElement(s->ts, hdrGetHeader(h), key, isUpgrade, NULL);
222 PyErr_SetString(pyrpmError, "adding package to transaction failed");
227 /* This should increment the usage count for me */
229 PyList_Append(s->keyList, key);
236 * @todo Permit finer control (i.e. not just --allmatches) of deleted elments.
239 rpmts_AddErase(rpmtsObject * s, PyObject * args, PyObject * kwds)
243 rpmdbMatchIterator mi = NULL;
245 char * kwlist[] = {"name", NULL};
248 fprintf(stderr, "*** rpmts_AddErase(%p) ts %p\n", s, s->ts);
250 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:AddErase", kwlist, &o))
253 if (PyString_Check(o)) {
254 char * name = PyString_AsString(o);
255 mi = rpmtsInitIterator(s->ts, RPMDBI_LABEL, name, 0);
256 } else if (PyInt_Check(o)) {
257 uint32_t recno = PyInt_AsLong(o);
258 mi = rpmtsInitIterator(s->ts, RPMDBI_PACKAGES, &recno, sizeof(recno));
260 PyErr_SetString(PyExc_TypeError, "string or integer expected");
264 while ((h = rpmdbNextIterator(mi)) != NULL) {
266 rpmtsAddEraseElement(s->ts, h, -1);
268 rpmdbFreeIterator(mi);
273 PyErr_SetString(pyrpmError, "package not installed");
281 rpmts_SolveCallback(rpmts ts, rpmds ds, const void * data)
283 struct rpmtsCallbackType_s * cbInfo = (struct rpmtsCallbackType_s *) data;
284 PyObject * args, * result;
288 fprintf(stderr, "*** rpmts_SolveCallback(%p,%p,%p) \"%s\"\n", ts, ds, data, rpmdsDNEVR(ds));
290 if (cbInfo->tso == NULL) return res;
291 if (cbInfo->cb == Py_None) return res;
293 PyEval_RestoreThread(cbInfo->_save);
295 args = Py_BuildValue("(Oissi)", cbInfo->tso,
296 rpmdsTagN(ds), rpmdsN(ds), rpmdsEVR(ds), rpmdsFlags(ds));
297 result = PyEval_CallObject(cbInfo->cb, args);
303 if (PyInt_Check(result))
304 res = PyInt_AsLong(result);
308 cbInfo->_save = PyEval_SaveThread();
316 rpmts_Check(rpmtsObject * s, PyObject * args, PyObject * kwds)
320 PyObject * list, * cf;
321 struct rpmtsCallbackType_s cbInfo;
323 char * kwlist[] = {"callback", NULL};
325 memset(&cbInfo, 0, sizeof(cbInfo));
326 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:Check", kwlist,
330 if (cbInfo.cb != NULL) {
331 if (!PyCallable_Check(cbInfo.cb)) {
332 PyErr_SetString(PyExc_TypeError, "expected a callable");
335 xx = rpmtsSetSolveCallback(s->ts, rpmts_SolveCallback, (void *)&cbInfo);
339 fprintf(stderr, "*** rpmts_Check(%p) ts %p cb %p\n", s, s->ts, cbInfo.cb);
342 cbInfo._save = PyEval_SaveThread();
344 xx = rpmtsCheck(s->ts);
345 ps = rpmtsProblems(s->ts);
347 PyEval_RestoreThread(cbInfo._save);
350 list = PyList_New(0);
351 rpmpsi psi = rpmpsInitIterator(ps);
353 /* XXX TODO: rpmlib >= 4.0.3 can return multiple suggested keys. */
354 while (rpmpsNextIterator(psi) >= 0) {
355 char * altNEVR, * needsName;
356 char * byName, * byVersion, * byRelease, *byArch;
357 char * needsOP, * needsVersion;
358 rpmsenseFlags needsFlags, sense;
361 p = rpmpsGetProblem(psi);
363 byName = xstrdup(rpmProblemGetPkgNEVR(p));
364 if ((byArch= strrchr(byName, '.')) != NULL)
366 if ((byRelease = strrchr(byName, '-')) != NULL)
368 if ((byVersion = strrchr(byName, '-')) != NULL)
371 key = rpmProblemGetKey(p);
373 altNEVR = needsName = xstrdup(rpmProblemGetAltNEVR(p));
374 if (needsName[1] == ' ') {
375 sense = (needsName[0] == 'C')
376 ? RPMDEP_SENSE_CONFLICTS : RPMDEP_SENSE_REQUIRES;
379 sense = RPMDEP_SENSE_REQUIRES;
380 if ((needsVersion = strrchr(needsName, ' ')) != NULL)
381 *needsVersion++ = '\0';
384 if ((needsOP = strrchr(needsName, ' ')) != NULL) {
385 for (*needsOP++ = '\0'; *needsOP != '\0'; needsOP++) {
386 if (*needsOP == '<') needsFlags |= RPMSENSE_LESS;
387 else if (*needsOP == '>') needsFlags |= RPMSENSE_GREATER;
388 else if (*needsOP == '=') needsFlags |= RPMSENSE_EQUAL;
392 cf = Py_BuildValue("((sss)(ss)iOi)", byName, byVersion, byRelease,
393 needsName, needsVersion, needsFlags,
394 (key != NULL ? key : Py_None),
396 PyList_Append(list, (PyObject *) cf);
402 psi = rpmpsFreeIterator(psi);
415 rpmts_Order(rpmtsObject * s)
420 fprintf(stderr, "*** rpmts_Order(%p) ts %p\n", s, s->ts);
422 Py_BEGIN_ALLOW_THREADS
423 rc = rpmtsOrder(s->ts);
426 return Py_BuildValue("i", rc);
432 rpmts_Clean(rpmtsObject * s)
435 fprintf(stderr, "*** rpmts_Clean(%p) ts %p\n", s, s->ts);
446 rpmts_OpenDB(rpmtsObject * s)
451 fprintf(stderr, "*** rpmts_OpenDB(%p) ts %p\n", s, s->ts);
453 dbmode = rpmtsGetDBMode(s->ts);
457 return Py_BuildValue("i", rpmtsOpenDB(s->ts, dbmode));
463 rpmts_CloseDB(rpmtsObject * s)
468 fprintf(stderr, "*** rpmts_CloseDB(%p) ts %p\n", s, s->ts);
470 rc = rpmtsCloseDB(s->ts);
471 rpmtsSetDBMode(s->ts, -1); /* XXX disable lazy opens */
473 return Py_BuildValue("i", rc);
479 rpmts_InitDB(rpmtsObject * s)
484 fprintf(stderr, "*** rpmts_InitDB(%p) ts %p\n", s, s->ts);
486 rc = rpmtsInitDB(s->ts, O_RDONLY);
488 rc = rpmtsCloseDB(s->ts);
490 return Py_BuildValue("i", rc);
496 rpmts_RebuildDB(rpmtsObject * s)
501 fprintf(stderr, "*** rpmts_RebuildDB(%p) ts %p\n", s, s->ts);
503 Py_BEGIN_ALLOW_THREADS
504 rc = rpmtsRebuildDB(s->ts);
507 return Py_BuildValue("i", rc);
513 rpmts_VerifyDB(rpmtsObject * s)
518 fprintf(stderr, "*** rpmts_VerifyDB(%p) ts %p\n", s, s->ts);
520 Py_BEGIN_ALLOW_THREADS
521 rc = rpmtsVerifyDB(s->ts);
524 return Py_BuildValue("i", rc);
530 rpmts_HdrFromFdno(rpmtsObject * s, PyObject * args, PyObject * kwds)
532 PyObject * result = NULL;
537 char * kwlist[] = {"fd", NULL};
539 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:HdrFromFdno", kwlist,
544 rpmrc = rpmReadPackageFile(s->ts, fd, "rpmts_HdrFromFdno", &h);
548 fprintf(stderr, "*** rpmts_HdrFromFdno(%p) ts %p rc %d\n", s, s->ts, rpmrc);
553 result = Py_BuildValue("N", hdr_Wrap(h));
554 h = headerFree(h); /* XXX ref held by result */
558 PyErr_SetString(pyrpmError, "public key not available");
561 case RPMRC_NOTTRUSTED:
562 PyErr_SetString(pyrpmError, "public key not trusted");
568 PyErr_SetString(pyrpmError, "error reading package header");
578 rpmts_HdrCheck(rpmtsObject * s, PyObject * args, PyObject * kwds)
581 PyObject * result = NULL;
586 char * kwlist[] = {"headers", NULL};
589 fprintf(stderr, "*** rpmts_HdrCheck(%p) ts %p\n", s, s->ts);
591 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:HdrCheck", kwlist, &blob))
594 if (blob == Py_None) {
598 if (!PyString_Check(blob)) {
599 PyErr_SetString(pyrpmError, "hdrCheck takes a string of octets");
602 uh = PyString_AsString(blob);
603 uc = PyString_Size(blob);
605 rpmrc = headerCheck(s->ts, uh, uc, &msg);
614 PyErr_SetString(pyrpmError, "public key not availaiable");
617 case RPMRC_NOTTRUSTED:
618 PyErr_SetString(pyrpmError, "public key not trusted");
623 PyErr_SetString(pyrpmError, msg);
634 rpmts_SetVSFlags(rpmtsObject * s, PyObject * args, PyObject * kwds)
637 char * kwlist[] = {"flags", NULL};
640 fprintf(stderr, "*** rpmts_SetVSFlags(%p) ts %p\n", s, s->ts);
642 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:SetVSFlags", kwlist,
646 /* XXX FIXME: value check on vsflags, or build pure python object
647 * for it, and require an object of that type */
649 return Py_BuildValue("i", rpmtsSetVSFlags(s->ts, vsflags));
655 rpmts_GetVSFlags(rpmtsObject * s)
657 return Py_BuildValue("i", rpmtsVSFlags(s->ts));
663 rpmts_SetColor(rpmtsObject * s, PyObject * args, PyObject * kwds)
666 char * kwlist[] = {"color", NULL};
669 fprintf(stderr, "*** rpmts_SetColor(%p) ts %p\n", s, s->ts);
671 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:Color", kwlist, &tscolor))
674 /* XXX FIXME: value check on tscolor, or build pure python object
675 * for it, and require an object of that type */
677 return Py_BuildValue("i", rpmtsSetColor(s->ts, tscolor));
683 rpmts_PgpPrtPkts(rpmtsObject * s, PyObject * args, PyObject * kwds)
689 char * kwlist[] = {"octets", NULL};
692 fprintf(stderr, "*** rpmts_PgpPrtPkts(%p) ts %p\n", s, s->ts);
694 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:PgpPrtPkts", kwlist, &blob))
697 if (blob == Py_None) {
701 if (!PyString_Check(blob)) {
702 PyErr_SetString(pyrpmError, "pgpPrtPkts takes a string of octets");
705 pkt = (unsigned char *)PyString_AsString(blob);
706 pktlen = PyString_Size(blob);
708 rc = pgpPrtPkts(pkt, pktlen, NULL, 1);
710 return Py_BuildValue("i", rc);
716 rpmts_PgpImportPubkey(rpmtsObject * s, PyObject * args, PyObject * kwds)
722 char * kwlist[] = {"pubkey", NULL};
725 fprintf(stderr, "*** rpmts_PgpImportPubkey(%p) ts %p\n", s, s->ts);
727 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:PgpImportPubkey",
731 if (blob == Py_None) {
735 if (!PyString_Check(blob)) {
736 PyErr_SetString(pyrpmError, "PgpImportPubkey takes a string of octets");
739 pkt = (unsigned char *)PyString_AsString(blob);
740 pktlen = PyString_Size(blob);
742 rc = rpmtsImportPubkey(s->ts, pkt, pktlen);
744 return Py_BuildValue("i", rc);
750 rpmts_GetKeys(rpmtsObject * s)
752 const void **data = NULL;
757 fprintf(stderr, "*** rpmts_GetKeys(%p) ts %p\n", s, s->ts);
759 rpmtsGetKeys(s->ts, &data, &num);
760 if (data == NULL || num <= 0) {
766 tuple = PyTuple_New(num);
768 for (i = 0; i < num; i++) {
770 obj = (data[i] ? (PyObject *) data[i] : Py_None);
772 PyTuple_SetItem(tuple, i, obj);
783 rpmtsCallback(const void * hd, const rpmCallbackType what,
784 const rpm_loff_t amount, const rpm_loff_t total,
785 const void * pkgKey, rpmCallbackData data)
787 Header h = (Header) hd;
788 struct rpmtsCallbackType_s * cbInfo = data;
789 PyObject * pkgObj = (PyObject *) pkgKey;
790 PyObject * args, * result;
793 if (cbInfo->cb == Py_None) return NULL;
795 /* Synthesize a python object for callback (if necessary). */
796 if (pkgObj == NULL) {
798 pkgObj = Py_BuildValue("s", headerGetString(h, RPMTAG_NAME));
806 PyEval_RestoreThread(cbInfo->_save);
808 args = Py_BuildValue("(iLLOO)", what, amount, total, pkgObj, cbInfo->data);
809 result = PyEval_CallObject(cbInfo->cb, args);
817 if (what == RPMCALLBACK_INST_OPEN_FILE) {
820 if (!PyArg_Parse(result, "i", &fdno)) {
824 cbInfo->_save = PyEval_SaveThread();
828 fprintf(stderr, "\t%p = fdDup(%d)\n", fd, fdno);
830 fcntl(Fileno(fd), F_SETFD, FD_CLOEXEC);
834 if (what == RPMCALLBACK_INST_CLOSE_FILE) {
836 fprintf(stderr, "\tFclose(%p)\n", fd);
840 fprintf(stderr, "\t%llu:%llu key %p\n", (long long) amount, (long long) total, pkgKey);
844 cbInfo->_save = PyEval_SaveThread();
852 rpmts_SetFlags(rpmtsObject * s, PyObject * args, PyObject * kwds)
854 rpmtransFlags transFlags = 0;
855 char * kwlist[] = {"flags", NULL};
857 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:SetFlags", kwlist,
862 fprintf(stderr, "*** rpmts_SetFlags(%p) ts %p transFlags %x\n", s, s->ts, transFlags);
864 /* XXX FIXME: value check on flags, or build pure python object
865 * for it, and require an object of that type */
867 return Py_BuildValue("i", rpmtsSetFlags(s->ts, transFlags));
873 rpmts_SetProbFilter(rpmtsObject * s, PyObject * args, PyObject * kwds)
875 rpmprobFilterFlags ignoreSet = 0;
876 rpmprobFilterFlags oignoreSet;
877 char * kwlist[] = {"ignoreSet", NULL};
879 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:ProbFilter", kwlist,
884 fprintf(stderr, "*** rpmts_SetProbFilter(%p) ts %p ignoreSet %x\n", s, s->ts, ignoreSet);
886 oignoreSet = s->ignoreSet;
887 s->ignoreSet = ignoreSet;
889 return Py_BuildValue("i", oignoreSet);
895 rpmts_Problems(rpmtsObject * s)
899 fprintf(stderr, "*** rpmts_Problems(%p) ts %p\n", s, s->ts);
901 return rpmps_Wrap( rpmtsProblems(s->ts) );
907 rpmts_Run(rpmtsObject * s, PyObject * args, PyObject * kwds)
913 struct rpmtsCallbackType_s cbInfo;
914 char * kwlist[] = {"callback", "data", NULL};
916 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO:Run", kwlist,
917 &cbInfo.cb, &cbInfo.data))
921 cbInfo._save = PyEval_SaveThread();
923 if (cbInfo.cb != NULL) {
924 if (!PyCallable_Check(cbInfo.cb)) {
925 PyErr_SetString(PyExc_TypeError, "expected a callable");
928 (void) rpmtsSetNotifyCallback(s->ts, rpmtsCallback, (void *) &cbInfo);
932 fprintf(stderr, "*** rpmts_Run(%p) ts %p ignore %x\n", s, s->ts, s->ignoreSet);
934 rc = rpmtsRun(s->ts, NULL, s->ignoreSet);
935 ps = rpmtsProblems(s->ts);
938 (void) rpmtsSetNotifyCallback(s->ts, NULL, NULL);
940 PyEval_RestoreThread(cbInfo._save);
943 list = PyList_New(0);
950 list = PyList_New(0);
951 psi = rpmpsInitIterator(ps);
952 while (rpmpsNextIterator(psi) >= 0) {
953 rpmProblem p = rpmpsGetProblem(psi);
954 char * ps = rpmProblemString(p);
955 PyObject * prob = Py_BuildValue("s(isN)", ps,
956 rpmProblemGetType(p),
958 PyLong_FromLongLong(rpmProblemGetDiskNeed(p)));
959 PyList_Append(list, prob);
964 psi = rpmpsFreeIterator(psi);
970 #if Py_TPFLAGS_HAVE_ITER
972 rpmts_iter(rpmtsObject * s)
975 fprintf(stderr, "*** rpmts_iter(%p) ts %p\n", s, s->ts);
978 return (PyObject *)s;
983 * @todo Add TR_ADDED filter to iterator.
986 rpmts_iternext(rpmtsObject * s)
988 PyObject * result = NULL;
992 fprintf(stderr, "*** rpmts_iternext(%p) ts %p tsi %p %d\n", s, s->ts, s->tsi, s->tsiFilter);
994 /* Reset iterator on 1st entry. */
995 if (s->tsi == NULL) {
996 s->tsi = rpmtsiInit(s->ts);
1002 te = rpmtsiNext(s->tsi, s->tsiFilter);
1004 result = (PyObject *) rpmte_Wrap(te);
1006 s->tsi = rpmtsiFree(s->tsi);
1014 * @todo Add TR_ADDED filter to iterator.
1017 rpmts_Next(rpmtsObject * s)
1022 fprintf(stderr, "*** rpmts_Next(%p) ts %p\n", s, s->ts);
1024 result = rpmts_iternext(s);
1026 if (result == NULL) {
1037 spec_Parse(rpmtsObject * s, PyObject * args, PyObject * kwds)
1039 const char * specfile;
1041 char * buildRoot = NULL;
1043 char * passPhrase = "";
1044 char *cookie = NULL;
1047 char * kwlist[] = {"specfile", NULL};
1049 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s:Parse", kwlist, &specfile))
1052 if (parseSpec(s->ts, specfile,"/", buildRoot,recursing, passPhrase,
1053 cookie, anyarch, force)!=0) {
1054 PyErr_SetString(pyrpmError, "can't parse specfile\n");
1058 spec = rpmtsSpec(s->ts);
1059 return spec_Wrap(spec);
1064 static rpmmiObject *
1065 rpmts_Match(rpmtsObject * s, PyObject * args, PyObject * kwds)
1067 PyObject *TagN = NULL;
1068 PyObject *Key = NULL;
1070 /* XXX lkey *must* be a 32 bit integer, int "works" on all known platforms. */
1073 rpmTag tag = RPMDBI_PACKAGES;
1074 char * kwlist[] = {"tagNumber", "key", NULL};
1077 fprintf(stderr, "*** rpmts_Match(%p) ts %p\n", s, s->ts);
1079 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:Match", kwlist,
1083 if (TagN && (tag = tagNumFromPyObject (TagN)) == -1) {
1084 PyErr_SetString(PyExc_TypeError, "unknown tag type");
1089 if (PyString_Check(Key)) {
1090 key = PyString_AsString(Key);
1091 len = PyString_Size(Key);
1092 } else if (PyInt_Check(Key)) {
1093 lkey = PyInt_AsLong(Key);
1094 key = (char *)&lkey;
1097 PyErr_SetString(PyExc_TypeError, "unknown key type");
1100 /* One of the conversions above failed, exception is set already */
1101 if (PyErr_Occurred()) {
1106 /* XXX If not already opened, open the database O_RDONLY now. */
1107 /* XXX FIXME: lazy default rdonly open also done by rpmtsInitIterator(). */
1108 if (rpmtsGetRdb(s->ts) == NULL) {
1109 int rc = rpmtsOpenDB(s->ts, O_RDONLY);
1110 if (rc || rpmtsGetRdb(s->ts) == NULL) {
1111 PyErr_SetString(PyExc_TypeError, "rpmdb open failed");
1116 return rpmmi_Wrap( rpmtsInitIterator(s->ts, tag, key, len), (PyObject*)s);
1121 static struct PyMethodDef rpmts_methods[] = {
1122 {"Debug", (PyCFunction)rpmts_Debug, METH_VARARGS|METH_KEYWORDS,
1125 {"addInstall", (PyCFunction) rpmts_AddInstall, METH_VARARGS|METH_KEYWORDS,
1127 {"addErase", (PyCFunction) rpmts_AddErase, METH_VARARGS|METH_KEYWORDS,
1129 {"check", (PyCFunction) rpmts_Check, METH_VARARGS|METH_KEYWORDS,
1131 {"order", (PyCFunction) rpmts_Order, METH_NOARGS,
1133 {"setFlags", (PyCFunction) rpmts_SetFlags, METH_VARARGS|METH_KEYWORDS,
1134 "ts.setFlags(transFlags) -> previous transFlags\n\
1135 - Set control bit(s) for executing ts.run().\n\
1136 Note: This method replaces the 1st argument to the old ts.run()\n" },
1137 {"setProbFilter", (PyCFunction) rpmts_SetProbFilter, METH_VARARGS|METH_KEYWORDS,
1138 "ts.setProbFilter(ignoreSet) -> previous ignoreSet\n\
1139 - Set control bit(s) for ignoring problems found by ts.run().\n\
1140 Note: This method replaces the 2nd argument to the old ts.run()\n" },
1141 {"problems", (PyCFunction) rpmts_Problems, METH_NOARGS,
1142 "ts.problems() -> ps\n\
1143 - Return current problem set.\n" },
1144 {"run", (PyCFunction) rpmts_Run, METH_VARARGS|METH_KEYWORDS,
1145 "ts.run(callback, data) -> (problems)\n\
1146 - Run a transaction set, returning list of problems found.\n\
1147 Note: The callback may not be None.\n" },
1148 {"clean", (PyCFunction) rpmts_Clean, METH_NOARGS,
1150 {"openDB", (PyCFunction) rpmts_OpenDB, METH_NOARGS,
1151 "ts.openDB() -> None\n\
1152 - Open the default transaction rpmdb.\n\
1153 Note: The transaction rpmdb is lazily opened, so ts.openDB() is seldom needed.\n" },
1154 {"closeDB", (PyCFunction) rpmts_CloseDB, METH_NOARGS,
1155 "ts.closeDB() -> None\n\
1156 - Close the default transaction rpmdb.\n\
1157 Note: ts.closeDB() disables lazy opens, and should hardly ever be used.\n" },
1158 {"initDB", (PyCFunction) rpmts_InitDB, METH_NOARGS,
1159 "ts.initDB() -> None\n\
1160 - Initialize the default transaction rpmdb.\n\
1161 Note: ts.initDB() is seldom needed anymore.\n" },
1162 {"rebuildDB", (PyCFunction) rpmts_RebuildDB, METH_NOARGS,
1163 "ts.rebuildDB() -> None\n\
1164 - Rebuild the default transaction rpmdb.\n" },
1165 {"verifyDB", (PyCFunction) rpmts_VerifyDB, METH_NOARGS,
1166 "ts.verifyDB() -> None\n\
1167 - Verify the default transaction rpmdb.\n" },
1168 {"hdrFromFdno",(PyCFunction) rpmts_HdrFromFdno,METH_VARARGS|METH_KEYWORDS,
1169 "ts.hdrFromFdno(fdno) -> hdr\n\
1170 - Read a package header from a file descriptor.\n" },
1171 {"hdrCheck", (PyCFunction) rpmts_HdrCheck, METH_VARARGS|METH_KEYWORDS,
1173 {"setVSFlags",(PyCFunction) rpmts_SetVSFlags, METH_VARARGS|METH_KEYWORDS,
1174 "ts.setVSFlags(vsflags) -> ovsflags\n\
1175 - Set signature verification flags. Values for vsflags are:\n\
1176 rpm.RPMVSF_NOHDRCHK if set, don't check rpmdb headers\n\
1177 rpm.RPMVSF_NEEDPAYLOAD if not set, check header+payload (if possible)\n\
1178 rpm.RPMVSF_NOSHA1HEADER if set, don't check header SHA1 digest\n\
1179 rpm.RPMVSF_NODSAHEADER if set, don't check header DSA signature\n\
1180 rpm.RPMVSF_NOMD5 if set, don't check header+payload MD5 digest\n\
1181 rpm.RPMVSF_NODSA if set, don't check header+payload DSA signature\n\
1182 rpm.RPMVSF_NORSA if set, don't check header+payload RSA signature\n\
1183 rpm._RPMVSF_NODIGESTS if set, don't check digest(s)\n\
1184 rpm._RPMVSF_NOSIGNATURES if set, don't check signature(s)\n" },
1185 {"getVSFlags",(PyCFunction) rpmts_GetVSFlags, METH_NOARGS,
1186 "ts.getVSFlags() -> vsflags\n\
1187 - Retrieve current signature verification flags from transaction\n" },
1188 {"setColor",(PyCFunction) rpmts_SetColor, METH_VARARGS|METH_KEYWORDS,
1190 {"pgpPrtPkts", (PyCFunction) rpmts_PgpPrtPkts, METH_VARARGS|METH_KEYWORDS,
1192 {"pgpImportPubkey", (PyCFunction) rpmts_PgpImportPubkey, METH_VARARGS|METH_KEYWORDS,
1194 {"getKeys", (PyCFunction) rpmts_GetKeys, METH_NOARGS,
1196 {"parseSpec", (PyCFunction) spec_Parse, METH_VARARGS|METH_KEYWORDS,
1197 "ts.parseSpec(\"/path/to/foo.spec\") -> spec\n\
1198 - Parse a spec file.\n" },
1199 {"dbMatch", (PyCFunction) rpmts_Match, METH_VARARGS|METH_KEYWORDS,
1200 "ts.dbMatch([TagN, [key, [len]]]) -> mi\n\
1201 - Create a match iterator for the default transaction rpmdb.\n" },
1202 {"next", (PyCFunction)rpmts_Next, METH_NOARGS,
1204 - Retrieve next transaction set element.\n" },
1205 {NULL, NULL} /* sentinel */
1210 static void rpmts_dealloc(rpmtsObject * s)
1214 fprintf(stderr, "%p -- ts %p db %p\n", s, s->ts, rpmtsGetRdb(s->ts));
1215 s->ts = rpmtsFree(s->ts);
1217 if (s->scriptFd) Fclose(s->scriptFd);
1218 /* this will free the keyList, and decrement the ref count of all
1219 the items on the list as well :-) */
1220 Py_DECREF(s->keyList);
1221 PyObject_Del((PyObject *)s);
1224 static PyObject * rpmts_getattro(PyObject * o, PyObject * n)
1226 return PyObject_GenericGetAttr(o, n);
1231 static int rpmts_setattro(PyObject * o, PyObject * n, PyObject * v)
1233 rpmtsObject *s = (rpmtsObject *)o;
1234 char * name = PyString_AsString(n);
1237 if (rstreq(name, "scriptFd")) {
1238 if (!PyArg_Parse(v, "i", &fdno)) return 0;
1240 PyErr_SetString(PyExc_TypeError, "bad file descriptor");
1243 s->scriptFd = fdDup(fdno);
1244 rpmtsSetScriptFd(s->ts, s->scriptFd);
1247 PyErr_SetString(PyExc_AttributeError, name);
1256 static int rpmts_init(rpmtsObject * s, PyObject *args, PyObject *kwds)
1258 /* nothing to do atm... */
1264 static void rpmts_free(rpmtsObject * s)
1267 fprintf(stderr, "%p -- ts %p db %p\n", s, s->ts, rpmtsGetRdb(s->ts));
1268 s->ts = rpmtsFree(s->ts);
1271 Fclose(s->scriptFd);
1273 /* this will free the keyList, and decrement the ref count of all
1274 the items on the list as well :-) */
1275 Py_DECREF(s->keyList);
1277 PyObject_Del((PyObject *)s);
1282 static PyObject * rpmts_alloc(PyTypeObject * subtype, int nitems)
1284 PyObject * s = PyType_GenericAlloc(subtype, nitems);
1286 if (_rpmts_debug < 0)
1287 fprintf(stderr, "*** rpmts_alloc(%p,%d) ret %p\n", subtype, nitems, s);
1293 static PyObject * rpmts_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds)
1295 rpmtsObject * s = (void *) PyObject_New(rpmtsObject, subtype);
1297 char * rootDir = "/";
1298 rpmVSFlags vsflags = rpmExpandNumeric("%{?__vsflags}");
1299 char * kwlist[] = {"rootdir", "vsflags", 0};
1301 if (_rpmts_debug < 0)
1302 fprintf(stderr, "*** rpmts_new(%p,%p,%p)\n", s, args, kwds);
1304 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|si:rpmts_init", kwlist,
1305 &rootDir, &vsflags))
1308 s->ts = rpmtsCreate();
1309 /* XXX: Why is there no rpmts_SetRootDir() ? */
1310 (void) rpmtsSetRootDir(s->ts, rootDir);
1311 /* XXX: make this use common code with rpmts_SetVSFlags() to check the
1313 (void) rpmtsSetVSFlags(s->ts, vsflags);
1314 s->keyList = PyList_New(0);
1320 fprintf(stderr, "%p ++ ts %p db %p\n", s, s->ts, rpmtsGetRdb(s->ts));
1322 return (PyObject *)s;
1327 static char rpmts_doc[] =
1332 PyTypeObject rpmts_Type = {
1333 PyObject_HEAD_INIT(&PyType_Type)
1335 "rpm.ts", /* tp_name */
1336 sizeof(rpmtsObject), /* tp_size */
1337 0, /* tp_itemsize */
1338 (destructor) rpmts_dealloc, /* tp_dealloc */
1340 (getattrfunc)0, /* tp_getattr */
1341 (setattrfunc)0, /* tp_setattr */
1344 0, /* tp_as_number */
1345 0, /* tp_as_sequence */
1346 0, /* tp_as_mapping */
1350 (getattrofunc) rpmts_getattro, /* tp_getattro */
1351 (setattrofunc) rpmts_setattro, /* tp_setattro */
1352 0, /* tp_as_buffer */
1353 Py_TPFLAGS_DEFAULT, /* tp_flags */
1354 rpmts_doc, /* tp_doc */
1355 #if Py_TPFLAGS_HAVE_ITER
1356 0, /* tp_traverse */
1358 0, /* tp_richcompare */
1359 0, /* tp_weaklistoffset */
1360 (getiterfunc) rpmts_iter, /* tp_iter */
1361 (iternextfunc) rpmts_iternext, /* tp_iternext */
1362 rpmts_methods, /* tp_methods */
1367 0, /* tp_descr_get */
1368 0, /* tp_descr_set */
1369 0, /* tp_dictoffset */
1370 (initproc) rpmts_init, /* tp_init */
1371 (allocfunc) rpmts_alloc, /* tp_alloc */
1372 (newfunc) rpmts_new, /* tp_new */
1373 (freefunc) rpmts_free, /* tp_free */
1381 rpmts_Create(PyObject * self, PyObject * args, PyObject * kwds)
1383 return PyObject_Call((PyObject *) &rpmts_Type, args, kwds);