Exemplo n.º 1
0
 def status(socket, addr):
     pool = lb.pool
     status = pool.status()
     writer = socket.makefile('w')
     writer.write(json.dumps(status) + '\n')
     writer.close()
     socket.close()
Exemplo n.º 2
0
    def _handle_connection(self, socket, addr):
        socket.setsockopt(gevent.socket.SOL_TCP, gevent.socket.TCP_NODELAY, 1)
        socket.setsockopt(gevent.socket.IPPROTO_TCP, gevent.socket.TCP_NODELAY, 1)
        f = socket.makefile('r+b', bufsize=0)

        #rsp_queue = gevent.queue.Queue()
        #is_connection_closed = [False]

  
        def recv_req():
            content = ""
            while True:
                try:
                    recv_buf = f.read()
                    content += recv_buf
                    #print " got  data .." + str(len(recv_buf))
                    if not recv_buf :
                        break
                except Exception, e:
                    logging.warning('recv_req error: ' + str(e))
                    break



            # Evaluate and execute the request
            rpcResponse = self.validateAndExecuteRequest(content)
            log.debug("Response to return to client \n %s" % rpcResponse)

            f.write(rpcResponse.SerializeToString())
            f.flush()
Exemplo n.º 3
0
	def listen( self, socket,address):
		f = socket.makefile()
		print "listen"

		while True:
			name = f.readline().strip()
			print name
    def stream_server_handler(self, socket, address):
        print('New connection from %s:%s' % address)

        serial_socket = None

        try:
            serial_socket = serial.Serial(self.serial_url,
                                          self.baudrate,
                                          timeout=1,
                                          writeTimeout=1)
            reader_sapwn = gevent.spawn(self.serial_socket_reader,
                                        serial_socket, socket)

            # using a makefile because we want to use readline()
            rfileobj = socket.makefile(mode='rb')

            while True:
                line = rfileobj.readline()

                if not line:
                    print("client disconnected")
                    break

                serial_socket.write(line)

            rfileobj.close()
        finally:
            if serial_socket:
                serial_socket.close()
def consumerForward(socket, address):
	fp = socket.makefile()
	myid = socket.recv(1024)
	glo_consumer_socket[myid] = socket
	gevent.sleep(0)
	while True:
		gevent.sleep(100)
Exemplo n.º 6
0
 def __init__(self, socket, address, server, rfile=None):
     self.socket = socket
     self.client_address = address
     self.server = server
     if rfile is None:
         self.rfile = socket.makefile('rb', -1)
     else:
         self.rfile = rfile
 def listen(self, socket, address):
     f = socket.makefile()
     while True:
         line = f.readline()
         if not line:
             print 'client died'
             break
         self.write_to_children(address, line)
Exemplo n.º 8
0
 def __init__(self, socket, address, server, rfile=None):
     self.socket = socket
     self.client_address = address
     self.server = server
     if rfile is None:
         self.rfile = socket.makefile('rb', -1)
     else:
         self.rfile = rfile
Exemplo n.º 9
0
	def checkClient(self, client):
		#Ping client, if it responds, then don't worry
		#if it doesn't, then remove it from self.clientlist
		socket, address = client
		socketFile = socket.makefile()
		socketFile.write('pinging, are you online?')
		socketFile.flush()
		socketFile.close()
 def listen(self, socket, address):
     f = socket.makefile()
     while True:
         line = f.readline()
         if not line:
             print 'client died'
             break
         self.write_to_children(address, line)
Exemplo n.º 11
0
    def __init__(self, socket):
        # Use file-likes instead of socket-like objects
        if isinstance(socket, gevent.socket.socket):
            self._act_socket = socket
            self._socket = socket.makefile("r+", 0)
        else:
            self._socket = socket

        self._run = True
Exemplo n.º 12
0
 def __init__(self, socket, address, connect_timeout=5):
     inactivity_timeout = 3600
     self.sock = socket
     # safe guard inactivity timeout
     self.sock.settimeout(inactivity_timeout)
     self.sockfd = socket.makefile()
     self.address = address
     self.timeout = connect_timeout
     self.closed = False
