Best Python code snippet using fMBT_python
serviceManager.py
Source:serviceManager.py
1#Embedded file name: c:/depot/games/branches/release/EVE-TRANQUILITY/carbon/common/script/sys/serviceManager.py2from __future__ import with_statement3import bluepy4import telemetry5import uthread6import service7import blue8import base9import stackless10import types11import weakref12import log13import sys14import time15import collections16import util17import const18import svc19import os20from timerstuff import ClockThis, ClockThisWithoutTheStars21class ServiceNotFound(StandardError):22 __guid__ = 'exceptions.ServiceNotFound'23 __persistvars__ = ['serviceName']24 def __init__(self, serviceName = None):25 self.serviceName = serviceName26 self.args = [serviceName]27 def __repr__(self):28 return 'The service ' + unicode(self.serviceName) + ' was not found.'29class MethodNotCalledFromClient(StandardError):30 __guid__ = 'exception.MethodNotCalledFromClient'31 __persistvars__ = ['methodName']32 def __init__(self, methodName = None):33 self.methodName = methodName34 self.args = [methodName]35 def __repr__(self):36 return 'The method ' + unicode(self.methodName) + ' can be called from the client only.'37import __builtin__38__builtin__.ServiceNotFound = ServiceNotFound39__builtin__.MethodNotCalledFromClient = MethodNotCalledFromClient40class ServiceManager(log.LogMixin):41 __guid__ = 'service.ServiceManager'42 def __init__(self, startInline = []):43 log.LogMixin.__init__(self, 'svc.ServiceManager')44 self.state = service.SERVICE_START_PENDING45 self.services = {}46 self.dependants = {}47 self.notify = {}48 self.notifyObs = {}49 self.startInline = startInline50 self.blockedServices = []51 self.startupTimes = {}52 import __builtin__53 if hasattr(__builtin__, 'sm'):54 log.Quit('Multiple instances of ServiceManager are not allowed in a process')55 __builtin__.sm = self56 def _BuildClassMap(self):57 self.classmap = dict([ (name, (name, getattr(svc, name))) for name in dir(svc) ])58 self.classmapWithReplacements = self.classmap.copy()59 for name, svcclass in self.classmap.values():60 self.classmapWithReplacements[name] = (name, getattr(svc, self.GetServiceImplementation(name)))61 def Run(self, servicesToRun, servicesToBlock = []):62 self.run = 163 if self.state not in (service.SERVICE_START_PENDING, service.SERVICE_RUNNING):64 if self.state == service.SERVICE_STOPPED:65 self.state = service.SERVICE_START_PENDING66 else:67 raise RuntimeError, "can't run a service when state is " + repr(self.state)68 self._BuildClassMap()69 blue.pyos.AddExitProc(self.Stop)70 for block in servicesToBlock:71 if block not in self.blockedServices:72 self.blockedServices.append(block)73 if len(servicesToRun):74 print 'Starting services'75 was = blue.pyos.taskletTimer.timesliceWarning76 try:77 blue.pyos.taskletTimer.timesliceWarning = 500078 for each in servicesToRun:79 self.StartService(each, reason='autoexec')80 for each in sm.services.keys():81 counter = 082 sleepTimeMs = 183 while sm.services[each].state == service.SERVICE_START_PENDING:84 counter += 185 if counter % 100 == 0:86 print "Service start still pending: '%s', sleeping..." % sm.services[each].__guid__87 blue.pyos.synchro.SleepWallclock(sleepTimeMs)88 sleepTimeMs *= 289 if sleepTimeMs > 100:90 sleepTimeMs = 10091 for each in sm.services.keys():92 if hasattr(sm.services[each], 'PostRun'):93 sm.services[each].PostRun()94 finally:95 blue.pyos.taskletTimer.timesliceWarning = was96 print 'Starting services - Done'97 self.state = service.SERVICE_RUNNING98 def Stop(self):99 self.logChannel.Log('ServiceManager.Stop(), stopping services')100 dag = util.DAG()101 for k, v in self.services.iteritems():102 depends = v.__dependencies__ + getattr(v, '__exitdependencies__', []) + getattr(v, '__startupdependencies__', [])103 for d in depends:104 if type(d) is not str:105 d = d[0]106 if d in self.services:107 dag.InsertEdge(k, d)108 dag.Invert()109 self.logChannel.Log('== BEGIN SERVICE LIST ==')110 order = []111 while dag.graph:112 leaves = dag.Leaves()113 if not leaves:114 break115 order.extend(leaves)116 for l in leaves:117 dag.RemoveNode(l)118 self.logChannel.Log('==leaves==')119 self.logChannel.Log(','.join(leaves))120 if dag.graph:121 leaves = dag.graph.keys()122 order.extend(leaves)123 self.logChannel.Log('==nonleaves==')124 self.logChannel.Log(','.join(leaves))125 self.logChannel.Log('== ENDOF SERVICE LIST ==')126 self.run = 0127 import blue128 old_block_trap = stackless.getcurrent().block_trap129 stackless.getcurrent().block_trap = 1130 self.state = service.SERVICE_STOP_PENDING131 try:132 for k in order:133 try:134 v = self.services[k]135 except KeyError:136 self.LogWarn('Warning, the service', k, 'has been stopped already but something that depends on it might still be running!')137 sys.exc_clear()138 continue139 if not hasattr(v, 'state'):140 self.logChannel.Log("ServiceManager.Stop(), service '" + str(k) + " doesn't have state therefore has already stopped")141 elif v.state not in (service.SERVICE_STOPPED, service.SERVICE_STOP_PENDING):142 self.logChannel.Log("ServiceManager.Stop(), stopping service '" + str(k) + "'")143 try:144 v.state = service.SERVICE_STOP_PENDING145 for notify in v.__notifyevents__:146 self.notify[notify].remove(v)147 v.Stop(blue.MemStream())148 except StandardError:149 log.LogException()150 sys.exc_clear()151 finally:152 v.state = service.SERVICE_STOPPED153 else:154 self.logChannel.Log("ServiceManager.Stop(), service '" + str(k) + "' is already stopping")155 finally:156 stackless.getcurrent().block_trap = old_block_trap157 self.state = service.SERVICE_STOPPED158 for v in self.services.itervalues():159 for a in v.__dict__.keys():160 if a not in ('logChannel', 'logContexts', 'state'):161 delattr(v, a)162 self.logChannel.Log('ServiceManager.Stop(), services stopped.')163 def ParseServiceClass(self, serviceName):164 return 'svc.' + serviceName165 def Reload(self, serviceList):166 import blue167 self._BuildClassMap()168 if len(serviceList):169 print 'Reloading services'170 for each in serviceList:171 if each not in self.services or each in self.services and getattr(self.services[each], '__update_on_reload__', True):172 continue173 ms = blue.MemStream()174 try:175 try:176 self.StopService(each, 0, ms)177 finally:178 if self.services.has_key(each):179 del self.services[each]180 self.GetService(each, ms)181 except Exception:182 log.LogException("Trying to reload service '%s" % each)183 sys.exc_clear()184 if len(serviceList):185 print 'Reloading services - Done'186 def StartServiceAndWaitForRunningState(self, serviceName, ms = None, reason = None):187 srv = self.StartService(serviceName, ms=None, reason=reason)188 desiredStates = (service.SERVICE_RUNNING,)189 errorStates = (service.SERVICE_FAILED, service.SERVICE_STOPPED)190 self.WaitForServiceObjectState(srv, desiredStates, errorStates)191 return srv192 @telemetry.ZONE_METHOD193 def WaitForServiceObjectState(self, svc, desiredStates, errorStates = (service.SERVICE_FAILED,)):194 i = 0195 sleepTimeMs = 1196 while svc.state not in desiredStates:197 if svc.state in errorStates:198 svc.LogError('Service ', svc.__logname__, ' got in an unexpected state', 'raising error')199 if svc.__error__:200 raise svc.__error__[1], None, svc.__error__[2]201 else:202 raise RuntimeError, 'Service %s made unexpected state transition' % svc.__logname__203 blue.pyos.synchro.SleepWallclock(sleepTimeMs)204 sleepTimeMs *= 2205 if sleepTimeMs > 100:206 sleepTimeMs = 100207 if i % 600 == 0 and i > 0:208 svc.LogWarn('WaitForServiceObjectState has been sleeping for a long time waiting for ', svc.__logname__, ' to either get to state ', desiredStates, 'current state is', svc.state)209 i += 1210 def GetServiceIfStarted(self, serviceName):211 if serviceName in self.services:212 srv = self.services[serviceName]213 self.WaitForServiceObjectState(srv, (service.SERVICE_RUNNING,))214 return srv215 def GetServiceIfRunning(self, serviceName):216 if self.IsServiceRunning(serviceName):217 srv = self.services[serviceName]218 return srv219 def GetService(self, serviceName, ms = None):220 srv = self.services.get(serviceName, None)221 if srv and srv.state == service.SERVICE_RUNNING:222 return srv223 if serviceName in self.services:224 if self.services[serviceName].state in (service.SERVICE_START_PENDING, service.SERVICE_STARTING_DEPENDENCIES) and '/thinclient' not in blue.pyos.GetArg():225 log.LogTraceback('Possible service deadlock detected!', toAlertSvc=False, severity=log.LGWARN)226 return self.StartServiceAndWaitForRunningState(serviceName, ms, reason='GetService')227 @telemetry.ZONE_METHOD228 def StartService(self, serviceName, ms = None, reason = 'StartService'):229 telemetry.APPEND_TO_ZONE(serviceName)230 srv = self.services.get(serviceName, None)231 if srv and srv.state == service.SERVICE_RUNNING:232 return srv233 if serviceName in self.services:234 srv = self.services[serviceName]235 else:236 if serviceName in self.blockedServices:237 raise RuntimeError('%s has been blocked from running on this system' % serviceName)238 srv = self.CreateServiceInstance(serviceName)239 self.services[serviceName] = srv240 if srv.state in (service.SERVICE_START_PENDING,):241 return srv242 if srv.state == service.SERVICE_STARTING_DEPENDENCIES:243 desiredStates = (service.SERVICE_START_PENDING, service.SERVICE_RUNNING)244 errorStates = (service.SERVICE_FAILED, service.SERVICE_STOPPED)245 self.WaitForServiceObjectState(srv, desiredStates, errorStates)246 return srv247 if self.state in (service.SERVICE_STOP_PENDING, service.SERVICE_STOPPED):248 raise RuntimeError, "Can't start service " + serviceName + ' when service manager is shutting down'249 if srv.state == service.SERVICE_FAILED:250 return srv251 try:252 r = reason253 if reason in ('GetService', 'StartService'):254 up = 4255 if reason == 'StartService':256 up = 2257 r = '%s - called from %s' % (reason, log.WhoCalledMe(up))258 self.LogInfo('Starting', serviceName, '. Reason:', r)259 except:260 pass261 srv.state = service.SERVICE_STARTING_DEPENDENCIES262 srv.__error__ = None263 try:264 self.dependants[serviceName] = []265 if srv.__startupdependencies__:266 self.LogInfo('starting startup dependencies for %s, which are: %s' % (serviceName, str(srv.__startupdependencies__)))267 for each in srv.__startupdependencies__:268 if each == srv.__guid__.split('.')[1]:269 self.LogError('Found a service with a dependancy on it self:', each, '. The service reference will not be assigned, things will probaly blow up')270 continue271 if type(each) is str:272 each = (each, each)273 depname, asname = each274 if not self.IsServiceRunning(depname):275 self.LogInfo(serviceName, 'is waiting while', depname, 'is started')276 depService = self.StartServiceAndWaitForRunningState(depname, reason='startup dependency for %s' % serviceName)277 self.dependants[depname].append(serviceName)278 if getattr(boot, 'replaceDependencyServiceWrappers', 'false').lower() != 'true' or not depService.IsRunning():279 setattr(srv, asname, srv.session.ConnectToService(depname))280 else:281 setattr(srv, asname, depService)282 srv.state = service.SERVICE_START_PENDING283 if srv.__dependencies__:284 uthread.new(self._LoadServiceDependenciesAsych, srv, serviceName).context = serviceName + ' _LoadServiceDependenciesAsych'285 for notify in srv.__notifyevents__:286 if not hasattr(srv, notify):287 raise RuntimeError('MissingSvcExportAttribute', serviceName, 'notify', notify)288 if not self.notify.has_key(notify):289 self.notify[notify] = []290 self.notify[notify].append(srv)291 except Exception as e:292 srv.state = service.SERVICE_FAILED293 srv.__error__ = sys.exc_info()294 raise 295 if ms:296 ms.Seek(0)297 args = (ms,)298 if serviceName in self.startInline:299 self.StartServiceRun(srv, args, serviceName)300 else:301 uthread.pool(serviceName + ' StartServiceRun', self.StartServiceRun, srv, args, serviceName)302 return srv303 def _LoadServiceDependenciesAsych(self, srv, serviceName):304 self.LogInfo('starting dependencies for %s, which are: %s' % (serviceName, str(srv.__dependencies__)))305 for each in srv.__dependencies__:306 if type(each) is str:307 each = (each, each)308 depname, asname = each309 depService = self.StartService(depname, reason='dependency for %s' % serviceName)310 self.dependants[depname].append(serviceName)311 if getattr(boot, 'replaceDependencyServiceWrappers', 'false').lower() != 'true' or not depService.IsRunning():312 setattr(srv, asname, srv.session.ConnectToService(depname))313 else:314 setattr(srv, asname, depService)315 @util.Memoized316 def GetServiceImplementation(self, target):317 found = target318 foundPriority = -1319 for name, svcclass in self.classmap.itervalues():320 replaces = getattr(svcclass, '__replaceservice__', None)321 if replaces == target:322 priority = getattr(svcclass, '__replacepriority__', 0)323 if priority > foundPriority:324 found = name325 foundPriority = priority326 return found327 @telemetry.ZONE_METHOD328 def CreateServiceInstance(self, serviceName):329 old_block_trap = stackless.getcurrent().block_trap330 stackless.getcurrent().block_trap = 1331 try:332 try:333 createName, createClass = self.classmapWithReplacements[serviceName]334 except KeyError:335 self._BuildClassMap()336 try:337 createName, createClass = self.classmapWithReplacements[serviceName]338 except KeyError:339 raise ServiceNotFound(serviceName)340 if createName != serviceName:341 print 'Replacing service %r with %r' % (serviceName, createName)342 replaceService = getattr(createClass, '__replaceservice__', None)343 if replaceService is not None and replaceService != serviceName:344 raise RuntimeError('Must not start %s directly as it replaces %s' % (serviceName, replaceService))345 srv = createClass()346 if not isinstance(srv, service.Service):347 raise RuntimeError('Service name %r does not resolve to a service class (%r)' % (serviceName, createClass))348 srv.__servicename__ = serviceName349 srv.session = base.GetServiceSession(serviceName)350 self.VerifyServiceExports(srv, serviceName)351 return srv352 finally:353 stackless.getcurrent().block_trap = old_block_trap354 def VerifyServiceExports(self, srv, serviceName):355 for funcName, paramList in srv.__exportedcalls__.iteritems():356 if not hasattr(srv, funcName):357 raise RuntimeError('MissingSvcExportAttribute', serviceName, 'exported call', funcName)358 if type(paramList) == types.ListType:359 tmp = {}360 if len(paramList):361 tmp['role'] = paramList[0]362 tmp['preargs'] = paramList[1:]363 paramList = tmp364 if type(paramList) == types.DictType:365 for k, v in paramList.iteritems():366 if k not in ('role', 'caching', 'preargs', 'fastcall', 'precall', 'postcall', 'callhandlerargs', 'input', 'output', 'resolve') or k == 'role' and type(v) not in (types.IntType, types.LongType):367 self.LogError('Service %s has illegal function declaration for %s: %s is not a valid metadata key' % (serviceName, funcName, k))368 elif k == 'fastcall':369 if int(v) not in (0, 1):370 self.LogError('Service %s has illegal function declaration for %s: %s is not a valid setting for fastcall' % (serviceName, funcName, v))371 elif k == 'preargs':372 for eachArg in v:373 if type(eachArg) != types.StringType or eachArg not in srv.session.__persistvars__ and eachArg not in srv.session.__nonpersistvars__:374 self.LogError('Service %s has illegal function declaration for %s: %s is not a valid prearg' % (serviceName, funcName, eachArg))375 elif k == 'output':376 if type(v) == types.InstanceType:377 import dataset378 setattr(dataset, funcName + 'Result', v)379 elif k == 'caching' and 'objectCaching' in sm.services:380 if not v:381 self.LogError('Service %s has illegal function declaration for %s: caching must have subinfo' % (serviceName, funcName))382 for k2, v2 in v.iteritems():383 if k2 not in ('sessionInfo', 'versionCheck', 'client', 'server', 'proxy'):384 self.LogError('Service %s has illegal function declaration for %s: %s is not a valid caching subinfo key' % (serviceName, funcName, k2))385 elif k2 == 'sessionInfo' and v2 not in srv.session.__persistvars__:386 self.LogError('Service %s has illegal function declaration for %s: %s is not a valid session info prearg' % (serviceName, funcName, v2))387 else:388 self.LogError('Service %s has illegal function declaration for %s: %s is not a valid metadata info type' % (serviceName, funcName, type(paramList)))389 @telemetry.ZONE_METHOD390 def StartServiceRun(self, svc, args, namen):391 try:392 t0 = time.clock()393 try:394 if boot.role == 'client' and getattr(prefs, 'clientServicesWait', 'true').lower() != 'true':395 svc.state = service.SERVICE_RUNNING396 svc.Run(*args)397 else:398 svc.state = service.SERVICE_START_PENDING399 with bluepy.Timer('StartService::ServiceStartRun::' + namen):400 svc.Run(*args)401 svc.state = service.SERVICE_RUNNING402 if getattr(boot, 'replaceDependencyServiceWrappers', 'false').lower() == 'true':403 for depName in self.dependants[namen]:404 depSvc = self.StartService(depName)405 setattr(depSvc, namen, svc)406 finally:407 t = time.clock() - t0408 except Exception as e:409 svc.state = service.SERVICE_FAILED410 svc.__error__ = sys.exc_info()411 self.LogError('Failed to start service %s' % namen)412 raise 413 self.startupTimes[namen] = t414 if t < 60:415 svc.LogInfo('Service', namen, 'required %-3.3f' % t, ' seconds to startup')416 else:417 svc.LogWarn('Service', namen, 'startup took %-3.3f' % t, ' seconds')418 print 'Service %s: %-3.3fs' % (namen, t)419 def StopService(self, serviceName, halt = 1, ms = None):420 self.LogInfo('Stopping %s' % serviceName)421 if not self.services.has_key(serviceName):422 return423 srv = self.services[serviceName]424 if srv.state in (service.SERVICE_STOP_PENDING, service.SERVICE_STOPPED):425 return426 oldstate = srv.state427 srv.state = service.SERVICE_STOP_PENDING428 for notify in srv.__notifyevents__:429 self.notify[notify].remove(srv)430 if halt:431 for each in self.dependants[serviceName]:432 self.StopService(each)433 try:434 srv.Stop(ms)435 except Exception:436 srv.state = oldstate437 log.LogException('Trying to stop service %s ' % serviceName)438 sys.exc_clear()439 if srv.state != service.SERVICE_STOPPED:440 srv.state = service.SERVICE_STOPPED441 del self.services[serviceName]442 def GetDependecyGraph(self, startupDependencies = False):443 depGraph = util.DAG()444 import svc445 depAttr = '__dependencies__'446 if startupDependencies:447 depAttr = '__startupdependencies__'448 for k, v in svc.__dict__.items():449 if hasattr(v, '__guid__'):450 depGraph.AddNode(k)451 for dep in getattr(v, depAttr, []):452 depGraph.InsertEdge(k, dep)453 return depGraph454 def HotStartService(self, serviceName):455 import blue456 ms = blue.MemStream()457 self.StopService(serviceName, ms)458 nasty.ReloadClass(self.ParseServiceClass(serviceName))459 self.StartService(serviceName, ms)460 def IsServiceRunning(self, serviceName):461 return serviceName in sm.services and self.services[serviceName].IsRunning()462 def RemoteSvc(self, serviceName):463 if boot.role == 'client':464 return session.ConnectToRemoteService(serviceName)465 self.LogError('The method sm.RemoteSvc can be called from the client only.')466 raise MethodNotCalledFromClient('sm.RemoteSvc')467 def ProxySvc(self, serviceName):468 if boot.role == 'client':469 return session.ConnectToRemoteService(serviceName, self.services['machoNet'].myProxyNodeID)470 self.LogError('The method sm.ProxySvc can be called from the client only.')471 raise MethodNotCalledFromClient('sm.ProxySvc')472 def GetActiveServices(self):473 ret = []474 for k, srv in self.services.iteritems():475 if srv.state == service.SERVICE_RUNNING:476 ret.append(k)477 return ret478 def GetServicesState(self):479 ret = {}480 for k, srv in self.services.iteritems():481 ret[k] = srv.state482 return ret483 def GetLogName(self, thingie):484 from macho import GetLogName as GetMachoLogName485 if isinstance(thingie, weakref.ref):486 thingie = thingie()487 return getattr(thingie, '__logname__', GetMachoLogName(thingie))488 def SendEvent(self, eventid, *args, **keywords):489 return self.SendEventWithoutTheStars(eventid, args, keywords)490 def SendEventWithoutTheStars(self, eventid, args, keywords = None):491 if keywords is None:492 keywords = {}493 if not eventid.startswith('Do'):494 self.LogError('SendEvent called with event ', eventid, ". All events sent via SendEvent should start with 'Do'")495 self.LogError("Not only is the programmer responsible for this a 10z3r, but he wears his mother's underwear as well")496 log.LogTraceback()497 if not self.notify.get(eventid, []) and self.notifyObs.get(eventid, []):498 self.LogWarn("Orphan'd event. ", eventid, "doesn't have any listeners")499 if util.IsFullLogging():500 self.LogMethodCall('SendEvent(', eventid, ',*args=', args, ',**kw=', keywords, ')')501 else:502 self.LogMethodCall('SendEvent(', eventid, ')')503 prefix = blue.pyos.taskletTimer.GetCurrent() + '::SendEvent_' + eventid + '::'504 old_block_trap = stackless.getcurrent().block_trap505 stackless.getcurrent().block_trap = 1506 ret = []507 try:508 for srv in self.notify.get(eventid, []):509 try:510 logname = prefix + self.GetLogName(srv)511 if srv.state == service.SERVICE_RUNNING:512 self.LogMethodCall('Calling ', logname)513 ret.append(ClockThisWithoutTheStars(logname, getattr(srv, eventid), args, keywords))514 else:515 self.LogMethodCall('Skipping ', logname, ' (service not running)')516 except StandardError:517 self.LogError('In %s.%s' % (getattr(srv, '__guid__', logname), eventid))518 log.LogException()519 sys.exc_clear()520 notifiedToRemove = []521 for weakObj in self.notifyObs.get(eventid, []):522 obj = weakObj()523 if obj is None:524 notifiedToRemove.append(weakObj)525 else:526 try:527 logname = prefix + str(obj)528 self.LogMethodCall('Calling ', logname)529 apply(getattr(obj, eventid), args, keywords)530 except StandardError:531 self.LogError('In %s.%s' % (getattr(weakObj, '__guid__', self.GetLogName(weakObj)), eventid))532 log.LogException()533 sys.exc_clear()534 for toRemove in notifiedToRemove:535 if toRemove in self.notifyObs[eventid]:536 self.notifyObs[eventid].remove(toRemove)537 finally:538 bt = 0539 if old_block_trap:540 bt = 1541 stackless.getcurrent().block_trap = bt542 return tuple(ret)543 def ChainEvent(self, eventid, *args, **keywords):544 return self.ChainEventWithoutTheStars(eventid, args, keywords)545 def ChainEventWithoutTheStars(self, eventid, args, keywords = None):546 if keywords is None:547 keywords = {}548 if not eventid.startswith('Process'):549 self.LogError('ChainEvent called with event ', eventid, ". All events sent via ChainEvent should start with 'Process'")550 self.LogError("Not only is the programmer responsible for this a 10z3r, but he wears his mother's underwear as well")551 log.LogTraceback()552 if stackless.getcurrent().block_trap or stackless.getcurrent().is_main:553 raise RuntimeError("ChainEvent is blocking by design, but you're block trapped. You have'll have to find some alternative means to do Your Thing, dude.")554 if not self.notify.get(eventid, []) and not self.notifyObs.get(eventid, []):555 self.LogWarn("Orphan'd event. ", eventid, "doesn't have any listeners")556 self.LogMethodCall('ChainEvent(', eventid, ',*args=', args, ',**kw=', keywords, ')')557 prefix = blue.pyos.taskletTimer.GetCurrent() + '::ChainEvent_' + eventid + '::'558 ret = []559 for srv in self.notify.get(eventid, []):560 contextName = self.GetLogName(srv)561 try:562 logname = prefix + contextName563 if srv.state == service.SERVICE_RUNNING:564 self.LogMethodCall('Calling ', logname)565 retval = ClockThisWithoutTheStars(logname, getattr(srv, eventid), args, keywords)566 ret.append(retval)567 else:568 self.LogMethodCall('Skipping ', logname, ' (service not running)')569 except StandardError:570 self.LogError('In %s.%s' % (contextName, eventid))571 log.LogException()572 sys.exc_clear()573 notifiedToRemove = []574 for weakObj in self.notifyObs.get(eventid, []):575 obj = weakObj()576 if obj is None:577 notifiedToRemove.append(weakObj)578 else:579 contextName = self.GetLogName(weakObj)580 try:581 logname = prefix + contextName582 self.LogMethodCall('Calling ', prefix + str(obj))583 ClockThisWithoutTheStars(logname, getattr(obj, eventid), args, keywords)584 except StandardError:585 self.LogError('In %s.%s:' % (contextName, eventid))586 log.LogException()587 sys.exc_clear()588 for toRemove in notifiedToRemove:589 if toRemove in self.notifyObs[eventid]:590 self.notifyObs[eventid].remove(toRemove)591 return tuple(ret)592 def ScatterEvent(self, eventid, *args, **keywords):593 return self.ScatterEventWithoutTheStars(eventid, args, keywords)594 def ScatterEventWithoutTheStars(self, eventid, args, keywords = None):595 if keywords is None:596 keywords = {}597 if not eventid.startswith('On'):598 self.LogError('ScatterEvent called with event ', eventid, ". All events sent via ScatterEvent should start with 'On'.")599 self.LogError("Not only is the programmer responsible for this a 10z3r, but he wears his mother's underwear as well")600 log.LogTraceback()601 if util.IsFullLogging():602 self.LogMethodCall('ScatterEvent(', eventid, ',*args=', args, ',**kw=', keywords, ')')603 else:604 self.LogMethodCall('ScatterEvent(', eventid, ')')605 prefix = blue.pyos.taskletTimer.GetCurrent() + '::ScatterEvent_' + eventid + '::'606 for srv in self.notify.get(eventid, []):607 try:608 logname = prefix + self.GetLogName(srv)609 if srv.state == service.SERVICE_RUNNING:610 self.LogMethodCall('Calling ', logname)611 srvGuid = getattr(srv, '__guid__', logname)612 uthread.worker(prefix + srvGuid, self.MollycoddledUthread, srvGuid, eventid, getattr(srv, eventid), args, keywords)613 else:614 self.LogMethodCall('Skipping ', logname, ' (service not running)')615 except Exception:616 log.LogException()617 notifiedToRemove = []618 for weakObj in self.notifyObs.get(eventid, []):619 try:620 obj = weakObj()621 func = getattr(obj, eventid, None)622 if obj is None or func is None:623 notifiedToRemove.append(weakObj)624 else:625 logname = prefix + str(obj)626 self.LogMethodCall('Calling ', logname)627 uthread.workerWithoutTheStars('', func, args, keywords)628 except Exception:629 log.LogException()630 for toRemove in notifiedToRemove:631 if toRemove in self.notifyObs[eventid]:632 self.notifyObs[eventid].remove(toRemove)633 def NotifySessionChange(self, eventid, *args, **keywords):634 return self.NotifySessionChangeWithoutTheStars(eventid, args, keywords)635 def NotifySessionChangeWithoutTheStars(self, eventid, args, keywords = None):636 if keywords is None:637 keywords = {}638 if eventid not in ('DoSessionChanging', 'ProcessSessionChange', 'OnSessionChanged'):639 raise RuntimeError('NotifySessionChange called with eventid ', eventid, '. Must be one of DoSessionChanging, ProcessSessionChange or OnSessionChanged')640 if eventid == 'ProcessSessionChange' and stackless.getcurrent().block_trap or stackless.getcurrent().is_main:641 raise RuntimeError("ChainEvent (NotifySessionChange) is blocking by design, but you're block trapped.")642 if eventid == 'DoSessionChanging':643 old_block_trap = stackless.getcurrent().block_trap644 stackless.getcurrent().block_trap = 1645 self.LogMethodCall('NotifySessionChange(', eventid, ',*args=', args, ',**kw=', keywords, ')')646 prefix = blue.pyos.taskletTimer.GetCurrent() + '::NotifySessionChange_' + eventid + '::'647 ret = []648 myNodeID = self.services['machoNet'].GetNodeID()649 isSessionRelevant = boot.role != 'server' or session.contextOnly650 try:651 for srv in self.notify.get(eventid, []):652 try:653 logname = prefix + self.GetLogName(srv)654 if srv.state == service.SERVICE_RUNNING:655 self.LogMethodCall('Calling ' + logname)656 if eventid == 'OnSessionChanged':657 if not isSessionRelevant and srv.MachoResolve(session.GetActualSession()) == myNodeID:658 isSessionRelevant = True659 srvGuid = getattr(srv, '__guid__', logname)660 uthread.worker(prefix + srvGuid, self.MollycoddledUthread, srvGuid, eventid, getattr(srv, eventid), args, keywords)661 else:662 retval = ClockThisWithoutTheStars(logname, getattr(srv, eventid), args, keywords)663 ret.append(retval)664 else:665 self.LogMethodCall('Skipping ', logname, ' (service not running)')666 except StandardError:667 log.LogException('In %s.%s' % (getattr(srv, '__guid__', self.GetLogName(srv)), eventid))668 sys.exc_clear()669 notifiedToRemove = []670 for weakObj in self.notifyObs.get(eventid, []):671 obj = weakObj()672 if obj is None or not getattr(obj, eventid, None):673 notifiedToRemove.append(weakObj)674 else:675 try:676 logname = prefix + str(obj)677 self.LogMethodCall('Calling ', logname)678 if eventid == 'OnSessionChanged':679 uthread.workerWithoutTheStars('', getattr(obj, eventid), args, keywords)680 else:681 ClockThisWithoutTheStars(prefix + self.GetLogName(weakObj), getattr(obj, eventid), args, keywords)682 except StandardError:683 self.LogError('In %s.%s' % (getattr(weakObj, '__guid__', self.GetLogName(srv)), eventid))684 log.LogException()685 sys.exc_clear()686 for toRemove in notifiedToRemove:687 if toRemove in self.notifyObs[eventid]:688 self.notifyObs[eventid].remove(toRemove)689 if not session.contextOnly:690 if not isSessionRelevant:691 if session.irrelevanceTime is None:692 session.irrelevanceTime = blue.os.GetWallclockTime()693 log.LogInfo('CTXSESS: session ', session.sid, ' = ', session, ' is no longer relevant to any service')694 elif session.irrelevanceTime is not None:695 log.LogInfo('CTXSESS: session ', session.sid, ' became relevant again')696 session.irrelevanceTime = None697 finally:698 if eventid == 'DoSessionChanging':699 bt = 0700 if old_block_trap:701 bt = 1702 stackless.getcurrent().block_trap = bt703 return tuple(ret)704 def MollycoddledUthread(self, guid, eventid, func, args, keywords):705 try:706 apply(func, args, keywords)707 except:708 self.LogError('In %s.%s' % (guid, eventid))709 log.LogException()710 sys.exc_clear()711 def FavourMe(self, fn):712 self.notify[fn.__name__].remove(fn.im_self)713 self.notify[fn.__name__] = [fn.im_self] + self.notify[fn.__name__]714 def UnfavourMe(self, fn):715 if fn.im_self in self.notify[fn.__name__]:716 self.notify[fn.__name__].remove(fn.im_self)717 self.notify[fn.__name__] = self.notify[fn.__name__] + [fn.im_self]718 else:719 self.LogWarn('Cannot unfavour ', fn.im_self, ' from ', fn.__name__, ", since it's not a notification listener")720 def RegisterForNotifyEvent(self, ob, notify):721 if not self.notify.has_key(notify):722 self.notify[notify] = []723 if ob not in self.notify[notify]:724 self.LogInfo('Adding event', notify, 'for', ob)725 self.notify[notify].append(ob)726 def RegisterNotify(self, ob):727 if hasattr(ob, '__notifyevents__'):728 if isinstance(ob, service.Service):729 for notify in ob.__notifyevents__:730 if not self.notify.has_key(notify):731 self.notify[notify] = []732 if ob not in self.notify[notify]:733 self.notify[notify].append(ob)734 else:735 for notify in ob.__notifyevents__:736 if not self.notifyObs.has_key(notify):737 self.notifyObs[notify] = []738 if weakref.ref(ob) not in self.notifyObs[notify]:739 self.notifyObs[notify].append(weakref.ref(ob))740 else:741 self.LogError('An object is calling registernotify without there being any notifyevents, the object is ', ob)742 log.LogTraceback()743 def UnregisterNotify(self, ob):744 if hasattr(ob, '__notifyevents__'):745 if isinstance(ob, service.Service):746 for notify in ob.__notifyevents__:747 if self.notify.has_key(notify):748 if ob in self.notify[notify]:749 self.notify[notify].remove(ob)750 else:751 for notify in ob.__notifyevents__:752 if self.notifyObs.has_key(notify):753 self.notifyObs[notify] = filter(lambda x: x != weakref.ref(ob) and x() is not None, self.notifyObs[notify])754 else:755 self.LogError('An object is calling unregisternotify without there being any notifyevents, the object is ', ob)...
ranger.py
Source:ranger.py
1import os2import logging3import requests4import ambari.api as api5from utils.utils import logmethodcall6class RangerRequestError(Exception):7 pass8class Ranger:9 def __init__(self, request_timeout=10):10 self.timeout = request_timeout11 self.ranger_schema = os.environ.get('RANGER_SCHEMA', 'http')12 self.ranger_host = os.environ.get('RANGER_HOST', 'sandbox.hortonworks.com')13 self.ranger_port = os.environ.get('RANGER_PORT', 6080)14 logging.basicConfig(level=logging.DEBUG,15 format='{asctime} ({levelname}) {funcName}(): {message}',16 style="{",17 filename='ranger.log')18 self.logger = logging.getLogger(__name__)19 @logmethodcall20 def get_ranger_url(self):21 return '{0}://{1}:{2}/'.format(self.ranger_schema, self.ranger_host, self.ranger_port)22 @logmethodcall23 def is_ranger_online(self):24 try:25 requests.get(self.get_ranger_url(), timeout=self.timeout)26 return True27 except:28 return False29 @logmethodcall30 def stop_ranger_admin(self):31 ambari = api.Api(logger=self.logger)32 ranger_admin_ambari_info = ambari.get_component_info('RANGER', 'RANGER_ADMIN')33 rnd_ranger_host, rnd_ranger_component = ambari.get_random_host_and_component_path(ranger_admin_ambari_info)34 self.logger.info("Selected random Ranger admin host for stopping: {0}, {1}"35 .format(rnd_ranger_host, rnd_ranger_component))36 ambari.change_host_component_state_and_wait(rnd_ranger_component, state='INSTALLED')37 @logmethodcall38 def check_ranger_status(self):39 ranger_url = '{0}://{1}:{2}/'.format(self.ranger_schema, self.ranger_host, self.ranger_port)40 self.logger.debug(ranger_url)41 response = requests.get(ranger_url, timeout=self.timeout)42 self.verify_ranger_response(response)43 @logmethodcall44 def verify_ranger_response(self, response):45 if response.status_code != 200:46 self.logger.error(47 "RangerResponse returned with error status [{0}], response was: {1}".format(response.status_code,48 response.text))...
auto_logger.py
Source:auto_logger.py
1import logging2from typing import Tuple3from .config import Config4logger = logging.getLogger(__name__)5def logMethodCall(func):6 def inner(self, *args, **kwargs):7 objStr = repr(self)8 ret = func(self, *args, **kwargs)9 if func.__name__ not in Config.ignoreMethods.get(type(self), set({})):10 Config.log(Config.format(args, kwargs, ret, objStr=objStr, method=func))11 return ret12 return inner13def logFuncCall(func):14 def inner(*args, **kwargs):15 ret = func(*args, **kwargs)16 Config.log(Config.format(args, kwargs, ret, func=func))17 return ret18 return inner19class MethodLoggerMeta(type):20 def __new__(cls, name: str, bases: Tuple[type], attrs: dict):21 attrs_copy = attrs.copy()22 for key, value in attrs.items():23 if callable(value) and not key.startswith("__"):24 attrs_copy[key] = logMethodCall(value)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!