// src/backend/pyloader/python_policy_manager.cc - Copyright 2005, 2006, University // of Padova, dept. of Pure and Applied // Mathematics // // This file is part of SGPEMv2. // // This is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // SGPEMv2 is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with SGPEMv2; if not, write to the Free Software // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include "python_policy_manager.hh" #include "global_preferences.hh" #include "policies_gatekeeper.hh" #include #include #include #include #include #include #include #include #include #include #include #include using namespace sgpem; using std::vector; using std::cout; using std::endl; // Concatenate a string with all the policies directories struct pol_dirs_concat : public std::unary_function { public: pol_dirs_concat(Glib::ustring& cat) : _cat(cat) {} void operator()(const Glib::ustring& add) { // Please note that this string will end finishing with // and additional ","! _cat += "'" + add + "', "; } private: Glib::ustring& _cat; }; //static object PythonPolicyManager* PythonPolicyManager::_instance = NULL; PythonPolicyManager::PythonPolicyManager() : _initialized(false) { PyEval_InitThreads(); } PythonPolicyManager::~PythonPolicyManager() { for_each(_policies.begin(), _policies.end(), ptr_fun(operator delete)); } PythonPolicyManager* const PythonPolicyManager::get_instance() { if(!_instance) _instance = new PythonPolicyManager(); return _instance; } // Policy& // PythonPolicyManager::get_policy() // { // // FIXME : assumes that _python_policy is always != NULL! // return *_python_policy; // } void PythonPolicyManager::init() { if(_initialized) // No-op return; Py_Initialize(); _initialized = true; // The following lines are ugly, but necessary if we use // non-standard installation directories. Theoretically, // it should be up to the user to set correct // environment variables. // FIXME: find better way to achieve this. Glib::ustring importdirs = "import sys\n" "sys.path[:0] = [ "; for_each(GlobalPreferences::instance().policies_dir_begin(), GlobalPreferences::instance().policies_dir_end(), pol_dirs_concat(importdirs)); importdirs += " '" SHAREDIR "' ]\n"; PyRun_SimpleString(importdirs.c_str()); // Okay, here we go. // Black magic at work. collect_policies(); } vector PythonPolicyManager::get_avail_policies() { return _policies; } void PythonPolicyManager::collect_policies() { GlobalPreferences::dir_iterator dir_it = GlobalPreferences::instance().policies_dir_begin(); GlobalPreferences::dir_iterator dir_end = GlobalPreferences::instance().policies_dir_end(); for(; dir_it != dir_end; ++dir_it) { Glib::Dir dir(dir_it->c_str()); cout << "Opening directory " << *dir_it << "..." << endl; for(Glib::DirIterator file_it = dir.begin(); file_it != dir.end(); ++file_it) { cout << "\tChecking if " << *file_it << " is a Python script... " << endl; Glib::PatternSpec dot_py("*.py"); if(dot_py.match(*file_it)) { cout << "\t\tIt is.\n"; //strip extension std::string policy_name = (*file_it).substr(0, (*file_it).size() - 3); PythonPolicy *pypolicy = new PythonPolicy(policy_name.c_str()); _policies.push_back(pypolicy); //FIXME remove me when get_policy is dropped if(policy_name == "fcfs") { _python_policy = pypolicy; PoliciesGatekeeper::get_instance().activate_policy(&History::get_instance(), pypolicy); } } } } }