Exemplo n.º 13
0
    def __init__(self, socket):
        # Use file-likes instead of socket-like objects
        if isinstance(socket, gevent.socket.socket):
            self._act_socket = socket
            self._socket = socket.makefile("r+", 0)
        else:
            self._socket = socket

        self._run = True
Exemplo n.º 14
0
    def connection_handler(self, socket, address):
        event_string = ""
        for l in socket.makefile('r'):
            event_string += l

        try:
            event = pickle.loads(event_string)
            self.send_event(event)
        except Exception:
            self.logger.error("Received invalid event format: {0}".format(event_string))
    def __call__(self, socket, address):
        print 'New connection from %s:%s' % address

        fileobj = socket.makefile()
        fileobj.write('Welcome to the chat server! Type quit to exit.\r\n')
        fileobj.write('In %s\n\n' % current_process().name)
        fileobj.flush()

        gevent.spawn(self.wait_for_green_events, fileobj)
        gevent.spawn(self.handle, fileobj, address).join()
Exemplo n.º 16
0
def test_leader_first_returns_existing_cluster():
    cm = ClusterManagerMock()
    cm.cluster = set(['a', 'b', 'c'])
    with cluster.PeerServer(cm, ('127.0.0.1', port)) as s:
        socket = gevent.socket.create_connection(('127.0.0.1', port))
        sockfile = socket.makefile()
        line = sockfile.readline()
        obj = json.loads(line.strip())
        assert set(obj['cluster']) == cm.cluster, \
            "First line didn't report correct cluster: %s" % line
 def __call__(self, socket, address):
     print 'New connection from %s:%s' % address
 
     fileobj = socket.makefile()
     fileobj.write('Welcome to the chat server! Type quit to exit.\r\n')
     fileobj.write('In %s\n\n' % current_process().name)
     fileobj.flush()
     
     gevent.spawn(self.wait_for_green_events, fileobj)
     gevent.spawn(self.handle, fileobj, address).join()
Exemplo n.º 18
0
 def __call__(self, socket, address):
     ("__call__("
      "socket:gevent.socket.socket, "
      "address:Tuple[str, int]"
      ") -> None")
     reader = socket.makefile(mode='rb')
     try:
         while 1:
             with gevent.Timeout(header_parsing_timeout):
                 try:
                     environ = new_environ(reader, server_side=True)
                 except:
                     if self.verbose and \
                        logging.DEBUG >= gvars.levels[self.verbose]:
                         raise
                     else:
                         return
             environ['REMOTE_ADDR'] = address[0]
             environ['REMOTE_PORT'] = address[1]
             rw = self.file_type(socket, reader, environ)
             try:
                 self.handler(rw)
             except:
                 if self.verbose and \
                    logging.DEBUG >= gvars.levels[self.verbose]:
                     raise
                 else:
                     return
             if not rw.closed or rw.disconnected:
                 return
             if environ.get('HTTP_CONNECTION', '') \
                       .lower() == 'keep_alive':
                 keep_alive = environ.get('HTTP_KEEP_ALIVE', '300')
             else:
                 keep_alive = environ.get('HTTP_KEEP_ALIVE')
             if keep_alive is None or \
                not regx_keep_alive.match(keep_alive):
                 return
             left = rw.left
             if 8192 > left > 0:
                 reader.read(left)
             if left != 0:
                 return
             n_keep_alive = min(int(keep_alive), max_keep_alive)
             try:
                 gevent.socket.wait_read(socket.fileno(), n_keep_alive)
             except:
                 if self.verbose and \
                    logging.DEBUG >= gvars.levels[self.verbose]:
                     raise
                 else:
                     return
     finally:
         reader.close()
         socket.close()
Exemplo n.º 19
0
def on_handle(socket, address):
    try:
        fileobj = socket.makefile()
        while True:
            data = fileobj.readline()
            if not data:
                # client disconnected
                break
            socket.sendall(data)
    finally:
        socket.close()
