Last active
August 29, 2024 00:56
-
-
Save westurner/9ffe6291e4ffa91b459e8afd70369230 to your computer and use it in GitHub Desktop.
Patch subprocess.Popen to log args, kwargs, and the cmd string
This file contains hidden or 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
from functools import partial | |
class Popen_with_logging(subprocess.Popen): | |
"""Patch subprocess.Popen to log args, kwargs, and the cmd string""" | |
def __init__(self, *args, cmdprefix='+', **kwargs): | |
print = kwargs.get("printfunc", | |
partial(__builtins__.print, file=sys.stderr)) | |
# print = log.debug ; printfunc=log.debug | |
if kwargs.get('shell'): | |
if len(args) == 1: | |
cmdstr = args[0] | |
else: | |
cmdstr = " ".join(str(x) for x in args) | |
else: # shell=False | |
if len(args) == 1: | |
cmdstr = " ".join(str(x) for x in args[0]) | |
else: | |
cmdstr = " ".join(str(x) for x in args) | |
event = dict(event='subprocess.Popen', args=args, kwargs=kwargs) | |
event['cmdstr'] = cmdstr | |
print(event) | |
print(f'>>> subprocess.Popen(*{args}, **{kwargs})') | |
print(cmdprefix, cmdstr) | |
super().__init__(*args, **kwargs) | |
# TODO: more proper monkeypatch than this: | |
_subprocess_Popen = subprocess.Popen | |
subprocess.Popen = Popen_with_logging | |
def test_subprocess_Popen_with_logging(): | |
subprocess.Popen(["echo", "test1"]) | |
subprocess.check_output("echo test1", shell=True) |
pytest docs > "Accessing captured output from a test function"
https://docs.pytest.org/en/stable/how-to/capture-stdout-stderr.html :
def test_myoutput(capsys): # or use "capfd" for fd-level
print("hello")
sys.stderr.write("world\n")
captured = capsys.readouterr()
assert captured.out == "hello\n"
assert captured.err == "world\n"
print("next")
captured = capsys.readouterr()
assert captured.out == "next\n"
sarge docs > "Why not just use subprocess?"
https://sarge.readthedocs.io/en/latest/overview.html#why-not-just-use-subprocess
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To log output:
https://stackoverflow.com/questions/72683597/can-i-log-a-subprocess-in-python(python2)bufsize=1
andtext=True
(instead of the deprecateduniversal_newlines=True
), stdout and stderr will be unbuffered, so that you don't have to explicitly call sys.stdout.flush() to actually see the text on the screen.You can explicitly specify unbuffered io for stdout and stderr with the
-u
option or thePYTHONUNBUFFERED=1
environment variable: