def test_log(tmpdir): from robocode_ls_core.robotframework_log import get_logger, configure_logger from robocode_ls_core.unittest_tools.fixtures import wait_for_test_condition somedir = str(tmpdir.join("somedir")) configure_logger("test", 2, os.path.join(somedir, "foo.log")) log = get_logger("my_logger") log.info("something\nfoo\nbar") try: raise AssertionError("someerror") except: log.exception("rara: %s - %s", "str1", "str2") def get_log_files(): log_files = [ x for x in os.listdir(somedir) if x.startswith("foo") and x.endswith(".log") ] return log_files if log_files else None wait_for_test_condition( get_log_files, msg=lambda: "Found: %s in %s" % (get_log_files(), somedir) ) log_files = get_log_files() with open(os.path.join(somedir, log_files[0]), "r") as stream: contents = stream.read() assert "someerror" in contents assert "something" in contents assert "rara" in contents assert "rara: str1 - str2" in contents
def start_server_process(args=(), python_exe=None, env=None): """ Calls this __main__ in another process. :param args: The list of arguments for the server process. i.e.: ["-vv", "--log-file=%s" % log_file] """ from robocode_ls_core.robotframework_log import get_logger from robocode_ls_core.subprocess_wrapper import subprocess import threading from robotframework_ls.options import Setup log = get_logger(__name__) if python_exe: if not os.path.exists(python_exe): raise RuntimeError("Expected %s to exist" % (python_exe, )) args = [python_exe or sys.executable, "-u", __file__] + list(args) log.debug('Starting server api process with args: "%s"' % ('" "'.join(args), )) environ = os.environ.copy() environ.pop("PYTHONPATH", "") environ.pop("PYTHONHOME", "") environ.pop("VIRTUAL_ENV", "") if env is not None: for key, val in env.items(): environ[key] = val environ["PYTHONIOENCODING"] = "utf-8" environ["PYTHONUNBUFFERED"] = "1" env_log = ["Environ:"] for key, val in environ.items(): env_log.append(" %s=%s" % (key, val)) if Setup.options.DEBUG_PROCESS_ENVIRON: log.debug("\n".join(env_log)) language_server_process = subprocess.Popen( args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, env=environ, bufsize=0, ) t = threading.Thread(target=_stderr_reader, args=(language_server_process.stderr, )) t.setName("Stderr from ServerAPI (%s)" % (args, )) t.setDaemon(True) t.start() return language_server_process
def main(args=None, after_bind=lambda server: None, language_server_class=None): original_args = args if args is not None else sys.argv[1:] try: import robotframework_ls except ImportError: # Automatically add it to the path if __main__ is being executed. sys.path.append( os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import robotframework_ls # @UnusedImport robotframework_ls.import_robocode_ls_core() from robotframework_ls.options import Setup, Options from robocode_ls_core.robotframework_log import ( configure_logger, log_args_and_python, get_logger, ) from robocode_ls_core.python_ls import ( start_io_lang_server, start_tcp_lang_server, binary_stdio, ) if language_server_class is None: from robotframework_ls.robotframework_ls_impl import ( RobotFrameworkLanguageServer, ) language_server_class = RobotFrameworkLanguageServer parser = argparse.ArgumentParser() add_arguments(parser) args = parser.parse_args(args=original_args) Setup.options = Options(args) verbose = args.verbose log_file = args.log_file or "" configure_logger("lsp", verbose, log_file) log = get_logger("robotframework_ls.__main__") log_args_and_python(log, original_args, robotframework_ls.__version__) if args.tcp: start_tcp_lang_server(args.host, args.port, language_server_class, after_bind=after_bind) else: stdin, stdout = binary_stdio() start_io_lang_server(stdin, stdout, language_server_class)
def _stderr_reader(stream): from robocode_ls_core.robotframework_log import get_logger log = get_logger(__name__) try: while True: line = stream.readline() sys.stderr.buffer.write(line) if not line: break except: log.exception("Error reading from server api process stream.") finally: log.debug("Finished reading from server api process stream.")
def on_setBreakpoints_request(self, request): from robotframework_debug_adapter.dap.dap_schema import SourceBreakpoint from robotframework_debug_adapter.dap.dap_schema import Breakpoint from robotframework_debug_adapter.dap.dap_schema import ( SetBreakpointsResponseBody, ) from robotframework_debug_adapter.dap import dap_base_schema from robotframework_debug_adapter import file_utils from robotframework_debug_adapter.debugger_impl import RobotBreakpoint from robocode_ls_core.robotframework_log import get_logger log = get_logger("robotframework_debug_adapter.run_robot__main__.py") # Just acknowledge that no breakpoints are valid. breakpoints = [] robot_breakpoints = [] source = request.arguments.source path = source.path filename = file_utils.norm_file_to_server(path) log.info("Normalized %s to %s", path, filename) if request.arguments.breakpoints: for bp in request.arguments.breakpoints: source_breakpoint = SourceBreakpoint(**bp) breakpoints.append( Breakpoint(verified=True, line=source_breakpoint.line, source=source).to_dict()) robot_breakpoints.append( RobotBreakpoint(source_breakpoint.line)) if self._debugger_impl: self._debugger_impl.set_breakpoints(filename, robot_breakpoints) else: if robot_breakpoints: get_log().info("Unable to set breakpoints (no debug mode).") self.write_message( dap_base_schema.build_response( request, kwargs=dict(body=SetBreakpointsResponseBody( breakpoints=breakpoints)), ))
def connect(port): from robotframework_ls.options import DEFAULT_TIMEOUT from robotframework_ls.impl.robot_lsp_constants import ENV_OPTION_ROBOT_DAP_TIMEOUT from robocode_ls_core.robotframework_log import get_logger log = get_logger("robotframework_debug_adapter.run_robot__main__.py") # Set TCP keepalive on an open socket. # It activates after 1 second (TCP_KEEPIDLE,) of idleness, # then sends a keepalive ping once every 3 seconds (TCP_KEEPINTVL), # and closes the connection after 5 failed ping (TCP_KEEPCNT), or 15 seconds s = socket_module.socket(socket_module.AF_INET, socket_module.SOCK_STREAM) try: IPPROTO_TCP, SO_KEEPALIVE, TCP_KEEPIDLE, TCP_KEEPINTVL, TCP_KEEPCNT = ( socket_module.IPPROTO_TCP, socket_module.SO_KEEPALIVE, socket_module.TCP_KEEPIDLE, # @UndefinedVariable socket_module.TCP_KEEPINTVL, # @UndefinedVariable socket_module.TCP_KEEPCNT, # @UndefinedVariable ) s.setsockopt(socket_module.SOL_SOCKET, SO_KEEPALIVE, 1) s.setsockopt(IPPROTO_TCP, TCP_KEEPIDLE, 1) s.setsockopt(IPPROTO_TCP, TCP_KEEPINTVL, 3) s.setsockopt(IPPROTO_TCP, TCP_KEEPCNT, 5) except AttributeError: pass # May not be available everywhere. try: # 10 seconds default timeout timeout = int( os.environ.get(ENV_OPTION_ROBOT_DAP_TIMEOUT, DEFAULT_TIMEOUT)) s.settimeout(timeout) s.connect(("127.0.0.1", port)) s.settimeout(None) # no timeout after connected log.info("Connected.") return s except: log.exception("Could not connect to: %s", (port, )) raise
def main(): """ Starts the debug adapter (creates a thread to read from stdin and another to write to stdout as expected by the vscode debug protocol). We pass the command processor to the reader thread as the idea is that the reader thread will read a message, convert it to an instance of the message in the schema and then forward it to the command processor which will interpret and act on it, posting the results to the writer queue. """ log = None try: import sys try: import robotframework_debug_adapter import robotframework_ls except ImportError: # Automatically add it to the path if __main__ is being executed. sys.path.append( os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import robotframework_debug_adapter # @UnusedImport import robotframework_ls robotframework_ls.import_robocode_ls_core() from robotframework_debug_adapter.debug_adapter_threads import ( STOP_WRITER_THREAD, ) from robocode_ls_core.robotframework_log import ( get_logger, configure_logger, log_args_and_python, ) from robotframework_debug_adapter.constants import LOG_FILENAME from robotframework_debug_adapter.constants import LOG_LEVEL configure_logger("dap", LOG_LEVEL, LOG_FILENAME) log = get_logger("robotframework_debug_adapter.__main__") log_args_and_python(log, sys.argv, robotframework_ls.__version__) from robotframework_debug_adapter.debug_adapter_threads import reader_thread from robotframework_debug_adapter.debug_adapter_threads import writer_thread from robotframework_debug_adapter.debug_adapter_comm import DebugAdapterComm try: from queue import Queue except ImportError: from Queue import Queue to_client_queue = Queue() comm = DebugAdapterComm(to_client_queue) write_to = sys.stdout read_from = sys.stdin if sys.version_info[0] <= 2: if sys.platform == "win32": # must read streams as binary on windows import msvcrt msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) else: # Py3 write_to = sys.stdout.buffer read_from = sys.stdin.buffer writer = threading.Thread( target=writer_thread, args=(write_to, to_client_queue, "write to client"), name="Write to client (dap __main__)", ) reader = threading.Thread( target=reader_thread, args=(read_from, comm.from_client, to_client_queue, b"read from client"), name="Read from client (dap __main__)", ) reader.start() writer.start() reader.join() log.debug("Exited reader.\n") to_client_queue.put(STOP_WRITER_THREAD) writer.join() log.debug("Exited writer.\n") except: if log is not None: log.exception("Error") # Critical error (the logging may not be set up properly). # Print to file and stderr. with open(_critical_error_log_file, "a+") as stream: traceback.print_exc(file=stream) traceback.print_exc() finally: if log is not None: log.debug("Exited main.\n")
from robotframework_ls.impl.robot_workspace import RobotDocument from robocode_ls_core.cache import instance_cache from robocode_ls_core.robotframework_log import get_logger log = get_logger(__name__) _NOT_SET = "NOT_SET" class _Memo(object): def __init__(self): self._followed_imports_variables = {} self._followed_imports = {} self._completed_libraries = {} def follow_import(self, uri): if uri not in self._followed_imports: self._followed_imports[uri] = True return True return False def follow_import_variables(self, uri): if uri not in self._followed_imports_variables: self._followed_imports_variables[uri] = True return True return False def complete_for_library(self, library_name): if library_name not in self._completed_libraries:
def get_log(): from robocode_ls_core.robotframework_log import get_logger return get_logger("robotframework_debug_adapter.run_robot__main__.py")
def main(args=None, after_bind=lambda server: None, language_server_class=None): original_args = args if args is not None else sys.argv[1:] parser = argparse.ArgumentParser() add_arguments(parser) args = parser.parse_args(args=original_args) verbose = args.verbose log_file = args.log_file or "" try: try: import robocode_vscode except ImportError: # Automatically add it to the path if __main__ is being executed. sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import robocode_vscode # @UnusedImport robocode_vscode.import_robocode_ls_core() from robocode_ls_core.robotframework_log import ( configure_logger, log_args_and_python, get_logger, ) except: # Failed before having setup the logger (but after reading args). log_file = os.path.expanduser(log_file) log_file = os.path.realpath(os.path.abspath(log_file)) dirname = os.path.dirname(log_file) basename = os.path.basename(log_file) try: os.makedirs(dirname) except: pass # Ignore error if it already exists. name, ext = os.path.splitext(basename) postfix = "lsp.init" log_file = os.path.join( dirname, name + "." + postfix + "." + str(os.getpid()) + ext ) with open(log_file, "a+") as stream: traceback.print_exc(file=stream) raise configure_logger("lsp", verbose, log_file) log = get_logger("robocode_vscode.__main__") log_args_and_python(log, original_args, robocode_vscode.__version__) try: from robocode_vscode.options import Setup, Options Setup.options = Options(args) from robocode_ls_core.python_ls import ( start_io_lang_server, start_tcp_lang_server, binary_stdio, ) if language_server_class is None: from robocode_vscode.robocode_language_server import RobocodeLanguageServer language_server_class = RobocodeLanguageServer if args.tcp: start_tcp_lang_server( args.host, args.port, language_server_class, after_bind=after_bind ) else: stdin, stdout = binary_stdio() start_io_lang_server(stdin, stdout, language_server_class) except: log.exception("Error initializing") raise