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>
14 #include "header-py.h"
15 #include "rpmds-py.h" /* XXX for rpmdsNew */
16 #include "rpmfi-py.h" /* XXX for rpmfiNew */
26 extern int _rpmts_debug;
32 * \brief A python rpm.ts object represents an RPM transaction set.
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.
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:
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
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).
54 * A rpm.ts object has the following methods:
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
66 * - addErase(name) Add an erase element to a transaction set.
67 * @param name the package name to be erased
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),
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.
105 * - ts.order() Do a topological sort of added element relations.
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
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
124 * - rpm.RPMTRANS_FLAG_KEEPOBSOLETE - do not remove obsoleted
126 * @return previous transFlags
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
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.
149 struct rpmtsCallbackType_s {
154 PyThreadState *_save;
160 rpmts_Debug(rpmtsObject * s, PyObject * args, PyObject * kwds)
162 char * kwlist[] = {"debugLevel", NULL};
164 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:Debug", kwlist,
168 if (_rpmts_debug < 0)
169 fprintf(stderr, "*** rpmts_Debug(%p) ts %p\n", s, s->ts);
177 * Add package to universe of possible packages to install in transaction set.
178 * @param ts transaction set
180 * @param key package private data
182 static void rpmtsAddAvailableElement(rpmts ts, Header h,
186 rpmds provides = rpmdsNew(h, RPMTAG_PROVIDENAME, scareMem);
187 rpmfi fi = rpmfiNew(ts, h, RPMTAG_BASENAMES, scareMem);
189 /* XXX FIXME: return code RPMAL_NOMATCH is error */
190 (void) rpmalAdd(&ts->availablePackages, RPMAL_NOMATCH, key,
191 provides, fi, rpmtsColor(ts));
193 provides = rpmdsFree(provides);
195 if (_rpmts_debug < 0)
196 fprintf(stderr, "\tAddAvailable(%p) list %p\n", ts, ts->availablePackages);
204 rpmts_AddInstall(rpmtsObject * s, PyObject * args, PyObject * kwds)
208 char * how = "u"; /* XXX default to upgrade element if missing */
210 char * kwlist[] = {"header", "key", "how", NULL};
213 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O|s:AddInstall", kwlist,
214 &hdr_Type, &h, &key, &how))
217 { PyObject * hObj = (PyObject *) h;
218 if (hObj->ob_type != &hdr_Type) {
219 PyErr_SetString(PyExc_TypeError, "bad type for header argument");
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);
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\"");
230 } else if (how && !strcmp(how, "u"))
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.
238 if (how && !strcmp(how, "a")) {
240 rpmtsAddAvailableElement(s->ts, hdrGetHeader(h), key);
242 PyErr_SetString(pyrpmError, "available package mechanism currently broken");
246 rc = rpmtsAddInstallElement(s->ts, hdrGetHeader(h), key, isUpgrade, NULL);
248 PyErr_SetString(pyrpmError, "adding package to transaction failed");
253 /* This should increment the usage count for me */
255 PyList_Append(s->keyList, key);
262 * @todo Permit finer control (i.e. not just --allmatches) of deleted elments.
265 rpmts_AddErase(rpmtsObject * s, PyObject * args, PyObject * kwds)
269 rpmdbMatchIterator mi;
270 char * kwlist[] = {"name", NULL};
273 fprintf(stderr, "*** rpmts_AddErase(%p) ts %p\n", s, s->ts);
275 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:AddErase", kwlist, &o))
278 if (PyString_Check(o)) {
279 char * name = PyString_AsString(o);
281 mi = rpmtsInitIterator(s->ts, RPMDBI_LABEL, name, 0);
282 count = rpmdbGetIteratorCount(mi);
284 mi = rpmdbFreeIterator(mi);
285 PyErr_SetString(pyrpmError, "package not installed");
287 } else { /* XXX: Note that we automatically choose to remove all matches */
289 while ((h = rpmdbNextIterator(mi)) != NULL) {
290 unsigned int recOffset = rpmdbGetIteratorOffset(mi);
292 rpmtsAddEraseElement(s->ts, h, recOffset);
295 mi = rpmdbFreeIterator(mi);
297 if (PyInt_Check(o)) {
298 uint32_t instance = PyInt_AsLong(o);
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");
307 while ((h = rpmdbNextIterator(mi)) != NULL) {
308 uint32_t recOffset = rpmdbGetIteratorOffset(mi);
310 rpmtsAddEraseElement(s->ts, h, recOffset);
314 mi = rpmdbFreeIterator(mi);
324 rpmts_SolveCallback(rpmts ts, rpmds ds, const void * data)
326 struct rpmtsCallbackType_s * cbInfo = (struct rpmtsCallbackType_s *) data;
327 PyObject * args, * result;
331 fprintf(stderr, "*** rpmts_SolveCallback(%p,%p,%p) \"%s\"\n", ts, ds, data, rpmdsDNEVR(ds));
333 if (cbInfo->tso == NULL) return res;
334 if (cbInfo->pythonError) return res;
335 if (cbInfo->cb == Py_None) return res;
337 PyEval_RestoreThread(cbInfo->_save);
339 args = Py_BuildValue("(Oissi)", cbInfo->tso,
340 rpmdsTagN(ds), rpmdsN(ds), rpmdsEVR(ds), rpmdsFlags(ds));
341 result = PyEval_CallObject(cbInfo->cb, args);
345 cbInfo->pythonError = 1;
347 if (PyInt_Check(result))
348 res = PyInt_AsLong(result);
352 cbInfo->_save = PyEval_SaveThread();
360 rpmts_Check(rpmtsObject * s, PyObject * args, PyObject * kwds)
364 PyObject * list, * cf;
365 struct rpmtsCallbackType_s cbInfo;
368 char * kwlist[] = {"callback", NULL};
370 memset(&cbInfo, 0, sizeof(cbInfo));
371 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:Check", kwlist,
375 if (cbInfo.cb != NULL) {
376 if (!PyCallable_Check(cbInfo.cb)) {
377 PyErr_SetString(PyExc_TypeError, "expected a callable");
380 xx = rpmtsSetSolveCallback(s->ts, rpmts_SolveCallback, (void *)&cbInfo);
384 fprintf(stderr, "*** rpmts_Check(%p) ts %p cb %p\n", s, s->ts, cbInfo.cb);
387 cbInfo.pythonError = 0;
388 cbInfo._save = PyEval_SaveThread();
391 /* XXX resurrect availablePackages one more time ... */
392 rpmalMakeIndex(s->ts->availablePackages);
395 xx = rpmtsCheck(s->ts);
396 ps = rpmtsProblems(s->ts);
399 xx = rpmtsSetSolveCallback(s->ts, rpmtsSolve, NULL);
401 PyEval_RestoreThread(cbInfo._save);
404 list = PyList_New(0);
405 rpmpsi psi = rpmpsInitIterator(ps);
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;
415 p = rpmpsGetProblem(psi);
417 byName = strdup(rpmProblemGetPkgNEVR(p));
418 if ((byArch= strrchr(byName, '.')) != NULL)
420 if ((byRelease = strrchr(byName, '-')) != NULL)
422 if ((byVersion = strrchr(byName, '-')) != NULL)
425 key = rpmProblemGetKey(p);
427 needsName = rpmProblemGetAltNEVR(p);
428 if (needsName[1] == ' ') {
429 sense = (needsName[0] == 'C')
430 ? RPMDEP_SENSE_CONFLICTS : RPMDEP_SENSE_REQUIRES;
433 sense = RPMDEP_SENSE_REQUIRES;
434 if ((needsVersion = strrchr(needsName, ' ')) != NULL)
435 *needsVersion++ = '\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;
446 cf = Py_BuildValue("((sss)(ss)iOi)", byName, byVersion, byRelease,
447 needsName, needsVersion, needsFlags,
448 (key != NULL ? key : Py_None),
450 PyList_Append(list, (PyObject *) cf);
455 psi = rpmpsFreeIterator(psi);
468 rpmts_Order(rpmtsObject * s)
473 fprintf(stderr, "*** rpmts_Order(%p) ts %p\n", s, s->ts);
475 Py_BEGIN_ALLOW_THREADS
476 rc = rpmtsOrder(s->ts);
479 return Py_BuildValue("i", rc);
485 rpmts_Clean(rpmtsObject * s)
488 fprintf(stderr, "*** rpmts_Clean(%p) ts %p\n", s, s->ts);
499 rpmts_IDTXload(rpmtsObject * s)
501 PyObject * result = NULL;
502 rpm_tag_t tag = RPMTAG_INSTALLTID;
506 fprintf(stderr, "*** rpmts_IDTXload(%p) ts %p\n", s, s->ts);
508 Py_BEGIN_ALLOW_THREADS
509 idtx = IDTXload(s->ts, tag);
512 if (idtx == NULL || idtx->nidt <= 0) {
521 result = PyTuple_New(idtx->nidt);
522 for (i = 0; i < idtx->nidt; 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);
531 idtx = IDTXfree(idtx);
539 rpmts_IDTXglob(rpmtsObject * s)
541 PyObject * result = NULL;
542 rpm_tag_t tag = RPMTAG_REMOVETID;
547 fprintf(stderr, "*** rpmts_IDTXglob(%p) ts %p\n", s, s->ts);
549 Py_BEGIN_ALLOW_THREADS
550 globstr = rpmExpand("%{_repackage_dir}/*.rpm", NULL);
551 idtx = IDTXglob(s->ts, globstr, tag);
552 globstr = _free(globstr);
555 if (idtx == NULL || idtx->nidt <= 0) {
564 result = PyTuple_New(idtx->nidt);
565 for (i = 0; i < idtx->nidt; 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);
574 idtx = IDTXfree(idtx);
582 rpmts_Rollback(rpmtsObject * s, PyObject * args, PyObject * kwds)
584 struct rpmInstallArguments_s * ia = alloca(sizeof(*ia));
585 rpmtransFlags transFlags;
586 const char ** av = NULL;
589 char * kwlist[] = {"transactionId", NULL};
592 fprintf(stderr, "*** rpmts_Rollback(%p) ts %p\n", s, s->ts);
594 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:Rollback", kwlist, &rbtid))
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);
604 ia->relocations = NULL;
605 ia->probFilter |= RPMPROB_FILTER_OLDPACKAGE;
607 transFlags = rpmtsSetFlags(s->ts, ia->transFlags);
608 rc = rpmRollback(s->ts, ia, av);
609 transFlags = rpmtsSetFlags(s->ts, transFlags);
612 return Py_BuildValue("i", rc);
618 rpmts_OpenDB(rpmtsObject * s)
623 fprintf(stderr, "*** rpmts_OpenDB(%p) ts %p\n", s, s->ts);
625 dbmode = rpmtsGetDBMode(s->ts);
629 return Py_BuildValue("i", rpmtsOpenDB(s->ts, dbmode));
635 rpmts_CloseDB(rpmtsObject * s)
640 fprintf(stderr, "*** rpmts_CloseDB(%p) ts %p\n", s, s->ts);
642 rc = rpmtsCloseDB(s->ts);
643 rpmtsSetDBMode(s->ts, -1); /* XXX disable lazy opens */
645 return Py_BuildValue("i", rc);
651 rpmts_InitDB(rpmtsObject * s)
656 fprintf(stderr, "*** rpmts_InitDB(%p) ts %p\n", s, s->ts);
658 rc = rpmtsInitDB(s->ts, O_RDONLY);
660 rc = rpmtsCloseDB(s->ts);
662 return Py_BuildValue("i", rc);
668 rpmts_RebuildDB(rpmtsObject * s)
673 fprintf(stderr, "*** rpmts_RebuildDB(%p) ts %p\n", s, s->ts);
675 Py_BEGIN_ALLOW_THREADS
676 rc = rpmtsRebuildDB(s->ts);
679 return Py_BuildValue("i", rc);
685 rpmts_VerifyDB(rpmtsObject * s)
690 fprintf(stderr, "*** rpmts_VerifyDB(%p) ts %p\n", s, s->ts);
692 Py_BEGIN_ALLOW_THREADS
693 rc = rpmtsVerifyDB(s->ts);
696 return Py_BuildValue("i", rc);
702 rpmts_HdrFromFdno(rpmtsObject * s, PyObject * args, PyObject * kwds)
704 PyObject * result = NULL;
709 char * kwlist[] = {"fd", NULL};
711 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:HdrFromFdno", kwlist,
716 rpmrc = rpmReadPackageFile(s->ts, fd, "rpmts_HdrFromFdno", &h);
720 fprintf(stderr, "*** rpmts_HdrFromFdno(%p) ts %p rc %d\n", s, s->ts, rpmrc);
725 result = Py_BuildValue("N", hdr_Wrap(h));
726 h = headerFree(h); /* XXX ref held by result */
730 PyErr_SetString(pyrpmError, "public key not available");
733 case RPMRC_NOTTRUSTED:
734 PyErr_SetString(pyrpmError, "public key not trusted");
740 PyErr_SetString(pyrpmError, "error reading package header");
750 rpmts_HdrCheck(rpmtsObject * s, PyObject * args, PyObject * kwds)
753 PyObject * result = NULL;
758 char * kwlist[] = {"headers", NULL};
761 fprintf(stderr, "*** rpmts_HdrCheck(%p) ts %p\n", s, s->ts);
763 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:HdrCheck", kwlist, &blob))
766 if (blob == Py_None) {
770 if (!PyString_Check(blob)) {
771 PyErr_SetString(pyrpmError, "hdrCheck takes a string of octets");
774 uh = PyString_AsString(blob);
775 uc = PyString_Size(blob);
777 rpmrc = headerCheck(s->ts, uh, uc, &msg);
786 PyErr_SetString(pyrpmError, "public key not availaiable");
789 case RPMRC_NOTTRUSTED:
790 PyErr_SetString(pyrpmError, "public key not trusted");
795 PyErr_SetString(pyrpmError, msg);
806 rpmts_SetVSFlags(rpmtsObject * s, PyObject * args, PyObject * kwds)
809 char * kwlist[] = {"flags", NULL};
812 fprintf(stderr, "*** rpmts_SetVSFlags(%p) ts %p\n", s, s->ts);
814 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:SetVSFlags", kwlist,
818 /* XXX FIXME: value check on vsflags, or build pure python object
819 * for it, and require an object of that type */
821 return Py_BuildValue("i", rpmtsSetVSFlags(s->ts, vsflags));
827 rpmts_GetVSFlags(rpmtsObject * s)
829 return Py_BuildValue("i", rpmtsVSFlags(s->ts));
835 rpmts_SetColor(rpmtsObject * s, PyObject * args, PyObject * kwds)
838 char * kwlist[] = {"color", NULL};
841 fprintf(stderr, "*** rpmts_SetColor(%p) ts %p\n", s, s->ts);
843 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:Color", kwlist, &tscolor))
846 /* XXX FIXME: value check on tscolor, or build pure python object
847 * for it, and require an object of that type */
849 return Py_BuildValue("i", rpmtsSetColor(s->ts, tscolor));
855 rpmts_PgpPrtPkts(rpmtsObject * s, PyObject * args, PyObject * kwds)
861 char * kwlist[] = {"octets", NULL};
864 fprintf(stderr, "*** rpmts_PgpPrtPkts(%p) ts %p\n", s, s->ts);
866 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:PgpPrtPkts", kwlist, &blob))
869 if (blob == Py_None) {
873 if (!PyString_Check(blob)) {
874 PyErr_SetString(pyrpmError, "pgpPrtPkts takes a string of octets");
877 pkt = (unsigned char *)PyString_AsString(blob);
878 pktlen = PyString_Size(blob);
880 rc = pgpPrtPkts(pkt, pktlen, NULL, 1);
882 return Py_BuildValue("i", rc);
888 rpmts_PgpImportPubkey(rpmtsObject * s, PyObject * args, PyObject * kwds)
894 char * kwlist[] = {"pubkey", NULL};
897 fprintf(stderr, "*** rpmts_PgpImportPubkey(%p) ts %p\n", s, s->ts);
899 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:PgpImportPubkey",
903 if (blob == Py_None) {
907 if (!PyString_Check(blob)) {
908 PyErr_SetString(pyrpmError, "PgpImportPubkey takes a string of octets");
911 pkt = (unsigned char *)PyString_AsString(blob);
912 pktlen = PyString_Size(blob);
914 rc = rpmtsImportPubkey(s->ts, pkt, pktlen);
916 return Py_BuildValue("i", rc);
922 rpmts_GetKeys(rpmtsObject * s)
924 const void **data = NULL;
929 fprintf(stderr, "*** rpmts_GetKeys(%p) ts %p\n", s, s->ts);
931 rpmtsGetKeys(s->ts, &data, &num);
932 if (data == NULL || num <= 0) {
938 tuple = PyTuple_New(num);
940 for (i = 0; i < num; i++) {
942 obj = (data[i] ? (PyObject *) data[i] : Py_None);
944 PyTuple_SetItem(tuple, i, obj);
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)
959 Header h = (Header) hd;
960 struct rpmtsCallbackType_s * cbInfo = data;
961 PyObject * pkgObj = (PyObject *) pkgKey;
962 PyObject * args, * result;
965 if (cbInfo->pythonError) return NULL;
966 if (cbInfo->cb == Py_None) return NULL;
968 /* Synthesize a python object for callback (if necessary). */
969 if (pkgObj == NULL) {
971 const char * n = NULL;
972 (void) headerNVR(h, &n, NULL, NULL);
973 pkgObj = Py_BuildValue("s", n);
981 PyEval_RestoreThread(cbInfo->_save);
983 args = Py_BuildValue("(illOO)", what, amount, total, pkgObj, cbInfo->data);
984 result = PyEval_CallObject(cbInfo->cb, args);
989 cbInfo->pythonError = 1;
990 cbInfo->_save = PyEval_SaveThread();
994 if (what == RPMCALLBACK_INST_OPEN_FILE) {
997 if (!PyArg_Parse(result, "i", &fdno)) {
998 cbInfo->pythonError = 1;
999 cbInfo->_save = PyEval_SaveThread();
1003 cbInfo->_save = PyEval_SaveThread();
1007 fprintf(stderr, "\t%p = fdDup(%d)\n", fd, fdno);
1009 fcntl(Fileno(fd), F_SETFD, FD_CLOEXEC);
1013 if (what == RPMCALLBACK_INST_CLOSE_FILE) {
1015 fprintf(stderr, "\tFclose(%p)\n", fd);
1019 fprintf(stderr, "\t%d:%d key %p\n", amount, total, pkgKey);
1023 cbInfo->_save = PyEval_SaveThread();
1031 rpmts_SetFlags(rpmtsObject * s, PyObject * args, PyObject * kwds)
1033 rpmtransFlags transFlags = 0;
1034 char * kwlist[] = {"flags", NULL};
1036 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:SetFlags", kwlist,
1041 fprintf(stderr, "*** rpmts_SetFlags(%p) ts %p transFlags %x\n", s, s->ts, transFlags);
1043 /* XXX FIXME: value check on flags, or build pure python object
1044 * for it, and require an object of that type */
1046 return Py_BuildValue("i", rpmtsSetFlags(s->ts, transFlags));
1052 rpmts_SetProbFilter(rpmtsObject * s, PyObject * args, PyObject * kwds)
1054 rpmprobFilterFlags ignoreSet = 0;
1055 rpmprobFilterFlags oignoreSet;
1056 char * kwlist[] = {"ignoreSet", NULL};
1058 if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:ProbFilter", kwlist,
1063 fprintf(stderr, "*** rpmts_SetProbFilter(%p) ts %p ignoreSet %x\n", s, s->ts, ignoreSet);
1065 oignoreSet = s->ignoreSet;
1066 s->ignoreSet = ignoreSet;
1068 return Py_BuildValue("i", oignoreSet);
1073 static rpmpsObject *
1074 rpmts_Problems(rpmtsObject * s)
1078 fprintf(stderr, "*** rpmts_Problems(%p) ts %p\n", s, s->ts);
1080 return rpmps_Wrap( rpmtsProblems(s->ts) );
1086 rpmts_Run(rpmtsObject * s, PyObject * args, PyObject * kwds)
1092 struct rpmtsCallbackType_s cbInfo;
1093 char * kwlist[] = {"callback", "data", NULL};
1095 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO:Run", kwlist,
1096 &cbInfo.cb, &cbInfo.data))
1100 cbInfo.pythonError = 0;
1101 cbInfo._save = PyEval_SaveThread();
1103 if (cbInfo.cb != NULL) {
1104 if (!PyCallable_Check(cbInfo.cb)) {
1105 PyErr_SetString(PyExc_TypeError, "expected a callable");
1108 (void) rpmtsSetNotifyCallback(s->ts, rpmtsCallback, (void *) &cbInfo);
1112 fprintf(stderr, "*** rpmts_Run(%p) ts %p ignore %x\n", s, s->ts, s->ignoreSet);
1114 rc = rpmtsRun(s->ts, NULL, s->ignoreSet);
1115 ps = rpmtsProblems(s->ts);
1118 (void) rpmtsSetNotifyCallback(s->ts, NULL, NULL);
1120 PyEval_RestoreThread(cbInfo._save);
1122 if (cbInfo.pythonError) {
1128 list = PyList_New(0);
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);
1149 psi = rpmpsFreeIterator(psi);
1155 #if Py_TPFLAGS_HAVE_ITER
1157 rpmts_iter(rpmtsObject * s)
1160 fprintf(stderr, "*** rpmts_iter(%p) ts %p\n", s, s->ts);
1163 return (PyObject *)s;
1168 * @todo Add TR_ADDED filter to iterator.
1171 rpmts_iternext(rpmtsObject * s)
1173 PyObject * result = NULL;
1177 fprintf(stderr, "*** rpmts_iternext(%p) ts %p tsi %p %d\n", s, s->ts, s->tsi, s->tsiFilter);
1179 /* Reset iterator on 1st entry. */
1180 if (s->tsi == NULL) {
1181 s->tsi = rpmtsiInit(s->ts);
1187 te = rpmtsiNext(s->tsi, s->tsiFilter);
1189 result = (PyObject *) rpmte_Wrap(te);
1191 s->tsi = rpmtsiFree(s->tsi);
1199 * @todo Add TR_ADDED filter to iterator.
1202 rpmts_Next(rpmtsObject * s)
1207 fprintf(stderr, "*** rpmts_Next(%p) ts %p\n", s, s->ts);
1209 result = rpmts_iternext(s);
1211 if (result == NULL) {
1222 spec_Parse(rpmtsObject * s, PyObject * args, PyObject * kwds)
1224 const char * specfile;
1226 char * buildRoot = NULL;
1228 char * passPhrase = "";
1229 char *cookie = NULL;
1232 char * kwlist[] = {"specfile", NULL};
1234 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s:Parse", kwlist, &specfile))
1237 if (parseSpec(s->ts, specfile,"/", buildRoot,recursing, passPhrase,
1238 cookie, anyarch, force)!=0) {
1239 PyErr_SetString(pyrpmError, "can't parse specfile\n");
1243 spec = rpmtsSpec(s->ts);
1244 return spec_Wrap(spec);
1249 static rpmmiObject *
1250 rpmts_Match(rpmtsObject * s, PyObject * args, PyObject * kwds)
1252 PyObject *TagN = NULL;
1253 PyObject *Key = NULL;
1255 /* XXX lkey *must* be a 32 bit integer, int "works" on all known platforms. */
1258 rpm_tag_t tag = RPMDBI_PACKAGES;
1259 char * kwlist[] = {"tagNumber", "key", NULL};
1262 fprintf(stderr, "*** rpmts_Match(%p) ts %p\n", s, s->ts);
1264 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:Match", kwlist,
1268 if (TagN && (tag = tagNumFromPyObject (TagN)) == -1) {
1269 PyErr_SetString(PyExc_TypeError, "unknown tag type");
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;
1282 PyErr_SetString(PyExc_TypeError, "unknown key type");
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");
1297 return rpmmi_Wrap( rpmtsInitIterator(s->ts, tag, key, len), (PyObject*)s);
1302 static struct PyMethodDef rpmts_methods[] = {
1303 {"Debug", (PyCFunction)rpmts_Debug, METH_VARARGS|METH_KEYWORDS,
1306 {"addInstall", (PyCFunction) rpmts_AddInstall, METH_VARARGS|METH_KEYWORDS,
1308 {"addErase", (PyCFunction) rpmts_AddErase, METH_VARARGS|METH_KEYWORDS,
1310 {"check", (PyCFunction) rpmts_Check, METH_VARARGS|METH_KEYWORDS,
1312 {"order", (PyCFunction) rpmts_Order, METH_NOARGS,
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,
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,
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,
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,
1379 {"pgpPrtPkts", (PyCFunction) rpmts_PgpPrtPkts, METH_VARARGS|METH_KEYWORDS,
1381 {"pgpImportPubkey", (PyCFunction) rpmts_PgpImportPubkey, METH_VARARGS|METH_KEYWORDS,
1383 {"getKeys", (PyCFunction) rpmts_GetKeys, METH_NOARGS,
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,
1393 - Retrieve next transaction set element.\n" },
1394 {NULL, NULL} /* sentinel */
1399 static void rpmts_dealloc(rpmtsObject * s)
1403 fprintf(stderr, "%p -- ts %p db %p\n", s, s->ts, rpmtsGetRdb(s->ts));
1404 s->ts = rpmtsFree(s->ts);
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);
1413 static PyObject * rpmts_getattro(PyObject * o, PyObject * n)
1415 return PyObject_GenericGetAttr(o, n);
1420 static int rpmts_setattro(PyObject * o, PyObject * n, PyObject * v)
1422 rpmtsObject *s = (rpmtsObject *)o;
1423 char * name = PyString_AsString(n);
1426 if (!strcmp(name, "scriptFd")) {
1427 if (!PyArg_Parse(v, "i", &fdno)) return 0;
1429 PyErr_SetString(PyExc_TypeError, "bad file descriptor");
1432 s->scriptFd = fdDup(fdno);
1433 rpmtsSetScriptFd(s->ts, s->scriptFd);
1436 PyErr_SetString(PyExc_AttributeError, name);
1445 static int rpmts_init(rpmtsObject * s, PyObject *args, PyObject *kwds)
1447 /* nothing to do atm... */
1453 static void rpmts_free(rpmtsObject * s)
1456 fprintf(stderr, "%p -- ts %p db %p\n", s, s->ts, rpmtsGetRdb(s->ts));
1457 s->ts = rpmtsFree(s->ts);
1460 Fclose(s->scriptFd);
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);
1466 PyObject_Del((PyObject *)s);
1471 static PyObject * rpmts_alloc(PyTypeObject * subtype, int nitems)
1473 PyObject * s = PyType_GenericAlloc(subtype, nitems);
1475 if (_rpmts_debug < 0)
1476 fprintf(stderr, "*** rpmts_alloc(%p,%d) ret %p\n", subtype, nitems, s);
1482 static PyObject * rpmts_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds)
1484 rpmtsObject * s = (void *) PyObject_New(rpmtsObject, subtype);
1486 char * rootDir = "/";
1487 rpmVSFlags vsflags = rpmExpandNumeric("%{?__vsflags}");
1488 char * kwlist[] = {"rootdir", "vsflags", 0};
1490 if (_rpmts_debug < 0)
1491 fprintf(stderr, "*** rpmts_new(%p,%p,%p)\n", s, args, kwds);
1493 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|si:rpmts_init", kwlist,
1494 &rootDir, &vsflags))
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
1502 (void) rpmtsSetVSFlags(s->ts, vsflags);
1503 s->keyList = PyList_New(0);
1509 fprintf(stderr, "%p ++ ts %p db %p\n", s, s->ts, rpmtsGetRdb(s->ts));
1511 return (PyObject *)s;
1516 static char rpmts_doc[] =
1521 PyTypeObject rpmts_Type = {
1522 PyObject_HEAD_INIT(&PyType_Type)
1524 "rpm.ts", /* tp_name */
1525 sizeof(rpmtsObject), /* tp_size */
1526 0, /* tp_itemsize */
1527 (destructor) rpmts_dealloc, /* tp_dealloc */
1529 (getattrfunc)0, /* tp_getattr */
1530 (setattrfunc)0, /* tp_setattr */
1533 0, /* tp_as_number */
1534 0, /* tp_as_sequence */
1535 0, /* tp_as_mapping */
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 */
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 */
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 */
1570 rpmts_Create(PyObject * self, PyObject * args, PyObject * kwds)
1572 return PyObject_Call((PyObject *) &rpmts_Type, args, kwds);