rpm  5.4.15
rpmmodule.c
Go to the documentation of this file.
1 
5 #include "system-py.h"
6 
7 #include <rpmio_internal.h>
8 #include <rpmcb.h>
9 #include <rpmsq.h>
10 #include <argv.h>
11 
12 #define _RPMTAG_INTERNAL
13 #include <rpmtag.h>
14 #define _RPMEVR_INTERNAL
15 #include <rpmevr.h>
16 #include <rpmdb.h>
17 #include <rpmcli.h> /* XXX for rpmCheckSig */
18 
19 #include "legacy.h"
20 #include "misc.h"
21 
22 #include "header-py.h"
23 #include "rpmal-py.h"
24 #include "rpmds-py.h"
25 #include "rpmfd-py.h"
26 #include "rpmfts-py.h"
27 #include "rpmfi-py.h"
28 #include "rpmkeyring-py.h"
29 #include "rpmmacro-py.h"
30 #include "rpmmi-py.h"
31 #include "rpmps-py.h"
32 #include "rpmtd-py.h"
33 #include "rpmte-py.h"
34 #include "rpmts-py.h"
35 #include "spec-py.h"
36 
37 #include "debug.h"
38 
39 #ifdef __LCLINT__
40 #undef PyObject_HEAD
41 #define PyObject_HEAD int _PyObjectHead
42 #endif
43 
48 
51 PyObject * pyrpmError;
52 
53 extern sigset_t rpmsqCaught;
54 
57 static PyObject * archScore(PyObject * s, PyObject * args,
58  PyObject * kwds)
59 {
60  char * arch;
61  char * platform;
62  int score;
63  char * kwlist[] = {"arch", NULL};
64 
65  if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &arch))
66  return NULL;
67 
68 #if defined(RPM_VENDOR_WINDRIVER)
69  platform = rpmExpand(arch, "-%{_host_vendor}", "-%{_host_os}%{?_gnu}", NULL);
70 #else
71  platform = rpmExpand(arch, "-", "%{_vendor}", "-", "%{_os}", NULL);
72 #endif
73  score = rpmPlatformScore(platform, NULL, 0);
74  platform = _free(platform);
75 
76  return Py_BuildValue("i", score);
77 }
78 
81 static PyObject * platformScore(PyObject * s, PyObject * args,
82  PyObject * kwds)
83 {
84  char * platform;
85  int score;
86  char * kwlist[] = {"platform", NULL};
87 
88  if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &platform))
89  return NULL;
90 
91  score = rpmPlatformScore(platform, NULL, 0);
92 
93  return Py_BuildValue("i", score);
94 }
95 
98 static PyObject * signalsCaught(PyObject * s, PyObject * check)
99 {
100  PyObject *caught, *o;
101  Py_ssize_t llen;
102  int signum, i;
103  sigset_t newMask, oldMask;
104 
105  if (!PyList_Check(check)) {
106  PyErr_SetString(PyExc_TypeError, "list expected");
107  return NULL;
108  }
109 
110  llen = PyList_Size(check);
111  caught = PyList_New(0);
112  if (!caught) {
113  return NULL;
114  }
115  /* block signals while checking for them */
116  (void) sigfillset(&newMask);
117  (void) sigprocmask(SIG_BLOCK, &newMask, &oldMask);
118 
119  for (i = 0; i < llen; i++) {
120  o = PyList_GetItem(check, i);
121  signum = PyInt_AsLong(o);
122  if (sigismember(&rpmsqCaught, signum)) {
123  PyList_Append(caught, o);
124  }
125  }
126  (void) sigprocmask(SIG_SETMASK, &oldMask, NULL);
127 
128  return caught;
129 }
130 
133 static PyObject * checkSignals(PyObject * s, PyObject * args)
134 {
135  if (!PyArg_ParseTuple(args, ":checkSignals")) return NULL;
137  Py_RETURN_NONE;
138 }
139 
142 static PyObject * setLogFile (PyObject * s, PyObject * args,
143  PyObject *kwds)
144 {
145  PyObject * fop = NULL;
146  FILE * fp = NULL;
147  char * kwlist[] = {"fileObject", NULL};
148 
149  if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:logSetFile", kwlist, &fop))
150  return NULL;
151 
152  if (fop) {
153  if (!PyFile_Check(fop)) {
154  PyErr_SetString(pyrpmError, "requires file object");
155  return NULL;
156  }
157  fp = PyFile_AsFile(fop);
158  }
159 
160  (void) rpmlogSetFile(fp);
161 
162  Py_INCREF(Py_None);
163  return (PyObject *) Py_None;
164 }
165 
168 static PyObject *
169 setVerbosity (PyObject * s, PyObject * args, PyObject *kwds)
170 {
171  int level;
172  char * kwlist[] = {"level", NULL};
173 
174  if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &level))
175  return NULL;
176 
177  rpmSetVerbosity(level);
178 
179  Py_INCREF(Py_None);
180  return (PyObject *) Py_None;
181 }
182 
185 static PyObject *
186 setEpochPromote (PyObject * s, PyObject * args, PyObject * kwds)
187 {
188  char * kwlist[] = {"promote", NULL};
189 
190  if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist,
192  return NULL;
193 
194  Py_INCREF(Py_None);
195  return (PyObject *) Py_None;
196 }
197 
200 static PyObject * setStats (PyObject * s, PyObject * args,
201  PyObject * kwds)
202 {
203  char * kwlist[] = {"stats", NULL};
204 
205  if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &_rpmts_stats))
206  return NULL;
207 
208  Py_INCREF(Py_None);
209  return (PyObject *) Py_None;
210 }
215 static PyMethodDef rpmModuleMethods[] = {
216  { "TransactionSet", (PyCFunction) rpmts_Create, METH_VARARGS|METH_KEYWORDS,
217 "rpm.TransactionSet([rootDir, [db]]) -> ts\n\
218 - Create a transaction set.\n" },
219 
220  { "addMacro", (PyCFunction) rpmmacro_AddMacro, METH_VARARGS|METH_KEYWORDS,
221  NULL },
222  { "delMacro", (PyCFunction) rpmmacro_DelMacro, METH_VARARGS|METH_KEYWORDS,
223  NULL },
224  { "expandMacro", (PyCFunction)rpmmacro_ExpandMacro, METH_VARARGS|METH_KEYWORDS,
225  NULL },
226  { "getMacros", (PyCFunction) rpmmacro_GetMacros, METH_VARARGS|METH_KEYWORDS,
227  NULL },
228 
229  { "archscore", (PyCFunction) archScore, METH_VARARGS|METH_KEYWORDS,
230  NULL },
231  { "platformscore", (PyCFunction) platformScore, METH_VARARGS|METH_KEYWORDS,
232  NULL },
233 
234  { "signalsCaught", (PyCFunction) signalsCaught, METH_O,
235  NULL },
236  { "checkSignals", (PyCFunction) checkSignals, METH_VARARGS,
237  NULL },
238 
239  { "headerLoad", (PyCFunction) hdrLoad, METH_VARARGS|METH_KEYWORDS,
240  NULL },
241 
242  { "readHeaderListFromFD", (PyCFunction) rpmHeaderFromFD, METH_VARARGS|METH_KEYWORDS,
243  NULL },
244  { "readHeaderListFromFile", (PyCFunction) rpmHeaderFromFile, METH_VARARGS|METH_KEYWORDS,
245  NULL },
246  { "readHeaderFromFD", (PyCFunction) rpmSingleHeaderFromFD, METH_VARARGS|METH_KEYWORDS,
247  NULL },
248  { "writeHeaderListToFD", (PyCFunction) rpmHeaderToFD, METH_VARARGS|METH_KEYWORDS,
249  NULL },
250  { "writeHeaderListToFile", (PyCFunction) rpmHeaderToFile, METH_VARARGS|METH_KEYWORDS,
251  NULL },
252 
253  { "setLogFile", (PyCFunction) setLogFile, METH_VARARGS|METH_KEYWORDS,
254  NULL },
255 
256  { "versionCompare", (PyCFunction) versionCompare, METH_VARARGS|METH_KEYWORDS,
257  NULL },
258  { "labelCompare", (PyCFunction) labelCompare, METH_VARARGS|METH_KEYWORDS,
259  NULL },
260  { "evrCompare", (PyCFunction) evrCompare, METH_VARARGS|METH_KEYWORDS,
261  NULL },
262  { "evrSplit", (PyCFunction) evrSplit, METH_VARARGS|METH_KEYWORDS,
263  NULL },
264  { "setVerbosity", (PyCFunction) setVerbosity, METH_VARARGS|METH_KEYWORDS,
265  NULL },
266  { "setEpochPromote", (PyCFunction) setEpochPromote, METH_VARARGS|METH_KEYWORDS,
267  NULL },
268  { "setStats", (PyCFunction) setStats, METH_VARARGS|METH_KEYWORDS,
269  NULL },
270 
271  { "dsSingle", (PyCFunction) rpmds_Single, METH_VARARGS|METH_KEYWORDS,
272 "rpm.dsSingle(TagN, N, [EVR, [Flags]] -> ds\n\
273 - Create a single element dependency set.\n" },
274  { NULL }
275 } ;
276 
277 /*
278  * Force clean up of open iterators and dbs on exit.
279  */
280 static void rpm_exithook(void)
281 {
283 }
284 
285 /*
286  * Add rpm tag dictionaries to the module
287  */
288 static void addRpmTags(PyObject *module)
289 {
290  PyObject * dict = PyDict_New();
291 
292 #ifdef NOTYET
293 
294  PyObject *pyval, *pyname;
295  rpmtd names = rpmtdNew();
296  rpmTagGetNames(names, 1);
297  const char *tagname, *shortname;
298  rpmTag tagval;
299 
300  while ((tagname = rpmtdNextString(names))) {
301  shortname = tagname + strlen("RPMTAG_");
302  tagval = rpmTagGetValue(shortname);
303 
304  PyModule_AddIntConstant(module, tagname, tagval);
305  pyval = PyInt_FromLong(tagval);
306  pyname = PyString_FromString(shortname);
307  PyDict_SetItem(dict, pyval, pyname);
308  Py_XDECREF(pyval);
309  Py_XDECREF(pyname);
310  }
311  PyModule_AddObject(module, "tagnames", dict);
312  rpmtdFreeData(names);
313  rpmtdFree(names);
314 
315 #else
316 
317  PyObject * d = PyModule_GetDict(module);
318  PyObject * o;
319 
320  { const struct headerTagTableEntry_s * t;
321  PyObject * to;
322  for (t = rpmTagTable; t && t->name; t++) {
323  PyDict_SetItemString(d, (char *) t->name, to=PyInt_FromLong(t->val));
324  Py_XDECREF(to);
325  PyDict_SetItem(dict, to, o=PyString_FromString(t->name + 7));
326  Py_XDECREF(o);
327  }
328  }
329 
332  PyObject * to;
333  int extNum;
334  for (ext = exts, extNum = 0; ext != NULL && ext->type != HEADER_EXT_LAST;
335  ext = (ext->type == HEADER_EXT_MORE ? *ext->u.more : ext+1), extNum++)
336  {
337  if (ext->name == NULL || ext->type != HEADER_EXT_TAG)
338  continue;
339  PyDict_SetItemString(d, (char *) ext->name, to=PyCObject_FromVoidPtr((void *)ext, NULL));
340  Py_XDECREF(to);
341  PyDict_SetItem(dict, to, o=PyString_FromString(ext->name + 7));
342  Py_XDECREF(o);
343  }
344  }
345 
346  PyDict_SetItemString(d, "tagnames", dict);
347  Py_XDECREF(dict);
348 
349 #endif
350 }
351 
354 static char rpm__doc__[] =
355 "";
356 
357 void init_rpm(void); /* XXX eliminate gcc warning */
360 void init_rpm(void)
361 {
362  PyObject * d, * o, * m;
363 
364 #if Py_TPFLAGS_HAVE_ITER /* XXX backport to python-1.5.2 */
365  if (PyType_Ready(&hdr_Type) < 0) return;
366  if (PyType_Ready(&rpmal_Type) < 0) return;
367  if (PyType_Ready(&rpmds_Type) < 0) return;
368  if (PyType_Ready(&rpmfd_Type) < 0) return;
369  if (PyType_Ready(&rpmfts_Type) < 0) return;
370  if (PyType_Ready(&rpmfi_Type) < 0) return;
371  if (PyType_Ready(&rpmmi_Type) < 0) return;
372  if (PyType_Ready(&rpmps_Type) < 0) return;
373 
374  if (PyType_Ready(&rpmtd_Type) < 0) return;
375  if (PyType_Ready(&rpmKeyring_Type) < 0) return;
376  if (PyType_Ready(&rpmPubkey_Type) < 0) return;
377 
378  if (PyType_Ready(&rpmte_Type) < 0) return;
379  if (PyType_Ready(&rpmts_Type) < 0) return;
380  if (PyType_Ready(&spec_Type) < 0) return;
381 #endif
382 
383  m = Py_InitModule3("_rpm", rpmModuleMethods, rpm__doc__);
384  if (m == NULL)
385  return;
386 
387  /*
388  * treat error to register rpm cleanup hook as fatal, tracebacks
389  * can and will leave stale locks around if we can't clean up
390  */
391  if (Py_AtExit(rpm_exithook) == -1)
392  return;
393 
394  rpmReadConfigFiles(NULL, NULL);
395 
396  d = PyModule_GetDict(m);
397 
398 #ifdef HACK
399  pyrpmError = PyString_FromString("_rpm.error");
400  PyDict_SetItemString(d, "error", pyrpmError);
401  Py_XDECREF(pyrpmError);
402 #else
403  pyrpmError = PyErr_NewException("_rpm.error", NULL, NULL);
404  if (pyrpmError != NULL)
405  PyDict_SetItemString(d, "error", pyrpmError);
406 #endif
407 
408 #if Py_TPFLAGS_HAVE_ITER /* XXX backport to python-1.5.2 */
409  Py_INCREF(&hdr_Type);
410  PyModule_AddObject(m, "hdr", (PyObject *) &hdr_Type);
411 
412  Py_INCREF(&rpmal_Type);
413  PyModule_AddObject(m, "al", (PyObject *) &rpmal_Type);
414 
415  Py_INCREF(&rpmds_Type);
416  PyModule_AddObject(m, "ds", (PyObject *) &rpmds_Type);
417 
418  Py_INCREF(&rpmfd_Type);
419  PyModule_AddObject(m, "fd", (PyObject *) &rpmfd_Type);
420 
421  Py_INCREF(&rpmfts_Type);
422  PyModule_AddObject(m, "fts", (PyObject *) &rpmfts_Type);
423 
424  Py_INCREF(&rpmfi_Type);
425  PyModule_AddObject(m, "fi", (PyObject *) &rpmfi_Type);
426 
427  Py_INCREF(&rpmKeyring_Type);
428  PyModule_AddObject(m, "Keyring", (PyObject *) &rpmKeyring_Type);
429  Py_INCREF(&rpmPubkey_Type);
430  PyModule_AddObject(m, "Pubkey", (PyObject *) &rpmPubkey_Type);
431 
432  Py_INCREF(&rpmmi_Type);
433  PyModule_AddObject(m, "mi", (PyObject *) &rpmmi_Type);
434 
435  Py_INCREF(&rpmps_Type);
436  PyModule_AddObject(m, "ps", (PyObject *) &rpmps_Type);
437 
438  Py_INCREF(&rpmtd_Type);
439  PyModule_AddObject(m, "td", (PyObject *) &rpmtd_Type);
440 
441  Py_INCREF(&rpmte_Type);
442  PyModule_AddObject(m, "te", (PyObject *) &rpmte_Type);
443 
444  Py_INCREF(&rpmts_Type);
445  PyModule_AddObject(m, "ts", (PyObject *) &rpmts_Type);
446 
447  Py_INCREF(&spec_Type);
448  PyModule_AddObject(m, "spec", (PyObject *) &spec_Type);
449 #else
450  hdr_Type.ob_type = &PyType_Type;
451  rpmal_Type.ob_type = &PyType_Type;
452  rpmds_Type.ob_type = &PyType_Type;
453  rpmfd_Type.ob_type = &PyType_Type;
454  rpmfts_Type.ob_type = &PyType_Type;
455  rpmfi_Type.ob_type = &PyType_Type;
456  rpmmi_Type.ob_type = &PyType_Type;
457  rpmps_Type.ob_type = &PyType_Type;
458  rpmte_Type.ob_type = &PyType_Type;
459  rpmts_Type.ob_type = &PyType_Type;
460  spec_Type.ob_type = &PyType_Type;
461 #endif
462 
463  addRpmTags(m);
464 
465 #define REGISTER_ENUM(val) \
466  PyDict_SetItemString(d, #val, o=PyInt_FromLong( val )); \
467  Py_XDECREF(o);
468 
474 
486 
489 
494  REGISTER_ENUM(RPMSENSE_FIND_REQUIRES);
495 #if defined(RPM_VENDOR_MANDRIVA) || defined(RPM_VENDOR_PLD)
497  REGISTER_ENUM(RPMSENSE_SCRIPT_PRE);
498  REGISTER_ENUM(RPMSENSE_SCRIPT_POST);
499  REGISTER_ENUM(RPMSENSE_SCRIPT_PREUN);
500  REGISTER_ENUM(RPMSENSE_SCRIPT_POSTUN)
501 #endif
502 #if defined(RPM_VENDOR_PLD)
503  REGISTER_ENUM(RPMSENSE_MISSINGOK);
504 #endif
505 
516 
536 
546 
564 
577 
580 
589 
594 
608 
611 
613 
615 }
const char const double d
Definition: bson.h:800
PyObject * rpmHeaderToFile(PyObject *self, PyObject *args, PyObject *kwds)
Definition: header-py.c:942
headerTagTableEntry rpmTagTable
Automatically generated table of tag name/value pairs.
Definition: tagtbl.c:239
PyObject * rpmHeaderFromFile(PyObject *self, PyObject *args, PyObject *kwds)
Definition: header-py.c:810
static PyObject * setStats(PyObject *s, PyObject *args, PyObject *kwds)
Definition: rpmmodule.c:200
int rpmReadConfigFiles(const char *file, const char *target)
Read macro configuration file(s) for a target.
Definition: rpmrc.c:1095
PyTypeObject rpmds_Type
Definition: rpmds-py.c:736
PyObject * labelCompare(PyObject *self, PyObject *args)
Definition: header-py.c:981
int rpmPlatformScore(const char *platform, void *mi_re, int mi_nre)
Return score of a platform string.
Definition: rpmrc.c:653
PyTypeObject rpmps_Type
Definition: rpmps-py.c:336
sigset_t rpmsqCaught
Definition: rpmsq.c:352
headerSprintfExtension rpmHeaderFormats
Table of query format extensions.
Definition: formats.c:305
PyObject * rpmHeaderToFD(PyObject *self, PyObject *args, PyObject *kwds)
Definition: header-py.c:920
int rpmdbCheckSignals(void)
Check for and exit on termination signals.
Definition: rpmdb.c:523
PyTypeObject rpmte_Type
Definition: rpmte-py.c:440
static PyMethodDef rpmModuleMethods[]
Definition: rpmmodule.c:215
PyObject * evrCompare(PyObject *self, PyObject *args, PyObject *kwds)
Definition: header-py.c:1017
static PyObject * setVerbosity(PyObject *s, PyObject *args, PyObject *kwds)
Definition: rpmmodule.c:169
FILE * rpmlogSetFile(FILE *fp)
Set rpmlog file handle.
Definition: rpmlog.c:165
int rpmdbCheckTerminate(int terminate)
Check rpmdb signal handler for trapped signal and/or requested exit.
Definition: rpmdb.c:476
PyObject * evrSplit(PyObject *self, PyObject *args, PyObject *kwds)
Definition: header-py.c:1040
#define RPMSENSE_NOTEQUAL
Definition: rpmevr.h:78
PyObject * rpmts_Create(PyObject *s, PyObject *args, PyObject *kwds)
Definition: rpmts-py.c:1699
static PyObject * setEpochPromote(PyObject *s, PyObject *args, PyObject *kwds)
Definition: rpmmodule.c:186
#define RPMAL_NOMATCH
Definition: rpmal.h:17
void init_rpm(void)
Definition: rpmmodule.c:360
static PyObject * checkSignals(PyObject *s, PyObject *args)
Definition: rpmmodule.c:133
static void rpm_exithook(void)
Definition: rpmmodule.c:280
PyObject * pyrpmError
Definition: rpmmodule.c:51
const struct headerSprintfExtension_s * headerSprintfExtension
Definition: rpmtag.h:134
char * rpmExpand(const char *arg,...)
Return (malloc'ed) concatenated macro expansion(s).
Definition: macro.c:3252
PyTypeObject hdr_Type
Definition: header-py.c:633
#define rpmSetVerbosity(_lvl)
Definition: rpmcb.h:13
PyTypeObject rpmal_Type
Definition: rpmal-py.c:125
PyTypeObject rpmmi_Type
Definition: rpmmi-py.c:214
static PyObject * signalsCaught(PyObject *s, PyObject *check)
Definition: rpmmodule.c:98
PyTypeObject rpmfi_Type
Definition: rpmfi-py.c:531
rpmdsObject * rpmds_Single(PyObject *s, PyObject *args, PyObject *kwds)
Definition: rpmds-py.c:804
int _rpmts_stats
Definition: rpmts.c:96
static char rpm__doc__[]
Definition: rpmmodule.c:354
Definition: rpmte.h:37
const char const int i
Definition: bson.h:778
#define REGISTER_ENUM(val)
PyTypeObject rpmfd_Type
Definition: rpmfd-py.c:281
static void * _free(const void *p)
Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
Definition: rpmiotypes.h:756
Structure(s) and routine(s) used for EVR parsing and comparison.
PyTypeObject spec_Type
Definition: spec-py.c:248
PyTypeObject rpmts_Type
Definition: rpmts-py.c:1650
static PyObject * archScore(PyObject *s, PyObject *args, PyObject *kwds)
Definition: rpmmodule.c:57
int _rpmds_nopromote
Definition: rpmds.c:126
PyObject * versionCompare(PyObject *self, PyObject *args, PyObject *kwds)
Definition: header-py.c:968
Access RPM indices using Berkeley DB interface(s).
enum rpmTag_e rpmTag
Definition: rpmtag.h:470
PyObject * rpmSingleHeaderFromFD(PyObject *self, PyObject *args, PyObject *kwds)
Definition: header-py.c:837
static const char * platform
Definition: rpmrc.c:41
PyObject * rpmHeaderFromFD(PyObject *self, PyObject *args, PyObject *kwds)
Definition: header-py.c:789
#define RPMDBI_PACKAGES
Pseudo-tags used by the rpmdb and rpmgi iterator API's.
Definition: rpmtag.h:479
static PyObject * platformScore(PyObject *s, PyObject *args, PyObject *kwds)
Definition: rpmmodule.c:81
PyObject * hdrLoad(PyObject *self, PyObject *args, PyObject *kwds)
Definition: header-py.c:692
static PyObject * setLogFile(PyObject *s, PyObject *args, PyObject *kwds)
Definition: rpmmodule.c:142
PyTypeObject rpmfts_Type
Definition: rpmfts-py.c:526
static void addRpmTags(PyObject *module)
Definition: rpmmodule.c:288