2 * \file python/rpmts-py.c
7 #include <rpm/rpmlib.h>
8 #include <rpm/rpmpgp.h>
10 #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'), or if it is just
61 * available to the transaction when computing
62 * dependencies but no action should be performed with it
65 * - addErase(name) Add an erase element to a transaction set.
66 * @param name the package name to be erased
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),
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.
104 * - ts.order() Do a topological sort of added element relations.
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
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
123 * - rpm.RPMTRANS_FLAG_KEEPOBSOLETE - do not remove obsoleted
125 * @return previous transFlags
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
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.
148 struct rpmtsCallbackType_s {
153 PyThreadState *_save;
159 rpmts_Debug(rpmtsObject * s, PyObject * args, PyObject * kwds)
161 char * kwlist[] = {"debugLevel", NULL};
163 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:Debug", kwlist,
167 if (_rpmts_debug < 0)
168 fprintf(stderr, "*** rpmts_Debug(%p) ts %p\n", s, s->ts);
176 * Add package to universe of possible packages to install in transaction set.
177 * @param ts transaction set
179 * @param key package private data
181 static void rpmtsAddAvailableElement(rpmts ts, Header h,
185 rpmds provides = rpmdsNew(h, RPMTAG_PROVIDENAME, scareMem);
186 rpmfi fi = rpmfiNew(ts, h, RPMTAG_BASENAMES, scareMem);
188 /* XXX FIXME: return code RPMAL_NOMATCH is error */
189 (void) rpmalAdd(&ts->availablePackages, RPMAL_NOMATCH, key,
190 provides, fi, rpmtsColor(ts));
192 provides = rpmdsFree(provides);
194 if (_rpmts_debug < 0)
195 fprintf(stderr, "\tAddAvailable(%p) list %p\n", ts, ts->availablePackages);
203 rpmts_AddInstall(rpmtsObject * s, PyObject * args, PyObject * kwds)
207 char * how = "u"; /* XXX default to upgrade element if missing */
209 char * kwlist[] = {"header", "key", "how", NULL};
212 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O|s:AddInstall", kwlist,
213 &hdr_Type, &h, &key, &how))
216 { PyObject * hObj = (PyObject *) h;
217 if (hObj->ob_type != &hdr_Type) {
218 PyErr_SetString(PyExc_TypeError, "bad type for header argument");
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);
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\"");
229 } else if (how && !strcmp(how, "u"))
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.
237 if (how && !strcmp(how, "a")) {
239 rpmtsAddAvailableElement(s->ts, hdrGetHeader(h), key);
241 PyErr_SetString(pyrpmError, "available package mechanism currently broken");
245 rc = rpmtsAddInstallElement(s->ts, hdrGetHeader(h), key, isUpgrade, NULL);
247 PyErr_SetString(pyrpmError, "adding package to transaction failed");
252 /* This should increment the usage count for me */
254 PyList_Append(s->keyList, key);
261 * @todo Permit finer control (i.e. not just --allmatches) of deleted elments.
264 rpmts_AddErase(rpmtsObject * s, PyObject * args, PyObject * kwds)
268 rpmdbMatchIterator mi;
269 char * kwlist[] = {"name", NULL};
272 fprintf(stderr, "*** rpmts_AddErase(%p) ts %p\n", s, s->ts);
274 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:AddErase", kwlist, &o))
277 if (PyString_Check(o)) {
278 char * name = PyString_AsString(o);
280 mi = rpmtsInitIterator(s->ts, RPMDBI_LABEL, name, 0);
281 count = rpmdbGetIteratorCount(mi);
283 mi = rpmdbFreeIterator(mi);
284 PyErr_SetString(pyrpmError, "package not installed");
286 } else { /* XXX: Note that we automatically choose to remove all matches */
288 while ((h = rpmdbNextIterator(mi)) != NULL) {
289 unsigned int recOffset = rpmdbGetIteratorOffset(mi);
291 rpmtsAddEraseElement(s->ts, h, recOffset);
294 mi = rpmdbFreeIterator(mi);
296 if (PyInt_Check(o)) {
297 uint32_t instance = PyInt_AsLong(o);
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");
306 while ((h = rpmdbNextIterator(mi)) != NULL) {
307 uint32_t recOffset = rpmdbGetIteratorOffset(mi);
309 rpmtsAddEraseElement(s->ts, h, recOffset);
313 mi = rpmdbFreeIterator(mi);
323 rpmts_SolveCallback(rpmts ts, rpmds ds, const void * data)
325 struct rpmtsCallbackType_s * cbInfo = (struct rpmtsCallbackType_s *) data;
326 PyObject * args, * result;
330 fprintf(stderr, "*** rpmts_SolveCallback(%p,%p,%p) \"%s\"\n", ts, ds, data, rpmdsDNEVR(ds));
332 if (cbInfo->tso == NULL) return res;
333 if (cbInfo->pythonError) return res;
334 if (cbInfo->cb == Py_None) return res;
336 PyEval_RestoreThread(cbInfo->_save);
338 args = Py_BuildValue("(Oissi)", cbInfo->tso,
339 rpmdsTagN(ds), rpmdsN(ds), rpmdsEVR(ds), rpmdsFlags(ds));
340 result = PyEval_CallObject(cbInfo->cb, args);
344 cbInfo->pythonError = 1;
346 if (PyInt_Check(result))
347 res = PyInt_AsLong(result);
351 cbInfo->_save = PyEval_SaveThread();
359 rpmts_Check(rpmtsObject * s, PyObject * args, PyObject * kwds)
363 PyObject * list, * cf;
364 struct rpmtsCallbackType_s cbInfo;
367 char * kwlist[] = {"callback", NULL};
369 memset(&cbInfo, 0, sizeof(cbInfo));
370 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:Check", kwlist,
374 if (cbInfo.cb != NULL) {
375 if (!PyCallable_Check(cbInfo.cb)) {
376 PyErr_SetString(PyExc_TypeError, "expected a callable");
379 xx = rpmtsSetSolveCallback(s->ts, rpmts_SolveCallback, (void *)&cbInfo);
383 fprintf(stderr, "*** rpmts_Check(%p) ts %p cb %p\n", s, s->ts, cbInfo.cb);
386 cbInfo.pythonError = 0;
387 cbInfo._save = PyEval_SaveThread();
390 /* XXX resurrect availablePackages one more time ... */
391 rpmalMakeIndex(s->ts->availablePackages);
394 xx = rpmtsCheck(s->ts);
395 ps = rpmtsProblems(s->ts);
398 xx = rpmtsSetSolveCallback(s->ts, rpmtsSolve, NULL);
400 PyEval_RestoreThread(cbInfo._save);
403 list = PyList_New(0);
404 rpmpsi psi = rpmpsInitIterator(ps);
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 int needsFlags, sense;
414 p = rpmpsGetProblem(psi);
416 /* XXX autorelocated i386 on ia64, fix system-config-packages! */
417 if (rpmProblemGetType(p) == RPMPROB_BADRELOCATE)
420 byName = strdup(rpmProblemGetPkgNEVR(p));
421 if ((byArch= strrchr(byName, '.')) != NULL)
423 if ((byRelease = strrchr(byName, '-')) != NULL)
425 if ((byVersion = strrchr(byName, '-')) != NULL)
428 key = rpmProblemGetKey(p);
430 needsName = rpmProblemGetAltNEVR(p);
431 if (needsName[1] == ' ') {
432 sense = (needsName[0] == 'C')
433 ? RPMDEP_SENSE_CONFLICTS : RPMDEP_SENSE_REQUIRES;
436 sense = RPMDEP_SENSE_REQUIRES;
437 if ((needsVersion = strrchr(needsName, ' ')) != NULL)
438 *needsVersion++ = '\0';
441 if ((needsOP = strrchr(needsName, ' ')) != NULL) {
442 for (*needsOP++ = '\0'; *needsOP != '\0'; needsOP++) {
443 if (*needsOP == '<') needsFlags |= RPMSENSE_LESS;
444 else if (*needsOP == '>') needsFlags |= RPMSENSE_GREATER;
445 else if (*needsOP == '=') needsFlags |= RPMSENSE_EQUAL;
449 cf = Py_BuildValue("((sss)(ss)iOi)", byName, byVersion, byRelease,
450 needsName, needsVersion, needsFlags,
451 (key != NULL ? key : Py_None),
453 PyList_Append(list, (PyObject *) cf);
458 psi = rpmpsFreeIterator(psi);
471 rpmts_Order(rpmtsObject * s)
476 fprintf(stderr, "*** rpmts_Order(%p) ts %p\n", s, s->ts);
478 Py_BEGIN_ALLOW_THREADS
479 rc = rpmtsOrder(s->ts);
482 return Py_BuildValue("i", rc);
488 rpmts_Clean(rpmtsObject * s)
491 fprintf(stderr, "*** rpmts_Clean(%p) ts %p\n", s, s->ts);
502 rpmts_IDTXload(rpmtsObject * s)
504 PyObject * result = NULL;
505 rpmTag tag = RPMTAG_INSTALLTID;
509 fprintf(stderr, "*** rpmts_IDTXload(%p) ts %p\n", s, s->ts);
511 Py_BEGIN_ALLOW_THREADS
512 idtx = IDTXload(s->ts, tag);
515 if (idtx == NULL || idtx->nidt <= 0) {
524 result = PyTuple_New(idtx->nidt);
525 for (i = 0; i < idtx->nidt; i++) {
527 ho = (PyObject *) hdr_Wrap(idt->h);
528 tuple = Py_BuildValue("(iOi)", idt->val.u32, ho, idt->instance);
529 PyTuple_SET_ITEM(result, i, tuple);
534 idtx = IDTXfree(idtx);
542 rpmts_IDTXglob(rpmtsObject * s)
544 PyObject * result = NULL;
545 rpmTag tag = RPMTAG_REMOVETID;
546 const char * globstr;
550 fprintf(stderr, "*** rpmts_IDTXglob(%p) ts %p\n", s, s->ts);
552 Py_BEGIN_ALLOW_THREADS
553 globstr = rpmExpand("%{_repackage_dir}/*.rpm", NULL);
554 idtx = IDTXglob(s->ts, globstr, tag);
555 globstr = _free(globstr);
558 if (idtx == NULL || idtx->nidt <= 0) {
567 result = PyTuple_New(idtx->nidt);
568 for (i = 0; i < idtx->nidt; i++) {
570 ho = (PyObject *) hdr_Wrap(idt->h);
571 tuple = Py_BuildValue("(iOs)", idt->val.u32, ho, idt->key);
572 PyTuple_SET_ITEM(result, i, tuple);
577 idtx = IDTXfree(idtx);
585 rpmts_Rollback(rpmtsObject * s, PyObject * args, PyObject * kwds)
587 struct rpmInstallArguments_s * ia = alloca(sizeof(*ia));
588 rpmtransFlags transFlags;
589 const char ** av = NULL;
592 char * kwlist[] = {"transactionId", NULL};
595 fprintf(stderr, "*** rpmts_Rollback(%p) ts %p\n", s, s->ts);
597 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:Rollback", kwlist, &rbtid))
600 Py_BEGIN_ALLOW_THREADS
601 memset(ia, 0, sizeof(*ia));
602 ia->qva_flags = (VERIFY_DIGEST|VERIFY_SIGNATURE|VERIFY_HDRCHK);
603 ia->transFlags |= (INSTALL_UPGRADE|INSTALL_FRESHEN|INSTALL_INSTALL);
604 ia->transFlags |= RPMTRANS_FLAG_NOMD5;
605 ia->installInterfaceFlags = (INSTALL_UPGRADE|INSTALL_FRESHEN|INSTALL_INSTALL);
607 ia->relocations = NULL;
608 ia->probFilter |= RPMPROB_FILTER_OLDPACKAGE;
610 transFlags = rpmtsSetFlags(s->ts, ia->transFlags);
611 rc = rpmRollback(s->ts, ia, av);
612 transFlags = rpmtsSetFlags(s->ts, transFlags);
615 return Py_BuildValue("i", rc);
621 rpmts_OpenDB(rpmtsObject * s)
626 fprintf(stderr, "*** rpmts_OpenDB(%p) ts %p\n", s, s->ts);
628 dbmode = rpmtsGetDBMode(s->ts);
632 return Py_BuildValue("i", rpmtsOpenDB(s->ts, dbmode));
638 rpmts_CloseDB(rpmtsObject * s)
643 fprintf(stderr, "*** rpmts_CloseDB(%p) ts %p\n", s, s->ts);
645 rc = rpmtsCloseDB(s->ts);
646 rpmtsSetDBMode(s->ts, -1); /* XXX disable lazy opens */
648 return Py_BuildValue("i", rc);
654 rpmts_InitDB(rpmtsObject * s)
659 fprintf(stderr, "*** rpmts_InitDB(%p) ts %p\n", s, s->ts);
661 rc = rpmtsInitDB(s->ts, O_RDONLY);
663 rc = rpmtsCloseDB(s->ts);
665 return Py_BuildValue("i", rc);
671 rpmts_RebuildDB(rpmtsObject * s)
676 fprintf(stderr, "*** rpmts_RebuildDB(%p) ts %p\n", s, s->ts);
678 Py_BEGIN_ALLOW_THREADS
679 rc = rpmtsRebuildDB(s->ts);
682 return Py_BuildValue("i", rc);
688 rpmts_VerifyDB(rpmtsObject * s)
693 fprintf(stderr, "*** rpmts_VerifyDB(%p) ts %p\n", s, s->ts);
695 Py_BEGIN_ALLOW_THREADS
696 rc = rpmtsVerifyDB(s->ts);
699 return Py_BuildValue("i", rc);
705 rpmts_HdrFromFdno(rpmtsObject * s, PyObject * args, PyObject * kwds)
707 PyObject * result = NULL;
712 char * kwlist[] = {"fd", NULL};
714 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:HdrFromFdno", kwlist,
719 rpmrc = rpmReadPackageFile(s->ts, fd, "rpmts_HdrFromFdno", &h);
723 fprintf(stderr, "*** rpmts_HdrFromFdno(%p) ts %p rc %d\n", s, s->ts, rpmrc);
728 result = Py_BuildValue("N", hdr_Wrap(h));
729 h = headerFree(h); /* XXX ref held by result */
733 PyErr_SetString(pyrpmError, "public key not available");
736 case RPMRC_NOTTRUSTED:
737 PyErr_SetString(pyrpmError, "public key not trusted");
743 PyErr_SetString(pyrpmError, "error reading package header");
753 rpmts_HdrCheck(rpmtsObject * s, PyObject * args, PyObject * kwds)
756 PyObject * result = NULL;
757 const char * msg = NULL;
761 char * kwlist[] = {"headers", NULL};
764 fprintf(stderr, "*** rpmts_HdrCheck(%p) ts %p\n", s, s->ts);
766 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:HdrCheck", kwlist, &blob))
769 if (blob == Py_None) {
773 if (!PyString_Check(blob)) {
774 PyErr_SetString(pyrpmError, "hdrCheck takes a string of octets");
777 uh = PyString_AsString(blob);
778 uc = PyString_Size(blob);
780 rpmrc = headerCheck(s->ts, uh, uc, &msg);
789 PyErr_SetString(pyrpmError, "public key not availaiable");
792 case RPMRC_NOTTRUSTED:
793 PyErr_SetString(pyrpmError, "public key not trusted");
798 PyErr_SetString(pyrpmError, msg);
809 rpmts_SetVSFlags(rpmtsObject * s, PyObject * args, PyObject * kwds)
812 char * kwlist[] = {"flags", NULL};
815 fprintf(stderr, "*** rpmts_SetVSFlags(%p) ts %p\n", s, s->ts);
817 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:SetVSFlags", kwlist,
821 /* XXX FIXME: value check on vsflags, or build pure python object
822 * for it, and require an object of that type */
824 return Py_BuildValue("i", rpmtsSetVSFlags(s->ts, vsflags));
830 rpmts_SetColor(rpmtsObject * s, PyObject * args, PyObject * kwds)
833 char * kwlist[] = {"color", NULL};
836 fprintf(stderr, "*** rpmts_SetColor(%p) ts %p\n", s, s->ts);
838 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:Color", kwlist, &tscolor))
841 /* XXX FIXME: value check on tscolor, or build pure python object
842 * for it, and require an object of that type */
844 return Py_BuildValue("i", rpmtsSetColor(s->ts, tscolor));
850 rpmts_PgpPrtPkts(rpmtsObject * s, PyObject * args, PyObject * kwds)
856 char * kwlist[] = {"octets", NULL};
859 fprintf(stderr, "*** rpmts_PgpPrtPkts(%p) ts %p\n", s, s->ts);
861 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:PgpPrtPkts", kwlist, &blob))
864 if (blob == Py_None) {
868 if (!PyString_Check(blob)) {
869 PyErr_SetString(pyrpmError, "pgpPrtPkts takes a string of octets");
872 pkt = (unsigned char *)PyString_AsString(blob);
873 pktlen = PyString_Size(blob);
875 rc = pgpPrtPkts(pkt, pktlen, NULL, 1);
877 return Py_BuildValue("i", rc);
883 rpmts_PgpImportPubkey(rpmtsObject * s, PyObject * args, PyObject * kwds)
889 char * kwlist[] = {"pubkey", NULL};
892 fprintf(stderr, "*** rpmts_PgpImportPubkey(%p) ts %p\n", s, s->ts);
894 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:PgpImportPubkey",
898 if (blob == Py_None) {
902 if (!PyString_Check(blob)) {
903 PyErr_SetString(pyrpmError, "PgpImportPubkey takes a string of octets");
906 pkt = (unsigned char *)PyString_AsString(blob);
907 pktlen = PyString_Size(blob);
909 rc = rpmtsImportPubkey(s->ts, pkt, pktlen);
911 return Py_BuildValue("i", rc);
917 rpmts_GetKeys(rpmtsObject * s)
919 const void **data = NULL;
924 fprintf(stderr, "*** rpmts_GetKeys(%p) ts %p\n", s, s->ts);
926 rpmtsGetKeys(s->ts, &data, &num);
927 if (data == NULL || num <= 0) {
933 tuple = PyTuple_New(num);
935 for (i = 0; i < num; i++) {
937 obj = (data[i] ? (PyObject *) data[i] : Py_None);
939 PyTuple_SetItem(tuple, i, obj);
950 rpmtsCallback(const void * hd, const rpmCallbackType what,
951 const unsigned long amount, const unsigned long total,
952 const void * pkgKey, rpmCallbackData data)
954 Header h = (Header) hd;
955 struct rpmtsCallbackType_s * cbInfo = data;
956 PyObject * pkgObj = (PyObject *) pkgKey;
957 PyObject * args, * result;
960 if (cbInfo->pythonError) return NULL;
961 if (cbInfo->cb == Py_None) return NULL;
963 /* Synthesize a python object for callback (if necessary). */
964 if (pkgObj == NULL) {
966 const char * n = NULL;
967 (void) headerNVR(h, &n, NULL, NULL);
968 pkgObj = Py_BuildValue("s", n);
976 PyEval_RestoreThread(cbInfo->_save);
978 args = Py_BuildValue("(illOO)", what, amount, total, pkgObj, cbInfo->data);
979 result = PyEval_CallObject(cbInfo->cb, args);
984 cbInfo->pythonError = 1;
985 cbInfo->_save = PyEval_SaveThread();
989 if (what == RPMCALLBACK_INST_OPEN_FILE) {
992 if (!PyArg_Parse(result, "i", &fdno)) {
993 cbInfo->pythonError = 1;
994 cbInfo->_save = PyEval_SaveThread();
998 cbInfo->_save = PyEval_SaveThread();
1002 fprintf(stderr, "\t%p = fdDup(%d)\n", fd, fdno);
1004 fcntl(Fileno(fd), F_SETFD, FD_CLOEXEC);
1008 if (what == RPMCALLBACK_INST_CLOSE_FILE) {
1010 fprintf(stderr, "\tFclose(%p)\n", fd);
1014 fprintf(stderr, "\t%ld:%ld key %p\n", amount, total, pkgKey);
1018 cbInfo->_save = PyEval_SaveThread();
1026 rpmts_SetFlags(rpmtsObject * s, PyObject * args, PyObject * kwds)
1028 rpmtransFlags transFlags = 0;
1029 char * kwlist[] = {"flags", NULL};
1031 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:SetFlags", kwlist,
1036 fprintf(stderr, "*** rpmts_SetFlags(%p) ts %p transFlags %x\n", s, s->ts, transFlags);
1038 /* XXX FIXME: value check on flags, or build pure python object
1039 * for it, and require an object of that type */
1041 return Py_BuildValue("i", rpmtsSetFlags(s->ts, transFlags));
1047 rpmts_SetProbFilter(rpmtsObject * s, PyObject * args, PyObject * kwds)
1049 rpmprobFilterFlags ignoreSet = 0;
1050 rpmprobFilterFlags oignoreSet;
1051 char * kwlist[] = {"ignoreSet", NULL};
1053 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:ProbFilter", kwlist,
1058 fprintf(stderr, "*** rpmts_SetProbFilter(%p) ts %p ignoreSet %x\n", s, s->ts, ignoreSet);
1060 oignoreSet = s->ignoreSet;
1061 s->ignoreSet = ignoreSet;
1063 return Py_BuildValue("i", oignoreSet);
1068 static rpmpsObject *
1069 rpmts_Problems(rpmtsObject * s)
1073 fprintf(stderr, "*** rpmts_Problems(%p) ts %p\n", s, s->ts);
1075 return rpmps_Wrap( rpmtsProblems(s->ts) );
1081 rpmts_Run(rpmtsObject * s, PyObject * args, PyObject * kwds)
1087 struct rpmtsCallbackType_s cbInfo;
1088 char * kwlist[] = {"callback", "data", NULL};
1090 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO:Run", kwlist,
1091 &cbInfo.cb, &cbInfo.data))
1095 cbInfo.pythonError = 0;
1096 cbInfo._save = PyEval_SaveThread();
1098 if (cbInfo.cb != NULL) {
1099 if (!PyCallable_Check(cbInfo.cb)) {
1100 PyErr_SetString(PyExc_TypeError, "expected a callable");
1103 (void) rpmtsSetNotifyCallback(s->ts, rpmtsCallback, (void *) &cbInfo);
1107 fprintf(stderr, "*** rpmts_Run(%p) ts %p ignore %x\n", s, s->ts, s->ignoreSet);
1109 rc = rpmtsRun(s->ts, NULL, s->ignoreSet);
1110 ps = rpmtsProblems(s->ts);
1113 (void) rpmtsSetNotifyCallback(s->ts, NULL, NULL);
1115 PyEval_RestoreThread(cbInfo._save);
1117 if (cbInfo.pythonError) {
1123 list = PyList_New(0);
1130 list = PyList_New(0);
1131 psi = rpmpsInitIterator(ps);
1132 while (rpmpsNextIterator(psi) >= 0) {
1133 rpmProblem p = rpmpsGetProblem(psi);
1134 PyObject * prob = Py_BuildValue("s(isN)", rpmProblemString(p),
1135 rpmProblemGetType(p),
1136 rpmProblemGetStr(p),
1137 PyLong_FromLongLong(rpmProblemGetLong(p)));
1138 PyList_Append(list, prob);
1142 psi = rpmpsFreeIterator(psi);
1148 #if Py_TPFLAGS_HAVE_ITER
1150 rpmts_iter(rpmtsObject * s)
1153 fprintf(stderr, "*** rpmts_iter(%p) ts %p\n", s, s->ts);
1156 return (PyObject *)s;
1161 * @todo Add TR_ADDED filter to iterator.
1164 rpmts_iternext(rpmtsObject * s)
1166 PyObject * result = NULL;
1170 fprintf(stderr, "*** rpmts_iternext(%p) ts %p tsi %p %d\n", s, s->ts, s->tsi, s->tsiFilter);
1172 /* Reset iterator on 1st entry. */
1173 if (s->tsi == NULL) {
1174 s->tsi = rpmtsiInit(s->ts);
1180 te = rpmtsiNext(s->tsi, s->tsiFilter);
1182 result = (PyObject *) rpmte_Wrap(te);
1184 s->tsi = rpmtsiFree(s->tsi);
1192 * @todo Add TR_ADDED filter to iterator.
1195 rpmts_Next(rpmtsObject * s)
1200 fprintf(stderr, "*** rpmts_Next(%p) ts %p\n", s, s->ts);
1202 result = rpmts_iternext(s);
1204 if (result == NULL) {
1215 spec_Parse(rpmtsObject * s, PyObject * args, PyObject * kwds)
1217 const char * specfile;
1219 char * buildRoot = NULL;
1221 char * passPhrase = "";
1222 char *cookie = NULL;
1225 char * kwlist[] = {"specfile", NULL};
1227 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s:Parse", kwlist, &specfile))
1230 if (parseSpec(s->ts, specfile,"/", buildRoot,recursing, passPhrase,
1231 cookie, anyarch, force)!=0) {
1232 PyErr_SetString(pyrpmError, "can't parse specfile\n");
1236 spec = rpmtsSpec(s->ts);
1237 return spec_Wrap(spec);
1242 static rpmmiObject *
1243 rpmts_Match(rpmtsObject * s, PyObject * args, PyObject * kwds)
1245 PyObject *TagN = NULL;
1246 PyObject *Key = NULL;
1248 /* XXX lkey *must* be a 32 bit integer, int "works" on all known platforms. */
1251 int tag = RPMDBI_PACKAGES;
1252 char * kwlist[] = {"tagNumber", "key", NULL};
1255 fprintf(stderr, "*** rpmts_Match(%p) ts %p\n", s, s->ts);
1257 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:Match", kwlist,
1261 if (TagN && (tag = tagNumFromPyObject (TagN)) == -1) {
1262 PyErr_SetString(PyExc_TypeError, "unknown tag type");
1267 if (PyString_Check(Key) || PyUnicode_Check(Key)) {
1268 key = PyString_AsString(Key);
1269 len = PyString_Size(Key);
1270 } else if (PyInt_Check(Key)) {
1271 lkey = PyInt_AsLong(Key);
1272 key = (char *)&lkey;
1275 PyErr_SetString(PyExc_TypeError, "unknown key type");
1280 /* XXX If not already opened, open the database O_RDONLY now. */
1281 /* XXX FIXME: lazy default rdonly open also done by rpmtsInitIterator(). */
1282 if (rpmtsGetRdb(s->ts) == NULL) {
1283 int rc = rpmtsOpenDB(s->ts, O_RDONLY);
1284 if (rc || rpmtsGetRdb(s->ts) == NULL) {
1285 PyErr_SetString(PyExc_TypeError, "rpmdb open failed");
1290 return rpmmi_Wrap( rpmtsInitIterator(s->ts, tag, key, len), (PyObject*)s);
1295 static struct PyMethodDef rpmts_methods[] = {
1296 {"Debug", (PyCFunction)rpmts_Debug, METH_VARARGS|METH_KEYWORDS,
1299 {"addInstall", (PyCFunction) rpmts_AddInstall, METH_VARARGS|METH_KEYWORDS,
1301 {"addErase", (PyCFunction) rpmts_AddErase, METH_VARARGS|METH_KEYWORDS,
1303 {"check", (PyCFunction) rpmts_Check, METH_VARARGS|METH_KEYWORDS,
1305 {"order", (PyCFunction) rpmts_Order, METH_NOARGS,
1307 {"setFlags", (PyCFunction) rpmts_SetFlags, METH_VARARGS|METH_KEYWORDS,
1308 "ts.setFlags(transFlags) -> previous transFlags\n\
1309 - Set control bit(s) for executing ts.run().\n\
1310 Note: This method replaces the 1st argument to the old ts.run()\n" },
1311 {"setProbFilter", (PyCFunction) rpmts_SetProbFilter, METH_VARARGS|METH_KEYWORDS,
1312 "ts.setProbFilter(ignoreSet) -> previous ignoreSet\n\
1313 - Set control bit(s) for ignoring problems found by ts.run().\n\
1314 Note: This method replaces the 2nd argument to the old ts.run()\n" },
1315 {"problems", (PyCFunction) rpmts_Problems, METH_NOARGS,
1316 "ts.problems() -> ps\n\
1317 - Return current problem set.\n" },
1318 {"run", (PyCFunction) rpmts_Run, METH_VARARGS|METH_KEYWORDS,
1319 "ts.run(callback, data) -> (problems)\n\
1320 - Run a transaction set, returning list of problems found.\n\
1321 Note: The callback may not be None.\n" },
1322 {"clean", (PyCFunction) rpmts_Clean, METH_NOARGS,
1324 {"IDTXload", (PyCFunction) rpmts_IDTXload, METH_NOARGS,
1325 "ts.IDTXload() -> ((tid,hdr,instance)+)\n\
1326 - Return list of installed packages reverse sorted by transaction id.\n" },
1327 {"IDTXglob", (PyCFunction) rpmts_IDTXglob, METH_NOARGS,
1328 "ts.IDTXglob() -> ((tid,hdr,instance)+)\n\
1329 - Return list of removed packages reverse sorted by transaction id.\n" },
1330 {"rollback", (PyCFunction) rpmts_Rollback, METH_VARARGS|METH_KEYWORDS,
1332 {"openDB", (PyCFunction) rpmts_OpenDB, METH_NOARGS,
1333 "ts.openDB() -> None\n\
1334 - Open the default transaction rpmdb.\n\
1335 Note: The transaction rpmdb is lazily opened, so ts.openDB() is seldom needed.\n" },
1336 {"closeDB", (PyCFunction) rpmts_CloseDB, METH_NOARGS,
1337 "ts.closeDB() -> None\n\
1338 - Close the default transaction rpmdb.\n\
1339 Note: ts.closeDB() disables lazy opens, and should hardly ever be used.\n" },
1340 {"initDB", (PyCFunction) rpmts_InitDB, METH_NOARGS,
1341 "ts.initDB() -> None\n\
1342 - Initialize the default transaction rpmdb.\n\
1343 Note: ts.initDB() is seldom needed anymore.\n" },
1344 {"rebuildDB", (PyCFunction) rpmts_RebuildDB, METH_NOARGS,
1345 "ts.rebuildDB() -> None\n\
1346 - Rebuild the default transaction rpmdb.\n" },
1347 {"verifyDB", (PyCFunction) rpmts_VerifyDB, METH_NOARGS,
1348 "ts.verifyDB() -> None\n\
1349 - Verify the default transaction rpmdb.\n" },
1350 {"hdrFromFdno",(PyCFunction) rpmts_HdrFromFdno,METH_VARARGS|METH_KEYWORDS,
1351 "ts.hdrFromFdno(fdno) -> hdr\n\
1352 - Read a package header from a file descriptor.\n" },
1353 {"hdrCheck", (PyCFunction) rpmts_HdrCheck, METH_VARARGS|METH_KEYWORDS,
1355 {"setVSFlags",(PyCFunction) rpmts_SetVSFlags, METH_VARARGS|METH_KEYWORDS,
1356 "ts.setVSFlags(vsflags) -> ovsflags\n\
1357 - Set signature verification flags. Values for vsflags are:\n\
1358 rpm.RPMVSF_NOHDRCHK if set, don't check rpmdb headers\n\
1359 rpm.RPMVSF_NEEDPAYLOAD if not set, check header+payload (if possible)\n\
1360 rpm.RPMVSF_NOSHA1HEADER if set, don't check header SHA1 digest\n\
1361 rpm.RPMVSF_NODSAHEADER if set, don't check header DSA signature\n\
1362 rpm.RPMVSF_NOMD5 if set, don't check header+payload MD5 digest\n\
1363 rpm.RPMVSF_NODSA if set, don't check header+payload DSA signature\n\
1364 rpm.RPMVSF_NORSA if set, don't check header+payload RSA signature\n\
1365 rpm._RPMVSF_NODIGESTS if set, don't check digest(s)\n\
1366 rpm._RPMVSF_NOSIGNATURES if set, don't check signature(s)\n" },
1367 {"setColor",(PyCFunction) rpmts_SetColor, METH_VARARGS|METH_KEYWORDS,
1369 {"pgpPrtPkts", (PyCFunction) rpmts_PgpPrtPkts, METH_VARARGS|METH_KEYWORDS,
1371 {"pgpImportPubkey", (PyCFunction) rpmts_PgpImportPubkey, METH_VARARGS|METH_KEYWORDS,
1373 {"getKeys", (PyCFunction) rpmts_GetKeys, METH_NOARGS,
1375 {"parseSpec", (PyCFunction) spec_Parse, METH_VARARGS|METH_KEYWORDS,
1376 "ts.parseSpec(\"/path/to/foo.spec\") -> spec\n\
1377 - Parse a spec file.\n" },
1378 {"dbMatch", (PyCFunction) rpmts_Match, METH_VARARGS|METH_KEYWORDS,
1379 "ts.dbMatch([TagN, [key, [len]]]) -> mi\n\
1380 - Create a match iterator for the default transaction rpmdb.\n" },
1381 {"next", (PyCFunction)rpmts_Next, METH_NOARGS,
1383 - Retrieve next transaction set element.\n" },
1384 {NULL, NULL} /* sentinel */
1389 static void rpmts_dealloc(rpmtsObject * s)
1393 fprintf(stderr, "%p -- ts %p db %p\n", s, s->ts, rpmtsGetRdb(s->ts));
1394 s->ts = rpmtsFree(s->ts);
1396 if (s->scriptFd) Fclose(s->scriptFd);
1397 /* this will free the keyList, and decrement the ref count of all
1398 the items on the list as well :-) */
1399 Py_DECREF(s->keyList);
1400 PyObject_Del((PyObject *)s);
1403 static PyObject * rpmts_getattro(PyObject * o, PyObject * n)
1405 return PyObject_GenericGetAttr(o, n);
1410 static int rpmts_setattro(PyObject * o, PyObject * n, PyObject * v)
1412 rpmtsObject *s = (rpmtsObject *)o;
1413 char * name = PyString_AsString(n);
1416 if (!strcmp(name, "scriptFd")) {
1417 if (!PyArg_Parse(v, "i", &fdno)) return 0;
1419 PyErr_SetString(PyExc_TypeError, "bad file descriptor");
1422 s->scriptFd = fdDup(fdno);
1423 rpmtsSetScriptFd(s->ts, s->scriptFd);
1426 PyErr_SetString(PyExc_AttributeError, name);
1435 static int rpmts_init(rpmtsObject * s, PyObject *args, PyObject *kwds)
1437 /* nothing to do atm... */
1443 static void rpmts_free(rpmtsObject * s)
1446 fprintf(stderr, "%p -- ts %p db %p\n", s, s->ts, rpmtsGetRdb(s->ts));
1447 s->ts = rpmtsFree(s->ts);
1450 Fclose(s->scriptFd);
1452 /* this will free the keyList, and decrement the ref count of all
1453 the items on the list as well :-) */
1454 Py_DECREF(s->keyList);
1456 PyObject_Del((PyObject *)s);
1461 static PyObject * rpmts_alloc(PyTypeObject * subtype, int nitems)
1463 PyObject * s = PyType_GenericAlloc(subtype, nitems);
1465 if (_rpmts_debug < 0)
1466 fprintf(stderr, "*** rpmts_alloc(%p,%d) ret %p\n", subtype, nitems, s);
1472 static PyObject * rpmts_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds)
1474 rpmtsObject * s = (void *) PyObject_New(rpmtsObject, subtype);
1476 char * rootDir = "/";
1477 int vsflags = rpmExpandNumeric("%{?__vsflags}");
1478 char * kwlist[] = {"rootdir", "vsflags", 0};
1480 if (_rpmts_debug < 0)
1481 fprintf(stderr, "*** rpmts_new(%p,%p,%p)\n", s, args, kwds);
1483 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|si:rpmts_init", kwlist,
1484 &rootDir, &vsflags))
1487 s->ts = rpmtsCreate();
1488 /* XXX: Why is there no rpmts_SetRootDir() ? */
1489 (void) rpmtsSetRootDir(s->ts, rootDir);
1490 /* XXX: make this use common code with rpmts_SetVSFlags() to check the
1492 (void) rpmtsSetVSFlags(s->ts, vsflags);
1493 s->keyList = PyList_New(0);
1499 fprintf(stderr, "%p ++ ts %p db %p\n", s, s->ts, rpmtsGetRdb(s->ts));
1501 return (PyObject *)s;
1506 static char rpmts_doc[] =
1511 PyTypeObject rpmts_Type = {
1512 PyObject_HEAD_INIT(&PyType_Type)
1514 "rpm.ts", /* tp_name */
1515 sizeof(rpmtsObject), /* tp_size */
1516 0, /* tp_itemsize */
1517 (destructor) rpmts_dealloc, /* tp_dealloc */
1519 (getattrfunc)0, /* tp_getattr */
1520 (setattrfunc)0, /* tp_setattr */
1523 0, /* tp_as_number */
1524 0, /* tp_as_sequence */
1525 0, /* tp_as_mapping */
1529 (getattrofunc) rpmts_getattro, /* tp_getattro */
1530 (setattrofunc) rpmts_setattro, /* tp_setattro */
1531 0, /* tp_as_buffer */
1532 Py_TPFLAGS_DEFAULT, /* tp_flags */
1533 rpmts_doc, /* tp_doc */
1534 #if Py_TPFLAGS_HAVE_ITER
1535 0, /* tp_traverse */
1537 0, /* tp_richcompare */
1538 0, /* tp_weaklistoffset */
1539 (getiterfunc) rpmts_iter, /* tp_iter */
1540 (iternextfunc) rpmts_iternext, /* tp_iternext */
1541 rpmts_methods, /* tp_methods */
1546 0, /* tp_descr_get */
1547 0, /* tp_descr_set */
1548 0, /* tp_dictoffset */
1549 (initproc) rpmts_init, /* tp_init */
1550 (allocfunc) rpmts_alloc, /* tp_alloc */
1551 (newfunc) rpmts_new, /* tp_new */
1552 (freefunc) rpmts_free, /* tp_free */
1560 rpmts_Create(PyObject * self, PyObject * args, PyObject * kwds)
1562 return PyObject_Call((PyObject *) &rpmts_Type, args, kwds);