diff options
author | Patrick Lehmann <Patrick.Lehmann@plc2.de> | 2020-12-28 00:03:46 +0100 |
---|---|---|
committer | Patrick Lehmann <Patrick.Lehmann@plc2.de> | 2020-12-28 00:03:46 +0100 |
commit | 12f8c883dee453c5bfc318334b0d43f952aeed18 (patch) | |
tree | 2afa06eee6b8d9aa5e6bead47c39780dbe5fe416 /pyGHDL/cli | |
parent | 06a089b3e1500e9e362212e5d1af80e203bff089 (diff) | |
parent | bd5671ea1feee3aa0d76cddb3c94f5c25738eaa2 (diff) | |
download | ghdl-12f8c883dee453c5bfc318334b0d43f952aeed18.tar.gz ghdl-12f8c883dee453c5bfc318334b0d43f952aeed18.tar.bz2 ghdl-12f8c883dee453c5bfc318334b0d43f952aeed18.zip |
Merge remote-tracking branch 'github-umarcor/py/GHDL' into paebbels/pyGHDL
Diffstat (limited to 'pyGHDL/cli')
-rw-r--r--[-rwxr-xr-x] | pyGHDL/cli/ghdl-ls | 109 |
1 files changed, 106 insertions, 3 deletions
diff --git a/pyGHDL/cli/ghdl-ls b/pyGHDL/cli/ghdl-ls index 9ca41f5ca..9b1175560 100755..100644 --- a/pyGHDL/cli/ghdl-ls +++ b/pyGHDL/cli/ghdl-ls @@ -1,4 +1,107 @@ -#! /usr/bin/env python3 -import pyGHDL.lsp.main as main +#!/usr/bin/env python +from __future__ import absolute_import -main.main() +import argparse +import logging +import sys +import os + +import pyGHDL.libghdl as libghdl + +from . import version +from . import lsp +from . import vhdl_ls + +logger = logging.getLogger("ghdl-ls") + + +def rotate_log_files(basename, num): + for i in range(num, 0, -1): + oldfile = "{}.{}".format(basename, i - 1) + if os.path.isfile(oldfile): + os.rename(oldfile, "{}.{}".format(basename, i)) + if os.path.isfile(basename): + os.rename(basename, "{}.0".format(basename)) + + +def main(): + parser = argparse.ArgumentParser(description="VHDL Language Protocol Server") + parser.add_argument( + "--version", "-V", action="version", version="%(prog)s " + version.__version__ + ) + parser.add_argument( + "--verbose", "-v", action="count", default=0, help="Show debug output" + ) + parser.add_argument( + "--log-file", help="Redirect logs to the given file instead of stderr" + ) + parser.add_argument("--trace-file", help="Save rpc data to FILE.in and FILE.out") + parser.add_argument("--input", "-i", help="Read request from file") + parser.add_argument( + "--disp-config", + action="store_true", + help="Disp installation configuration and exit", + ) + + args = parser.parse_args() + + if args.disp_config: + libghdl.errorout_console.Install_Handler() + libghdl.disp_config() + return + + # Setup logging + if args.verbose >= 2: + loglevel = logging.DEBUG + elif args.verbose >= 1: + loglevel = logging.INFO + else: + loglevel = logging.ERROR + + if args.log_file: + rotate_log_files(args.log_file, 5) + logstream = open(args.log_file, "w") + else: + logstream = sys.stderr + logging.basicConfig( + format="%(asctime)-15s [%(levelname)s] %(message)s", + stream=logstream, + level=loglevel, + ) + + if args.verbose != 0: + sys.stderr.write("Args: {}\n".format(sys.argv)) + sys.stderr.write("Current directory: {}\n".format(os.getcwd())) + + logger.info("Args: %s", sys.argv) + logger.info("Current directory is %s", os.getcwd()) + + # Connection + instream = sys.stdin.buffer + if args.input is not None: + instream = open(args.input, "rb") + + conn = lsp.LSPConn(instream, sys.stdout.buffer) + + trace_file = args.trace_file + if trace_file is None: + trace_file = os.environ.get("GHDL_LS_TRACE") + if trace_file is not None: + if args.input is None: + rotate_log_files(trace_file + ".in", 5) + rotate_log_files(trace_file + ".out", 5) + conn = LSPConnTrace(trace_file, conn) + else: + logger.info("Traces disabled when -i/--input") + + handler = vhdl_ls.VhdlLanguageServer() + + try: + server = lsp.LanguageProtocolServer(handler, conn) + server.run() + except Exception: + logger.exception("Uncaught error") + sys.exit(1) + +if __name__ = '__main__': + main() |