def start(cmd, input=None, **kwds): kwds['stdout'] = PIPE kwds['stderr'] = PIPE if input is None and 'stdin' not in kwds: kwds['stdin'] = None else: kwds['stdin'] = PIPE proc = yield From(asyncio.create_subprocess_shell(cmd, **kwds)) tasks = [] if input is not None: tasks.append(send_input(proc.stdin, input)) else: print('No stdin') if proc.stderr is not None: tasks.append(log_errors(proc.stderr)) else: print('No stderr') if proc.stdout is not None: tasks.append(read_stdout(proc.stdout)) else: print('No stdout') if tasks: # feed stdin while consuming stdout to avoid hang # when stdin pipe is full yield From(asyncio.wait(tasks)) exitcode = yield From(proc.wait()) print("exit code: %s" % exitcode)
def test_start_new_session(self): def start_new_session(): os.setsid() # start the new process in a new session create = asyncio.create_subprocess_shell("exit 8", preexec_fn=start_new_session, loop=self.loop) proc = self.loop.run_until_complete(create) exitcode = self.loop.run_until_complete(proc.wait()) self.assertEqual(exitcode, 8)
def test_start_new_session(self): def start_new_session(): os.setsid() # start the new process in a new session create = asyncio.create_subprocess_shell('exit 8', preexec_fn=start_new_session, loop=self.loop) proc = self.loop.run_until_complete(create) exitcode = self.loop.run_until_complete(proc.wait()) self.assertEqual(exitcode, 8)
def cat(loop): proc = yield From( asyncio.create_subprocess_shell("cat", stdin=PIPE, stdout=PIPE)) print("pid: %s" % proc.pid) message = "Hello World!" print("cat write: %r" % message) stdout, stderr = yield From(proc.communicate(message.encode('ascii'))) print("cat read: %r" % stdout.decode('ascii')) exitcode = yield From(proc.wait()) print("(exit code %s)" % exitcode)
def _run_deploy_process(self, addr): # subprocess_exec is broken in gbulb/base_events.py, so use shell deploy_cmd = "RHOST=%s %s start" % ( 'odroid@' + addr, os.path.join( os.path.dirname(__file__), 'deploy-vserver.sh')) logger = logging.getLogger('deploy') logger.warning('Running: %s', deploy_cmd) # We would like to capture stdout/stderr and redirect to logger, so that # we properly record deploy error messages, but gbulb seems to be # broken. proc = yield From( asyncio.create_subprocess_shell( deploy_cmd, close_fds=True, #stdin=open('/dev/stdin', 'r'), stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT )) recent = list() _LOG_RE = re.compile( r'^[0-9][0-9]:[0-9][0-9]:[0-9][0-9][.][0-9]{3} ' r'\[[A-Z]\] ') started = False while True: line = yield From(proc.stdout.readline()) if line == '': break line = vui_helpers.sanitize_stdout(line) if not (started and _LOG_RE.match(line)): # To avoid double-display of logs, suppress what looks like # log messages, and only show them if deply fails. logger.info(line) else: recent.append(line) while len(recent) > 100: recent.pop(0) if '*** Live logs ***' in line: started = True retcode = yield From(proc.wait()) if retcode != 0: logger.warning('Replaying suppressed lines') for line in recent: logger.info('| ' + line) logger.warning('Process exited, code %r', retcode) assert retcode == 0, 'Deploy returned failing error code' assert started, 'Server process never got started'
def _run_deploy_process(self, addr): # subprocess_exec is broken in gbulb/base_events.py, so use shell deploy_cmd = "RHOST=%s %s start" % ( 'odroid@' + addr, os.path.join(os.path.dirname(__file__), 'deploy-vserver.sh')) logger = logging.getLogger('deploy') logger.warning('Running: %s', deploy_cmd) # We would like to capture stdout/stderr and redirect to logger, so that # we properly record deploy error messages, but gbulb seems to be # broken. proc = yield From( asyncio.create_subprocess_shell( deploy_cmd, close_fds=True, #stdin=open('/dev/stdin', 'r'), stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT)) recent = list() _LOG_RE = re.compile(r'^[0-9][0-9]:[0-9][0-9]:[0-9][0-9][.][0-9]{3} ' r'\[[A-Z]\] ') started = False while True: line = yield From(proc.stdout.readline()) if line == '': break line = vui_helpers.sanitize_stdout(line) if not (started and _LOG_RE.match(line)): # To avoid double-display of logs, suppress what looks like # log messages, and only show them if deply fails. logger.info(line) else: recent.append(line) while len(recent) > 100: recent.pop(0) if '*** Live logs ***' in line: started = True retcode = yield From(proc.wait()) if retcode != 0: logger.warning('Replaying suppressed lines') for line in recent: logger.info('| ' + line) logger.warning('Process exited, code %r', retcode) assert retcode == 0, 'Deploy returned failing error code' assert started, 'Server process never got started'
def test_shell(self): create = asyncio.create_subprocess_shell('exit 7', loop=self.loop) proc = self.loop.run_until_complete(create) exitcode = self.loop.run_until_complete(proc.wait()) self.assertEqual(exitcode, 7)