rpm  4.5
rpmmodule.c
Go to the documentation of this file.
1 
5 #include "system.h"
6 
7 #include <rpmio_internal.h>
8 #include <rpmcli.h> /* XXX for rpmCheckSig */
9 #include <rpmdb.h>
10 #include <rpmsq.h>
11 
12 #include "legacy.h"
13 #include "misc.h"
14 #include "header_internal.h"
15 
16 #include "header-py.h"
17 #include "rpmal-py.h"
18 #include "rpmds-py.h"
19 #include "rpmfd-py.h"
20 #include "rpmfts-py.h"
21 #include "rpmfi-py.h"
22 #include "rpmmi-py.h"
23 #include "rpmps-py.h"
24 #include "rpmrc-py.h"
25 #include "rpmte-py.h"
26 #include "rpmts-py.h"
27 #include "spec-py.h"
28 
29 #include "debug.h"
30 
31 #ifdef __LCLINT__
32 #undef PyObject_HEAD
33 #define PyObject_HEAD int _PyObjectHead
34 #endif
35 
40 
43 PyObject * pyrpmError;
44 
45 extern sigset_t rpmsqCaught;
46 
47 #if PY_VERSION_HEX < 0x02050000
48 typedef int Py_ssize_t;
49 #endif
50 
51 
54 static PyObject * expandMacro(PyObject * self, PyObject * args, PyObject * kwds)
55 {
56  char * macro;
57 
58  if (!PyArg_ParseTuple(args, "s", &macro))
59  return NULL;
60 
61  return Py_BuildValue("s", rpmExpand(macro, NULL));
62 }
63 
66 static PyObject * archScore(PyObject * s, PyObject * args,
67  PyObject * kwds)
68 {
69  char * arch;
70  char * platform;
71  int score;
72  char * kwlist[] = {"arch", NULL};
73 
74  if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &arch))
75  return NULL;
76 
77  platform = rpmExpand(arch, "-", "%{_vendor}", "-", "%{_os}", NULL);
78  score = rpmPlatformScore(platform, NULL, 0);
79  platform = _free(platform);
80 
81  return Py_BuildValue("i", score);
82 }
83 
86 static PyObject * platformScore(PyObject * s, PyObject * args,
87  PyObject * kwds)
88 {
89  char * platform;
90  int score;
91  char * kwlist[] = {"platform", NULL};
92 
93  if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &platform))
94  return NULL;
95 
96  score = rpmPlatformScore(platform, NULL, 0);
97 
98  return Py_BuildValue("i", score);
99 }
100 
103 static PyObject * signalsCaught(PyObject * self, PyObject * check)
104 {
105  PyObject *caught, *o;
106  Py_ssize_t llen;
107  int signum, i;
108  sigset_t newMask, oldMask;
109 
110  if (!PyList_Check(check)) {
111  PyErr_SetString(PyExc_TypeError, "list expected");
112  return NULL;
113  }
114 
115  llen = PyList_Size(check);
116  caught = PyList_New(0);
117 
118  /* block signals while checking for them */
119  (void) sigfillset(&newMask);
120  (void) sigprocmask(SIG_BLOCK, &newMask, &oldMask);
121 
122  for (i = 0; i < llen; i++) {
123  o = PyList_GetItem(check, i);
124  signum = PyInt_AsLong(o);
125  if (sigismember(&rpmsqCaught, signum)) {
126  PyList_Append(caught, o);
127  }
128  }
129  (void) sigprocmask(SIG_SETMASK, &oldMask, NULL);
130 
131  return caught;
132 }
133 
136 static PyObject * checkSignals(PyObject * self, PyObject * args)
137 {
138  if (!PyArg_ParseTuple(args, ":checkSignals")) return NULL;
140  Py_INCREF(Py_None);
141  return Py_None;
142 }
143 
146 static PyObject * setLogFile (PyObject * self, PyObject * args, PyObject *kwds)
147 {
148  PyObject * fop = NULL;
149  FILE * fp = NULL;
150  char * kwlist[] = {"fileObject", NULL};
151 
152  if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:logSetFile", kwlist, &fop))
153  return NULL;
154 
155  if (fop) {
156  if (!PyFile_Check(fop)) {
157  PyErr_SetString(pyrpmError, "requires file object");
158  return NULL;
159  }
160  fp = PyFile_AsFile(fop);
161  }
162 
163  (void) rpmlogSetFile(fp);
164 
165  Py_INCREF(Py_None);
166  return (PyObject *) Py_None;
167 }
168 
171 static PyObject *
172 setVerbosity (PyObject * self, PyObject * args, PyObject *kwds)
173 {
174  int level;
175  char * kwlist[] = {"level", NULL};
176 
177  if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &level))
178  return NULL;
179 
180  rpmSetVerbosity(level);
181 
182  Py_INCREF(Py_None);
183  return (PyObject *) Py_None;
184 }
185 
188 static PyObject *
189 setEpochPromote (PyObject * self, PyObject * args, PyObject * kwds)
190 {
191  char * kwlist[] = {"promote", NULL};
192 
193  if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist,
195  return NULL;
196 
197  Py_INCREF(Py_None);
198  return (PyObject *) Py_None;
199 }
200 
203 static PyObject * setStats (PyObject * self, PyObject * args, PyObject * kwds)
204 {
205  char * kwlist[] = {"stats", NULL};
206 
207  if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &_rpmts_stats))
208  return NULL;
209 
210  Py_INCREF(Py_None);
211  return (PyObject *) Py_None;
212 }
217 static PyMethodDef rpmModuleMethods[] = {
218  { "TransactionSet", (PyCFunction) rpmts_Create, METH_VARARGS|METH_KEYWORDS,
219 "rpm.TransactionSet([rootDir, [db]]) -> ts\n\
220 - Create a transaction set.\n" },
221 
222 #if Py_TPFLAGS_HAVE_ITER /* XXX backport to python-1.5.2 */
223  { "newrc", (PyCFunction) rpmrc_Create, METH_VARARGS|METH_KEYWORDS,
224  NULL },
225 #endif
226  { "addMacro", (PyCFunction) rpmrc_AddMacro, METH_VARARGS|METH_KEYWORDS,
227  NULL },
228  { "delMacro", (PyCFunction) rpmrc_DelMacro, METH_VARARGS|METH_KEYWORDS,
229  NULL },
230  { "expandMacro", (PyCFunction) expandMacro, METH_VARARGS|METH_KEYWORDS,
231  NULL },
232 
233  { "archscore", (PyCFunction) archScore, METH_VARARGS|METH_KEYWORDS,
234  NULL },
235  { "platformscore", (PyCFunction) platformScore, METH_VARARGS|METH_KEYWORDS,
236  NULL },
237 
238  { "signalsCaught", (PyCFunction) signalsCaught, METH_O,
239  NULL },
240  { "checkSignals", (PyCFunction) checkSignals, METH_VARARGS,
241  NULL },
242 
243  { "headerLoad", (PyCFunction) hdrLoad, METH_VARARGS|METH_KEYWORDS,
244  NULL },
245  { "mergeHeaderListFromFD", (PyCFunction) rpmMergeHeadersFromFD, METH_VARARGS|METH_KEYWORDS,
246  NULL },
247  { "readHeaderListFromFD", (PyCFunction) rpmHeaderFromFD, METH_VARARGS|METH_KEYWORDS,
248  NULL },
249  { "readHeaderListFromFile", (PyCFunction) rpmHeaderFromFile, METH_VARARGS|METH_KEYWORDS,
250  NULL },
251  { "readHeaderFromFD", (PyCFunction) rpmSingleHeaderFromFD, METH_VARARGS|METH_KEYWORDS,
252  NULL },
253 
254  { "setLogFile", (PyCFunction) setLogFile, METH_VARARGS|METH_KEYWORDS,
255  NULL },
256 
257  { "versionCompare", (PyCFunction) versionCompare, METH_VARARGS|METH_KEYWORDS,
258  NULL },
259  { "labelCompare", (PyCFunction) labelCompare, METH_VARARGS|METH_KEYWORDS,
260  NULL },
261  { "setVerbosity", (PyCFunction) setVerbosity, METH_VARARGS|METH_KEYWORDS,
262  NULL },
263  { "setEpochPromote", (PyCFunction) setEpochPromote, METH_VARARGS|METH_KEYWORDS,
264  NULL },
265  { "setStats", (PyCFunction) setStats, METH_VARARGS|METH_KEYWORDS,
266  NULL },
267 
268  { "dsSingle", (PyCFunction) rpmds_Single, METH_VARARGS|METH_KEYWORDS,
269 "rpm.dsSingle(TagN, N, [EVR, [Flags]] -> ds\n\
270 - Create a single element dependency set.\n" },
271  { NULL }
272 } ;
273 
274 /*
275  * Force clean up of open iterators and dbs on exit.
276  */
277 static void rpm_exithook(void)
278 {
280 }
281 
284 static char rpm__doc__[] =
285 "";
286 
287 void init_rpm(void); /* XXX eliminate gcc warning */
290 void init_rpm(void)
291 {
292  PyObject * d, *o, * tag = NULL, * dict;
293  int i;
294  const struct headerSprintfExtension_s * extensions = rpmHeaderFormats;
295  struct headerSprintfExtension_s * ext;
296  PyObject * m;
297 
298 #if Py_TPFLAGS_HAVE_ITER /* XXX backport to python-1.5.2 */
299  if (PyType_Ready(&hdr_Type) < 0) return;
300  if (PyType_Ready(&rpmal_Type) < 0) return;
301  if (PyType_Ready(&rpmds_Type) < 0) return;
302  if (PyType_Ready(&rpmfd_Type) < 0) return;
303  if (PyType_Ready(&rpmfts_Type) < 0) return;
304  if (PyType_Ready(&rpmfi_Type) < 0) return;
305  if (PyType_Ready(&rpmmi_Type) < 0) return;
306  if (PyType_Ready(&rpmps_Type) < 0) return;
307 
308  rpmrc_Type.tp_base = &PyDict_Type;
309  if (PyType_Ready(&rpmrc_Type) < 0) return;
310 
311  if (PyType_Ready(&rpmte_Type) < 0) return;
312  if (PyType_Ready(&rpmts_Type) < 0) return;
313  if (PyType_Ready(&spec_Type) < 0) return;
314 #endif
315 
316  m = Py_InitModule3("_rpm", rpmModuleMethods, rpm__doc__);
317  if (m == NULL)
318  return;
319 
320  /*
321  * treat error to register rpm cleanup hook as fatal, tracebacks
322  * can and will leave stale locks around if we can't clean up
323  */
324  if (Py_AtExit(rpm_exithook) == -1)
325  return;
326 
327  rpmReadConfigFiles(NULL, NULL);
328 
329  d = PyModule_GetDict(m);
330 
331 #ifdef HACK
332  pyrpmError = PyString_FromString("_rpm.error");
333  PyDict_SetItemString(d, "error", pyrpmError);
334  Py_DECREF(pyrpmError);
335 #else
336  pyrpmError = PyErr_NewException("_rpm.error", NULL, NULL);
337  if (pyrpmError != NULL)
338  PyDict_SetItemString(d, "error", pyrpmError);
339 #endif
340 
341 #if Py_TPFLAGS_HAVE_ITER /* XXX backport to python-1.5.2 */
342  Py_INCREF(&hdr_Type);
343  PyModule_AddObject(m, "hdr", (PyObject *) &hdr_Type);
344 
345  Py_INCREF(&rpmal_Type);
346  PyModule_AddObject(m, "al", (PyObject *) &rpmal_Type);
347 
348  Py_INCREF(&rpmds_Type);
349  PyModule_AddObject(m, "ds", (PyObject *) &rpmds_Type);
350 
351  Py_INCREF(&rpmfd_Type);
352  PyModule_AddObject(m, "fd", (PyObject *) &rpmfd_Type);
353 
354  Py_INCREF(&rpmfts_Type);
355  PyModule_AddObject(m, "fts", (PyObject *) &rpmfts_Type);
356 
357  Py_INCREF(&rpmfi_Type);
358  PyModule_AddObject(m, "fi", (PyObject *) &rpmfi_Type);
359 
360  Py_INCREF(&rpmmi_Type);
361  PyModule_AddObject(m, "mi", (PyObject *) &rpmmi_Type);
362 
363  Py_INCREF(&rpmps_Type);
364  PyModule_AddObject(m, "ps", (PyObject *) &rpmps_Type);
365 
366  Py_INCREF(&rpmrc_Type);
367  PyModule_AddObject(m, "rc", (PyObject *) &rpmrc_Type);
368 
369  Py_INCREF(&rpmte_Type);
370  PyModule_AddObject(m, "te", (PyObject *) &rpmte_Type);
371 
372  Py_INCREF(&rpmts_Type);
373  PyModule_AddObject(m, "ts", (PyObject *) &rpmts_Type);
374 
375  Py_INCREF(&spec_Type);
376  PyModule_AddObject(m, "spec", (PyObject *) &spec_Type);
377 #else
378  hdr_Type.ob_type = &PyType_Type;
379  rpmal_Type.ob_type = &PyType_Type;
380  rpmds_Type.ob_type = &PyType_Type;
381  rpmfd_Type.ob_type = &PyType_Type;
382  rpmfts_Type.ob_type = &PyType_Type;
383  rpmfi_Type.ob_type = &PyType_Type;
384  rpmmi_Type.ob_type = &PyType_Type;
385  rpmps_Type.ob_type = &PyType_Type;
386  rpmte_Type.ob_type = &PyType_Type;
387  rpmts_Type.ob_type = &PyType_Type;
388  spec_Type.ob_type = &PyType_Type;
389 #endif
390 
391  dict = PyDict_New();
392 
393  for (i = 0; i < rpmTagTableSize; i++) {
394  tag = PyInt_FromLong(rpmTagTable[i].val);
395  PyDict_SetItemString(d, (char *) rpmTagTable[i].name, tag);
396  Py_DECREF(tag);
397  PyDict_SetItem(dict, tag, o=PyString_FromString(rpmTagTable[i].name + 7));
398  Py_DECREF(o);
399  }
400 
401  while (extensions->name) {
402  if (extensions->type == HEADER_EXT_TAG) {
403  ext = extensions;
404  PyDict_SetItemString(d, (char *) extensions->name, o=PyCObject_FromVoidPtr(ext, NULL));
405  Py_DECREF(o);
406  PyDict_SetItem(dict, tag, o=PyString_FromString(ext->name + 7));
407  Py_DECREF(o);
408  }
409  extensions++;
410  }
411 
412  PyDict_SetItemString(d, "tagnames", dict);
413  Py_DECREF(dict);
414 
415 
416 #define REGISTER_ENUM(val) \
417  PyDict_SetItemString(d, #val, o=PyInt_FromLong( val )); \
418  Py_DECREF(o);
419 
425 
437 
440 
446 
457 
477 
487 
504 
517 
520 
529 
530  REGISTER_ENUM(RPMMIRE_DEFAULT);
531  REGISTER_ENUM(RPMMIRE_STRCMP);
532  REGISTER_ENUM(RPMMIRE_REGEX);
533  REGISTER_ENUM(RPMMIRE_GLOB);
534 
550 
553 
555 
557 }