def test_process_env(self): self.exit_cb_called = 0 self.close_cb_called = 0 self.received_output = None def handle_close_cb(handle): self.close_cb_called += 1 def proc_exit_cb(proc, exit_status, term_signal): self.assertEqual(exit_status, 0) self.exit_cb_called += 1 proc.close(handle_close_cb) def stdout_read_cb(handle, data, error): self.received_output = data.strip() handle.close(handle_close_cb) stdout_pipe = pyuv.Pipe(self.loop) stdio = [] stdio.append(pyuv.StdIO(flags=pyuv.UV_IGNORE)) stdio.append( pyuv.StdIO(stream=stdout_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) proc = pyuv.Process.spawn(self.loop, args=[sys.executable, "proc_env_stdout.py"], env={"TEST": "TEST"}, exit_callback=proc_exit_cb, stdio=stdio) stdout_pipe.start_read(stdout_read_cb) self.loop.run() self.assertEqual(self.exit_cb_called, 1) self.assertEqual(self.close_cb_called, 2) self.assertEqual(self.received_output, b"TEST")
def webserver_spawn(): stdio = [] stdio.append(pyuv.StdIO(flags=pyuv.UV_IGNORE)) stdio.append( pyuv.StdIO(stream=webserver_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) stdio.append( pyuv.StdIO(stream=webserver_error_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) if sys.platform == 'win32': webserver.spawn(file=pythoner, args=[m_arg, r'%s' % websvr], exit_callback=webserver_exit_cb, stdio=stdio) else: webserver.spawn(file=pythoner, args=[m_arg, r'%s' % websvr], exit_callback=webserver_exit_cb, stdio=stdio) if std_print: webserver_pipe.start_read(webserver_read_cb) webserver_error_pipe.start_read(webserver_read_cb) print u'webserver start' process_pid["webserver"] = webserver.pid webserver_log.write(log_t('webserver', webserver.pid, 'start')) webserver_log.flush()
def AutoCalculate_spawn(): stdio = [] stdio.append(pyuv.StdIO(flags=pyuv.UV_IGNORE)) stdio.append( pyuv.StdIO(stream=AutoCalculate_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) stdio.append( pyuv.StdIO(stream=AutoCalculate_error_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) if sys.platform == 'win32': AutoCalculate.spawn(file=pythoner, args=[m_arg, manage, 'autocalculate'], exit_callback=AutoCalculate_exit_cb, stdio=stdio) else: AutoCalculate.spawn(file=pythoner, args=[m_arg, r'%s' % manage, 'autocalculate'], exit_callback=AutoCalculate_exit_cb, stdio=stdio) if std_print: AutoCalculate_pipe.start_read(AutoCalculate_read_cb) AutoCalculate_error_pipe.start_read(AutoCalculate_read_cb) print u'AutoCalculate start' process_pid["AutoCalculate"] = AutoCalculate.pid AutoCalculate_log.write(log_t('AutoCalculate', AutoCalculate.pid, 'start')) AutoCalculate_log.flush()
def WriteData_spawn(): stdio = [] stdio.append(pyuv.StdIO(flags=pyuv.UV_IGNORE)) stdio.append( pyuv.StdIO(stream=WriteData_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) stdio.append( pyuv.StdIO(stream=WriteData_error_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) if sys.platform == 'win32': WriteData.spawn(file=pythoner, args=[m_arg, manage, 'writedata'], exit_callback=WriteData_exit_cb, stdio=stdio) else: WriteData.spawn(file=pythoner, args=[m_arg, r'%s' % manage, 'writedata'], exit_callback=WriteData_exit_cb, stdio=stdio) if std_print: WriteData_pipe.start_read(WriteData_read_cb) WriteData_error_pipe.start_read(WriteData_read_cb) print u'WriteData start' process_pid["WriteData"] = WriteData.pid WriteData_log.write(log_t('WriteData', WriteData.pid, 'start')) WriteData_log.flush()
def InstantMsg_spawn(): stdio = [] stdio.append(pyuv.StdIO(flags=pyuv.UV_IGNORE)) stdio.append( pyuv.StdIO(stream=InstantMsg_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) stdio.append( pyuv.StdIO(stream=InstantMsg_error_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) if sys.platform == 'win32': InstantMsg.spawn(file=pythoner, args=[m_arg, manage, 'instantmsg'], exit_callback=InstantMsg_exit_cb, stdio=stdio) else: InstantMsg.spawn(file=pythoner, args=[m_arg, r'%s' % manage, 'instantmsg'], exit_callback=InstantMsg_exit_cb, stdio=stdio) if std_print: InstantMsg_pipe.start_read(InstantMsg_read_cb) InstantMsg_error_pipe.start_read(InstantMsg_read_cb) print u'InstantMsg start' process_pid["InstantMsg"] = InstantMsg.pid InstantMsg_log.write(log_t('InstantMsg', InstantMsg.pid, 'start')) InstantMsg_log.flush()
def FTPSync_spawn(): stdio = [] stdio.append(pyuv.StdIO(flags=pyuv.UV_IGNORE)) stdio.append( pyuv.StdIO(stream=FTPSync_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) stdio.append( pyuv.StdIO(stream=FTPSync_error_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) if sys.platform == 'win32': FTPSync.spawn(file=pythoner, args=[m_arg, manage, 'ftp_sync'], exit_callback=FTPSync_exit_cb, stdio=stdio) else: FTPSync.spawn(file=pythoner, args=[m_arg, r'%s' % manage, 'ftp_sync'], exit_callback=FTPSync_exit_cb, stdio=stdio) if std_print: FTPSync_pipe.start_read(FTPSync_read_cb) FTPSync_error_pipe.start_read(FTPSync_read_cb) print u'FTPSync start' process_pid["FTPSync"] = FTPSync.pid FTPSync_log.write(log_t('FTPSync', FTPSync.pid, 'start')) FTPSync_log.flush()
def __init__(self, loop, process, stdio=[]): self.loop = loop self.process = process self._emitter = EventEmitter(loop) self._stdio = [] self._channels = [] if not stdio: self._stdio = [ pyuv.StdIO(flags=pyuv.UV_IGNORE), pyuv.StdIO(flags=pyuv.UV_IGNORE) ] else: # create (channel, stdio) pairs for label in stdio[:2]: # io registered can any label, so it's easy to redirect # stderr to stdout, just use the same label. p = pyuv.Pipe(loop) io = pyuv.StdIO(stream=p, flags=pyuv.UV_CREATE_PIPE | \ pyuv.UV_READABLE_PIPE | \ pyuv.UV_WRITABLE_PIPE) setattr(p, 'label', label) self._channels.append(p) self._stdio.append(io)
def ZkposAdms_spawn(): stdio = [] stdio.append(pyuv.StdIO(flags=pyuv.UV_IGNORE)) stdio.append( pyuv.StdIO(stream=ZkposAdms_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) stdio.append( pyuv.StdIO(stream=ZkposAdms_error_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) if sys.platform == 'win32': ZkposAdms.spawn(file=pythoner, args=[m_arg, manage, 'zkpos_adms'], exit_callback=ZkposAdms_exit_cb, stdio=stdio) else: ZkposAdms.spawn(file=pythoner, args=[m_arg, r'%s' % manage, 'zkpos_adms'], exit_callback=ZkposAdms_exit_cb, stdio=stdio) if std_print: ZkposAdms_pipe.start_read(ZkposAdms_read_cb) ZkposAdms_error_pipe.start_read(ZkposAdms_read_cb) print u'ZkposAdms start' process_pid["ZkposAdms"] = ZkposAdms.pid ZkposAdms_log.write(log_t('ZkposAdms', ZkposAdms.pid, 'start')) ZkposAdms_log.flush()
def DataCommCenter_spawn(): stdio = [] stdio.append(pyuv.StdIO(flags=pyuv.UV_IGNORE)) stdio.append( pyuv.StdIO(stream=DataCommCenter_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) stdio.append( pyuv.StdIO(stream=DataCommCenter_error_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) if sys.platform == 'win32': DataCommCenter.spawn(file=pythoner, args=[m_arg, manage, 'datacommcenter'], exit_callback=DataCommCenter_exit_cb, stdio=stdio) else: DataCommCenter.spawn(file=pythoner, args=[m_arg, r'%s' % manage, 'datacommcenter'], exit_callback=DataCommCenter_exit_cb, stdio=stdio) if std_print: DataCommCenter_pipe.start_read(DataCommCenter_read_cb) DataCommCenter_error_pipe.start_read(DataCommCenter_read_cb) print u'DataCommCenter start' process_pid["DataCommCenter"] = DataCommCenter.pid DataCommCenter_log.write( log_t('DataCommCenter', DataCommCenter.pid, 'start')) DataCommCenter_log.flush()
def RedisSelf_spawn(): stdio = [] stdio.append(pyuv.StdIO(flags=pyuv.UV_IGNORE)) stdio.append( pyuv.StdIO(stream=RedisSelf_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) stdio.append( pyuv.StdIO(stream=RedisSelf_error_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) if sys.platform == 'win32': RedisSelf.spawn(file=pythoner, args=[m_arg, manage, 'redis_self'], exit_callback=RedisSelf_exit_cb, stdio=stdio) else: RedisSelf.spawn(file=pythoner, args=[m_arg, r'%s' % manage, 'redis_self'], exit_callback=RedisSelf_exit_cb, stdio=stdio) if std_print: RedisSelf_pipe.start_read(RedisSelf_read_cb) RedisSelf_error_pipe.start_read(RedisSelf_read_cb) print u'RedisSelf start' process_pid["RedisSelf"] = RedisSelf.pid RedisSelf_log.write(log_t('RedisSelf', RedisSelf.pid, 'start')) RedisSelf_log.flush()
def spawn_process(self): args = sys.argv[1:] self.proc = pyuv.Process(self.loop) stdout_pipe = pyuv.Pipe(self.loop) stderr_pipe = pyuv.Pipe(self.loop) stdio = [] stdio.append(pyuv.StdIO(flags=pyuv.UV_IGNORE)) stdio.append( pyuv.StdIO(stream=stdout_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) stdio.append( pyuv.StdIO(stream=stderr_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) self.state = "RUNNING" self.proc.spawn(file=args[0], args=args[1:], cwd=os.getcwd(), exit_callback=self.proc_exit_cb, stdio=stdio) stdout_pipe.start_read(self.stdout_read_cb) stderr_pipe.start_read(self.stderr_read_cb)
def _start_process_async(self, async_handle): # check config if self._proc_config == None: raise TypeError mc_w_config = self._proc_config # set args cmd_args = [ mc_w_config.java_bin, "-Xms%sM" % int(mc_w_config.min_RAM), "-Xmx%sM" % int(mc_w_config.max_RAM), "-jar", mc_w_config.jar_file, "nogui" ] self._init_env(mc_w_config.proc_cwd, mc_w_config.port, mc_w_config.max_player) logger.debug("cmd args: %s" % cmd_args) # init pipes self._stdin_pipe = pyuv.Pipe(self._loop, True) self._stdout_pipe = pyuv.Pipe(self._loop, True) self._stderr_pipe = pyuv.Pipe(self._loop, True) # set pipes stdin = pyuv.StdIO(stream=self._stdin_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_READABLE_PIPE) stdout = pyuv.StdIO(stream=self._stdout_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE) stderr = pyuv.StdIO(stream=self._stderr_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE) # spawn process self._proc = pyuv.Process.spawn(self._loop, args=cmd_args, exit_callback=self.on_exit, stdio=[stdin, stdout, stderr], cwd=mc_w_config.proc_cwd, flags=pyuv.UV_PROCESS_DETACHED) # set pid self._pid = self._proc.pid logger.info("Start Process pid=(%s)" % self._pid) # on read self._stdout_pipe.start_read(self.on_stdout_read) self._stderr_pipe.start_read(self.on_stderr_read) # incr active count, reset crash count, run callbacks and so on self._proc_pool.incr_active_count() logger.debug("active count = %s" % self._proc_pool.get_active_count()) # run callback self.on_instance_start(self.inst_id) return True
def test_process_stdin(self): self.exit_cb_called = 0 self.close_cb_called = 0 self.received_output = None self.exit_status = -1 self.term_signal = 0 def handle_close_cb(handle): self.close_cb_called += 1 def proc_exit_cb(proc, exit_status, term_signal): self.exit_cb_called += 1 self.exit_status = exit_status self.term_signal = term_signal proc.close(handle_close_cb) def stdout_read_cb(handle, data, error): if data: self.received_output = data.strip() handle.close(handle_close_cb) def stdin_write_cb(handle, error): handle.close(handle_close_cb) loop = pyuv.Loop.default_loop() stdin_pipe = pyuv.Pipe(loop) stdout_pipe = pyuv.Pipe(loop) stdio = [] stdio.append( pyuv.StdIO(stream=stdin_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_READABLE_PIPE)) stdio.append( pyuv.StdIO(stream=stdout_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) proc = pyuv.Process(loop) if sys.platform == 'win32': proc.spawn(file="cmd.exe", args=["/c", "proc_stdin_stdout.py"], exit_callback=proc_exit_cb, stdio=stdio) else: proc.spawn(file="./proc_stdin_stdout.py", exit_callback=proc_exit_cb, stdio=stdio) stdout_pipe.start_read(stdout_read_cb) stdin_pipe.write(b"TEST" + common.linesep, stdin_write_cb) loop.run() self.assertEqual(self.exit_cb_called, 1) self.assertEqual(self.close_cb_called, 3) self.assertEqual(self.received_output, b"TEST")
def _connect_child(self, argv): self._write_stream = pyuv.Pipe(self._loop) self._read_stream = pyuv.Pipe(self._loop) self._error_stream = pyuv.Pipe(self._loop) stdin = pyuv.StdIO(self._write_stream, flags=pyuv.UV_CREATE_PIPE + pyuv.UV_READABLE_PIPE) stdout = pyuv.StdIO(self._read_stream, flags=pyuv.UV_CREATE_PIPE + pyuv.UV_WRITABLE_PIPE) stderr = pyuv.StdIO(self._error_stream, flags=pyuv.UV_CREATE_PIPE + pyuv.UV_WRITABLE_PIPE) pyuv.Process.spawn(self._loop, args=argv, exit_callback=self._on_exit, flags=pyuv.UV_PROCESS_WINDOWS_HIDE, stdio=(stdin, stdout, stderr,)) self._error_stream.start_read(self._on_read)
def _do_test(self): self.channel = pyuv.Pipe(self.loop, True) stdio = [pyuv.StdIO(stream=self.channel, flags=pyuv.UV_CREATE_PIPE|pyuv.UV_READABLE_PIPE|pyuv.UV_WRITABLE_PIPE)] proc = pyuv.Process(self.loop) proc.spawn(file=sys.executable, args=["proc_ipc_echo.py"], exit_callback=self.proc_exit_cb, stdio=stdio) self.channel.write2(b".", self.send_handle) self.channel.start_read2(partial(self.on_channel_read, self.send_handle_type)) self.loop.run()
def _setup_stdio(self): # for now we ignore all stdin if not self.redirect_input: self._stdio = [pyuv.StdIO(flags=pyuv.UV_IGNORE)] else: self._redirect_in = RedirectStdin(self.loop, self) self._stdio = [self._redirect_in.stdio] self._redirect_io = RedirectIO(self.loop, self, self.redirect_output) self._stdio.extend(self._redirect_io.stdio)
def __init__(self, loop, process): self.loop = loop self.process = process self.channel = pyuv.Pipe(loop) self.stdio = pyuv.StdIO(stream=self.channel, flags=pyuv.UV_CREATE_PIPE | \ pyuv.UV_READABLE_PIPE | \ pyuv.UV_WRITABLE_PIPE ) self._emitter = EventEmitter(loop)
def test_process_stdin(self): self.exit_cb_called = 0 self.close_cb_called = 0 self.received_output = None self.exit_status = -1 self.term_signal = 0 def handle_close_cb(handle): self.close_cb_called += 1 def proc_exit_cb(proc, exit_status, term_signal): self.exit_cb_called += 1 self.exit_status = exit_status self.term_signal = term_signal proc.close(handle_close_cb) def stdout_read_cb(handle, data, error): if data: self.received_output = data.strip() handle.close(handle_close_cb) def stdin_write_cb(handle, error): handle.close(handle_close_cb) stdin_pipe = pyuv.Pipe(self.loop) stdout_pipe = pyuv.Pipe(self.loop) stdio = [] stdio.append( pyuv.StdIO(stream=stdin_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_READABLE_PIPE)) stdio.append( pyuv.StdIO(stream=stdout_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) proc = pyuv.Process.spawn( self.loop, args=[sys.executable, "proc_stdin_stdout.py"], exit_callback=proc_exit_cb, stdio=stdio) stdout_pipe.start_read(stdout_read_cb) stdin_pipe.write(b"TEST" + linesep, stdin_write_cb) self.loop.run() self.assertEqual(self.exit_cb_called, 1) self.assertEqual(self.close_cb_called, 3) self.assertEqual(self.received_output, b"TEST")
def runAProgramStraight(self, args): """Run a program without anything special. Leaves stdin/stdout/stderr alone. This should be run when the prompt is inactive.""" res=[] def finish(proc,status,signal): proc.close() proc.loop.stop() res.append(status) stdio = [ pyuv.StdIO(fd=sys.stdin.fileno(), flags=pyuv.UV_INHERIT_FD), pyuv.StdIO(fd=sys.stdout.fileno(), flags=pyuv.UV_INHERIT_FD), pyuv.StdIO(fd=sys.stderr.fileno(), flags=pyuv.UV_INHERIT_FD), ] self.ttyBusy = True s = pyuv.Process.spawn(self.ptkevloop.realloop, args, stdio=stdio, exit_callback=finish) self.ptkevloop.realloop.run() self.ttyBusy = False return res[0]
def _do_test(self, test_type): self.connections = [] self.local_conn_accepted = False self.tcp_server = None self.channel = pyuv.Pipe(self.loop, True) stdio = [pyuv.StdIO(stream=self.channel, flags=pyuv.UV_CREATE_PIPE|pyuv.UV_READABLE_PIPE|pyuv.UV_WRITABLE_PIPE)] proc = pyuv.Process(self.loop) proc.spawn(file=sys.executable, args=["proc_ipc.py", test_type, str(TEST_PORT)], exit_callback=self.proc_exit_cb, stdio=stdio) self.channel.start_read2(self.on_channel_read) self.loop.run()
def __start_process(self): callbak_newdata = self.add_output callback_newerr = self.add_stderr callback_exit = self.callback_exit event_loop = core.core.core_event_loop stdio = [] self.pipe_stdin = pyuv.Pipe(event_loop) self.pipe_stdout = pyuv.Pipe(event_loop) self.pipe_stderr = pyuv.Pipe(event_loop) # Создание пайпа для stdin. В pipewrite мы будем писать на нашем конце, а piperead передадим child-у (piperead, pipewrite) = os.pipe() self.pipe_stdin.open(pipewrite) stdio.append(pyuv.StdIO(fd=piperead, flags=pyuv.UV_INHERIT_FD)) stdio.append( pyuv.StdIO(self.pipe_stdout, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) stdio.append( pyuv.StdIO(self.pipe_stderr, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) job_callback_exit = lambda proc, exit_status, term_signal: WebConsole.process_exit( self, exit_status, term_signal, callback_exit) self.proc = pyuv.Process.spawn(event_loop, executable="cmd.exe", args=["cmd.exe"], cwd=self.physical_path, exit_callback=job_callback_exit, env=self.env, stdio=stdio) self.pipe_stdout.start_read( lambda handle, data, error: WebConsole.read_cb( self, handle, data, error, callbak_newdata)) self.pipe_stderr.start_read( lambda handle, data, error: WebConsole.read_cb( self, handle, data, error, callback_newerr)) self.on_callback_exit = None
def runAProgramGetOutput(self, args): """Run a program obtaining standard output as a string. Leaves stdin/stderr alone. This should be run when the prompt is inactive.""" res=[] def finish(proc,status,signal): proc.close() proc.loop.stop() res.append(status) com = pyuv.Pipe(self.ptkevloop.realloop, True) stdio = [ pyuv.StdIO(fd=sys.stdin.fileno(), flags=pyuv.UV_INHERIT_FD), pyuv.StdIO(stream=com, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE), pyuv.StdIO(fd=sys.stderr.fileno(), flags=pyuv.UV_INHERIT_FD), ] self.ttyBusy = True s = pyuv.Process.spawn(self.ptkevloop.realloop, args, stdio=stdio, exit_callback=finish) self.ptkevloop.realloop.run() self.ttyBusy = False return res[0]
def test_process_env(self): self.exit_cb_called = 0 self.close_cb_called = 0 self.received_output = None def handle_close_cb(handle): self.close_cb_called += 1 def proc_exit_cb(proc, exit_status, term_signal): self.assertEqual(exit_status, 0) self.exit_cb_called += 1 proc.close(handle_close_cb) def stdout_read_cb(handle, data, error): self.received_output = data.strip() handle.close(handle_close_cb) loop = pyuv.Loop.default_loop() stdout_pipe = pyuv.Pipe(loop) stdio = [] stdio.append(pyuv.StdIO(flags=pyuv.UV_IGNORE)) stdio.append( pyuv.StdIO(stream=stdout_pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE)) proc = pyuv.Process(loop) if sys.platform == 'win32': proc.spawn(file="cmd.exe", args=["/c", "proc_env_stdout.py"], env={"TEST": "TEST"}, exit_callback=proc_exit_cb, stdio=stdio) else: proc.spawn(file="./proc_env_stdout.py", env={"TEST": "TEST"}, exit_callback=proc_exit_cb, stdio=stdio) stdout_pipe.start_read(stdout_read_cb) loop.run() self.assertEqual(self.exit_cb_called, 1) self.assertEqual(self.close_cb_called, 2) self.assertEqual(self.received_output, b"TEST")
def _setup_stdio(self): # for now we ignore all stdin if not self.redirect_input: self._stdio = [pyuv.StdIO(flags=pyuv.UV_IGNORE)] else: self._redirect_in = RedirectStdin(self.loop, self) self._stdio = [self._redirect_in.stdio] self._redirect_io = RedirectIO(self.loop, self, self.redirect_output) self._stdio.extend(self._redirect_io.stdio) # create custom streams, for label in self.custom_streams: stream = self.streams[label] = Stream(self.loop, self, len(self._stdio)) self._stdio.append(stream.stdio) # create containers for custom channels. for channel in self.custom_channels: assert not channel.closed, \ "Closed channel {0!r} can't be passed to process!" \ .format(channel) self._stdio.append( pyuv.StdIO(stream=channel, flags=pyuv.UV_INHERIT_STREAM))
def runAProgramAsFilter(self, args, data): """Run a program with the given input. Return the output. Leaves stderr alone. This should be run when the prompt is inactive.""" res=[None, b""] done=[0] def finish(proc,status,signal): proc.close() proc.loop.stop() res[0] = status com = pyuv.Pipe(self.ptkevloop.realloop, True) como = pyuv.Pipe(self.ptkevloop.realloop, True) stdio = [ pyuv.StdIO(stream=com, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_READABLE_PIPE), pyuv.StdIO(stream=como, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE), pyuv.StdIO(fd=sys.stderr.fileno(), flags=pyuv.UV_INHERIT_FD), ] self.ttyBusy = True s = pyuv.Process.spawn(self.ptkevloop.realloop, args, stdio=stdio, exit_callback=finish) def closeWhenDone(handle, error): # TODO: Maybe report error? handle.close() def doneWriting(handle, error): # TODO: Maybe report error? if done[0]: closeWhenDone(handle, error) done[0] = True def readCb(handle, data, error): if data is None: handle.stop_read() if done[0]: closeWhenDone(handle, error) done[0] = True return res[1] += data como.start_read(readCb) com.write(data, closeWhenDone) self.ptkevloop.realloop.run() self.ttyBusy = False return res
def test_ipc2(self): self.connections = [] self.local_conn_accepted = False self.tcp_server = None self.channel = pyuv.Pipe(self.loop, True) stdio = [pyuv.StdIO(stream=self.channel, flags=pyuv.UV_CREATE_PIPE|pyuv.UV_READABLE_PIPE|pyuv.UV_WRITABLE_PIPE)] proc = pyuv.Process(self.loop) if sys.platform == 'win32': proc.spawn(file="cmd.exe", args=["/c", " proc_ipc.py", "listen_after_write"], exit_callback=self.proc_exit_cb, stdio=stdio) else: proc.spawn(file=sys.executable, args=["proc_ipc.py", "listen_after_write"], exit_callback=self.proc_exit_cb, stdio=stdio) self.channel.start_read2(self.on_channel_read) self.loop.run()
def test_ipc_send_recv(self): # Handle that will be sent to the process and back self.send_pipe = pyuv.Pipe(self.loop, True) self.send_pipe.bind(TEST_PIPE) self.channel = pyuv.Pipe(self.loop, True) stdio = [pyuv.StdIO(stream=self.channel, flags=pyuv.UV_CREATE_PIPE|pyuv.UV_READABLE_PIPE|pyuv.UV_WRITABLE_PIPE)] proc = pyuv.Process(self.loop) if sys.platform == 'win32': proc.spawn(file="cmd.exe", args=["/c", " proc_ipc_echo.py"], exit_callback=self.proc_exit_cb, stdio=stdio) else: proc.spawn(file=sys.executable, args=["proc_ipc_echo.py"], exit_callback=self.proc_exit_cb, stdio=stdio) self.channel.write2(b".", self.send_pipe) self.channel.start_read2(self.on_channel_read) self.loop.run()
def run_command(self, view): file_name = view.file_name() if file_name is None: return loop = pyuv.Loop.default_loop() pipe = LineReaderPipe(loop) ios = [ pyuv.StdIO(), # stdin - ignore pyuv.StdIO(pipe, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_WRITABLE_PIPE) ] # stdout - create pipe exit_cb = functools.partial(self.command_finished, view) args = [self.command] + self.args + [file_name] log.debug('Running %s' % ' '.join(args)) pyuv.Process.spawn(loop, args, exit_callback=exit_cb, stdio=ios, flags=pyuv.UV_PROCESS_WINDOWS_HIDE) line_cb = functools.partial(self.process_lines, view) pipe.start_read(line_cb)
def runAProgramWithInput(self, args, data): """Run a program with the given input. Leaves stdout/stderr alone. This should be run when the prompt is inactive.""" res=[] def finish(proc,status,signal): proc.close() proc.loop.stop() res.append(status) com = pyuv.Pipe(self.ptkevloop.realloop, True) stdio = [ pyuv.StdIO(stream=com, flags=pyuv.UV_CREATE_PIPE | pyuv.UV_READABLE_PIPE), pyuv.StdIO(fd=sys.stdout.fileno(), flags=pyuv.UV_INHERIT_FD), pyuv.StdIO(fd=sys.stderr.fileno(), flags=pyuv.UV_INHERIT_FD), ] self.ttyBusy = True s = pyuv.Process.spawn(self.ptkevloop.realloop, args, stdio=stdio, exit_callback=finish) def closeWhenDone(handle, error): # TODO: Maybe report error? handle.close() com.write(data, closeWhenDone) self.ptkevloop.realloop.run() self.ttyBusy = False return res[0]
def _create_stdio(self, name, loop, handle, fd): # Create a pyuv.StdIO container if handle is None: stdio = pyuv.StdIO(fd=fd, flags=pyuv.UV_INHERIT_FD) elif handle == PIPE: stdio = pyuv.StdIO(stream=pyuv.Pipe(loop), flags=CREATE_PIPE) elif handle == DEVNULL: stdio = pyuv.StdIO(flags=pyuv.UV_IGNORE) elif isinstance(handle, pyuv.Stream): stdio = pyuv.StdIO(stream=handle, flags=CREATE_PIPE) elif isinstance(handle, int) and handle >= 0: stdio = pyuv.StdIO(fd=handle, flags=pyuv.UV_INHERIT_FD) elif hasattr(handle, 'fileno'): stdio = pyuv.StdIO(fd=handle.fileno(), flags=CREATE_PIPE) else: raise TypeError('{}: must be PIPE, an fd, a Stream, or a file-like object' ' (got {!r})'.format(name, type(handle).__name__)) return stdio