]> jfr.im git - irc/evilnet/x3.git/commitdiff
mod-python: generalised the setting of the PYTHONPATH environment variable
authorhstuart <redacted>
Wed, 6 May 2009 19:57:46 +0000 (19:57 +0000)
committerhstuart <redacted>
Wed, 6 May 2009 19:57:46 +0000 (19:57 +0000)
ChangeLog
src/mod-python.c

index 104334b6c29f1f341cd762e7698473f5f5d86425..b50c5722275c064f870a8154c8670513e81a1fb0 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,11 @@
 /***********************************************************************
 X3 ChangeLog
 
+2009-05-06  Henrik Stuart  <evilnet@hstuart.dk>
+
+       * src/mod-python.c: generalised PYTHONPATH environment setting to
+       not explicitly set Alex's home directory.
+
 2009-05-05  Henrik Stuart  <evilnet@hstuart.dk>
 
        * src/proto-p10.c: introduced irc_sno for SNO messages. Removed broken
index ca3344c73cbf48041211eabc34d74f216ad67f7e..ffa3c8a216752354f5e8f9441f58bdb383de19c6 100644 (file)
@@ -51,6 +51,13 @@ static const struct message_entry msgtab[] = {
     { NULL, NULL } /* sentenal */
 };
 
+#define MODPYTHON_CONF_NAME "modules/python"
+
+static
+struct {
+    char const* scripts_dir;
+} modpython_conf;
+
 static struct log_type *PY_LOG;
 const char *python_module_deps[] = { NULL };
 static struct module *python_module;
@@ -676,8 +683,22 @@ int python_load() {
        This is called during x3 startup, and on a python reload
     */
     PyObject *pName;
+    char* buffer;
+    char* env = getenv("PYTHONPATH");
+
+    if (env)
+        env = strdup(env);
+
+    if (!env)
+        setenv("PYTHONPATH", modpython_conf.scripts_dir, 1);
+    else if (!strstr(env, modpython_conf.scripts_dir)) {
+        buffer = (char*)malloc(strlen(env) + strlen(modpython_conf.scripts_dir) + 2);
+        sprintf(buffer, "%s:%s", modpython_conf.scripts_dir, env);
+        setenv("PYTHONPATH", buffer, 1);
+        free(buffer);
+        free(env);
+    }
 
-    setenv("PYTHONPATH", "/home/rubin/afternet/services/x3/x3-run/", 1);
     Py_Initialize();
     Py_InitModule("svc", EmbMethods);
     /* TODO: get "modpython" from x3.conf */
@@ -773,6 +794,19 @@ static MODCMD_FUNC(cmd_command) {
     return 1;
 }
 
+static void modpython_conf_read(void) {
+    dict_t conf_node;
+    char const* str;
+
+    if (!(conf_node = conf_get_data(MODPYTHON_CONF_NAME, RECDB_OBJECT))) {
+        log_module(PY_LOG, LOG_ERROR, "config node '%s' is missing or has wrong type", MODPYTHON_CONF_NAME);
+        return;
+    }
+
+    str = database_get_data(conf_node, "scripts_dir", RECDB_QSTRING);
+    modpython_conf.scripts_dir = str ? str : "./";
+}
+
 int python_init(void) {
     /* X3 calls this function on init of the module during startup. We use it to
        do all our setup tasks and bindings 
@@ -780,6 +814,8 @@ int python_init(void) {
 
     PY_LOG = log_register_type("Python", "file:python.log");
     python_module = module_register("python", PY_LOG, "mod-python.help", NULL);
+    conf_register_reload(modpython_conf_read);
+
     log_module(PY_LOG, LOG_INFO, "python module init");
     message_register_table(msgtab);