def mesage_stdouts(prefix, stdout, stderr, enc='utf8'): msg = prefix if not stdout and not stderr: return msg msg += ", output follows:" if stdout: msg += "\n==STDOUT==\n" + utils.b2s(stdout, enc) if stderr: msg += "\n==STDERR==\n" + utils.b2s(stderr, enc) return msg
def file_as_string(fname, enc='utf8'): b = pkg_resources.resource_string(__name__, fname) # @UndefinedVariable return utils.b2s(b, enc)
def loop(self): self.connect_monitor_host() inputlist = [self.monitor_socket] just_re_connected = False while 1: if (just_re_connected): just_re_connected = False inputready, _, _ = select.select( inputlist + list(self._dfms_sock_dict.values()), [], [], delay) for the_socket in inputready: if (just_re_connected): continue if (the_socket == self.monitor_socket): data = recv_from_monitor(the_socket) if (data is None): logger.warning("Socket to dfms monitor is broken") inputlist.remove(the_socket) if (self.monitor_socket): self.monitor_socket.close() self.monitor_socket = None logger.info("Try reconnecting to dfms monitor...") self.connect_monitor_host() just_re_connected = True inputlist.append(self.monitor_socket) continue at = data.find(delimit) if (at == -1): logger.error( 'No tag id from dfms_monitor, discard the message') continue else: tag = data[0:at] logger.debug("Received {0} from Monitor".format(tag)) dfms_sock = self._dfms_sock_dict.get(tag, None) to_send = data[at + dl:] if (dfms_sock is None): if (len(to_send) > 0): dfms_sock = self.connect_to_host( self._dfms_host, self._dfms_port) self._dfms_sock_dict[tag] = dfms_sock self._dfms_sock_tag_dict[dfms_sock] = tag send_to_dfms = True else: send_to_dfms = False else: send_to_dfms = True if (send_to_dfms): try: dfms_sock.sendall(to_send) logger.debug( "Sent {0} to DFMS manager".format(tag)) except socket.error: self.close_dfms_socket(dfms_sock, tag) else: # from one of the DFMS sockets data = the_socket.recv(BUFF_SIZE) tag = self._dfms_sock_tag_dict.get(the_socket, None) logger.debug("Received {0} from DFMS manager".format( b2s(tag))) if (tag is None): logger.error('Tag for dfms socket {0} is gone'.format( the_socket)) else: send_to_monitor(self.monitor_socket, delimit.join([tag, data])) logger.debug("Sent {0} to Monitor".format(b2s(tag))) if (len(data) == 0): self.close_dfms_socket(the_socket, tag)