Пример #1
0
def run_server(host, port, log_path=None):
    global scriptpath
    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_neu_grammar import recompile_grammar
    # recompile_grammar(os.path.join(scriptpath, 'neu.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('neu_ebnf_ERRORS.txt', encoding='utf-8') as f:
            print(f.read())
        sys.exit(1)
    from neuParser import compile_src
    from DHParser.server import Server, gen_lsp_table
    config_filename = get_config_filename()
    try:
        with open(config_filename, 'w') as f:
            f.write(host + ' ' + str(port))
    except PermissionError:
        print('PermissionError: Could not write temporary config file: ' +
              config_filename)

    print('Starting server on %s:%i' % (host, port))
    neu_lsp = neuLanguageServerProtocol()
    lsp_table = neu_lsp.lsp_fulltable.copy()
    lsp_table.setdefault('default', compile_src)
    neu_server = Server(rpc_functions=lsp_table,
                        cpu_bound=neu_lsp.cpu_bound.lsp_table.keys(),
                        blocking=neu_lsp.blocking.lsp_table.keys(),
                        connection_callback=neu_lsp.connect,
                        server_name="neuServer",
                        strict_lsp=True)
    if log_path is not None:
        neu_server.echo_log = True
        print(neu_server.start_logging(log_path))
    try:
        neu_server.run_server(host,
                              port)  # returns only after server has stopped
    except OSError as e:
        print(e)
        print('Could not start server. Shutting down!')
        sys.exit(1)
    finally:
        cfg_filename = get_config_filename()
        print('Server on %s:%i stopped' % (host, port))
        try:
            os.remove(cfg_filename)
            print('Removing temporary config file: "{}".'.format(cfg_filename))
        except FileNotFoundError:
            print('Config file "{}" does not exist any more.'.format(
                cfg_filename))
Пример #2
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
Пример #3
0
def run_server(host, port, log_path=None):
    global scriptpath
    grammar_src = os.path.abspath(__file__).replace('Server.py', '.ebnf')
    dhparserdir = os.path.abspath(
        os.path.join(scriptpath, os.path.join('..', '..')))
    if scriptpath not in sys.path:
        sys.path.append(scriptpath)
    if dhparserdir not in sys.path:
        sys.path.append(dhparserdir)
    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('LaTeX_ebnf_ERRORS.txt', encoding='utf-8') as f:
            print(f.read())
        sys.exit(1)
    recompile_grammar(os.path.join(scriptpath, 'LaTeX.ebnf'), force=False)
    from LaTeXCompiler import compile_src
    from DHParser.server import Server
    from DHParser.lsp import gen_lsp_table
    config_filename = get_config_filename()
    try:
        with open(config_filename, 'w') as f:
            f.write(host + ' ' + str(port))
    except PermissionError:
        print('PermissionError: Could not write temporary config file: ' +
              config_filename)

    print('Starting server on %s:%i' % (host, port))
    LaTeX_lsp = LaTeXLanguageServerProtocol()
    lsp_table = gen_lsp_table(LaTeX_lsp, prefix='lsp_')
    lsp_table.update({'default': compile_src})
    non_blocking = frozenset(('initialize', 'initialized', 'shutdown', 'exit'))
    LaTeX_server = Server(rpc_functions=lsp_table,
                          cpu_bound=set(lsp_table.keys() - non_blocking),
                          blocking=frozenset())
    if log_path is not None:
        LaTeX_server.echo_log = True
        print(LaTeX_server.start_logging(log_path))

    try:
        LaTeX_server.run_tcp_server(
            host, port)  # returns only after server has stopped
    except OSError as e:
        print(e)
        print('Could not start server. Shutting down!')
        return 1
    except KeyboardInterrupt:
        print('Keyboard interrupt received.')
        return 1
    finally:
        print('Server at host: {} port: {} has been stopped.'.format(
            host, port))
        cfg_filename = get_config_filename()
        try:
            os.remove(cfg_filename)
            print('Removing temporary config file: "{}".'.format(cfg_filename))
        except FileNotFoundError:
            print('Config file "{}" does not exist any more.'.format(
                cfg_filename))
    return 0
Пример #4
0
def run_server(host, port, log_path=None):
    """
    Starts a new DSLServer. If `port` is already occupied, different
    ports will be tried.
    """
    global KNOWN_HOST, KNOWN_PORT
    global scriptpath, servername
    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)
    try:
        from EBNFParser import compile_src
    except ModuleNotFoundError:
        from tst_EBNF_grammar import recompile_grammar
        recompile_grammar(grammar_src, force=False)
        from EBNFParser import compile_src
    from DHParser.server import Server, probe_tcp_server
    from DHParser.lsp import gen_lsp_table

    EBNF_lsp = EBNFLanguageServerProtocol()
    lsp_table = EBNF_lsp.lsp_fulltable.copy()
    lsp_table.setdefault('default', compile_src)
    EBNF_server = Server(rpc_functions=lsp_table,
                         cpu_bound=set(EBNF_lsp.cpu_bound.lsp_table.keys()),
                         blocking=set(EBNF_lsp.blocking.lsp_table.keys()),
                         connection_callback=EBNF_lsp.connect,
                         server_name='EBNFServer',
                         strict_lsp=True)

    if log_path is not None:
        EBNF_server.echo_log = True
        print(EBNF_server.start_logging(log_path.strip('" \'')))

    cfg_filename = get_config_filename()
    overwrite = not os.path.exists(cfg_filename)
    ports = ALTERNATIVE_PORTS.copy()
    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):
                    print(
                        '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:
                    print('"%s" already occupies %s:%i. Trying port %i' %
                          (ident, host, port, ports[-1]))
                    continue
                else:
                    print(
                        '"%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:
                print('%s: Could not write temporary config file: "%s"' %
                      (str(e), cfg_filename))
                ports = []
        else:
            print(
                '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))
            EBNF_server.run_tcp_server(
                host, port)  # returns only after server has stopped
            ports = []
        except OSError as e:
            if not (ports and e.errno == 98):
                print(e)
                print('Could not start server. Shutting down!')
                sys.exit(1)
            elif ports:
                print('Could not start server on %s:%i. Trying port %s' %
                      (host, port, ports[-1]))
            else:
                print(
                    'Could not start server on %s:%i. No more ports to try.' %
                    (host, port))
        finally:
            if not ports:
                print('Server on %s:%i stopped' % (host, port))
                if overwrite:
                    try:
                        os.remove(cfg_filename)
                        debug('removing temporary config file: ' +
                              cfg_filename)
                    except FileNotFoundError:
                        pass