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