def received(self): consumed_data = False for process in list(self._process): try: proc = self._process[process] poll = proc.poll() # proc.poll returns None if the process is still fine # -[signal], like -15, if the process was terminated if poll is not None: self._handle_problem(process) return r, _, _ = select.select([ proc.stdout, ], [], [], 0) if not r: continue try: # Calling next() on Linux and OSX works perfectly well # but not on OpenBSD where it always raise StopIteration # and only readline() works buf = str_ascii(proc.stdout.read(16384)) if buf == '' and poll is not None: # if proc.poll() is None then # process is fine, we received an empty line because # we're doing .readline() on a non-blocking pipe and # the process maybe has nothing to send yet self._handle_problem(process) continue raw = self._buffer.get(process, '') + buf while '\n' in raw: line, raw = raw.split('\n', 1) line = line.rstrip() consumed_data = True self.logger.debug( 'command from process %s : %s ' % (process, line), 'process') yield (process, formated(line)) self._buffer[process] = raw except IOError as exc: if not exc.errno or exc.errno in error.fatal: # if the program exits we can get an IOError with errno code zero ! self._handle_problem(process) elif exc.errno in error.block: # we often see errno.EINTR: call interrupted and # we most likely have data, we will try to read them a the next loop iteration pass else: self.logger.debug( 'unexpected errno received from forked process (%s)' % errstr(exc), 'process') except StopIteration: if not consumed_data: self._handle_problem(process) except (subprocess.CalledProcessError, OSError, ValueError): self._handle_problem(process)
def received(self): consumed_data = False buffered = {} for process in list(self._process): try: proc = self._process[process] # proc.poll returns None if the process is still fine # -[signal], like -15, if the process was terminated if proc.poll() is not None and self.reactor.respawn: raise ValueError('child died') r, _, _ = select.select([ proc.stdout, ], [], [], 0) if r: try: while True: # Calling next() on Linux and OSX works perfectly well # but not on OpenBSD where it always raise StopIteration # and only readline() works raw = buffered.get(process, '') + proc.stdout.readline() if not raw.endswith('\n'): buffered[process] = raw continue buffered[process] = '' line = raw.rstrip() consumed_data = True self.logger.processes( "Command from process %s : %s " % (process, line)) if raw == '': raise IOError('Child process died') yield (process, formated(line)) except IOError, exc: if not exc.errno or exc.errno in error.fatal: # if the program exists we can get an IOError with errno code zero ! self.logger.processes( "Issue with the process, terminating it and restarting it" ) self._terminate(process) self._start(process) elif exc.errno in error.block: # we often see errno.EINTR: call interrupted and # we most likely have data, we will try to read them a the next loop iteration pass else: self.logger.processes( "unexpected errno received from forked process (%s)" % errstr(exc)) except StopIteration: if not consumed_data: self.logger.processes( "The process died, trying to respawn it") self._terminate(process) self._start(process)
def api_refresh(self, command): tokens = formated(command).split(' ')[2:] if len(tokens) != 2: return False afi = AFI.value(tokens.pop(0)) safi = SAFI.value(tokens.pop(0)) if afi is None or safi is None: return False return RouteRefresh(afi, safi)
def api_refresh (self, command): tokens = formated(command).split(' ')[2:] if len(tokens) != 2: return False afi = AFI.value(tokens.pop(0)) safi = SAFI.value(tokens.pop(0)) if afi is None or safi is None: return False return RouteRefresh(afi,safi)
def received (self): consumed_data = False for process in list(self._process): try: proc = self._process[process] poll = proc.poll() # proc.poll returns None if the process is still fine # -[signal], like -15, if the process was terminated if poll is not None: self._handle_problem(process) return r,_,_ = select.select([proc.stdout,],[],[],0) if not r: continue try: # Calling next() on Linux and OSX works perfectly well # but not on OpenBSD where it always raise StopIteration # and only readline() works buf = str_ascii(proc.stdout.read(16384)) if buf == '' and poll is not None: # if proc.poll() is None then # process is fine, we received an empty line because # we're doing .readline() on a non-blocking pipe and # the process maybe has nothing to send yet self._handle_problem(process) continue raw = self._buffer.get(process,'') + buf while '\n' in raw: line,raw = raw.split('\n',1) line = line.rstrip() consumed_data = True self.logger.debug('command from process %s : %s ' % (process,line),'process') yield (process,formated(line)) self._buffer[process] = raw except IOError as exc: if not exc.errno or exc.errno in error.fatal: # if the program exits we can get an IOError with errno code zero ! self._handle_problem(process) elif exc.errno in error.block: # we often see errno.EINTR: call interrupted and # we most likely have data, we will try to read them a the next loop iteration pass else: self.logger.debug('unexpected errno received from forked process (%s)' % errstr(exc),'process') except StopIteration: if not consumed_data: self._handle_problem(process) except (subprocess.CalledProcessError,OSError,ValueError): self._handle_problem(process)
def api_operational(self, command): tokens = formated(command).split(' ') op = tokens[1].lower() what = tokens[2].lower() if op != 'operational': return False self.configuration.tokeniser.iterate.replenish(tokens[3:]) # None or a class return operational(what, self.configuration.tokeniser.iterate)
def api_operational (self, command): tokens = formated(command).split(' ') op = tokens[1].lower() what = tokens[2].lower() if op != 'operational': return False self.configuration.tokeniser.iterate.replenish(tokens[3:]) # None or a class return operational(what,self.configuration.tokeniser.iterate)
def api_operational (self, command): tokens = formated(command).split(' ',3) if len(tokens) != 4: return False operational = tokens[1].lower() what = tokens[2].lower() if operational != 'operational': return False # None or a class return self.operational.operational(what,tokens[3])
def received (self): consumed_data = False buffered = {} for process in list(self._process): try: proc = self._process[process] # proc.poll returns None if the process is still fine # -[signal], like -15, if the process was terminated if proc.poll() is not None and self.reactor.respawn: raise ValueError('child died') r,_,_ = select.select([proc.stdout,],[],[],0) if r: try: while True: # Calling next() on Linux and OSX works perfectly well # but not on OpenBSD where it always raise StopIteration # and only readline() works raw = buffered.get(process,'') + proc.stdout.readline() if not raw.endswith('\n'): buffered[process] = raw continue buffered[process] = '' line = raw.rstrip() consumed_data = True self.logger.processes("Command from process %s : %s " % (process,line)) if raw == '': raise IOError('Child process died') yield (process,formated(line)) except IOError,exc: if not exc.errno or exc.errno in error.fatal: # if the program exists we can get an IOError with errno code zero ! self.logger.processes("Issue with the process, terminating it and restarting it") self._terminate(process) self._start(process) elif exc.errno in error.block: # we often see errno.EINTR: call interrupted and # we most likely have data, we will try to read them a the next loop iteration pass else: self.logger.processes("unexpected errno received from forked process (%s)" % errstr(exc)) except StopIteration: if not consumed_data: self.logger.processes("The process died, trying to respawn it") self._terminate(process) self._start(process)
def api_eor(self, command): tokens = formated(command).split(' ')[2:] number = len(tokens) if not number: return Family(1, 1) if number != 2: return False afi = AFI.fromString(tokens[0]) if afi == AFI.undefined: return False safi = SAFI.fromString(tokens[1]) if safi == SAFI.undefined: return False return Family(afi, safi)
def api_eor (self, command): tokens = formated(command).split(' ')[2:] number = len(tokens) if not number: return Family(1,1) if number != 2: return False afi = AFI.fromString(tokens[0]) if afi == AFI.undefined: return False safi = SAFI.fromString(tokens[1]) if safi == SAFI.undefined: return False return Family(afi,safi)