def main(): """Entrypoint of GHDL's Language Protocol Server.""" logger = getLogger(__loggerName) parser = _generateCLIParser() args = parser.parse_args() if args.disp_config: errorout_console.Install_Handler() libghdl.disp_config() print("python:") print("sys.platform: {}, os.name: {}".format(sys.platform, os.name)) print(sys.version) return # Setup logging if args.verbose >= 2: loglevel = DEBUG elif args.verbose >= 1: loglevel = INFO else: loglevel = ERROR if args.log_file: __rotate_log_files(args.log_file, 5) logstream = open(args.log_file, "w") else: logstream = sys_stderr 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 = 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 = VhdlLanguageServer() try: server = LanguageProtocolServer(handler, conn) server.run() except Exception: logger.exception("Uncaught error") sys_exit(1)
def _RequestResponse(self, requestName: str, responseName: Optional[str] = None): root = str(self._LSPTestDirectory) root_uri = self._LSPTestDirectory.as_uri() assert(root_uri.startswith("file://")) root_uri = root_uri[7:] requestFile = self._LSPTestDirectory / self.subdir / requestName # Convert the JSON input file to an LSP string. with requestFile.open('r') as file: res = json_load(file) res = root_subst(res, root, root_uri) conn = StrConn() ls = LanguageProtocolServer(None, conn) for req in res: ls.write_output(req) # Run p = subprocess_run( [executable, '-m', 'pyGHDL.cli.lsp'], input=conn.res.encode('utf-8'), stdout=PIPE) self.assertEqual(p.returncode, 0, "Language server executable exit with a non-zero return code.") if responseName is None: return responseFile = self._LSPTestDirectory / self.subdir / responseName # Check output in_io = BytesIO(p.stdout) conn = LSPConn(in_io, None) ls = LanguageProtocolServer(None, conn) with responseFile.open('r') as file: ref = json_load(file) ref = root_subst(ref, root, root_uri) errs = 0 json_res = [] for i, r in enumerate(ref): rep = ls.read_request() if rep is None: print('FAIL: number of reply does not match') errs += 1 break rep = json_loads(rep) json_res.append(rep) # self.assertEqual(rep, r, "reply does not match for {!s}".format(requestFile)) if rep != r: print(self.__class__.__name__) show_diffs("[{}]".format(i), r, rep) errs += 1 rep = ls.read_request() self.assertIsNone(rep, "Too many replies.") if errs != 0: print('FAILURE between output and {!s} (for {!s})'.format(responseFile, requestFile)) print('Writing result output to result.json') with open('result.json', 'w') as f: f.write(json_dumps(json_res, indent=2)) f.write('\n') with open('request.json', 'w') as f: f.write(json_dumps(res, indent=2)) f.write('\n') self.fail()