CMS 3D CMS Logo

cond2xml.py
Go to the documentation of this file.
1 from __future__ import print_function
2 
3 import os
4 import shutil
5 import sys
6 import time
7 import glob
8 import importlib
9 import logging
10 import subprocess
11 
12 # as we need to load the shared lib from here, make sure it's in our path:
13 if os.path.join( os.environ['CMSSW_BASE'], 'src') not in sys.path:
14  sys.path.append( os.path.join( os.environ['CMSSW_BASE'], 'src') )
15 
16 # -------------------------------------------------------------------------------------------------------
17 
18 payload2xmlCodeTemplate = """
19 
20 #include "CondCore/Utilities/interface/Payload2XMLModule.h"
21 #include "CondCore/Utilities/src/CondFormats.h"
22 
23 PAYLOAD_2XML_MODULE( %s ){
24  PAYLOAD_2XML_CLASS( %s );
25 }
26 
27 """
28 
29 buildFileTemplate = """
30 <flags CXXFLAGS="-Wno-sign-compare -Wno-unused-variable -Os"/>
31 <library file="%s" name="%s">
32  <use name="CondCore/Utilities"/>
33  <use name="boost_python"/>
34 </library>
35 <export>
36  <lib name="1"/>
37 </export>
38 """
39 
40 # helper function
41 def sanitize(typeName):
42  return typeName.replace(' ','').replace('<','_').replace('>','')
43 
44 def localLibName( payloadType ):
45  # required to avoid ( unlikely ) clashes between lib names from templates and lib names from classes
46  prefix = ''
47  if '<' in payloadType and '>' in payloadType:
48  prefix = 't'
49  return "%s_%spayload2xml" %(sanitize(payloadType),prefix)
50 
52 
53  def __init__(self, condDBIn):
54  self.conddb = condDBIn
55 
56  if not os.path.exists( os.path.join( os.environ['CMSSW_BASE'], 'src') ):
57  raise Exception("Looks like you are not running in a CMSSW developer area, $CMSSW_BASE/src/ does not exist")
58 
59  self.fakePkgName = "fakeSubSys4pl/fakePkg4pl"
60  self._pl2xml_tmpDir = os.path.join( os.environ['CMSSW_BASE'], 'src', self.fakePkgName )
61 
62  self.doCleanup = False
63 
64  def __del__(self):
65 
66  if self.doCleanup:
67  shutil.rmtree( '/'.join( self._pl2xml_tmpDir.split('/')[:-1] ) )
68  return
69 
70  def discover(self, payloadType):
71 
72  libName = 'pluginUtilities_payload2xml.so'
73  # first search: developer area or main release
74  libDir = os.path.join( os.environ["CMSSW_BASE"], 'lib', os.environ["SCRAM_ARCH"] )
75  devLibDir = libDir
76  libPath = os.path.join( devLibDir, libName )
77  releaseBase = os.environ["CMSSW_RELEASE_BASE"]
78  devCheckout = (releaseBase != '')
79  if not devCheckout:
80  logging.debug('Looks like the current working environment is a read-only release')
81  if not os.path.exists( libPath ) and devCheckout:
82  # main release ( for dev checkouts )
83  libDir = os.path.join( releaseBase, 'lib', os.environ["SCRAM_ARCH"] )
84  libPath = os.path.join( libDir, libName )
85  if not os.path.exists( libPath ):
86  if "CMSSW_FULL_RELEASE_BASE" in os.environ:
87  libDir = os.path.join( os.environ["CMSSW_FULL_RELEASE_BASE"], 'lib', os.environ["SCRAM_ARCH"] )
88  libPath = os.path.join( libDir, libName )
89  if not os.path.exists( libPath ):
90  # it should never happen!
91  raise Exception('No built-in library %s found with XML converters.' %libPath)
92  logging.debug("Importing built-in library %s" %libPath)
93  module = importlib.import_module( libName.replace('.so', '') )
94  functors = dir(module)
95  funcName = payloadType+'2xml'
96  if funcName in functors:
97  logging.info('XML converter for payload class %s found in the built-in library.' %payloadType)
98  return getattr( module, funcName)
99  if not devCheckout:
100  # give-up if it is a read-only release...
101  raise Exception('No XML converter suitable for payload class %s has been found in the built-in library.')
102  libName = 'plugin%s.so' %localLibName( payloadType )
103  libPath = os.path.join( devLibDir, libName )
104  if os.path.exists( libPath ):
105  logging.info('Found local library with XML converter for class %s' %payloadType )
106  module = importlib.import_module( libName.replace('.so', '') )
107  return getattr( module, funcName)
108  logging.warning('No XML converter for payload class %s found in the built-in library.' %payloadType)
109  return None
110 
111  def prepPayload2xml(self, payloadType):
112 
113  converter = self.discover(payloadType)
114  if converter: return converter
115 
116  #otherwise, go for the code generation in the local checkout area.
117  startTime = time.time()
118 
119  libName = localLibName( payloadType )
120  pluginName = 'plugin%s' % libName
121  tmpLibName = "Tmp_payload2xml"
122  tmpPluginName = 'plugin%s' %tmpLibName
123 
124  libDir = os.path.join( os.environ["CMSSW_BASE"], 'lib', os.environ["SCRAM_ARCH"] )
125  tmpLibFile = os.path.join( libDir,tmpPluginName+'.so' )
126  code = payload2xmlCodeTemplate %(pluginName,payloadType)
127 
128  tmpSrcFileName = 'Local_2XML.cpp'
129  tmpDir = self._pl2xml_tmpDir
130  if ( os.path.exists( tmpDir ) ) :
131  msg = '\nERROR: %s already exists, please remove if you did not create that manually !!' % tmpDir
132  raise Exception(msg)
133 
134  logging.debug('Creating temporary package %s' %self._pl2xml_tmpDir)
135  os.makedirs( tmpDir+'/plugins' )
136 
137  buildFileName = "%s/plugins/BuildFile.xml" % (tmpDir,)
138  with open(buildFileName, 'w') as buildFile:
139  buildFile.write( buildFileTemplate %(tmpSrcFileName,tmpLibName) )
140  buildFile.close()
141 
142  tmpSrcFilePath = "%s/plugins/%s" % (tmpDir, tmpSrcFileName,)
143  with open(tmpSrcFilePath, 'w') as codeFile:
144  codeFile.write(code)
145  codeFile.close()
146 
147  cmd = "source $CMS_PATH/cmsset_default.sh;"
148  cmd += "(cd %s ; scram b 2>&1 >build.log)" %tmpDir
149  pipe = subprocess.Popen( cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
150  out, err = pipe.communicate()
151  ret = pipe.returncode
152 
153  buildTime = time.time()-startTime
154  logging.info("Building done in %s sec., return code from build: %s" %(buildTime,ret) )
155 
156  if (ret != 0):
157  logging.error("Local build for xml dump failed.")
158  return None
159 
160  libFile = os.path.join(libDir,pluginName + '.so')
161  shutil.copyfile(tmpLibFile,libFile)
162 
163  module = importlib.import_module( pluginName )
164  funcName = payloadType+'2xml'
165  functor = getattr( module, funcName )
166  self.doCleanup = True
167  return functor
168 
169  def payload2xml(self, session, payloadHash, destFile):
170 
171  Payload = session.get_dbtype(self.conddb.Payload)
172  # get payload from DB:
173  result = session.query(Payload.data, Payload.object_type).filter(Payload.hash == payloadHash).one()
174  data, plType = result
175  logging.info('Found payload of type %s' %plType)
176 
177  convFuncName = sanitize(plType)+'2xml'
178  xmlConverter = self.prepPayload2xml(plType)
179 
180  if xmlConverter is not None:
181  obj = xmlConverter()
182  resultXML = obj.write( str(data) )
183  if destFile is None:
184  print(resultXML)
185  else:
186  with open(destFile, 'w') as outFile:
187  outFile.write(resultXML)
188  outFile.close()
def __init__(self, condDBIn)
Definition: cond2xml.py:53
def localLibName(payloadType)
Definition: cond2xml.py:44
def payload2xml(self, session, payloadHash, destFile)
Definition: cond2xml.py:169
def replace(string, replacements)
def sanitize(typeName)
Definition: cond2xml.py:41
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65
def prepPayload2xml(self, payloadType)
Definition: cond2xml.py:111
def discover(self, payloadType)
Definition: cond2xml.py:70
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
dbl *** dir
Definition: mlp_gen.cc:35
#define str(s)