Exemplo n.º 1
0
def _kill_process_and_subprocess_linux(pid):
    from robocode_ls_core.subprocess_wrapper import subprocess

    # Ask to stop forking
    subprocess.call(
        ["kill", "-stop", str(pid)],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        stdin=subprocess.PIPE,
    )

    process = subprocess.Popen(
        ["pgrep", "-P", str(pid)],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        stdin=subprocess.PIPE,
    )
    for pid in process.communicate()[0].splitlines():
        _kill_process_and_subprocess_linux(pid.strip())

    subprocess.call(
        ["kill", "-KILL", str(pid)],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        stdin=subprocess.PIPE,
    )
Exemplo n.º 2
0
    def on_any_event(self, event):
        from string import Template

        if self.drop_during_process and self.process and self.process.poll(
        ) is None:
            return

        if event.is_directory:
            object_type = 'directory'
        else:
            object_type = 'file'

        context = {
            'watch_src_path': event.src_path,
            'watch_dest_path': '',
            'watch_event_type': event.event_type,
            'watch_object': object_type,
        }

        if self.shell_command is None:
            if has_attribute(event, 'dest_path'):
                context.update({'dest_path': event.dest_path})
                command = 'echo "${watch_event_type} ${watch_object} from ${watch_src_path} to ${watch_dest_path}"'
            else:
                command = 'echo "${watch_event_type} ${watch_object} ${watch_src_path}"'
        else:
            if has_attribute(event, 'dest_path'):
                context.update({'watch_dest_path': event.dest_path})
            command = self.shell_command

        command = Template(command).safe_substitute(**context)
        self.process = subprocess.Popen(command, shell=True)
        if self.wait_for_process:
            self.process.wait()
def test_exit_with_parent_process_died(
    language_server_process: IRobocodeLanguageServerClient,
    language_server_io,
    ws_root_path,
):
    """
    :note: Only check with the language_server_io (because that's in another process).
    """
    from robocode_ls_core.subprocess_wrapper import subprocess
    from robocode_ls_core.basic import is_process_alive
    from robocode_ls_core.basic import kill_process_and_subprocesses
    from robocode_ls_core.unittest_tools.fixtures import wait_for_test_condition

    language_server = language_server_io
    dummy_process = subprocess.Popen(
        [sys.executable, "-c", "import time;time.sleep(10000)"])

    language_server.initialize(ws_root_path, process_id=dummy_process.pid)

    assert is_process_alive(dummy_process.pid)
    assert is_process_alive(language_server_process.pid)

    kill_process_and_subprocesses(dummy_process.pid)

    wait_for_test_condition(lambda: not is_process_alive(dummy_process.pid))
    wait_for_test_condition(
        lambda: not is_process_alive(language_server_process.pid))
    language_server_io.require_exit_messages = False
Exemplo n.º 4
0
    def is_process_alive(pid):
        from robocode_ls_core.subprocess_wrapper import subprocess

        if _is_process_alive(pid):
            # Check if zombie...
            try:
                cmd = ["ps", "-p", str(pid), "-o", "stat"]
                try:
                    process = subprocess.Popen(cmd,
                                               stdout=subprocess.PIPE,
                                               stderr=subprocess.PIPE)
                except:
                    log.exception("Error calling: %s." % (cmd, ))
                else:
                    stdout, _ = process.communicate()
                    stdout = stdout.decode("utf-8", "replace")
                    lines = [line.strip() for line in stdout.splitlines()]
                    if len(lines) > 1:
                        if lines[1].startswith("Z"):
                            return False  # It's a zombie
            except:
                log.exception("Error checking if process is alive.")

            return True
        return False
Exemplo n.º 5
0
def create_language_server_process(log_file, __main__module):
    from robocode_ls_core.basic import kill_process_and_subprocesses

    from robocode_ls_core.subprocess_wrapper import subprocess

    language_server_process = subprocess.Popen(
        [
            sys.executable,
            "-u",
            __main__module.__file__,
            "-vv",
            "--log-file=%s" % log_file,
        ],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        stdin=subprocess.PIPE,
    )
    stderr = language_server_process.stderr
    buf = []
    t = threading.Thread(target=_stderr_reader, args=(stderr, buf))
    t.name = "Stderr reader"
    t.start()

    returncode = language_server_process.poll()
    assert returncode is None
    try:
        yield language_server_process
    finally:
        t.join(1)
        stderr_contents = b"".join(buf).decode("utf-8")
        if stderr_contents:
            sys.stderr.write(f"Found stderr contents: >>\n{stderr_contents}\n<<")
        returncode = language_server_process.poll()
        if returncode is None:
            kill_process_and_subprocesses(language_server_process.pid)
Exemplo n.º 6
0
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
Exemplo n.º 7
0
    def launch(
        self,
        target,
        debug=True,
        success=True,
        terminal="none",
        args: Optional[Iterable[str]] = None,
    ):
        """
        :param args:
            The arguments to the launch (for instance:
                ["--variable", "my_var:22"]
            )
        """
        from robotframework_debug_adapter.dap.dap_schema import LaunchRequest
        from robotframework_debug_adapter.dap.dap_schema import LaunchRequestArguments
        from robotframework_debug_adapter.dap.dap_schema import LaunchResponse
        from robotframework_debug_adapter.dap.dap_schema import RunInTerminalRequest
        from robocode_ls_core.basic import as_str
        from robotframework_debug_adapter.dap.dap_schema import InitializedEvent
        from robotframework_debug_adapter.dap.dap_schema import Response

        launch_args = LaunchRequestArguments(
            __sessionId="some_id", noDebug=not debug, target=target, terminal=terminal
        )
        if args:
            launch_args.kwargs["args"] = args
        self.write(LaunchRequest(launch_args))

        if terminal == "external":
            run_in_terminal_request = self.read(RunInTerminalRequest)
            env = os.environ.copy()
            for key, val in run_in_terminal_request.arguments.env.to_dict().items():
                env[as_str(key)] = as_str(val)

            cwd = run_in_terminal_request.arguments.cwd
            popen_args = run_in_terminal_request.arguments.args

            subprocess.Popen(popen_args, cwd=cwd, env=env)

        if success:
            # Initialized is sent just before the launch response (at which
            # point it's possible to send breakpoints).
            event = self.read(InitializedEvent)
            assert isinstance(event, InitializedEvent)

        if success:
            launch_response = self.read(LaunchResponse)
        else:
            launch_response = self.read(Response)
        assert launch_response.success == success
Exemplo n.º 8
0
def dap_process(dap_log_file, dap_process_stderr_file):
    from robotframework_debug_adapter import __main__
    from robocode_ls_core.basic import kill_process_and_subprocesses

    env = os.environ.copy()
    env["ROBOTFRAMEWORK_DAP_LOG_LEVEL"] = "3"
    env["ROBOTFRAMEWORK_DAP_LOG_FILENAME"] = dap_log_file

    dap_process = subprocess.Popen(
        [sys.executable, "-u", __main__.__file__],
        stdout=subprocess.PIPE,
        stderr=dap_process_stderr_file,
        stdin=subprocess.PIPE,
        env=env,
    )
    assert dap_process.returncode is None
    yield dap_process
    if dap_process.returncode is None:
        kill_process_and_subprocesses(dap_process.pid)
Exemplo n.º 9
0
 def start(self):
     self.process = subprocess.Popen(self.command, preexec_fn=os.setsid)