Last active
December 6, 2024 14:45
-
-
Save dhrrgn/6073120 to your computer and use it in GitHub Desktop.
Running a command in Python and optionally processing the output in realtime.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import shlex | |
import subprocess | |
import sys | |
def run_cmd(cmd, callback=None, watch=False): | |
"""Runs the given command and gathers the output. | |
If a callback is provided, then the output is sent to it, otherwise it | |
is just returned. | |
Optionally, the output of the command can be "watched" and whenever new | |
output is detected, it will be sent to the given `callback`. | |
Args: | |
cmd (str): The command to run. | |
Kwargs: | |
callback (func): The callback to send the output to. | |
watch (bool): Whether to watch the output of the command. If True, | |
then `callback` is required. | |
Returns: | |
A string containing the output of the command, or None if a `callback` | |
was given. | |
Raises: | |
RuntimeError: When `watch` is True, but no callback is given. | |
""" | |
if watch and not callback: | |
raise RuntimeError('You must provide a callback when watching a process.') | |
output = None | |
try: | |
proc = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE) | |
if watch: | |
while proc.poll() is None: | |
line = proc.stdout.readline() | |
if line != "": | |
callback(line) | |
# Sometimes the process exits before we have all of the output, so | |
# we need to gather the remainder of the output. | |
remainder = proc.communicate()[0] | |
if remainder: | |
callback(remainder) | |
else: | |
output = proc.communicate()[0] | |
except: | |
err = str(sys.exc_info()[1]) + "\n" | |
output = err | |
if callback and output is not None: | |
callback(output) | |
return None | |
return output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from cmd import run_cmd | |
print run_cmd("ls -al") | |
def flush(message): | |
sys.stdout.write(message) | |
sys.stdout.flush() | |
run_cmd("ping google.com", callback=flush, watch=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment