Back to index

nordugrid-arc-nox  1.1.0~rc6
arc_init.cpp
Go to the documentation of this file.
00001 #include <cstdlib>
00002 #include <dlfcn.h>
00003 
00004 #include <string>
00005 
00006 #include <glibmm/module.h>
00007 #include <glibmm/miscutils.h>
00008 #include <arc/ArcLocation.h>
00009 #include <arc/Utils.h>
00010 #include <arc/Logger.h>
00011 
00012 #include <Python.h>
00013 
00014 
00015 __attribute__((constructor)) void __arc_init(void) {
00016 
00017 /*
00018 char* Py_GetPath ()
00019 Return the default module search path; this is computed from the program
00020 name (set by Py_SetProgramName() above) and some environment variables. The
00021 returned string consists of a series of directory names separated by a
00022 platform dependent delimiter character. The delimiter character is ":" on
00023 Unix, ";" on DOS/Windows, and "\n" (the ASCII newline character) on
00024 Macintosh. The returned string points into static storage; the caller should
00025 not modify its value. The value is available to Python code as the list
00026 sys.path, which may be modified to change the future search path for loaded
00027 modules.
00028 Note: it seems like Python is hiding site-packages part of path. Maybe it 
00029 is hardcoded inside Python somewhere. But at least part till site-packages
00030 seems to be present.
00031 */
00032   std::string pythonpath = Py_GetPath();
00033   std::string::size_type start = 0;
00034   std::string::size_type end = pythonpath.find_first_of(";:\n");
00035   if(end == std::string::npos) end=pythonpath.length();
00036   for(;start<pythonpath.length();) {
00037     std::string path;
00038     std::string modulepath;
00039     Glib::Module *module = NULL;
00040     path = pythonpath.substr(start,end-start);
00041     modulepath = Glib::build_filename(path,std::string("_arc.")+G_MODULE_SUFFIX);
00042 #ifdef HAVE_GLIBMM_BIND_LOCAL
00043     module = new Glib::Module(modulepath,Glib::MODULE_BIND_GLOBAL);
00044 #else
00045     module = new Glib::Module(modulepath);
00046 #endif
00047     if(module != NULL) {
00048       if(*module) return;
00049       delete module;
00050     };
00051     path = Glib::build_filename(path,"site-packages");
00052     modulepath = Glib::build_filename(path,std::string("_arc.")+G_MODULE_SUFFIX);
00053 #ifdef HAVE_GLIBMM_BIND_LOCAL
00054     module = new Glib::Module(modulepath,Glib::MODULE_BIND_GLOBAL);
00055 #else
00056     module = new Glib::Module(modulepath);
00057 #endif
00058     if(module != NULL) {
00059       if(*module) return;
00060       delete module;
00061     };
00062     path = Glib::build_filename(path,"site-packages");
00063     start=end+1;
00064     end=pythonpath.find_first_of(";:\n",start);
00065     if(end == std::string::npos) end=pythonpath.length();
00066   };
00067   Arc::Logger::getRootLogger().msg(Arc::WARNING,"Failed to export symbols of ARC python module");
00068 }
00069