Exemplo n.º 20
0
def on_handle(socket, address):
    try:
        fileobj = socket.makefile()
        while True:
            data = fileobj.readline()
            if not data:
                # client disconnected
                break
            socket.sendall(data)
    finally:
        socket.close()
Exemplo n.º 21
0
def on_handle(socket, address):
    try:
        fileobj = socket.makefile() #encoding="utf-8", errors="ignore"
        while True:
            data = fileobj.readline()
            if not data:
                # client disconnected
                break            
            socket.sendall(bytes(data, "utf-8"))
    finally:
        socket.close()
Exemplo n.º 22
0
def test_nonleader_redirects_to_leader_with_port():
    cm = ClusterManagerMock()
    cm.is_leader = False
    with cluster.PeerServer(cm, ('127.0.0.1', port)) as s:
        socket = gevent.socket.create_connection(('127.0.0.1', port))
        sockfile = socket.makefile()
        line = sockfile.readline()
        obj = json.loads(line.strip())
        assert obj['leader'] == cm.client.leader_address[0], \
            "First line didn't point to leader: %s" % line
        assert 'port' in obj, "Did not include port"
Exemplo n.º 23
0
    def connection_handler(self, socket, address):
        event_string = ""
        for l in socket.makefile('r'):
            event_string += l

        try:
            event = pickle.loads(event_string)
            self.send_event(event)
        except:
            self.logger.error(
                "Received invalid event format: {0}".format(event_string))
Exemplo n.º 24
0
def test_leader_adds_node_to_cluster_by_hostip():
    cm = ClusterManagerMock()
    cm.is_leader = True
    with cluster.PeerServer(cm, ('127.0.0.1', port)) as s:
        socket = gevent.socket.create_connection(('127.0.0.1', port))
        sockfile = socket.makefile()
        line = sockfile.readline()
        socket.send("\n")
        line = sockfile.readline()
        assert '127.0.0.1' in cm.cluster, \
            "Leader did not add host to cluster: %s" % line
Exemplo n.º 25
0
def on_handle(socket, address):
    try:
        fileobj = socket.makefile()  #encoding="utf-8", errors="ignore"
        while True:
            data = fileobj.readline()
            if not data:
                # client disconnected
                break
            socket.sendall(bytes(data, "utf-8"))
    finally:
        socket.close()
 def handle(self, socket, address):
     print("new connection")
     # using a makefile because we want to use readline()
     socket_file = socket.makefile()
     while True:
         i_sample = socket_file.readline()
         preds = socket_file.readline()
         print("Number of predictions", len(self.i_pred_samples) + 1)
         print(i_sample[:-1])  # :-1 => without newline
         print(preds[:-1])
         self.all_preds.append(preds)
         self.i_pred_samples.append(i_sample)
         print("")
Exemplo n.º 27
0
 def handle(self, socket, address):
     print ("new connection")
     # using a makefile because we want to use readline()
     socket_file = socket.makefile()
     while True:
         i_sample = socket_file.readline()
         preds = socket_file.readline()
         print("Number of predictions", len(self.i_pred_samples) + 1)
         print(i_sample[:-1]) # :-1 => without newline
         print(preds[:-1])
         self.all_preds.append(preds)
         self.i_pred_samples.append(i_sample)
         print("")
Exemplo n.º 28
0
def line_protocol(socket, strip=True):
    fileobj = socket.makefile()
    while True:
        try:
            line = fileobj.readline() # returns None on EOF
            if line is not None and strip:
                line = line.strip()
        except IOError:
            line = None
        if line:
            yield line
        else:
            break
Exemplo n.º 29
0
    def __init__(self, socket, address, server, rfile=None):
        # Deprecation: The rfile kwarg was introduced in 1.0a1 as part
        # of a refactoring. It was never documented or used. It is
        # considered DEPRECATED and may be removed in the future. Its
        # use is not supported.

        self.socket = socket
        self.client_address = address
        self.server = server
        if rfile is None:
            self.rfile = socket.makefile('rb', -1)
        else:
            self.rfile = rfile
