3 #define _RPMIOB_INTERNAL 8 #define _RPMPYTHON_INTERNAL 11 #if defined(MODULE_EMBED) 13 #include <cStringIO.h> 14 #undef WITH_PYTHONEMBED 17 #if defined(WITH_PYTHONEMBED) 30 #if defined(WITH_PYTHONEMBED) 31 static int dlopened = 0;
32 static rpmpython (*rpmpythonNew_p) (
char ** av, uint32_t flags);
33 static rpmRC (*rpmpythonRunFile_p) (
rpmpython python,
const char * fn,
const char ** resultp);
34 static rpmRC (*rpmpythonRun_p) (
rpmpython python,
const char * str,
const char ** resultp);
43 #if defined(MODULE_EMBED) 58 if (_rpmpythonPool == NULL) {
67 #if defined(MODULE_EMBED) 68 static const char * _rpmpythonI_init =
"\ 70 from cStringIO import StringIO\n\ 71 sys.stdout = StringIO()\n\ 84 #if defined(WITH_PYTHONEMBED) 85 static void loadModule(
void) {
86 const char librpmpython[] =
"rpmpython.so";
89 h = dlopen (librpmpython, RTLD_NOW|RTLD_GLOBAL);
93 "embedded python will not be available\n"),
94 librpmpython, dlerror());
96 else if(!((rpmpythonNew_p = dlsym(h,
"rpmpythonNew"))
97 && (rpmpythonRunFile_p = dlsym(h,
"rpmpythonRunFile"))
98 && (rpmpythonRun_p = dlsym(h,
"rpmpythonRun")))) {
100 "embedded python will not be available\n"),
101 librpmpython, dlerror());
112 #if defined(WITH_PYTHONEMBED) 113 if (!dlopened) loadModule();
114 if (dlopened)
return rpmpythonNew_p(av, flags);
116 static char * _av[] = {
"rpmpython", NULL };
117 #if defined(MODULE_EMBED) 118 int initialize = (!(flags & 0x80000000) ||
_rpmpythonI == NULL);
124 fprintf(stderr,
"==> %s(%p, %d) python %p\n", __FUNCTION__, av, flags, python);
126 if (av == NULL) av = _av;
128 #if defined(MODULE_EMBED) 129 if (!Py_IsInitialized()) {
130 Py_SetProgramName((
char *)_av[0]);
133 if (PycStringIO == NULL)
134 PycStringIO = (
struct PycStringIO_CAPI *)
135 PyCObject_Import(
"cStringIO",
"cStringIO_CAPI");
138 static const char _pythonI_init[] =
"%{?_pythonI_init}";
139 const char * s =
rpmExpand(_rpmpythonI_init, _pythonI_init, NULL);
141 (void) PySys_SetArgv(ac, (
char **)av);
142 fprintf(stderr,
"==========\n%s\n==========\n", s);
153 #if defined(WITH_PYTHONEMBED) 154 if (dlopened)
return rpmpythonRunFile_p(python, fn, resultp);
159 fprintf(stderr,
"==> %s(%p,%s)\n", __FUNCTION__, python, fn);
164 #if defined(MODULE_EMBED) 165 const char * pyfn = ((fn == NULL || !strcmp(fn,
"-")) ?
"<stdin>" : fn);
166 FILE * pyfp = (!strcmp(pyfn,
"<stdin>") ? stdin : fopen(fn,
"rb"));
167 int closeit = (pyfp != stdin);
168 PyCompilerFlags cf = { 0 };
171 PyRun_AnyFileExFlags(pyfp, pyfn, closeit, &cf);
183 const char * val = NULL;
187 if (!strcmp(arg,
"-")) {
190 if ((arg[0] ==
'/' || strchr(arg,
' ') == NULL)
192 && S_ISREG(sb.st_mode)) {
205 #if defined(WITH_PYTHONEMBED) 206 if (dlopened)
return rpmpythonRun_p(python, str, resultp);
211 fprintf(stderr,
"==> %s(%p,%s,%p)\n", __FUNCTION__, python, str, resultp);
217 #if defined(MODULE_EMBED) 218 PyCompilerFlags cf = { 0 };
219 PyObject * m = PyImport_AddModule(
"__main__");
220 PyObject * d = (m ? PyModule_GetDict(m) : NULL);
221 PyObject * v = (m ? PyRun_StringFlags(val, Py_single_input, d, d, &cf) : NULL);
226 if (resultp != NULL) {
227 PyObject * sys_stdout = PySys_GetObject((
char *)
"stdout");
228 if (sys_stdout != NULL && PycStringIO_OutputCheck(sys_stdout)) {
229 PyObject * o = (*PycStringIO->cgetvalue)(sys_stdout);
230 *resultp = (PyString_Check(o) ? PyString_AsString(o) :
"");
char * xstrdup(const char *str)
static rpmpython rpmpythonI(void)
int Stat(const char *path, struct stat *st)
stat(2) clone.
rpmRC rpmpythonRunFile(rpmpython python, const char *fn, const char **resultp)
Execute python from a file.
rpmiob rpmiobFree(rpmiob iob)
Destroy a I/O buffer instance.
static void rpmlog(int code, const char *fmt,...)
rpmiob rpmiobAppend(rpmiob iob, const char *s, size_t nl)
Append string to I/O buffer.
int rpmiobSlurp(const char *fn, rpmiob *iobp)
static void rpmpythonFini(void *_python)
Yet Another syslog(3) API clone.
static rpmpython rpmpythonGetPool(rpmioPool pool)
static const char * rpmpythonSlurp(const char *arg)
rpmioItem rpmioGetPool(rpmioPool pool, size_t size)
Get unused item from pool, or alloc a new item.
rpmRC rpmpythonRun(rpmpython python, const char *str, const char **resultp)
Execute python string.
int argvCount(const ARGV_t argv)
Return no.
struct rpmpython_s * rpmpython
rpmpython rpmpythonNew(char **av, uint32_t flags)
Create and load a python interpreter.
char * rpmExpand(const char *arg,...)
Return (malloc'ed) concatenated macro expansion(s).
rpmiob rpmiobNew(size_t len)
Create an I/O buffer.
enum rpmRC_e rpmRC
RPM return codes.
char * rpmiobStr(rpmiob iob)
Return I/O buffer (as string).
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.
static void * _free(const void *p)
Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
rpmpython rpmpythonLink(rpmpython python)
Reference a python interpreter instance.