CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/CondCore/Utilities/src/CondPyInterface.cc

Go to the documentation of this file.
00001 #include "CondCore/Utilities/interface/CondPyInterface.h"
00002 
00003 #include <exception>
00004 #include <iostream>
00005 #include <iomanip>
00006 #include <fstream>
00007 #include <string>
00008 #include <vector>
00009 #include "FWCore/Utilities/interface/Exception.h"
00010 
00011 #include "FWCore/PluginManager/interface/ProblemTracker.h"
00012 #include "FWCore/Utilities/interface/Presence.h"
00013 #include "FWCore/PluginManager/interface/PresenceFactory.h"
00014 #include "FWCore/ServiceRegistry/interface/Service.h"
00015 
00016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00017 
00018 
00019 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00020 #include "FWCore/PluginManager/interface/PluginManager.h"
00021 #include "FWCore/PluginManager/interface/standard.h"
00022 //#include "FWCore/PluginManager/interface/PluginCapabilities.h"
00023 #include "FWCore/PluginManager/interface/PluginFactory.h"
00024 
00025 #include "CondCore/DBCommon/interface/Auth.h"
00026 #include "CondCore/DBCommon/interface/DbTransaction.h"
00027 #include "CondCore/DBCommon/interface/Exception.h"
00028 #include "CondCore/DBCommon/interface/FipProtocolParser.h"
00029 #include "CondCore/DBCommon/interface/ClassID.h"
00030 #include "CondCore/MetaDataService/interface/MetaData.h"
00031 #include "CondCore/DBCommon/interface/Logger.h"
00032 #include "CondCore/TagCollection/interface/TagCollectionRetriever.h"
00033 
00034 #include <iterator>
00035 #include <iostream>
00036 #include <sstream>
00037 
00038 
00039 namespace cond {
00040   
00041   static void topinit(){
00042     if(!edmplugin::PluginManager::isAvailable())
00043       edmplugin::PluginManager::configure(edmplugin::standard::config());
00044     return;
00045   }
00046 
00047   namespace impl {
00048     struct FWMagic {
00049       // A.  Instantiate a plug-in manager first.
00050       edm::AssertHandler ah;
00051       boost::shared_ptr<edm::ServiceRegistry::Operate> operate;
00052     };
00053   }
00054 
00055   FWIncantation::~FWIncantation(){}
00056   
00057 
00058   FWIncantation::FWIncantation() : magic(new impl::FWMagic) {
00059     topinit();
00060     // B.  Load the message service plug-in.  Forget this and bad things happen!
00061     //     In particular, the job hangs as soon as the output buffer fills up.
00062     //     That's because, without the message service, there is no mechanism for
00063     //     emptying the buffers.
00064     boost::shared_ptr<edm::Presence> theMessageServicePresence;
00065     theMessageServicePresence = boost::shared_ptr<edm::Presence>(edm::PresenceFactory::get()->
00066                                                                  makePresence("MessageServicePresence").release());
00067     
00068     // C.  Manufacture a configuration and establish it.
00069 
00070     /*
00071     std::string config =
00072       "process x = {"
00073       "service = MessageLogger {"
00074       "untracked vstring destinations = {'infos.mlog','warnings.mlog'}"
00075       "untracked PSet infos = {"
00076       "untracked string threshold = 'INFO'"
00077       "untracked PSet default = {untracked int32 limit = 1000000}"
00078       "untracked PSet FwkJob = {untracked int32 limit = 0}"
00079       "}"
00080       "untracked PSet warnings = {"
00081       "untracked string threshold = 'WARNING'"
00082       "untracked PSet default = {untracked int32 limit = 1000000}"
00083       "}"
00084       "untracked vstring fwkJobReports = {'FrameworkJobReport.xml'}"
00085       "untracked vstring categories = {'FwkJob'}"
00086       "untracked PSet FrameworkJobReport.xml = {"
00087       "untracked PSet default = {untracked int32 limit = 0}"
00088       "untracked PSet FwkJob = {untracked int32 limit = 10000000}"
00089       "}"
00090       "}"
00091       "service = JobReportService{}"
00092       "service = SiteLocalConfigService{}"
00093       "}";
00094     */
00095     /*
00096     std::string config =
00097       "import FWCore.ParameterSet.Config as cms\n"
00098       "process = cms.Process('x')\n"
00099       "JobReportService = cms.Service('JobReportService')\n"
00100       "SiteLocalConfigService = cms.Service('SiteLocalConfigService')\n"
00101       ;
00102     */
00103     
00104     boost::shared_ptr<std::vector<edm::ParameterSet> > psets(new std::vector<edm::ParameterSet>);
00105     edm::ParameterSet pSet;
00106     pSet.addParameter("@service_type",std::string("SiteLocalConfigService"));
00107     psets->push_back(pSet);
00108     
00109     // D.  Create the services.
00110     edm::ServiceToken tempToken(edm::ServiceRegistry::createSet(*psets.get()));
00111     
00112     // E.  Make the services available.
00113     magic->operate.reset(new edm::ServiceRegistry::Operate(tempToken));
00114     
00115   }
00116   
00117   //------------------------------------------------------------
00118 
00119 
00120   CondDB::CondDB() : me(){
00121     //topinit();    
00122   }
00123   CondDB::CondDB(cond::DbSession& session, boost::shared_ptr<cond::Logger> ilog) :
00124     me(session), logger(ilog) {
00125     //topinit();
00126   }
00127 
00128   // move ownership....
00129   CondDB::CondDB(const CondDB & other) : me(other.me), logger(other.logger) {
00130   }
00131 
00132   CondDB & CondDB::operator=(const CondDB & other) {
00133     if (this==&other) return *this; // unless =0 this is an error condition!
00134     me = other.me;
00135     logger = other.logger;
00136     return *this;
00137   }
00138 
00139   CondDB::~CondDB() {
00140   }
00141   
00142 
00143   std::string CondDB::allTags() const {
00144     std::ostringstream ss;
00145 
00146     cond::MetaData metadata_svc(me);
00147     std::vector<std::string> alltags;
00148     metadata_svc.listAllTags(alltags);
00149     
00150     std::copy (alltags.begin(),
00151                alltags.end(),
00152                std::ostream_iterator<std::string>(ss," ")
00153                );
00154     return ss.str();
00155   }
00156 
00157   std::string CondDB::iovToken(std::string const & tag) const {
00158     cond::MetaData metadata_svc(me);
00159     std::string token=metadata_svc.getToken(tag);
00160     return token;
00161   }
00162   
00163   // fix commit problem....
00164   IOVProxy CondDB::iov(std::string const & tag) const {
00165     return IOVProxy(me,iovToken(tag));
00166   }
00167   
00168   IOVProxy CondDB::iovWithLib(std::string const & tag) const {
00169     return IOVProxy(me,iovToken(tag));
00170   }
00171 
00172   IOVElementProxy CondDB::payLoad(std::string const & token) const {
00174     return IOVElementProxy(0,0,token);
00175   }
00176 
00177 
00178   cond::LogDBEntry CondDB::lastLogEntry(std::string const & tag) const {
00179     cond::LogDBEntry entry;
00180     if (logger)
00181       logger->LookupLastEntryByTag(tag,entry,false);
00182     return entry;
00183   }
00184 
00185   cond::LogDBEntry CondDB::lastLogEntryOK(std::string const & tag) const{
00186     cond::LogDBEntry entry;
00187     if (logger)
00188       logger->LookupLastEntryByTag(tag,entry,true);
00189     return entry;
00190   }
00191 
00192   void CondDB::startTransaction() const {
00193     me.transaction().start();
00194   }
00195   void CondDB::startReadOnlyTransaction() const {
00196     me.transaction().start(true);
00197   }
00198   void CondDB::commitTransaction() const {
00199     me.transaction().commit();
00200   }
00201 
00202   void CondDB::closeSession() const {
00203     //FIXME: does the session disconnection fix the socket failure in FroNTier (bug #84265)?
00204     me.close();
00205   }
00206   
00207   RDBMS::RDBMS() : connection(new DbConnection) {
00208     //topinit();
00209     connection->configure( cond::CmsDefaults );
00210   }
00211   RDBMS::~RDBMS() {}
00212 
00213   RDBMS::RDBMS(std::string const & authPath,  bool debug) : connection(new DbConnection) {
00214     //topinit();
00215     connection->configuration().setAuthenticationPath(authPath);
00216     if (debug) 
00217       connection->configuration().setMessageLevel( coral::Debug );
00218     else
00219       connection->configuration().setMessageLevel( coral::Error );
00220     connection->configuration().setPoolAutomaticCleanUp( false );
00221     connection->configuration().setConnectionTimeOut(0);
00222 
00223     connection->configure();
00224   }
00225   
00226   RDBMS::RDBMS(std::string const & user,std::string const & pass) : connection(new DbConnection) {
00227     //topinit();
00228     std::string userenv(std::string("CORAL_AUTH_USER=")+user);
00229     std::string passenv(std::string("CORAL_AUTH_PASSWORD=")+pass);
00230     ::putenv(const_cast<char*>(userenv.c_str()));
00231     ::putenv(const_cast<char*>(passenv.c_str()));
00232     connection->configuration().setMessageLevel( coral::Error );
00233     connection->configuration().setPoolAutomaticCleanUp( false );
00234     connection->configuration().setConnectionTimeOut(0);
00235     connection->configure();
00236   }
00237 
00238   void RDBMS::setLogger(std::string const & connstr) {
00239     DbSession loggerSession = connection->createSession();
00240     logger.reset(new cond::Logger(loggerSession));
00241     logger->connect( connstr, true );
00242   }
00243 
00244 
00245   
00246   CondDB RDBMS::getDB(std::string const & db) {
00247     DbSession dbSession = connection->createSession();
00248     dbSession.open( db, cond::Auth::COND_WRITER_ROLE );
00249     return CondDB(dbSession,logger);
00250   }
00251 
00252   CondDB RDBMS::getReadOnlyDB(std::string const & db) {
00253     DbSession dbSession = connection->createSession();
00254     dbSession.open( db, cond::Auth::COND_READER_ROLE, true );
00255     return CondDB(dbSession,logger);
00256   }
00257   
00258   GlobalTag const &  RDBMS::globalTag(std::string const & connstr, 
00259                                       std::string const & gname,
00260                                       std::string const & prefix, 
00261                                       std::string const & postfix) const {
00262     DbSession session = connection->createSession();
00263     session.open( connstr, true );
00264     session.transaction().start( true );
00265     TagCollectionRetriever gtr(session, prefix,postfix);
00266     const_cast<GlobalTag&>(m_globalTag).clear();
00267     gtr.getTagCollection(gname,const_cast<GlobalTag&>(m_globalTag));
00268     session.transaction().commit();
00269     return m_globalTag;  
00270   }
00271 
00272 
00273 }