Skip to content

Factor out a subprocess log-and-check helper. #11888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 7 additions & 24 deletions lib/matplotlib/backends/backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down
91 changes: 23 additions & 68 deletions lib/matplotlib/backends/backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
17 changes: 17 additions & 0 deletions lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import os
from pathlib import Path
import re
import subprocess
import sys
import time
import traceback
Expand Down Expand Up @@ -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