Exemplo n.º 30
0
    def handle(self, socket, address):
        fd = socket.makefile()
        while True:
            line = fd.readline()
            if not line:
                break

            name, value, timestamp = line.split()
            ds = self.context.get_data_source(name)
            ds.submit(int(timestamp), float(value))

        socket.shutdown(gevent.socket.SHUT_RDWR)
        socket.close()
Exemplo n.º 31
0
    def handle(self, socket, address):
        fd = socket.makefile()
        while True:
            line = fd.readline()
            if not line:
                break

            name, value, timestamp = line.split()
            ds = self.context.get_data_source(name)
            ds.submit(int(timestamp), float(value))

        socket.shutdown(gevent.socket.SHUT_RDWR)
        socket.close()
Exemplo n.º 32
0
    def __init__(self, socket, address, server, rfile=None):
        # Deprecation: The rfile kwarg was introduced in 1.0a1 as part
        # of a refactoring. It was never documented or used. It is
        # considered DEPRECATED and may be removed in the future. Its
        # use is not supported.

        self.socket = socket
        self.client_address = address
        self.server = server
        if rfile is None:
            self.rfile = socket.makefile('rb', -1)
        else:
            self.rfile = rfile
Exemplo n.º 33
0
	def listen2test( self, socket,address):

		print "listen"

		try	:
			f = socket.makefile()
			ports = f.readline().strip()
			port_list = ports.split()
			print port_list
			if port_list :
				ipport_list = [(address[0],port) for port in port_list ]
				self.pool.map(self.testPort,ipport_list)
		except Exception,e:
			raise
Exemplo n.º 34
0
def test_leader_adds_node_to_cluster_by_name():
    node_name = 'node-a'
    cm = ClusterManagerMock()
    cm.is_leader = True
    with cluster.PeerServer(cm, ('127.0.0.1', port)) as s:
        socket = gevent.socket.create_connection(('127.0.0.1', port))
        sockfile = socket.makefile()
        line = sockfile.readline()
        socket.send("%s\n" % node_name)
        line = sockfile.readline()
        assert node_name in cm.cluster, \
            "Leader did not add name to cluster: %s" % line
    del socket
    del sockfile
Exemplo n.º 35
0
 def handle(self, socket, address):
     fp = socket.makefile()
     while True:
         line = fp.readline()
         if not line:
             break
         player, command, args = line.strip().split(" ", 2)
         interface = media_player_interfaces[player]
         if command == "OPEN":
             interface.open(args)
         elif command == "QUEUE":
             interface.queue(args)
         elif command == "ACTION":
             interface.send_action(*args.split())
def producerForward(socket, address):
	fp = socket.makefile()
	destfp = ""
	myid = socket.recv(1024)
	gevent.sleep(0)
	while True:

		chunk = socket.recv(1024)
		if destfp == "":
			if myid in glo_consumer_socket:
				destfp = glo_consumer_socket[myid]
		else:
			destfp.send(chunk)
		gevent.sleep(0)
Exemplo n.º 37
0
    def handle(self, socket, address):
        fd = socket.makefile()
        while True:
            line = fd.readline()
            if not line:
                break

            name, value, timestamp = line.split()
            _, _, datapoint = name.partition('.')
            ds = self.context.get_data_source(
                'localhost.{0}'.format(datapoint))
            ds.submit(int(timestamp), float(value))

        socket.shutdown(gevent.socket.SHUT_RDWR)
        socket.close()
Exemplo n.º 38
0
def echo(socket, address):
    print ('New connection from %s:%s' % address)
    socket.sendall('Welcome to the echo server! Type quit to exit.\r\n'.encode('ascii'))
    # using a makefile because we want to use readline()
    fileobj = socket.makefile(mode='rw')
    while True:
        line = fileobj.readline()
        if not line:
            print ("client disconnected")
            break
        if line.strip().lower() == 'quit':
            print ("client quit")
            break
        fileobj.write(line)
        fileobj.flush()
        print ("echoed %r" % line)
