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))
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)))
180 from Configurables
import ApplicationMgr
182 if "GAUDIAPPNAME" in os.environ:
183 appMgr.AppName = str(os.environ[
"GAUDIAPPNAME"])
184 if "GAUDIAPPVERSION" in os.environ:
185 appMgr.AppVersion = str(os.environ[
"GAUDIAPPVERSION"])
186 self.
log = logging.getLogger(__name__)
195 import multiprocessing
197 from time
import ctime
199 datetime = datetime.replace(
' ',
'_')
200 outfile = open(
'gaudirun-%s.log' % (datetime),
'w')
202 streamhandler = logging.StreamHandler(stream=outfile)
203 console = logging.StreamHandler()
205 formatter = logging.Formatter(
206 "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
208 streamhandler.setFormatter(formatter)
209 console.setFormatter(formatter)
213 self.
log = multiprocessing.log_to_stderr()
214 self.
log.setLevel(logging.INFO)
215 self.
log.name =
'Gaudi/Main.py Logger' 216 self.
log.handlers = []
218 self.
log.addHandler(streamhandler)
219 self.
log.addHandler(console)
220 self.
log.removeHandler(console)
222 self.
log.setLevel = logging.INFO
226 from pprint
import pformat
227 conf_dict = Configuration.configurationDict(all)
228 return pformat(conf_dict)
231 from pprint
import pformat
232 conf_dict = Configuration.configurationDict(all)
234 names = conf_dict.keys()
237 props = conf_dict[n].keys()
240 out.append(
'%s.%s = %s;' % (n, p,
toOpt(conf_dict[n][p])))
241 return "\n".join(out)
246 output = open(filename,
'wb')
248 from GaudiKernel.Proxy.Configurable
import getNeededConfigurables
251 to_dump[n] = Configuration.allConfigurables[n]
252 pickle.dump(to_dump, output, -1)
256 msg =
'Dumping all configurables and properties' 258 msg +=
' (different from default)' 260 conf_dict = Configuration.configurationDict(all)
267 write = {
".pkl":
lambda filename, all: self.
_writepickle(filename),
268 ".py":
lambda filename, all: open(filename,
"w").write(self.
generatePyOutput(all) +
"\n"),
269 ".opts":
lambda filename, all: open(filename,
"w").write(self.
generateOptsOutput(all) +
"\n"),
271 from os.path
import splitext
272 ext = splitext(filename)[1]
274 write[ext](filename, all)
276 log.error(
"Unknown file type '%s'. Must be any of %r.", ext,
282 def run(self, attach_debugger, ncpus=None):
293 self.
log.info(
'attaching debugger to PID ' + str(os.getpid()))
294 pid = os.spawnvp(os.P_NOWAIT, debugger,
295 [debugger,
'-q',
'python',
303 os.waitpid(pid, os.WNOHANG)
309 from GaudiKernel.Proxy.Configurable
import expandvars
315 from GaudiKernel.Proxy.Configurable
import Configurable, getNeededConfigurables
317 self.
log.debug(
'runSerial: apply options')
318 conf_dict = {
'ApplicationMgr.JobOptionsType':
'"NONE"'}
322 for c
in Configurable.allConfigurables.values():
323 if hasattr(c,
'getValuedProperties'):
324 c.getValuedProperties()
327 c = Configurable.allConfigurables[n]
328 for p, v
in c.getValuedProperties().items():
331 if hasattr(Configurable,
"PropertyReference")
and type(
332 v) == Configurable.PropertyReference:
338 v =
'"%s"' % v.replace(
'"',
'\\"')
339 elif type(v) == long:
341 conf_dict[
'{}.{}'.
format(n, p)] = str(v)
344 conf_dict[
'ApplicationMgr.PrintAlgsSequence'] =
'true' 346 if hasattr(Configurable,
"_configurationLocked"):
347 Configurable._configurationLocked =
True 352 self.
log.debug(
'-' * 80)
353 self.
log.debug(
'%s: running in serial mode', __name__)
354 self.
log.debug(
'-' * 80)
361 sysTime =
time() - sysStart
362 self.
log.debug(
'-' * 80)
363 self.
log.debug(
'%s: serial system finished, time taken: %5.4fs',
365 self.
log.debug(
'-' * 80)
373 c = Configurable.allConfigurables
374 self.
log.info(
'-' * 80)
375 self.
log.info(
'%s: Parallel Mode : %i ', __name__, ncpus)
377 (
'platrofm',
' '.join(os.uname())),
378 (
'config', os.environ.get(
'BINARY_TAG')
379 or os.environ.get(
'CMTCONFIG')),
380 (
'app. name', os.environ.get(
'GAUDIAPPNAME')),
381 (
'app. version', os.environ.get(
'GAUDIAPPVERSION')),
383 self.
log.info(
'%s: %30s : %s ', __name__, name, value
386 events = str(c[
'ApplicationMgr'].EvtMax)
388 events =
"Undetermined" 389 self.
log.info(
'%s: Events Specified : %s ', __name__, events)
390 self.
log.info(
'-' * 80)
392 Parall = gpp.Coord(ncpus, c, self.
log)
395 self.
log.info(
'MAIN.PY : received %s from Coordinator' % (sc))
398 sysTime =
time() - sysStart
399 self.
log.name =
'Gaudi/Main.py Logger' 400 self.
log.info(
'-' * 80)
401 self.
log.info(
'%s: parallel system finished, time taken: %5.4fs',
403 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 runParallel(self, ncpus)
def hookDebugger(self, debugger='gdb')
def generatePyOutput(self, all=False)
def __init__(self, ptr, lib)
void py_helper_printAlgsSequences(IInterface *app)
Helper to call printAlgsSequences from Pyhton ctypes.
struct GAUDI_API map
Parametrisation class for map-like implementation.
def getProperty(self, name)
def getService(self, name)
def createApplicationMgr(self)
def create(cls, appType, opts)
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)
def setProperty(self, name, value)
def runSerial(self, attach_debugger)