From 06a91061566d3fb72774f639cec278e83e7c12ec Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 25 Apr 2019 22:10:18 +0200 Subject: [PATCH] Pretty-format subprocess logs. e.g. subprocess spawned by the animation machinery are now logged as ``` MovieWriter._run: running command: ffmpeg -f rawvideo -vcodec rawvideo -s 640x480 -pix_fmt rgba -r 20.0 -i pipe: -vcodec h264 -pix_fmt yuv420p -y movie.mp4 ``` instead of ``` MovieWriter.run: running command: ['ffmpeg', '-f', 'rawvideo', '-vcodec', 'rawvideo', '-s', '640x480', '-pix_fmt', 'rgba', '-r', '20.0', '-i', 'pipe:', '-vcodec', 'h264', '-pix_fmt', 'yuv420p', '-y', 'movie.mp4'] ``` --- lib/matplotlib/animation.py | 3 ++- lib/matplotlib/cbook/__init__.py | 11 +++++++++-- lib/matplotlib/texmanager.py | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/animation.py b/lib/matplotlib/animation.py index d607c4940362..066633d47601 100644 --- a/lib/matplotlib/animation.py +++ b/lib/matplotlib/animation.py @@ -354,7 +354,8 @@ def _run(self): # movie file. *args* returns the sequence of command line arguments # from a few configuration options. command = self._args() - _log.info('MovieWriter.run: running command: %s', command) + _log.info('MovieWriter._run: running command: %s', + cbook._pformat_subprocess(command)) PIPE = subprocess.PIPE self._proc = subprocess.Popen( command, stdin=PIPE, stdout=PIPE, stderr=PIPE, diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index bb59960ff57e..bbeb82b18f1a 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -19,6 +19,7 @@ import os from pathlib import Path import re +import shlex import subprocess import sys import time @@ -2119,6 +2120,12 @@ def _unmultiplied_rgba8888_to_premultiplied_argb32(rgba8888): return argb32 +def _pformat_subprocess(command): + """Pretty-format a subprocess command for printing/logging purposes.""" + return (command if isinstance(command, str) + else " ".join(shlex.quote(os.fspath(arg)) for arg in command)) + + def _check_and_log_subprocess(command, logger, **kwargs): """ Run *command* using `subprocess.check_output`. If it succeeds, return the @@ -2126,7 +2133,7 @@ def _check_and_log_subprocess(command, logger, **kwargs): the failed command and captured output. Both the command and the output are logged at DEBUG level on *logger*. """ - logger.debug(command) + logger.debug('%s', _pformat_subprocess(command)) try: report = subprocess.check_output( command, stderr=subprocess.STDOUT, **kwargs) @@ -2136,7 +2143,7 @@ def _check_and_log_subprocess(command, logger, **kwargs): ' {}\n' 'failed and generated the following output:\n' '{}' - .format(command, exc.output.decode('utf-8'))) + .format(_pformat_subprocess(command), exc.output.decode('utf-8'))) logger.debug(report) return report diff --git a/lib/matplotlib/texmanager.py b/lib/matplotlib/texmanager.py index 0b1773bd5648..c970526976bc 100644 --- a/lib/matplotlib/texmanager.py +++ b/lib/matplotlib/texmanager.py @@ -297,7 +297,7 @@ def make_tex_preview(self, tex, fontsize): return texfile def _run_checked_subprocess(self, command, tex): - _log.debug(command) + _log.debug(cbook._pformat_subprocess(command)) try: report = subprocess.check_output(command, cwd=self.texcache,