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