4 from Gaudi
import Configuration
7 log = logging.getLogger(__name__)
18 __nonzero__ = __bool__
29 class Property(object):
34 return str(self.
value)
46 self.lib.py_bootstrap_fsm_configure(self.
ptr))
50 self.lib.py_bootstrap_fsm_initialize(self.
ptr))
54 self.lib.py_bootstrap_fsm_start(self.
ptr))
58 self.lib.py_bootstrap_app_run(self.
ptr, nevt))
62 self.lib.py_bootstrap_fsm_stop(self.
ptr))
66 self.lib.py_bootstrap_fsm_finalize(self.
ptr))
70 self.lib.py_bootstrap_fsm_terminate(self.
ptr))
73 return self.lib.py_bootstrap_getService(self.
ptr, name)
77 self.lib.py_bootstrap_setProperty(self.
ptr, name, value))
81 self.lib.py_bootstrap_getProperty(self.
ptr, name))
84 return self.lib.py_helper_printAlgsSequences(self.
ptr)
87 from ctypes
import (PyDLL, util, c_void_p, c_bool, c_char_p, c_int,
93 class IInterface_p(c_void_p):
95 return "IInterface_p(0x%x)" % (0
if self.value
is None else 98 self.
log = logging.getLogger(
'BootstrapHelper')
99 libname = util.find_library(
'GaudiKernel')
or 'libGaudiKernel.so' 100 self.log.debug(
'loading GaudiKernel (%s)', libname)
104 self.
lib = gkl = PyDLL(libname, mode=RTLD_GLOBAL)
107 (
'createApplicationMgr', IInterface_p, []),
108 (
'getService', IInterface_p, [IInterface_p, c_char_p]),
109 (
'setProperty', c_bool, [IInterface_p, c_char_p, c_char_p]),
110 (
'getProperty', c_char_p, [IInterface_p, c_char_p]),
111 (
'addPropertyToCatalogue', c_bool,
112 [IInterface_p, c_char_p, c_char_p, c_char_p]),
113 (
'ROOT_VERSION_CODE', c_int, []),
116 for name, restype, argtypes
in functions:
117 f = getattr(gkl,
'py_bootstrap_%s' % name)
118 f.restype, f.argtypes = restype, argtypes
121 if name
not in self.__class__.__dict__:
122 setattr(self, name, f)
124 for name
in (
'configure',
'initialize',
'start',
'stop',
'finalize',
126 f = getattr(gkl,
'py_bootstrap_fsm_%s' % name)
127 f.restype, f.argtypes = c_bool, [IInterface_p]
128 gkl.py_bootstrap_app_run.restype = c_bool
129 gkl.py_bootstrap_app_run.argtypes = [IInterface_p, c_int]
131 gkl.py_helper_printAlgsSequences.restype =
None 132 gkl.py_helper_printAlgsSequences.argtypes = [IInterface_p]
135 ptr = self.lib.py_bootstrap_createApplicationMgr()
140 return self.lib.py_bootstrap_ROOT_VERSION_CODE()
145 a = root_version_code >> 16 & 0xff
146 b = root_version_code >> 8 & 0xff
147 c = root_version_code & 0xff
156 Helper to convert values to old .opts format. 158 >>> print toOpt('some "text"') 160 >>> print toOpt('first\\nsecond') 163 >>> print toOpt({'a': [1, 2, '3']}) 166 if isinstance(value, basestring):
167 return '"{0}"'.
format(value.replace(
'"',
'\\"'))
168 elif isinstance(value, dict):
169 return '{{{0}}}'.
format(
', '.join(
171 for k, v
in value.iteritems()))
172 elif hasattr(value,
'__iter__'):
173 return '[{0}]'.
format(
', '.join(
map(toOpt, value)))
183 from Configurables
import ApplicationMgr
185 if "GAUDIAPPNAME" in os.environ:
186 appMgr.AppName = str(os.environ[
"GAUDIAPPNAME"])
187 if "GAUDIAPPVERSION" in os.environ:
188 appMgr.AppVersion = str(os.environ[
"GAUDIAPPVERSION"])
189 self.
log = logging.getLogger(__name__)
197 import multiprocessing
199 from time
import ctime
201 datetime = datetime.replace(
' ',
'_')
202 outfile = open(
'gaudirun-%s.log' % (datetime),
'w')
204 streamhandler = logging.StreamHandler(stream=outfile)
205 console = logging.StreamHandler()
207 formatter = logging.Formatter(
208 "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
210 streamhandler.setFormatter(formatter)
211 console.setFormatter(formatter)
215 self.
log = multiprocessing.log_to_stderr()
216 self.log.setLevel(logging.INFO)
217 self.log.name =
'Gaudi/Main.py Logger' 218 self.log.handlers = []
220 self.log.addHandler(streamhandler)
221 self.log.addHandler(console)
222 self.log.removeHandler(console)
224 self.log.setLevel = logging.INFO
228 from pprint
import pformat
229 conf_dict = Configuration.configurationDict(all)
230 return pformat(conf_dict)
233 from pprint
import pformat
234 conf_dict = Configuration.configurationDict(all)
236 names = conf_dict.keys()
239 props = conf_dict[n].keys()
242 out.append(
'%s.%s = %s;' % (n, p,
toOpt(conf_dict[n][p])))
243 return "\n".join(out)
248 output = open(filename,
'wb')
250 from GaudiKernel.Proxy.Configurable
import getNeededConfigurables
253 to_dump[n] = Configuration.allConfigurables[n]
254 pickle.dump(to_dump, output, -1)
258 msg =
'Dumping all configurables and properties' 260 msg +=
' (different from default)' 262 conf_dict = Configuration.configurationDict(all)
269 write = {
".pkl":
lambda filename, all: self.
_writepickle(filename),
270 ".py":
lambda filename, all: open(filename,
"w").write(self.
generatePyOutput(all) +
"\n"),
271 ".opts":
lambda filename, all: open(filename,
"w").write(self.
generateOptsOutput(all) +
"\n"),
273 from os.path
import splitext
274 ext = splitext(filename)[1]
276 write[ext](filename, all)
278 log.error(
"Unknown file type '%s'. Must be any of %r.", ext,
284 def run(self, attach_debugger, ncpus=None):
295 self.log.info(
'attaching debugger to PID ' + str(os.getpid()))
296 pid = os.spawnvp(os.P_NOWAIT, debugger,
297 [debugger,
'-q',
'python',
305 os.waitpid(pid, os.WNOHANG)
311 Bootstrap the application with minimal use of Python bindings. 314 from GaudiKernel.Proxy.Configurable
import expandvars
320 from GaudiKernel.Proxy.Configurable
import Configurable, getNeededConfigurables
323 if _bootstrap
is None:
326 self.log.debug(
'basicInit: instantiate ApplicationMgr')
327 self.
ip = self.
g = _bootstrap.createApplicationMgr()
329 self.log.debug(
'basicInit: apply options')
332 comp =
'ApplicationMgr' 333 props = Configurable.allConfigurables.get(comp, {})
335 props =
expandvars(props.getValuedProperties())
336 for p, v
in props.items() + [(
'JobOptionsType',
'NONE')]:
337 if not self.g.setProperty(p, str(v)):
338 self.log.error(
'Cannot set property %s.%s to %s', comp, p, v)
341 if _bootstrap.ROOT_VERSION < (6, 2, 7):
349 msp = self.g.getService(comp)
351 self.log.error(
'Cannot get service %s', comp)
353 props = Configurable.allConfigurables.get(comp, {})
355 props =
expandvars(props.getValuedProperties())
356 for p, v
in props.items():
357 if not _bootstrap.setProperty(msp, p, str(v)):
358 self.log.error(
'Cannot set property %s.%s to %s', comp, p, v)
362 comp =
'JobOptionsSvc' 363 jos = self.g.getService(comp)
365 self.log.error(
'Cannot get service %s', comp)
368 c = Configurable.allConfigurables[n]
369 if n
in [
'ApplicationMgr',
'MessageSvc']:
371 for p, v
in c.getValuedProperties().items():
374 if hasattr(Configurable,
"PropertyReference")
and type(
375 v) == Configurable.PropertyReference:
381 elif type(v) == long:
383 _bootstrap.addPropertyToCatalogue(jos, n, p, str(v))
384 if hasattr(Configurable,
"_configurationLocked"):
385 Configurable._configurationLocked =
True 386 self.log.debug(
'basicInit: done')
390 Initialize the application with full Python bindings. 392 self.log.debug(
'gaudiPythonInit: import GaudiPython')
394 self.log.debug(
'gaudiPythonInit: instantiate ApplicationMgr')
397 self.log.debug(
'gaudiPythonInit: done')
401 if (self.
mainLoop or os.environ.get(
'GAUDIRUN_USE_GAUDIPYTHON')):
406 self.log.debug(
'-' * 80)
407 self.log.debug(
'%s: running in serial mode', __name__)
408 self.log.debug(
'-' * 80)
415 def runner(app, nevt):
416 self.log.debug(
'initialize')
417 sc = app.initialize()
420 app.printAlgsSequences()
421 self.log.debug(
'start')
424 self.log.debug(
'run(%d)', nevt)
426 self.log.debug(
'stop')
428 self.log.debug(
'finalize')
429 app.finalize().ignore()
430 self.log.debug(
'terminate')
431 sc1 = app.terminate()
436 self.log.debug(
'status code: %s',
437 'SUCCESS' if sc.isSuccess()
else 'FAILURE')
440 if (attach_debugger ==
True):
444 statuscode = runner(self.
g,
445 int(self.ip.getProperty(
'EvtMax').
toString()))
448 self.ip.setProperty(
'ReturnCode', str(128 + 11))
451 print 'Exception:', x
453 self.ip.setProperty(
'ReturnCode',
'1')
455 if hasattr(statuscode,
"isSuccess"):
456 success = statuscode.isSuccess()
460 if not success
and self.ip.getProperty(
'ReturnCode').
toString() ==
'0':
462 self.ip.setProperty(
'ReturnCode',
'1')
463 sysTime =
time() - sysStart
464 self.log.debug(
'-' * 80)
465 self.log.debug(
'%s: serial system finished, time taken: %5.4fs',
467 self.log.debug(
'-' * 80)
468 return int(self.ip.getProperty(
'ReturnCode').
toString())
473 "Cannot use custom main loop in multi-process mode, check your options" 479 c = Configurable.allConfigurables
480 self.log.info(
'-' * 80)
481 self.log.info(
'%s: Parallel Mode : %i ', __name__, ncpus)
483 (
'platrofm',
' '.join(os.uname())),
484 (
'config', os.environ.get(
'BINARY_TAG')
485 or os.environ.get(
'CMTCONFIG')),
486 (
'app. name', os.environ.get(
'GAUDIAPPNAME')),
487 (
'app. version', os.environ.get(
'GAUDIAPPVERSION')),
489 self.log.info(
'%s: %30s : %s ', __name__, name, value
492 events = str(c[
'ApplicationMgr'].EvtMax)
494 events =
"Undetermined" 495 self.log.info(
'%s: Events Specified : %s ', __name__, events)
496 self.log.info(
'-' * 80)
498 Parall = gpp.Coord(ncpus, c, self.
log)
501 self.log.info(
'MAIN.PY : received %s from Coordinator' % (sc))
504 sysTime =
time() - sysStart
505 self.log.name =
'Gaudi/Main.py Logger' 506 self.log.info(
'-' * 80)
507 self.log.info(
'%s: parallel system finished, time taken: %5.4fs',
509 self.log.info(
'-' * 80)
def printAlgsSequences(self)
def run(self, attach_debugger, ncpus=None)
def setupParallelLogging(self)
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
def _writepickle(self, filename)
def generateOptsOutput(self, all=False)
def gaudiPythonInit(self)
def runParallel(self, ncpus)
def hookDebugger(self, debugger='gdb')
def generatePyOutput(self, all=False)
def __init__(self, ptr, lib)
struct GAUDI_API map
Parametrisation class for map-like implementation.
def getProperty(self, name)
def getService(self, name)
def createApplicationMgr(self)
def __init__(self, value)
The Application Manager class.
def ROOT_VERSION_CODE(self)
def __init__(self, value)
def printconfig(self, old_format=False, all=False)
def writeconfig(self, filename, all=False)
std::string toString(const Type &)
def setProperty(self, name, value)
def runSerial(self, attach_debugger)