void Python::initpaths()
{
    /*
     *  char pwd[2048];
     *  getcwd (pwd,2047);
     *  pwd[2047]='\0';
     *  for (int i=0;pwd[i]!='\0';i++) {
     *       if (pwd[i]=='\\')
     *               pwd[i]=DELIM;
     *  }
     */
    std::string moduledir( vs_config->getVariable( "data", "python_modules", "modules" ) );
    std::string basesdir( vs_config->getVariable( "data", "python_bases", "bases" ) );

    /*
     *  std::string changepath ("import sys\nprint sys.path\nsys.path = ["
     *                       "\""+std::string(pwd)+DELIMSTR"modules"DELIMSTR"builtin\""
     *                       ",\""+std::string(pwd)+DELIMSTR+moduledir+string("\"")+
     *                       ",\""+std::string(pwd)+DELIMSTR+basesdir + string("\"")+
     *                       "]\n");
     */
    std::string modpaths( "\"\"," );
    //Find all the mods dir (ignore homedir)
    for (unsigned int i = 1; i < VSFileSystem::Rootdir.size(); i++) {
        modpaths += "r\""+VSFileSystem::Rootdir[i]+PATHSEP+moduledir+PATHSEP "builtin\",";
        modpaths += "r\""+VSFileSystem::Rootdir[i]+PATHSEP+moduledir+PATHSEP "quests\",";
        modpaths += "r\""+VSFileSystem::Rootdir[i]+PATHSEP+moduledir+PATHSEP "missions\",";
        modpaths += "r\""+VSFileSystem::Rootdir[i]+PATHSEP+moduledir+PATHSEP "ai\",";
        modpaths += "r\""+VSFileSystem::Rootdir[i]+PATHSEP+moduledir+"\",";
        modpaths += "r\""+VSFileSystem::Rootdir[i]+PATHSEP+basesdir+"\"";
        if ( i+1 < VSFileSystem::Rootdir.size() )
            modpaths += ",";
    }
    /*
     *  string::size_type backslash;
     *  while ((backslash=modpaths.find("\\"))!=std::string::npos) {
     *  modpaths[backslash]='/';
     *  }*/
    std::string changepath( "import sys\nprint(sys.path)\nsys.path = ["+modpaths+"] + sys.path\n" );
    /*
     *  std::string changepath ("import sys\nprint sys.path\nsys.path = ["
     *                       "\""+VSFileSystem::datadir+DELIMSTR"modules"DELIMSTR"builtin\""
     *                       ",\""+VSFileSystem::datadir+DELIMSTR+moduledir+string("\"")+
     *                       ",\""+VSFileSystem::datadir+DELIMSTR+basesdir + string("\"")+
     *                       "]\n");
     */
    VSFileSystem::vs_fprintf( stderr, "running %s", changepath.c_str() );
    char *temppython = strdup( changepath.c_str() );
    PyRun_SimpleString( temppython );
    Python::reseterrors();
    free( temppython );
}
Beispiel #2
0
    const fs::path&
    module_runtime_path(const std::string& dtype)
    {
      path_map& paths(module_paths());
      path_map::iterator ipath(paths.find(dtype));

      // Is this a valid dtype?
      if (ipath == paths.end())
        {
          boost::format fmt("Invalid runtime path type “%1%”");
          fmt % dtype;
          throw std::logic_error(fmt.str());
        }

      Module& module = ipath->second;

      // Return cached result if previously determined.
      if(!module.realpath.empty())
        return module.realpath;

      // dtype set explicitly in environment.
      if (getenv(module.envvar.c_str()))
        {
          fs::path dir(getenv(module.envvar.c_str()));
          if (validate_path(dir))
            {
              module.realpath = boost::filesystem::canonical(dir);
              return module.realpath;
            }
        }

      // Full module path in environment + relative component
      if (getenv(module.module_envvar.c_str()))
        {
          fs::path home(getenv(module.module_envvar.c_str()));
          home /= module.relpath;
          if (validate_path(home))
            {
              module.realpath = boost::filesystem::canonical(home);
              return module.realpath;
            }
        }

      // Full root path in environment + relative component
      if (getenv(module.root_envvar.c_str()))
        {
          fs::path home(getenv(module.root_envvar.c_str()));
          home /= module.relpath;
          if (validate_path(home))
            {
              module.realpath = boost::filesystem::canonical(home);
              return module.realpath;
            }
        }

      // Full prefix is available only when configured explicitly.
      if (validate_path(module.install_prefix))
        {
          // Full specific path.
          if (validate_path(module.abspath))
            {
              module.realpath = boost::filesystem::canonical(module.abspath);
              return module.realpath;
            }

          // Full root path + relative component
          fs::path home(module.install_prefix);
          home /= module.relpath;
          if (validate_path(home))
            {
              module.realpath = boost::filesystem::canonical(home);
              return module.realpath;
            }
        }
      else
        {
          fs::path module_lib_path;
          if (module.module_path)
            {
              module_lib_path = module.module_path();
            }
          if (module_lib_path.has_parent_path())
            {
              fs::path moduledir(module_lib_path.parent_path());
              bool match = true;

              fs::path libdir(module.shlibpath);

              while(!libdir.empty())
                {
                  if (libdir.filename() == moduledir.filename())
                    {
                      libdir = libdir.parent_path();
                      moduledir = moduledir.parent_path();
                    }
                  else
                    {
                      match = false;
                      break;
                    }
                }
              if (match && validate_path(moduledir))
                {
                  moduledir /= module.relpath;
                  if (validate_path(moduledir))
                    {
                      module.realpath = boost::filesystem::canonical(moduledir);
                      return module.realpath;
                    }
                }
            }
        }
      boost::format fmt("Could not determine Bio-Formats runtime path for “%1%” directory");
      fmt % dtype;
      throw std::runtime_error(fmt.str());
    }