rpm  5.4.15
rpmpython.c
Go to the documentation of this file.
1 #include "config.h"
2 #if defined(WITH_PYTHONEMBED)
3 #include <Python.h>
4 #include <cStringIO.h>
5 #endif
6 
7 #include "system.h"
8 
9 #define _RPMIOB_INTERNAL /* XXX necessary? */
10 #include <rpmiotypes.h>
11 #include <rpmmacro.h>
12 #include <argv.h>
13 
14 #define _RPMPYTHON_INTERNAL
15 #include "rpmpython.h"
16 
17 #include "debug.h"
18 
19 /*@unchecked@*/
21 
22 /*@unchecked@*/ /*@relnull@*/
24 
25 static void rpmpythonFini(void * _python)
26  /*@globals fileSystem @*/
27  /*@modifies *_python, fileSystem @*/
28 {
29  rpmpython python = (rpmpython) _python;
30 
31 #if defined(WITH_PYTHONEMBED)
32  Py_Finalize();
33 #endif
34  python->I = NULL;
35 }
36 
37 /*@unchecked@*/ /*@only@*/ /*@null@*/
39 
41  /*@globals _rpmpythonPool, fileSystem @*/
42  /*@modifies pool, _rpmpythonPool, fileSystem @*/
43 {
44  rpmpython python;
45 
46  if (_rpmpythonPool == NULL) {
47  _rpmpythonPool = rpmioNewPool("python", sizeof(*python), -1, _rpmpython_debug,
48  NULL, NULL, rpmpythonFini);
49  pool = _rpmpythonPool;
50  }
51  return (rpmpython) rpmioGetPool(pool, sizeof(*python));
52 }
53 
54 /*@unchecked@*/
55 #if defined(WITH_PYTHONEMBED)
56 static const char * _rpmpythonI_init = "\
57 import sys;\
58 from cStringIO import StringIO;\
59 sys.stdout = StringIO();\
60 ";
61 #endif
62 
63 static rpmpython rpmpythonI(void)
64  /*@globals _rpmpythonI @*/
65  /*@modifies _rpmpythonI @*/
66 {
67  if (_rpmpythonI == NULL)
68  _rpmpythonI = rpmpythonNew(NULL, 0);
69  return _rpmpythonI;
70 }
71 
72 rpmpython rpmpythonNew(char ** av, uint32_t flags)
73 {
74  static char * _av[] = { (char *) "rpmpython", NULL };
75 #if defined(WITH_PYTHONEMBED)
76  int initialize = (!(flags & 0x80000000) || _rpmpythonI == NULL);
77 #endif
78  rpmpython python = (flags & 0x80000000)
79  ? rpmpythonI() : rpmpythonGetPool(_rpmpythonPool);
80 
82 fprintf(stderr, "==> %s(%p, %d) python %p\n", __FUNCTION__, av, flags, python);
83 
84  if (av == NULL) av = _av;
85 
86 #if defined(WITH_PYTHONEMBED)
87  if (!Py_IsInitialized()) {
88  Py_SetProgramName((char *)_av[0]);
89  Py_Initialize();
90  }
91  if (PycStringIO == NULL)
92  PycStringIO = (struct PycStringIO_CAPI *)
93  PyCObject_Import("cStringIO", "cStringIO_CAPI");
94 
95  if (initialize) {
96  static const char _pythonI_init[] = "%{?_pythonI_init}";
97  const char * s = rpmExpand(_rpmpythonI_init, _pythonI_init, NULL);
98  int ac = argvCount((ARGV_t)av);
99  (void) PySys_SetArgv(ac, (char **)av);
100 if (_rpmpython_debug)
101 fprintf(stderr, "==========\n%s\n==========\n", s);
102  (void) rpmpythonRun(python, s, NULL);
103  s = _free(s);
104  }
105 #endif
106 
107  return rpmpythonLink(python);
108 }
109 
110 rpmRC rpmpythonRunFile(rpmpython python, const char * fn, const char ** resultp)
111 {
112  rpmRC rc = RPMRC_FAIL;
113 
114 if (_rpmpython_debug)
115 fprintf(stderr, "==> %s(%p,%s)\n", __FUNCTION__, python, fn);
116 
117  if (python == NULL) python = rpmpythonI();
118 
119  if (fn != NULL) {
120 #if defined(WITH_PYTHONEMBED)
121  const char * pyfn = ((fn == NULL || !strcmp(fn, "-")) ? "<stdin>" : fn);
122  FILE * pyfp = (!strcmp(pyfn, "<stdin>") ? stdin : fopen(fn, "rb"));
123  int closeit = (pyfp != stdin);
124  PyCompilerFlags cf = { 0 };
125 
126  if (pyfp != NULL) {
127  PyRun_AnyFileExFlags(pyfp, pyfn, closeit, &cf);
128  rc = RPMRC_OK;
129  }
130 #endif
131  }
132  return rc;
133 }
134 
135 static const char * rpmpythonSlurp(const char * arg)
136  /*@*/
137 {
138  rpmiob iob = NULL;
139  const char * val = NULL;
140  struct stat sb;
141  int xx;
142 
143  if (!strcmp(arg, "-")) { /* Macros from stdin arg. */
144  xx = rpmiobSlurp(arg, &iob);
145  } else
146  if ((arg[0] == '/' || strchr(arg, ' ') == NULL)
147  && !Stat(arg, &sb)
148  && S_ISREG(sb.st_mode)) { /* Macros from a file arg. */
149  xx = rpmiobSlurp(arg, &iob);
150  } else { /* Macros from string arg. */
151  iob = rpmiobAppend(rpmiobNew(strlen(arg)+1), arg, 0);
152  }
153 
154  val = xstrdup(rpmiobStr(iob));
155  iob = rpmiobFree(iob);
156  return val;
157 }
158 
159 rpmRC rpmpythonRun(rpmpython python, const char * str, const char ** resultp)
160 {
161  rpmRC rc = RPMRC_FAIL;
162 
163 if (_rpmpython_debug)
164 fprintf(stderr, "==> %s(%p,%s,%p)\n", __FUNCTION__, python, str, resultp);
165 
166  if (python == NULL) python = rpmpythonI();
167 
168  if (str != NULL) {
169  const char * val = rpmpythonSlurp(str);
170 #if defined(WITH_PYTHONEMBED)
171  PyCompilerFlags cf = { 0 };
172  PyObject * m = PyImport_AddModule("__main__");
173  PyObject * d = (m ? PyModule_GetDict(m) : NULL);
174  PyObject * v = (m ? PyRun_StringFlags(val, Py_single_input, d, d, &cf) : NULL);
175 
176  if (v == NULL) {
177  PyErr_Print();
178  } else {
179  if (resultp != NULL) {
180  PyObject * sys_stdout = PySys_GetObject((char *)"stdout");
181  if (sys_stdout != NULL && PycStringIO_OutputCheck(sys_stdout)) {
182  PyObject * o = (*PycStringIO->cgetvalue)(sys_stdout);
183  *resultp = (PyString_Check(o) ? PyString_AsString(o) : "");
184  PyObject_CallMethod(sys_stdout, "seek", "i",0);
185  PyObject_CallMethod(sys_stdout, "truncate", NULL);
186  } else
187  *resultp = "";
188  }
189  Py_XDECREF(v);
190  if (Py_FlushLine())
191  PyErr_Clear();
192  rc = RPMRC_OK;
193  }
194 #endif
195  val = _free(val);
196  }
197  return rc;
198 }
const char const double d
Definition: bson.h:800
rpmioPool _rpmpythonPool
Definition: rpmpython.c:38
char * xstrdup(const char *str)
Definition: rpmmalloc.c:321
static rpmpython rpmpythonI(void)
Definition: rpmpython.c:63
int Stat(const char *path, struct stat *st)
stat(2) clone.
Definition: rpmrpc.c:1361
rpmRC rpmpythonRunFile(rpmpython python, const char *fn, const char **resultp)
Execute python from a file.
Definition: rpmpython.c:110
rpmiob rpmiobFree(rpmiob iob)
Destroy a I/O buffer instance.
rpmiob rpmiobAppend(rpmiob iob, const char *s, size_t nl)
Append string to I/O buffer.
Definition: rpmiob.c:77
rpmpython _rpmpythonI
Definition: rpmpython.c:23
int rpmiobSlurp(const char *fn, rpmiob *iobp)
Definition: rpmiob.c:129
static void rpmpythonFini(void *_python)
Definition: rpmpython.c:25
const char * str
Definition: bson.h:593
static rpmpython rpmpythonGetPool(rpmioPool pool)
Definition: rpmpython.c:40
static const char * rpmpythonSlurp(const char *arg)
Definition: rpmpython.c:135
const char const bson_bool_t v
Definition: bson.h:919
rpmioItem rpmioGetPool(rpmioPool pool, size_t size)
Get unused item from pool, or alloc a new item.
Definition: rpmmalloc.c:220
rpmRC rpmpythonRun(rpmpython python, const char *str, const char **resultp)
Execute python string.
Definition: rpmpython.c:159
int argvCount(const ARGV_t argv)
Return no.
Definition: argv.c:71
struct rpmpython_s * rpmpython
Definition: rpmpython.h:11
rpmpython rpmpythonNew(char **av, uint32_t flags)
Create and load a python interpreter.
Definition: rpmpython.c:72
int _rpmpython_debug
Definition: rpmpython.c:20
char * rpmExpand(const char *arg,...)
Return (malloc'ed) concatenated macro expansion(s).
Definition: macro.c:3238
const char const char int arg
Definition: mongo.h:777
rpmiob rpmiobNew(size_t len)
Create an I/O buffer.
Definition: rpmiob.c:44
const char const bson int mongo_write_concern int flags
Definition: mongo.h:485
enum rpmRC_e rpmRC
RPM return codes.
char * rpmiobStr(rpmiob iob)
Return I/O buffer (as string).
Definition: rpmiob.c:112
rpmioPool rpmioNewPool(const char *name, size_t size, int limit, int flags, char *(*dbg)(void *item), void(*init)(void *item), void(*fini)(void *item))
Create a memory pool.
Definition: rpmmalloc.c:109
static void * _free(const void *p)
Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
Definition: rpmiotypes.h:756
struct rpmiob_s * rpmiob
Definition: rpmiotypes.h:60
ARGstr_t * ARGV_t
Definition: argv.h:12
rpmpython rpmpythonLink(rpmpython python)
Reference a python interpreter instance.