Skip to content

Commit

Permalink
Merge pull request #20 from pgehring/fix/stdio_handling
Browse files Browse the repository at this point in the history
fix stderr stdout output handling for remote connections
  • Loading branch information
Deleh authored Oct 25, 2024
2 parents fd0634a + 8f4a2ef commit 640c948
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions robmuxinator/robmuxinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,22 +180,24 @@ def send_cmd(self, cmd, wait_for_exit_status=True, get_pty=False):
logger.debug(f"{cmd}")
if wait_for_exit_status:
returncode = stdout.channel.recv_exit_status()

stdout = stdout.read().decode()
stderr = stderr.read().decode()
else:
logger.debug(" using local connection")
process = subprocess.Popen([cmd],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
text=True)
stdout = process.stdout
stderr = process.stderr

if wait_for_exit_status:
stdout_str, stderr_str = process.communicate()
# wrap output in IO buffers so it is compatible with .readlines()
stdout = io.StringIO(stdout_str)
stderr = io.StringIO(stderr_str)
stdout, stderr = process.communicate()
returncode = process.returncode
else:
stdout = process.stdout.read()
stderr = process.stderr.read()

logger.debug(
"send_cmd: {} took {} secs".format(
cmd, (datetime.now() - start).total_seconds()
Expand Down Expand Up @@ -235,13 +237,11 @@ def stop_session(self, session_name):
pid_session = None

if not returncode:
pid_session = stdout.readlines()
pid_session = pid_session[0].rstrip("\n") if len(pid_session) > 0 else None
pid_session = stdout[0].rstrip("\n") if len(stdout) > 0 else None
if pid_session:
cmd = "pgrep -P {}".format(pid_session)
returncode, stdout, stderr = self.send_cmd(cmd)
pid = stdout.readlines()
pid = pid[0].rstrip("\n") if len(pid) > 0 else None
pid = stdout[0].rstrip("\n") if len(stdout) > 0 else None
if not pid:
logger.error(f" session {session_name}: could not get process pid for session pid")
else:
Expand Down Expand Up @@ -533,8 +533,8 @@ def start(self):
)
logger.debug("pre_condition: {}".format(self._pre_condition))
logger.debug("ret: {}".format(ret))
logger.debug("stdout: {}".format(stdout.getvalue()))
logger.debug("stderr: {}".format(stderr.getvalue()))
logger.debug("stdout: {}".format(stdout))
logger.debug("stderr: {}".format(stderr))
if not ret:
break
time.sleep(0.25)
Expand Down

0 comments on commit 640c948

Please sign in to comment.