def dumpThreadList(self):
        """Sends the list of threads"""
        self.updateThreadList()
        threadList = []
        if len(self.threads) > 1:
            currentId = _thread.get_ident()
            # update thread names set by user (threading.setName)
            threadNames = {t.ident: t.getName() for t in threading.enumerate()}

            for threadId, thd in self.threads.items():
                threadProps = {"id": threadId}
                try:
                    threadProps["name"] = threadNames.get(threadId, thd.name)
                    threadProps["broken"] = thd.isBroken
                except Exception:
                    threadProps["name"] = 'UnknownThread'
                    threadProps["broken"] = False

                threadList.append(threadProps)
        else:
            currentId = -1
            threadProps = {"id": -1}
            threadProps["name"] = "MainThread"
            threadProps["broken"] = self.isBroken
            threadList.append(threadProps)

        sendJSONCommand(self.socket, METHOD_THREAD_LIST,
                        self.procuuid,
                        {"currentID": currentId, "threadList": threadList})
 def input(self, prompt, echo):
     """Implements 'input' using the redirected input"""
     sendJSONCommand(self.__socket, METHOD_STDIN, self.__procuuid, {
         'prompt': prompt,
         'echo': echo
     })
     params = waitForIDEMessage(self.__socket, METHOD_STDIN,
                                60 * 60 * 24 * 7)
     return params['input']
    def main(self):
        """Run wrapper driver"""
        if '--' not in sys.argv:
            print("Unexpected arguments", file=sys.stderr)
            return 1

        self.__procuuid, host, port, args = self.parseArgs()
        if self.__procuuid is None or host is None or port is None:
            print("Not enough arguments", file=sys.stderr)
            return 1

        remoteAddress = self.resolveHost(host)
        self.connect(remoteAddress, port)
        sendJSONCommand(self.__socket, METHOD_PROC_ID_INFO, self.__procuuid,
                        None)

        try:
            waitForIDEMessage(self.__socket, METHOD_PROLOGUE_CONTINUE,
                              WAIT_CONTINUE_TIMEOUT)
        except Exception as exc:
            print(str(exc), file=sys.stderr)
            return 1

        # Setup redirections
        stdoutOld = sys.stdout
        stderrOld = sys.stderr
        sys.stdout = OutStreamRedirector(self.__socket, True, self.__procuuid)
        sys.stderr = OutStreamRedirector(self.__socket, False, self.__procuuid)
        self.__redirected = True

        # Run the script
        retCode = 0
        try:
            self.__runScript(args)
        except SystemExit as exc:
            if CLIENT_DEBUG:
                print(traceback.format_exc(), file=sys.__stderr__)
            if exc.code is None:
                retCode = 0
            elif isinstance(exc.code, int):
                retCode = exc.code
            else:
                retCode = 1
                print(str(exc.code), file=sys.stderr)
        except KeyboardInterrupt as exc:
            if CLIENT_DEBUG:
                print(traceback.format_exc(), file=sys.__stderr__)
            retCode = 1
            print(traceback.format_exc(), file=sys.stderr)
        except Exception as exc:
            if CLIENT_DEBUG:
                print(traceback.format_exc(), file=sys.__stderr__)
            retCode = 1
            print(traceback.format_exc(), file=sys.stderr)
        except:
            if CLIENT_DEBUG:
                print(traceback.format_exc(), file=sys.__stderr__)
            retCode = 1
            print(traceback.format_exc(), file=sys.stderr)

        sys.stderr = stderrOld
        sys.stdout = stdoutOld
        self.__redirected = False

        # Send the return code back
        try:
            sendJSONCommand(self.__socket, METHOD_EPILOGUE_EXIT_CODE,
                            self.__procuuid, {'exitCode': retCode})
        except Exception as exc:
            print(str(exc), file=sys.stderr)
            self.close()
            return 1

        self.close()
        return 0
Example #4
0
 def write(self, data):
     """Writes a string to the file"""
     method = METHOD_STDERR
     if self.isStdout:
         method = METHOD_STDOUT
     sendJSONCommand(self.sock, method, self.procid, {'text': data})