CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
cond2xml.py
Go to the documentation of this file.
1 
2 import os
3 import shutil
4 import sys
5 import time
6 
7 # -------------------------------------------------------------------------------------------------------
8 
9 payload2xmlCodeTemplate = """
10 
11 #include <iostream>
12 #include <string>
13 #include <memory>
14 
15 #include <boost/python/class.hpp>
16 #include <boost/python/module.hpp>
17 #include <boost/python/init.hpp>
18 #include <boost/python/def.hpp>
19 #include <iostream>
20 #include <string>
21 #include <sstream>
22 
23 #include "boost/archive/xml_oarchive.hpp"
24 #include "CondFormats/Serialization/interface/Serializable.h"
25 #include "CondFormats/Serialization/interface/Archive.h"
26 
27 #include "CondCore/Utilities/src/CondFormats.h"
28 
29 namespace { // Avoid cluttering the global namespace.
30 
31  std::string payload2xml( const std::string &payloadData, const std::string &payloadType ) {
32 
33  // now to convert
34  std::unique_ptr< %(plType)s > payload;
35 
36  std::stringbuf sdataBuf;
37  sdataBuf.pubsetbuf( const_cast<char *> ( payloadData.c_str() ), payloadData.size() );
38 
39  std::istream inBuffer( &sdataBuf );
40  eos::portable_iarchive ia( inBuffer );
41  payload.reset( new %(plType)s );
42  ia >> (*payload);
43 
44  // now we have the object in memory, convert it to xml in a string and return it
45 
46  std::ostringstream outBuffer;
47  boost::archive::xml_oarchive xmlResult( outBuffer );
48  xmlResult << boost::serialization::make_nvp( "cmsCondPayload", *payload );
49 
50  return outBuffer.str();
51  }
52 
53 } // end namespace
54 
55 
56 BOOST_PYTHON_MODULE(%(mdName)s)
57 {
58  using namespace boost::python;
59  def ("payload2xml", payload2xml);
60 }
61 
62 """
63 
64 buildFileTemplate = """
65 <flags CXXFLAGS="-Wno-sign-compare -Wno-unused-variable -Os"/>
66 <use name="boost"/>
67 <use name="boost_python"/>
68 <use name="boost_iostreams"/>
69 <use name="boost_serialization"/>
70 <use name="boost_program_options"/>
71 <use name="CondCore/DBCommon"/>
72 <use name="CondCore/IOVService"/>
73 <use name="CondCore/MetaDataService"/>
74 <use name="CondCore/TagCollection"/>
75 <use name="CondCore/CondDB"/>
76 <use name="CondFormats/HLTObjects"/>
77 <use name="CondFormats/Alignment"/>
78 <use name="CondFormats/BeamSpotObjects"/>
79 <use name="CondFormats/CastorObjects"/>
80 <use name="CondFormats/HIObjects"/>
81 <use name="CondFormats/CSCObjects"/>
82 <use name="CondFormats/DTObjects"/>
83 <use name="CondFormats/ESObjects"/>
84 <use name="CondFormats/EcalObjects"/>
85 <use name="CondFormats/EgammaObjects"/>
86 <use name="CondFormats/Luminosity"/>
87 <use name="CondFormats/HcalObjects"/>
88 <use name="CondFormats/JetMETObjects"/>
89 <use name="CondFormats/L1TObjects"/>
90 <use name="CondFormats/PhysicsToolsObjects"/>
91 <use name="CondFormats/GeometryObjects"/>
92 <use name="CondFormats/RecoMuonObjects"/>
93 <use name="CondFormats/RPCObjects"/>
94 <use name="CondFormats/RunInfo"/>
95 <use name="CondFormats/SiPixelObjects"/>
96 <use name="CondFormats/SiStripObjects"/>
97 <use name="CondFormats/Common"/>
98 <use name="CondFormats/BTauObjects"/>
99 <use name="CondFormats/MFObjects"/>
100 <export>
101  <lib name="1"/>
102 </export>
103 """
104 
106 
107  def __init__(self, condDBIn):
108  self.conddb = condDBIn
109  self._pl2xml_isPrepared = False
110  self._pl2xml_tmpDir = "fakeSubSys4pl/fakePkg4pl"
111  self.doCleanup = True
112 
113  def __del__(self):
114 
115  if self.doCleanup:
116  shutil.rmtree( self._pl2xml_tmpDir.split('/')[0] )
117  os.unlink('./pl2xmlComp.so')
118  return
119 
120  def prepPayload2xml(self, session, payload):
121 
122  startTime = time.time()
123 
124  # get payload from DB:
125  result = session.query(self.conddb.Payload.data, self.conddb.Payload.object_type).filter(self.conddb.Payload.hash == payload).one()
126  data, plType = result
127 
128  info = { "mdName" : "pl2xmlComp",
129  'plType' : plType,
130  }
131 
132  code = payload2xmlCodeTemplate % info
133 
134  tmpDir = self._pl2xml_tmpDir
135  if ( os.path.exists( tmpDir.split('/')[0] ) or
136  os.path.exists( tmpDir ) ) :
137  msg = '\nERROR: %s already exists, please remove if you did not create that manually !!' % tmpDir
138  self.doCleanup = False
139  raise Exception(msg)
140 
141  os.makedirs( tmpDir+'/src' )
142 
143  buildFileName = "%s/BuildFile.xml" % (tmpDir,)
144  with open(buildFileName, 'w') as buildFile:
145  buildFile.write( buildFileTemplate )
146  buildFile.close()
147 
148  tmpFileName = "%s/src/%s" % (tmpDir, info['mdName'],)
149  with open(tmpFileName+'.cpp', 'w') as codeFile:
150  codeFile.write(code)
151  codeFile.close()
152 
153  cmd = "source /afs/cern.ch/cms/cmsset_default.sh;"
154  cmd += "(cd %s ; scram b 2>&1 >build.log && cp %s/tmp/%s/src/%s/src/%s/lib%s.so ../../pl2xmlComp.so )" % (tmpDir, os.environ["CMSSW_BASE"], os.environ["SCRAM_ARCH"], tmpDir, tmpDir.replace('/',''), tmpDir.replace('/','') )
155  ret = os.system(cmd)
156  if ret != 0 : self.doCleanup = False
157 
158  buildTime = time.time()-startTime
159  print >> sys.stderr, "buillding done in ", buildTime, 'sec., return code from build: ', ret
160 
161  return (ret == 0)
162 
163  def payload2xml(self, session, payload):
164 
165  if not self._pl2xml_isPrepared:
166  if not self.prepPayload2xml(session, payload):
167  msg = "Error preparing code for "+payload
168  raise Exception(msg)
169  self._pl2xml_isPrepared = True
170 
171  # get payload from DB:
172  result = session.query(self.conddb.Payload.data, self.conddb.Payload.object_type).filter(self.conddb.Payload.hash == payload).one()
173  data, plType = result
174 
175  sys.path.append('.')
176  import pl2xmlComp
177  resultXML = pl2xmlComp.payload2xml( data, plType )
178  print resultXML
179 
list object
Definition: dbtoconf.py:77