Exemplo n.º 39
0
def echo(socket, address):
    print('New connection from %s:%s' % address)
    socket.sendall(
        'Welcome to the echo server! Type quit to exit.\r\n'.encode('ascii'))
    # using a makefile because we want to use readline()
    fileobj = socket.makefile(mode='rw')
    while True:
        line = fileobj.readline()
        if not line:
            print("client disconnected")
            break
        if line.strip().lower() == 'quit':
            print("client quit")
            break
        fileobj.write(line)
        fileobj.flush()
        print("echoed %r" % line)
Exemplo n.º 40
0
def test_drop_node_on_disconnect():
    node_name = 'node'
    cm = ClusterManagerMock()
    cm.is_leader = True
    with cluster.PeerServer(cm, ('127.0.0.1', port)) as s:
        socket = gevent.socket.create_connection(('127.0.0.1', port))
        sockfile = socket.makefile()
        line = sockfile.readline() # cluster list pre-join
        socket.send("%s\n" % node_name)
        line = sockfile.readline() # cluster list post-join
        assert node_name in cm.cluster, \
            "Leader did not add host to cluster: %s" % line
        socket.shutdown(0)
        # Make sure to cleanup file descriptors:
        del sockfile
        del socket
        yield_(2) # Yield to let PeerServer catch the disconnect
        assert not node_name in cm.cluster, \
            "Leader did not remove host from cluster."
Exemplo n.º 41
0
 def status(socket, addr):
     pool = lb.pool
     writer = socket.makefile('w')
     writer.write(json.dumps(
         dict(
             backlog = pool.backlog,
             mean_backlog = pool.mbacklog,
             workers = [
                 (worker.__name__,
                  worker.backlog,
                  worker.mbacklog,
                  (int(worker.oldest_time)
                   if worker.oldest_time else None),
                  )
                 for worker in sorted(
                     pool.workers, key=lambda w: w.__name__)
                 ]
             ))+'\n')
     writer.close()
     socket.close()
Exemplo n.º 42
0
def connect(socket, address):
    log.info('New connection from %s:%s' % address)
    conn = socket.makefile()
    conn.write("User: "******"town_square")
    try:
        while player.running:
            action = None
            try:
                gevent.socket.wait_read(conn.fileno(),
                                        timeout=0.01,
                                        timeout_exc=NoInput())
            except NoInput:
                pass
            else:
                line = conn.readline().strip()
                if line:
                    action = player.interpret(line)

            if not action:
                try:
                    action = player.actions.get(block=True, timeout=0.01)
                except gevent.queue.Empty:
                    pass

            if action:
                # If it's just a string, print it out
                func = getattr(action, 'execute', lambda: str(action))
                message = func()
                conn.write(message + player.prompt)
                conn.flush()

    finally:
        log.info("client disconnected")
        player.close()
Exemplo n.º 43
0
 def create_connection(self, target_addr):
     socket = gevent.socket.create_connection(self.bind_addr)
     reader = socket.makefile(mode='rb')
     socket.sendall((f'GET {self.path}{gvars.gen_filename()} HTTP/1.1\r\n'
                     f'Host: {self.host}\r\n'
                     f'User-Agent: {ins.random()}\r\n'
                     f'Accept: {gvars.accept}\r\n'
                     f'Cookie: {gvars.token_name}='
                     f'{utils.make_token(self.password)}\r\n'
                     f'Content-Type: {gvars.content_type}\r\n'
                     'Transfer-Encoding: chunked\r\n'
                     f'Accept-Language: {gvars.accept_language}\r\n'
                     'Accept-Encoding: gzip, deflate\r\n'
                     'DNT: 1\r\n'
                     'Connection: keep-alive\r\n\r\n').encode())
     environ = slowdown.http.new_environ(reader, server_side=False)
     if '200' != environ['RESPONSE_STATUS']:
         raise BrokenPipeError(errno.EPIPE, 'Authentication failed')
     rw = SSRWPair(socket, reader, environ)
     via_socket = ShadowSocksSocket(self.cipher, rw)
     via_socket.sendall(utils.pack_addr(target_addr))
     return via_socket
