PyObject *uwsgi_pyimport_by_filename(char *name, char *filename) { #ifdef UWSGI_PYPY uwsgi_log("import by filename is currently not supported on PyPy !!!\n"); return NULL; #else FILE *pyfile; struct _node *py_file_node = NULL; PyObject *py_compiled_node, *py_file_module; int is_a_package = 0; struct stat pystat; char *real_filename = filename; if (!uwsgi_check_scheme(filename)) { pyfile = fopen(filename, "r"); if (!pyfile) { uwsgi_log("failed to open python file %s\n", filename); return NULL; } if (fstat(fileno(pyfile), &pystat)) { uwsgi_error("fstat()"); return NULL; } if (S_ISDIR(pystat.st_mode)) { is_a_package = 1; fclose(pyfile); real_filename = uwsgi_concat2(filename, "/__init__.py"); pyfile = fopen(real_filename, "r"); if (!pyfile) { uwsgi_error_open(real_filename); free(real_filename); return NULL; } } py_file_node = PyParser_SimpleParseFile(pyfile, real_filename, Py_file_input); if (!py_file_node) { PyErr_Print(); uwsgi_log("failed to parse file %s\n", real_filename); if (is_a_package) free(real_filename); fclose(pyfile); return NULL; } fclose(pyfile); } else { int pycontent_size = 0; char *pycontent = uwsgi_open_and_read(filename, &pycontent_size, 1, NULL); if (pycontent) { py_file_node = PyParser_SimpleParseString(pycontent, Py_file_input); if (!py_file_node) { PyErr_Print(); uwsgi_log("failed to parse url %s\n", real_filename); return NULL; } } } py_compiled_node = (PyObject *) PyNode_Compile(py_file_node, real_filename); if (!py_compiled_node) { PyErr_Print(); uwsgi_log("failed to compile python file %s\n", real_filename); return NULL; } if (is_a_package) { py_file_module = PyImport_AddModule(name); if (py_file_module) { PyModule_AddObject(py_file_module, "__path__", Py_BuildValue("[O]", PyString_FromString(filename))); } free(real_filename); } py_file_module = PyImport_ExecCodeModule(name, py_compiled_node); if (!py_file_module) { PyErr_Print(); return NULL; } Py_DECREF(py_compiled_node); return py_file_module; #endif }
void uwsgi_ini_config(char *file, char *magic_table[]) { size_t len = 0; char *ini; char *ini_line; char *section = ""; char *key; char *val; int lines = 1; char *section_asked = "uwsgi"; char *colon; if (uwsgi_check_scheme(file)) { colon = uwsgi_get_last_char(file, '/'); colon = uwsgi_get_last_char(colon, ':'); } else { colon = uwsgi_get_last_char(file, ':'); } if (colon) { colon[0] = 0; if (colon[1] != 0) { section_asked = colon + 1; } } if (file[0] != 0) { uwsgi_log_initial("[uWSGI] getting INI configuration from %s\n", file); } ini = uwsgi_open_and_read(file, &len, 1, magic_table); while (len) { ini_line = ini_get_line(ini, len); if (ini_line == NULL) { break; } lines++; // skip empty line key = ini_lstrip(ini); ini_rstrip(key); if (key[0] != 0) { if (key[0] == '[') { section = key + 1; section[strlen(section) - 1] = 0; } else if (key[0] == ';' || key[0] == '#') { // this is a comment } else { // val is always valid, but (obviously) can be ignored val = ini_get_key(key); if (!strcmp(section, section_asked)) { ini_rstrip(key); val = ini_lstrip(val); ini_rstrip(val); add_exported_option((char *) key, val, 0); } } } len -= (ini_line - ini); ini += (ini_line - ini); } if (colon) { colon[0] = ':'; } }
void uwsgi_yaml_config(char *file, char *magic_table[]) { int len = 0; char *yaml; int in_uwsgi_section = 0; char *key = NULL; char *val = NULL; char *section_asked = "uwsgi"; char *colon; if (uwsgi_check_scheme(file)) { colon = uwsgi_get_last_char(file, '/'); colon = uwsgi_get_last_char(colon, ':'); } else { colon = uwsgi_get_last_char(file, ':'); } if (colon) { colon[0] = 0; if (colon[1] != 0) { section_asked = colon + 1; } } uwsgi_log("[uWSGI] getting YAML configuration from %s\n", file); yaml = uwsgi_open_and_read(file, &len, 1, magic_table); #ifdef UWSGI_LIBYAML yaml_parser_t parser; yaml_token_t token; int status = 0; int parsing = 1; if (!yaml_parser_initialize(&parser)) { uwsgi_log("unable to initialize YAML parser (libyaml)\n"); exit(1); } yaml_parser_set_input_string(&parser, (const unsigned char *) yaml, (size_t) len - 1); while (parsing) { if (!yaml_parser_scan(&parser, &token)) { uwsgi_log("error parsing YAML file: %s (%c)\n", parser.problem, yaml[parser.problem_offset]); exit(1); } switch (token.type) { case YAML_STREAM_END_TOKEN: parsing = 0; break; case YAML_KEY_TOKEN: status = 1; break; case YAML_VALUE_TOKEN: status = 2; break; case YAML_FLOW_SEQUENCE_START_TOKEN: case YAML_BLOCK_SEQUENCE_START_TOKEN: status = 3; break; case YAML_BLOCK_MAPPING_START_TOKEN: if (!in_uwsgi_section) { if (key) { if (!strcmp(section_asked, key)) { in_uwsgi_section = 1; } } } break; case YAML_BLOCK_END_TOKEN: if (in_uwsgi_section) { parsing = 0; break; } break; case YAML_SCALAR_TOKEN: case YAML_FLOW_ENTRY_TOKEN: case YAML_BLOCK_ENTRY_TOKEN: if (status == 1) { key = (char *) token.data.scalar.value; } else if (status == 2) { val = (char *) token.data.scalar.value; if (key && val && in_uwsgi_section) { add_exported_option(key, val, 0); } status = 0; } else if (status == 3) { val = (char *) token.data.scalar.value; if (key && val && in_uwsgi_section) { add_exported_option(key, val, 0); } } else { uwsgi_log("unsupported YAML token in %s block\n", section_asked); parsing = 0; break; } break; default: status = 0; } } #else int depth; int current_depth = 0; int lines = 1; char *yaml_line; char *section = ""; while (len) { yaml_line = yaml_get_line(yaml, len); if (yaml_line == NULL) { break; } lines++; // skip empty line if (yaml[0] == 0) goto next; depth = yaml_get_depth(yaml); if (depth <= current_depth) { current_depth = depth; // end the parsing cycle if (in_uwsgi_section) return; } else if (depth > current_depth && !in_uwsgi_section) { goto next; } key = yaml_lstrip(yaml); // skip empty line if (key[0] == 0) goto next; // skip list and {} defined dict if (key[0] == '-' || key[0] == '[' || key[0] == '{') { if (in_uwsgi_section) return; goto next; } if (!in_uwsgi_section) { section = strchr(key, ':'); if (!section) goto next; section[0] = 0; if (!strcmp(key, section_asked)) { in_uwsgi_section = 1; } } else { // get dict value val = strstr(key, ": "); if (!val) { val = strstr(key, ":\t"); } if (!val) return; // get the right key val[0] = 0; // yeah overengeneering.... yaml_rstrip(key); val = yaml_lstrip(val + 2); yaml_rstrip(val); //uwsgi_log("YAML: %s = %s\n", key, val); add_exported_option((char *) key, val, 0); } next: len -= (yaml_line - yaml); yaml += (yaml_line - yaml); } #endif }
void uwsgi_json_config(char *file, char *magic_table[]) { size_t len = 0; char *json_data; const char *key; json_t *root; json_error_t error; json_t *config; json_t *config_value, *config_array_item; void *config_iter; char *object_asked = "uwsgi"; char *colon; int i; if (uwsgi_check_scheme(file)) { colon = uwsgi_get_last_char(file, '/'); colon = uwsgi_get_last_char(colon, ':'); } else { colon = uwsgi_get_last_char(file, ':'); } if (colon) { colon[0] = 0; if (colon[1] != 0) { object_asked = colon + 1; } } uwsgi_log_initial("[uWSGI] getting JSON configuration from %s\n", file); json_data = uwsgi_open_and_read(file, &len, 1, magic_table); #ifdef JANSSON_MAJOR_VERSION root = json_loads(json_data, 0, &error); #else root = json_loads(json_data, &error); #endif if (!root) { uwsgi_log("error parsing JSON data: line %d %s\n", error.line, error.text); exit(1); } config = json_object_get(root, object_asked); if (!json_is_object(config)) { uwsgi_log("you must define a object named %s in your JSON data\n", object_asked); exit(1); } config_iter = json_object_iter(config); while (config_iter) { key = json_object_iter_key(config_iter); config_value = json_object_iter_value(config_iter); if (json_is_string(config_value)) { add_exported_option((char *) key, (char *) json_string_value(config_value), 0); } else if (json_is_true(config_value)) { add_exported_option((char *) key, strdup("1"), 0); } else if (json_is_false(config_value) || json_is_null(config_value)) { add_exported_option((char *) key, strdup("0"), 0); } else if (json_is_integer(config_value)) { add_exported_option((char *) key, uwsgi_num2str(json_integer_value(config_value)), 0); } else if (json_is_array(config_value)) { for (i = 0; i < (int) json_array_size(config_value); i++) { config_array_item = json_array_get(config_value, i); if (json_is_string(config_array_item)) { add_exported_option((char *) key, (char *) json_string_value(config_array_item), 0); } else if (json_is_true(config_array_item)) { add_exported_option((char *) key, strdup("1"), 0); } else if (json_is_false(config_array_item) || json_is_null(config_array_item)) { add_exported_option((char *) key, strdup("0"), 0); } else if (json_is_integer(config_array_item)) { add_exported_option((char *) key, uwsgi_num2str(json_integer_value(config_array_item)), 0); } } } config_iter = json_object_iter_next(config, config_iter); } if (colon) colon[0] = ':'; }