diff --git a/lib/matplotlib/backends/backend_pgf.py b/lib/matplotlib/backends/backend_pgf.py index d26cd94547b5..415b2141e644 100644 --- a/lib/matplotlib/backends/backend_pgf.py +++ b/lib/matplotlib/backends/backend_pgf.py @@ -870,15 +870,9 @@ def _print_pdf_to_fh(self, fh, *args, **kwargs): pathlib.Path(fname_tex).write_text(latexcode, encoding="utf-8") texcommand = rcParams["pgf.texsystem"] - cmdargs = [texcommand, "-interaction=nonstopmode", - "-halt-on-error", "figure.tex"] - try: - subprocess.check_output( - cmdargs, stderr=subprocess.STDOUT, cwd=tmpdir) - except subprocess.CalledProcessError as e: - raise RuntimeError( - "%s was not able to process your file.\n\nFull log:\n%s" - % (texcommand, e.output)) + cbook._check_and_log_subprocess( + [texcommand, "-interaction=nonstopmode", "-halt-on-error", + "figure.tex"], _log, cwd=tmpdir) # copy file contents to target with open(fname_pdf, "rb") as fh_src: @@ -1101,21 +1095,10 @@ def close(self): def _run_latex(self): texcommand = rcParams["pgf.texsystem"] - cmdargs = [ - texcommand, - "-interaction=nonstopmode", - "-halt-on-error", - os.path.basename(self._fname_tex), - ] - try: - subprocess.check_output( - cmdargs, stderr=subprocess.STDOUT, cwd=self._tmpdir - ) - except subprocess.CalledProcessError as e: - raise RuntimeError( - "%s was not able to process your file.\n\nFull log:\n%s" - % (texcommand, e.output.decode('utf-8'))) - + cbook._check_and_log_subprocess( + [texcommand, "-interaction=nonstopmode", "-halt-on-error", + os.path.basename(self._fname_tex)], + _log, cwd=self._tmpdir) # copy file contents to target shutil.copyfile(self._fname_pdf, self._outputfile) diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index fff177c61ac8..d9af2ed24328 100644 --- a/lib/matplotlib/backends/backend_ps.py +++ b/lib/matplotlib/backends/backend_ps.py @@ -1421,34 +1421,13 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble, latexfile = latexfile.replace("\\", "/") # Replace ~ so Latex does not think it is line break latexfile = latexfile.replace("~", "\\string~") - command = ["latex", "-interaction=nonstopmode", '"%s"' % latexfile] - _log.debug('%s', command) - try: - report = subprocess.check_output(command, cwd=tmpdir, - stderr=subprocess.STDOUT) - except subprocess.CalledProcessError as exc: - raise RuntimeError( - ('LaTeX was not able to process the following ' - 'file:\n%s\n\n' - 'Here is the full report generated by LaTeX:\n%s ' - '\n\n' % (latexfile, - exc.output.decode("utf-8")))) - _log.debug(report) - - command = ['dvips', '-q', '-R0', '-o', os.path.basename(psfile), - os.path.basename(dvifile)] - _log.debug(command) - try: - report = subprocess.check_output(command, cwd=tmpdir, - stderr=subprocess.STDOUT) - except subprocess.CalledProcessError as exc: - raise RuntimeError( - ('dvips was not able to process the following ' - 'file:\n%s\n\n' - 'Here is the full report generated by dvips:\n%s ' - '\n\n' % (dvifile, - exc.output.decode("utf-8")))) - _log.debug(report) + + cbook._check_and_log_subprocess( + ["latex", "-interaction=nonstopmode", '"%s"' % latexfile], + _log, cwd=tmpdir) + cbook._check_and_log_subprocess( + ['dvips', '-q', '-R0', '-o', os.path.basename(psfile), + os.path.basename(dvifile)], _log, cwd=tmpdir) os.remove(epsfile) shutil.move(psfile, tmpfile) @@ -1493,18 +1472,11 @@ def gs_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False): else: device_name = "pswrite" - command = [str(gs_exe), "-dBATCH", "-dNOPAUSE", "-r%d" % dpi, - "-sDEVICE=%s" % device_name, paper_option, - "-sOutputFile=%s" % psfile, tmpfile] - _log.debug(command) - try: - report = subprocess.check_output(command, stderr=subprocess.STDOUT) - except subprocess.CalledProcessError as exc: - raise RuntimeError( - ('ghostscript was not able to process your image.\n' - 'Here is the full report generated by ghostscript:\n%s ' - '\n\n' % exc.output.decode("utf-8"))) - _log.debug(report) + cbook._check_and_log_subprocess( + [gs_exe, "-dBATCH", "-dNOPAUSE", "-r%d" % dpi, + "-sDEVICE=%s" % device_name, paper_option, + "-sOutputFile=%s" % psfile, tmpfile], _log) + os.remove(tmpfile) shutil.move(psfile, tmpfile) @@ -1535,35 +1507,18 @@ def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False): # Pass options as `-foo#bar` instead of `-foo=bar` to keep Windows happy # (https://www.ghostscript.com/doc/9.22/Use.htm#MS_Windows). - command = ["ps2pdf", - "-dAutoFilterColorImages#false", - "-dAutoFilterGrayImages#false", - "-dAutoRotatePages#false", - "-sGrayImageFilter#FlateEncode", - "-sColorImageFilter#FlateEncode", - "-dEPSCrop" if eps else "-sPAPERSIZE#%s" % ptype, - tmpfile, pdffile] - _log.debug(command) - - try: - report = subprocess.check_output(command, stderr=subprocess.STDOUT) - except subprocess.CalledProcessError as exc: - raise RuntimeError( - ('ps2pdf was not able to process your image.\n' - 'Here is the full report generated by ps2pdf:\n%s ' - '\n\n' % exc.output.decode("utf-8"))) - _log.debug(report) + cbook._check_and_log_subprocess( + ["ps2pdf", + "-dAutoFilterColorImages#false", + "-dAutoFilterGrayImages#false", + "-dAutoRotatePages#false", + "-sGrayImageFilter#FlateEncode", + "-sColorImageFilter#FlateEncode", + "-dEPSCrop" if eps else "-sPAPERSIZE#%s" % ptype, + tmpfile, pdffile], _log) + cbook._check_and_log_subprocess( + ["pdftops", "-paper", "match", "-level2", pdffile, psfile], _log) - command = ["pdftops", "-paper", "match", "-level2", pdffile, psfile] - _log.debug(command) - try: - report = subprocess.check_output(command, stderr=subprocess.STDOUT) - except subprocess.CalledProcessError as exc: - raise RuntimeError( - ('pdftops was not able to process your image.\n' - 'Here is the full report generated by pdftops:\n%s ' - '\n\n' % exc.output.decode("utf-8"))) - _log.debug(report) os.remove(tmpfile) shutil.move(psfile, tmpfile) diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index 2c5661253873..3afa4a524ac2 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -22,6 +22,7 @@ import os from pathlib import Path import re +import subprocess import sys import time import traceback @@ -2075,3 +2076,19 @@ def _unmultipled_rgba8888_to_premultiplied_argb32(rgba8888): if alpha8.min() != 0xff: np.multiply(rgb24, alpha8 / 0xff, out=rgb24, casting="unsafe") return argb32 + + +def _check_and_log_subprocess(command, logger, **kwargs): + logger.debug(command) + try: + report = subprocess.check_output( + command, stderr=subprocess.STDOUT, **kwargs) + except subprocess.CalledProcessError as exc: + raise RuntimeError( + 'The command\n' + ' {}\n' + 'failed and generated the following output:\n' + '{}' + .format(command, exc.output.decode('utf-8'))) + logger.debug(report) + return report