예제 #1
0
 def setup(self):
     self.pipeA = MockStream('pipeA')
     self.pipeB = MockStream('pipeB')
     self.readerA = StreamReaderProxy(self.pipeA)
     self.writerA = StreamWriterProxy(self.pipeA)
     self.readerB = StreamReaderProxy(self.pipeB)
     self.writerB = StreamWriterProxy(self.pipeB)
예제 #2
0
 def setup(self):
     self.EBNF_lsp = EBNFServer.EBNFLanguageServerProtocol()
     self.lsp_table = self.EBNF_lsp.lsp_fulltable.copy()
     self.lsp_table.setdefault('default', EBNFParser.compile_src)
     self.pipeA = MockStream('pipeA')
     self.pipeB = MockStream('pipeB')
     self.readerA = StreamReaderProxy(self.pipeA)
     self.writerA = StreamWriterProxy(self.pipeA)
     self.readerB = StreamReaderProxy(self.pipeB)
     self.writerB = StreamWriterProxy(self.pipeB)
예제 #3
0
def run_server(host, port, log_path=None):
    """
    Starts a new atfServer. If `port` is already occupied, different
    ports will be tried.
    """
    global KNOWN_HOST, KNOWN_PORT
    global scriptpath, servername

    from multiprocessing import set_start_method
    # 'forkserver' or 'spawn' required to avoid broken process pools
    if sys.platform.lower().startswith('linux'): set_start_method('forkserver')
    else: set_start_method('spawn')

    grammar_src = os.path.abspath(__file__).replace('Server.py', '.ebnf')
    dhparserdir = os.path.abspath(os.path.join(scriptpath, '../..'))
    if scriptpath not in sys.path:
        sys.path.append(scriptpath)
    if dhparserdir not in sys.path:
        sys.path.append(dhparserdir)
    # from tst_atf_grammar import recompile_grammar
    # recompile_grammar(os.path.join(scriptpath, 'atf.ebnf'), force=False)
    from DHParser.dsl import recompile_grammar
    if not recompile_grammar(
            grammar_src,
            force=False,
            notify=lambda: print('recompiling ' + grammar_src)):
        print('\nErrors while recompiling "%s":' % grammar_src +
              '\n--------------------------------------\n\n')
        with open('atf_ebnf_ERRORS.txt', encoding='utf-8') as f:
            print(f.read())
        sys.exit(1)
    from atfParser import compile_src
    from DHParser.server import Server, probe_tcp_server, StreamReaderProxy, StreamWriterProxy
    from DHParser.lsp import gen_lsp_table

    atf_lsp = atfLanguageServerProtocol()
    lsp_table = atf_lsp.lsp_fulltable.copy()
    lsp_table.setdefault('default', compile_src)
    atf_server = Server(rpc_functions=lsp_table,
                        cpu_bound=atf_lsp.cpu_bound.lsp_table.keys(),
                        blocking=atf_lsp.blocking.lsp_table.keys(),
                        connection_callback=atf_lsp.connect,
                        server_name="atfServer",
                        strict_lsp=True)
    if log_path is not None:
        # echoing does not work with stream connections!
        atf_server.echo_log = True if port >= 0 and host else False
        msg = atf_server.start_logging(log_path.strip('" \''))
        if atf_server.echo_log: echo(msg)

    if port < 0 or not host:
        # communication via streams instead of tcp server
        reader = StreamReaderProxy(sys.stdin)
        writer = StreamWriterProxy(sys.stdout)
        atf_server.run_stream_server(reader, writer)
        return

    cfg_filename = get_config_filename()
    overwrite = not os.path.exists(cfg_filename)
    ports = ALTERNATIVE_PORTS.copy() if port == DEFAULT_PORT else []
    if port in ports:
        ports.remove(port)
    ports.append(port)

    while ports:
        port = ports.pop()
        if (host, port) == (KNOWN_HOST, KNOWN_PORT):
            ident = asyncio_run(
                probe_tcp_server(host, port, SERVER_REPLY_TIMEOUT))
            if ident:
                if ident.endswith(servername):
                    echo(
                        'A server of type "%s" already exists on %s:%i.' %
                        (servername, host, port) +
                        ' Use --port option to start a secondary server on a different port.'
                    )
                    sys.exit(1)
                if ports:
                    echo('"%s" already occupies %s:%i. Trying port %i' %
                         (ident, host, port, ports[-1]))
                    continue
                else:
                    echo('"%s" already occupies %s:%i. No more ports to try.' %
                         (ident, host, port))
                    sys.exit(1)
        if overwrite:
            try:
                with open(cfg_filename, 'w') as f:
                    debug('Storing host and port value %s:%i in file "%s".' %
                          (host, port, cfg_filename))
                    f.write(host + ' ' + str(port))
            except (PermissionError, IOError) as e:
                echo('%s: Could not write temporary config file: "%s"' %
                     (str(e), cfg_filename))
                ports = []
        else:
            echo(
                'Configuration file "%s" already existed and was not overwritten. '
                'Use option "--port %i" to stop this server!' %
                (cfg_filename, port))
        try:
            debug('Starting server on %s:%i' % (host, port))
            atf_server.run_tcp_server(
                host, port)  # returns only after server has stopped!
            ports = []
        except OSError as e:
            if not (ports and e.errno == 98):
                echo(e)
                echo('Could not start server. Shutting down!')
                sys.exit(1)
            elif ports:
                echo('Could not start server on %s:%i. Trying port %s' %
                     (host, port, ports[-1]))
            else:
                echo('Could not start server on %s:%i. No more ports to try.' %
                     (host, port))
        finally:
            if not ports:
                echo('Server on %s:%i stopped' % (host, port))
                if overwrite:
                    try:
                        os.remove(cfg_filename)
                        debug('removing temporary config file: ' +
                              cfg_filename)
                    except FileNotFoundError:
                        pass
예제 #4
0
 def setup(self):
     self.pipe = MockStream()
     self.reader = StreamReaderProxy(self.pipe)
     self.writer = StreamWriterProxy(self.pipe)