added python module aliases

This commit is contained in:
roberto@sirius
2010-11-20 12:47:52 +01:00
parent 356d59a38d
commit 0beb7108e4
6 changed files with 100 additions and 41 deletions
+3 -35
View File
@@ -410,41 +410,11 @@ PyObject *uwsgi_dyn_loader(void *arg1) {
PyObject *uwsgi_file_loader(void *arg1) {
char *filename = (char *) arg1;
FILE *wsgifile;
struct _node *wsgi_file_node = NULL;
PyObject *wsgi_compiled_node, *wsgi_file_module, *wsgi_file_dict;
PyObject *wsgi_file_module, *wsgi_file_dict;
PyObject *wsgi_file_callable;
wsgifile = fopen(filename, "r");
if (!wsgifile) {
uwsgi_error("fopen()");
exit(1);
}
wsgi_file_node = PyParser_SimpleParseFile(wsgifile, filename, Py_file_input);
if (!wsgi_file_node) {
PyErr_Print();
uwsgi_log( "failed to parse file %s\n", filename);
exit(1);
}
fclose(wsgifile);
wsgi_compiled_node = (PyObject *) PyNode_Compile(wsgi_file_node, filename);
if (!wsgi_compiled_node) {
PyErr_Print();
uwsgi_log( "failed to compile wsgi file %s\n", filename);
exit(1);
}
wsgi_file_module = PyImport_ExecCodeModule("uwsgi_wsgi_file", wsgi_compiled_node);
if (!wsgi_file_module) {
PyErr_Print();
exit(1);
}
Py_DECREF(wsgi_compiled_node);
wsgi_file_module = uwsgi_pyimport_by_filename("uwsgi_wsgi_file", filename);
// no need to check here for module import as it is already done by uwsgi_pyimport_by_file
wsgi_file_dict = PyModule_GetDict(wsgi_file_module);
if (!wsgi_file_dict) {
@@ -452,7 +422,6 @@ PyObject *uwsgi_file_loader(void *arg1) {
exit(1);
}
wsgi_file_callable = PyDict_GetItemString(wsgi_file_dict, "application");
if (!wsgi_file_callable) {
PyErr_Print();
@@ -465,7 +434,6 @@ PyObject *uwsgi_file_loader(void *arg1) {
exit(1);
}
return wsgi_file_callable;
}
+82 -1
View File
@@ -13,6 +13,7 @@ struct option uwsgi_python_options[] = {
{"home", required_argument, 0, 'H'},
{"pythonpath", required_argument, 0, LONG_ARGS_PYTHONPATH},
{"python-path", required_argument, 0, LONG_ARGS_PYTHONPATH},
{"pymodule-alias", required_argument, 0, LONG_ARGS_PYMODULE_ALIAS},
{"pp", required_argument, 0, LONG_ARGS_PYTHONPATH},
{"pyargv", required_argument, 0, LONG_ARGS_PYARGV},
{"optimize", required_argument, 0, 'O'},
@@ -164,11 +165,56 @@ void uwsgi_python_post_fork() {
}
PyObject *uwsgi_pyimport_by_filename(char *name, char *filename) {
FILE *pyfile;
struct _node *py_file_node = NULL;
PyObject *py_compiled_node, *py_file_module;
pyfile = fopen(filename, "r");
if (!pyfile) {
uwsgi_error("fopen()");
exit(1);
}
py_file_node = PyParser_SimpleParseFile(pyfile, filename, Py_file_input);
if (!py_file_node) {
PyErr_Print();
uwsgi_log( "failed to parse file %s\n", filename);
exit(1);
}
fclose(pyfile);
py_compiled_node = (PyObject *) PyNode_Compile(py_file_node, filename);
if (!py_compiled_node) {
PyErr_Print();
uwsgi_log( "failed to compile python file %s\n", filename);
exit(1);
}
py_file_module = PyImport_ExecCodeModule(name, py_compiled_node);
if (!py_file_module) {
PyErr_Print();
exit(1);
}
Py_DECREF(py_compiled_node);
return py_file_module;
}
void init_uwsgi_vars() {
int i;
PyObject *pysys, *pysys_dict, *pypath;
PyObject *modules = PyImport_GetModuleDict();
PyObject *tmp_module;
#ifdef UWSGI_MINTERPRETERS
char venv_version[15];
PyObject *site_module;
@@ -233,6 +279,33 @@ void init_uwsgi_vars() {
}
}
for(i=0;i<up.pymodule_alias_cnt;i++) {
// split key=value
char *value = strchr(up.pymodule_alias[i], '=');
if (!value) {
uwsgi_log("invalid pymodule-alias syntax\n");
continue;
}
value[0] = 0;
if (!strchr(value+1, '/')) {
// this is a standard pymodule
tmp_module = PyImport_ImportModule(value+1);
if (!tmp_module) {
PyErr_Print();
exit(1);
}
PyDict_SetItemString(modules, up.pymodule_alias[i], tmp_module);
}
else {
// this is a filepath that need to be mapped
tmp_module = uwsgi_pyimport_by_filename(up.pymodule_alias[i], value+1);
}
uwsgi_log("mapped virtual pymodule \"%s\" to real pymodule \"%s\"\n", up.pymodule_alias[i], value+1);
// reset original value
value[0] = '=';
}
}
@@ -588,8 +661,16 @@ int uwsgi_python_magic(char *mountpoint, char *lazy) {
case LONG_ARGS_FILE_CONFIG:
up.file_config = optarg;
return 1;
case LONG_ARGS_PYMODULE_ALIAS:
if (up.pymodule_alias_cnt < MAX_PYMODULE_ALIAS) {
up.pymodule_alias[up.pymodule_alias_cnt] = optarg;
up.pymodule_alias_cnt++;
}
else {
uwsgi_log( "you can specify at most %d --pymodule-alias options\n", MAX_PYMODULE_ALIAS);
}
return 1;
case LONG_ARGS_PYTHONPATH:
uwsgi_log("found PYTHONPATH\n");
if (up.python_path_cnt < MAX_PYTHONPATH) {
up.python_path[up.python_path_cnt] = optarg;
up.python_path_cnt++;
+13
View File
@@ -3,6 +3,14 @@
#define MAX_PYTHONPATH 64
#define MAX_PYMODULE_ALIAS 64
#define LONG_ARGS_PYTHON_BASE 17000 + ((0 + 1) * 100)
#define LONG_ARGS_PYTHONPATH LONG_ARGS_PYTHON_BASE + 1
#define LONG_ARGS_PASTE LONG_ARGS_PYTHON_BASE + 2
#define LONG_ARGS_PYARGV LONG_ARGS_PYTHON_BASE + 3
#define LONG_ARGS_PYMODULE_ALIAS LONG_ARGS_PYTHON_BASE + 4
#ifdef UWSGI_STACKLESS
#include <stackless_api.h>
@@ -100,6 +108,9 @@ struct uwsgi_python {
PyObject *embedded_dict;
PyObject *embedded_args;
PyObject *fastfuncslist;
char *pymodule_alias[MAX_PYMODULE_ALIAS];
int pymodule_alias_cnt;
};
@@ -184,3 +195,5 @@ void gil_fake_release(void);
void init_uwsgi_module_advanced(PyObject *);
void init_uwsgi_module_spooler(PyObject *);
void init_uwsgi_module_sharedarea(PyObject *);
PyObject *uwsgi_pyimport_by_filename(char *, char *);