Exemplo n.º 44
0
def handle_incoming_req(socket, address):
    print('New connection from %s:%s' % address)
    # using a makefile because we want to use the socket as an FD
    fileobj = socket.makefile()
    while True:
        line = fileobj.readline()
        if not line or line.strip().lower() == 'quit':
            print("client %s:%s disconnected" % address)
            break
        try:
            # Split the input on whitespace to get the two integers provided
            first, second = (int(x) for x in line.split())
        except:
            continue

        if (first > second):
            lower = second
            upper = first
        else:
            lower = first
            upper = second

        greenlets = [gevent.spawn(compute_3nplus1, x) for x in range(lower,
                                                                     upper + 1)]
        gevent.joinall(greenlets)

        #Fetch the computed cycle-lengths and determine the max
        max_cycle_len = 0
        for greenlet in greenlets:
            if (greenlet.value > max_cycle_len):
                max_cycle_len = greenlet.value

        resp = (str(first) + " " + str(second) + " " + str(max_cycle_len) + 
                "\r\n")
        fileobj.write(resp)
        fileobj.flush()
        print("Response %r" % resp)
Exemplo n.º 45
0
def connect(socket, address):
    log.info('New connection from %s:%s' % address)
    conn = socket.makefile()
    conn.write("User: "******"town_square")
    try:
        while player.running:
            action = None
            try:
                gevent.socket.wait_read(conn.fileno(), timeout=0.01, timeout_exc=NoInput())
            except NoInput:
                pass
            else:
                line = conn.readline().strip()
                if line:
                    action = player.interpret(line)

            if not action:
                try:
                    action = player.actions.get(block=True, timeout=0.01)
                except gevent.queue.Empty:
                    pass

            if action:
                # If it's just a string, print it out
                func = getattr(action, 'execute', lambda: str(action))
                message = func()
                conn.write(message + player.prompt)
                conn.flush()

    finally:
        log.info("client disconnected")
        player.close()
Exemplo n.º 46
0
    def parser(self, socket, address):
        """
        Parses everything that is sent to MRserver
        """
        
        self.log.info('Client connected from %s'% address[0])
        
        
        # makefile() allows to use readline()
        fileobj = socket.makefile()
        
        # Ready to get commands
        self.send(fileobj, msgs['001']) # Welcome message
        
        # This is local, because making it self.quit would lead to synchronation
        # errors between "threads"
        # quit = False
        
        authorized = False # Not authorized yet.
        auth_tries = 3 
        
        # Serve forever
        while True:
        
            # Read received line, make it unicode, expecting utf-8 or iso-8859-1
            try:
                line = fileobj.readline()
            
            except Exception, e:
                self.log.info(e)
                self.log.info("Disconnecting session.")
                break
            
            if not line:
                self.log.info("Client disconnected.")
                break

            line = useful.uni(line).strip()
            args = line.split(u' ')
            wordline = u' '.join(args[1:])
            command = args[0].lower()
            
            
            if command == 'quit':
                self.log.info("Client quit.")
                break
            
            # Auth command is here to make loop process simpler.
            if command == 'auth':
                
                if authorized: # Already authed.
                    self.send(fileobj, msgs['201'])
                    continue

                    
                auth_tries -= 1
                
                hashed = useful.sha256_hex(wordline)
                if hashed == self.bot.sets['mr_password']: # Pass was correct
                    self.send(fileobj, msgs['002']) # Auth ok
                    authorized = True
                
                else: # Wrong password
                    self.send(fileobj, msgs['202']) # Wrong password.
                    self.log.error(['Wrong password from %s'%address[0]])
                    
                    if auth_tries < 1: # Too many auth attempts
                        self.send(fileobj, msgs['203'])
                        self.log.error(['Max auth tries from %s'%address[0]])
                        break
                    
                    gevent.sleep(1) # Give time to think.
                    
                continue # Start loop again
            
            if command not in self.cmds:
                self.send(fileobj, msgs['207']) # Unknown command.  
                continue
            
            if authorized:            
                    
                # Received a known command.
                if command in self.cmds:
                        
                        # Pass arguments to function
                        self.cmds[command](fileobj, wordline)
                        
            else: # Not authorized
                
                self.send(fileobj, msgs['204']) # Not authed.
