def test_logger(): """ Print out some messages with different log levels """ cl = getLogger('engine') cl = getLogger('engine') assert cl is not None cl.debug('debug') cl.info('info') cl.warning('warning') cl.error('error') cl.fatal('fatal')
def __init__ (self, adaptor_info, adaptor_options=[]) : self._info = adaptor_info self._opts = adaptor_options self._name = adaptor_info['name'] self._schemas = adaptor_info['schemas'] self._logger = sul.getLogger (self._name) has_enabled = False for option in self._opts : if option['name'] == 'enabled' : has_enabled = True if not has_enabled : # *every* adaptor needs an 'enabled' option! self._opts.append ({ 'category' : self._name, 'name' : 'enabled', 'type' : bool, 'default' : True, 'valid_options' : [True, False], 'documentation' : "Enable / disable loading of the adaptor", 'env_variable' : None } ) suc.Configurable.__init__ (self, self._name, self._opts)
def __init__ (self) : self.logger = sul.getLogger ('PTYShellFactory') self.registry = {} self.rlock = threading.RLock () self.logger.debug ("PTYShellFactory init %s" % self)
def __init__ (self, url, session=None, logger=None, init=None, opts={}) : if None != logger : self.logger = logger else : self.logger = sul.getLogger ('PTYShell') self.logger.debug ("PTYShell init %s" % self) self.url = url # describes the shell to run self.init = init # call after reconnect self.opts = opts # options... self.latency = 0.0 # set by factory self.prompt = None self.prompt_re = None self.initialized = False # we need a local dir for file staging caches. At this point we use # $HOME, but should make this configurable (FIXME) self.base = os.environ['HOME'] + '/.saga/adaptors/shell/' try: os.makedirs (self.base) except OSError as e: if e.errno == errno.EEXIST and os.path.isdir (self.base): pass else: raise se.NoSuccess ("could not create staging dir: %s" % e) self.factory = supsf.PTYShellFactory () self.pty_info = self.factory.initialize (url, session, self.logger) self.pty_shell = self.factory.run_shell (self.pty_info) self.initialize ()
def __init__ (self) : """ Make sure the object cache dict is initialized, exactly once. """ with self._lock : self._cache = {} self._logger = slog.getLogger ('saga.utils.object_cache')
def initialize (self, url, session=None, logger=None) : with self.rlock : # make sure we have a valid session, and a valid url type if not session : session = saga.Session (default=True) url = saga.Url (url) if not logger : logger = sul.getLogger ('PTYShellFactory') # collect all information we have/need about the requested master # connection info = self._create_master_entry (url, session, logger) # we got master info - register the master, and create the instance! type_s = str(info['type']) user_s = str(info['user']) host_s = str(info['host_str']) # Now, if we don't have that master, yet, we need to instantiate it if not host_s in self.registry : self.registry[host_s] = {} if not user_s in self.registry[host_s] : self.registry[host_s][user_s] = {} if not type_s in self.registry[host_s][user_s] : # new master: create an instance, and register it m_cmd = _SCRIPTS[info['type']]['master'] % info self.logger.debug ("open master pty for [%s] [%s] %s: %s'" \ % (type_s, host_s, user_s, m_cmd)) info['pty'] = saga.utils.pty_process.PTYProcess (m_cmd, logger=logger) if not info['pty'].alive () : raise se.NoSuccess._log (logger, \ "Shell not connected to %s" % info['host_str']) # authorization, prompt setup, etc self._initialize_pty (info['pty'], info) # master was created - register it self.registry[host_s][user_s][type_s] = info else : # we already have a master: make sure it is alive, and restart as # needed info = self.registry[host_s][user_s][type_s] if not info['pty'].alive (recover=True) : raise se.IncorrectState._log (logger, \ "Lost shell connection to %s" % info['host_str']) return info
def __init__ (self, url) : if url.scheme != 'redis' : raise BadParameter ("scheme in url is not supported (%s != redis://...)" % url) self.url = url self.host = 'localhost' self.port = 6379 self.db = 0 self.password = None self.errors = 'strict' if url.host : self.host = url.host if url.port : self.port = url.port if url.username : self.username = url.username if url.password : self.password = url.password # create redis client t1 = time.time () redis.Redis.__init__ (self, host = self.host, port = self.port, db = self.db, password = self.password, errors = self.errors) t2 = time.time () # add a logger self.logger = getLogger ("redis-%s" % self.host) # create a cache dict and attach to redis client instance. Cache # lifetime is set to 10 times the redis-connect latency. self.cache = redis_cache.Cache (logger=self.logger, ttl=((t2-t1)*10)) # create a second client to manage the (blocking) # pubsub communication for event notifications self.r2 = redis.Redis (host = self.host, port = self.port, db = self.db, password = self.password, errors = self.errors) # set up pubsub endpoint, and start a thread to monitor channels self.callbacks = {} self.pub = self.r2.pubsub () self.pub.subscribe (MON) # FIXME: create one pubsub channel per path (for paths which have # callbacks registered) self.monitor = redis_ns_monitor (self, self.pub) self.monitor.start ()
def __init__ (self): # set the default configuration options for this object sconf.Configurable.__init__(self, 'saga.tests', _config_options) self._global_cfg = sconf.Configuration () self.read_config () # Initialize the logging self._logger = slog.getLogger ('saga.tests') self._test_cfg_d = {} self._bench_cfg_d = {}
def __init__ (self, command, logger=None) : """ The class constructor, which runs (execvpe) command in a separately forked process. The bew process will inherit the environment of the application process. :type command: string or list of strings :param command: The given command is what is run as a child, and fed/drained via pty pipes. If given as string, command is split into an array of strings, using :func:`shlex.split`. :type logger: :class:`saga.utils.logger.Logger` instance :param logger: logger stream to send status messages to. """ self.logger = logger if not self.logger : self.logger = sul.getLogger ('PTYProcess') self.logger.debug ("PTYProcess init %s" % self) if isinstance (command, basestring) : command = shlex.split (command) if not isinstance (command, list) : raise se.BadParameter ("PTYProcess expects string or list command") if len(command) < 1 : raise se.BadParameter ("PTYProcess expects non-empty command") self.rlock = threading.RLock () self.command = command # list of strings too run() self.cache = "" # data cache self.child = None # the process as created by subprocess.Popen self.ptyio = None # the process' io channel, from pty.fork() self.exit_code = None # child died with code (may be revived) self.exit_signal = None # child kill by signal (may be revived) self.recover_max = 3 # TODO: make configure option. This does not self.recover_attempts = 0 # apply for recovers triggered by gc_timeout! try : self.initialize () except Exception as e : raise se.NoSuccess ("pty or process creation failed (%s)" % e)
def __init__(self): # Engine manages cpis from adaptors self._adaptor_registry = {} # set the configuration options for this object sconf.Configurable.__init__(self, 'saga.engine', _config_options) self._cfg = self.get_config() # Initialize the logging self._logger = slog.getLogger ('saga.engine') # load adaptors self._load_adaptors ()
def __init__ (self, api, adaptor) : self._session = None self._adaptor = adaptor self._cpi_cname = self.__class__.__name__ self._logger = sul.getLogger (self._cpi_cname) # The API object must obviously keep an adaptor instance. If we also # keep an API instance ref in this adaptor base, we create a ref cycle # which will annoy (i.e. disable) garbage collection. We thus use weak # references to break that cycle. The inheriting classes MUST use # get_api() to obtain the API reference. if api : self._api = weakref.ref (api) else : self._api = None # by default, we assume that no bulk optimizations are supported by the # adaptor class. Any adaptor class supporting bulks ops must overwrite # the ``_container`` attribute (via # ``self._set_container(container=None)``, and have it point to the # class which implements the respective ``container_*`` methods. self._container = None
def test_singleton(): """ Test if the logger behaves like a singleton """ # make sure singleton works assert getLogger() == getLogger() assert getLogger('engine') == getLogger('engine')
def __init__ (self) : self.logger = sul.getLogger ('PTYShellFactory') self.registry = {} self.rlock = sut.RLock ('pty shell factory')