00001 #include "FWCore/ParameterSet/interface/PythonProcessDesc.h"
00002 #include "FWCore/ParameterSet/src/PythonModule.h"
00003 #include "FWCore/ParameterSet/src/PythonWrapper.h"
00004 #include <sstream>
00005 #include <iostream>
00006 #include <boost/foreach.hpp>
00007 using namespace boost::python;
00008
00009 bool PythonProcessDesc::initialized_ = false;
00010
00011 PythonProcessDesc::PythonProcessDesc()
00012 : theProcessPSet(),
00013 theServices(),
00014 theMainModule(),
00015 theMainNamespace()
00016 {
00017 }
00018
00019
00020 PythonProcessDesc::PythonProcessDesc(std::string const& config)
00021 : theProcessPSet(),
00022 theServices(),
00023 theMainModule(),
00024 theMainNamespace()
00025 {
00026 prepareToRead();
00027 read(config);
00028 Py_Finalize();
00029 }
00030
00031 PythonProcessDesc::PythonProcessDesc(std::string const& config, int argc, char * argv[])
00032 : theProcessPSet(),
00033 theServices(),
00034 theMainModule(),
00035 theMainNamespace()
00036 {
00037 prepareToRead();
00038 PySys_SetArgv(argc, argv);
00039 read(config);
00040 Py_Finalize();
00041 }
00042
00043
00044 void PythonProcessDesc::prepareToRead()
00045 {
00046 PyImport_AppendInittab( "libFWCoreParameterSet", &initlibFWCoreParameterSet );
00047 Py_Initialize();
00048 if(!initialized_)
00049 {
00050 PyImport_ImportModule("libFWCoreParameterSet");
00051 initialized_ = true;
00052 }
00053
00054 theMainModule = object(handle<>(borrowed(PyImport_AddModule("__main__"))));
00055
00056 theMainNamespace = theMainModule.attr("__dict__");
00057 theMainNamespace["processDesc"] = ptr(this);
00058 theMainNamespace["processPSet"] = ptr(&theProcessPSet);
00059 }
00060
00061
00062 void PythonProcessDesc::read(std::string const& config)
00063 {
00064 try {
00065
00066 if(config.substr(config.size()-3) == ".py")
00067 {
00068 readFile(config);
00069 }
00070 else
00071 {
00072 readString(config);
00073 }
00074 }
00075 catch( error_already_set ) {
00076 edm::pythonToCppException("Configuration");
00077 Py_Finalize();
00078 }
00079 }
00080
00081
00082 void PythonProcessDesc::readFile(std::string const& fileName)
00083 {
00084 std::string initCommand("import FWCore.ParameterSet.Config as cms\n"
00085 "execfile('");
00086 initCommand += fileName + "')";
00087
00088
00089 handle<>(PyRun_String(initCommand.c_str(),
00090 Py_file_input,
00091 theMainNamespace.ptr(),
00092 theMainNamespace.ptr()));
00093 std::string command("process.fillProcessDesc(processDesc, processPSet)");
00094 handle<>(PyRun_String(command.c_str(),
00095 Py_eval_input,
00096 theMainNamespace.ptr(),
00097 theMainNamespace.ptr()));
00098 }
00099
00100
00101 void PythonProcessDesc::readString(std::string const& pyConfig)
00102 {
00103 std::string command = pyConfig;
00104 command += "\nprocess.fillProcessDesc(processDesc, processPSet)";
00105 handle<>(PyRun_String(command.c_str(),
00106 Py_file_input,
00107 theMainNamespace.ptr(),
00108 theMainNamespace.ptr()));
00109 }
00110
00111
00112 boost::shared_ptr<edm::ProcessDesc> PythonProcessDesc::processDesc()
00113 {
00114 boost::shared_ptr<edm::ProcessDesc> result(new edm::ProcessDesc(theProcessPSet.pset()));
00115 BOOST_FOREACH(PythonParameterSet service, theServices)
00116 {
00117 result->addService(service.pset());
00118 }
00119 return result;
00120 }
00121
00122
00123 std::string PythonProcessDesc::dump() const
00124 {
00125 std::ostringstream os;
00126 os << theProcessPSet.dump();
00127 BOOST_FOREACH(PythonParameterSet service, theServices)
00128 {
00129 os << service.dump();
00130 }
00131 return os.str();
00132 }
00133
00134