Exemplo n.º 47
0
    def command_server(self, socket, address):
        self.log('New connection from %s:%s' % address)
        socket.sendall('Welcome to the Democratic Daemon server!\n')
        help_message = 'Commands are "stop", "list", "json" and "approve"\n'
        socket.sendall(help_message)
        fileobj = socket.makefile()
        while True:
            try:
                line = fileobj.readline()
                if not line:
                    self.log("client disconnected")
                    return

                command = line.decode().strip().lower()
                single_cmd = False
                if command.startswith('$'):
                    command = command[1:]
                    single_cmd = True
                    self.log('Received single command ' + repr(command))

                if command == 'stop':
                    self.log("client told server to stop")
                    fileobj.write(('STOPPING SERVER\n').encode())
                    fileobj.flush()
                    self.quit_event.set()
                    self.server.stop()
                    socket.shutdown(gevent.socket.SHUT_RDWR)
                    return

                elif command == 'list':
                    empty = True
                    for repo in self.pr_db.repos():
                        for pr in self.pr_db.pull_requests(repo):
                            fileobj.write(pr.pretty_str().encode())
                            empty = False
                    if empty:
                        fileobj.write('No pull requests\n'.encode())

                elif command == 'json':
                    data = prs_to_json(self.pr_db.pull_requests()).encode()
                    fileobj.write(data)

                elif command.startswith('approve'):
                    r = re.match(
                        'approve\s+(?P<repo>\S+)\s+(?P<issue_id>\d+)\s*$',
                        command)
                    issue_id = None
                    repo = None
                    if r:
                        repo = r.group('repo')
                        try:
                            issue_id = int(r.group('issue_id'))
                        except Exception as e:
                            fileobj.write(
                                ('error parsing integer issue number\n' +
                                 str(e) + '\n').encode())
                    else:
                        fileobj.write(
                            ('error - usage is "approve [repo] [issue_id]"\n'
                             ).encode())

                    found_pr = None
                    if issue_id and repo and repo in self.pr_db.repos():
                        for pr in self.pr_db.pull_requests(repo):
                            if pr.state == pr.state_idx('COMMENTED'):
                                if pr.issue_id == issue_id:
                                    found_pr = pr
                                    break

                    if found_pr:
                        fileobj.write(('PULL REQUEST APPROVED\n').encode())
                        fileobj.write(found_pr.pretty_str().encode())

                        found_pr.set_state('APPROVED')
                        self.pr_db.write_pull_requests(found_pr.repo)
                        self.build_queue.put((found_pr.repo, found_pr.key()))

                    else:
                        fileobj.write(
                            ('No pull request "' + str(repo) + '/issue #' +
                             str(issue_id) + '" ready for merging\n').encode())

                else:
                    fileobj.write(
                        ('Unknown command "' + command + '"\n').encode())
                    fileobj.write(help_message.encode())

                fileobj.flush()
                if single_cmd:
                    socket.shutdown(gevent.socket.SHUT_RDWR)
                    return

            except Exception as e:
                self.log(e)
                try:
                    socket.shutdown(gevent.socket.SHUT_RDWR)
                except Exception as e2:
                    self.log(e2)
                return
Exemplo n.º 48
0
 def __init__(self, socket, addr):
   self.getLog( )
   self.socket, self.addr = socket, addr
   self.rfile = socket.makefile()