CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/FWCore/PythonParameterSet/src/pythonFileToConfigure.cc

Go to the documentation of this file.
00001 /*
00002  *  pythonToConfigure.cpp
00003  *  CMSSW
00004  *
00005  *  Created by Chris Jones on 10/3/06.
00006  *
00007  */
00008 
00009 #include "FWCore/PythonParameterSet/interface/pythonFileToConfigure.h"
00010 #include "FWCore/PythonParameterSet/interface/BoostPython.h"
00011 #include "FWCore/PythonParameterSet/src/PythonWrapper.h"
00012 
00013 
00014 std::string edm::pythonFileToConfigure(const std::string& iPythonFileName)
00015 {
00016   std::string returnValue;
00017   std::string initCommand("import FWCore.ParameterSet.Config as cms\n"
00018                           "fileDict = dict()\n"
00019                           "execfile('");
00020   initCommand += iPythonFileName+"',fileDict)";
00021   
00022   Py_InitializeEx(0);
00023   using namespace boost::python;
00024   
00025   object main_module((
00026                       boost::python::handle<PyObject>(borrowed(PyImport_AddModule(const_cast<char *>("__main__"))))));
00027   object main_namespace = main_module.attr("__dict__");
00028   try {
00029     try {
00030       object result((boost::python::handle<>(PyRun_String(initCommand.c_str(),
00031                                                           Py_file_input,
00032                                                           main_namespace.ptr(),
00033                                                           main_namespace.ptr()))));
00034     } catch(error_already_set) {
00035       edm::pythonToCppException("Configuration");
00036     }
00037     try {
00038       std::string command("cms.findProcess(fileDict).dumpConfig()");
00039       object result((handle<>(PyRun_String(command.c_str(),
00040                                            Py_eval_input,
00041                                            main_namespace.ptr(),
00042                                            main_namespace.ptr()))));
00043       returnValue= extract<std::string>(result);
00044       
00045     }catch( error_already_set ) {
00046       edm::pythonToCppException("Configuration");
00047     }
00048   }catch(...) {
00049     Py_Finalize();
00050     throw;
00051   }
00052   Py_Finalize();
00053   return returnValue;
00054 }