Skip to content

Commit 7e78b00

Browse files
committed
Factor out a subprocess log-and-check helper.
1 parent 9f74ea7 commit 7e78b00

File tree

3 files changed

+45
-92
lines changed

3 files changed

+45
-92
lines changed

lib/matplotlib/backends/backend_pgf.py

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -870,15 +870,9 @@ def _print_pdf_to_fh(self, fh, *args, **kwargs):
870870
pathlib.Path(fname_tex).write_text(latexcode, encoding="utf-8")
871871

872872
texcommand = rcParams["pgf.texsystem"]
873-
cmdargs = [texcommand, "-interaction=nonstopmode",
874-
"-halt-on-error", "figure.tex"]
875-
try:
876-
subprocess.check_output(
877-
cmdargs, stderr=subprocess.STDOUT, cwd=tmpdir)
878-
except subprocess.CalledProcessError as e:
879-
raise RuntimeError(
880-
"%s was not able to process your file.\n\nFull log:\n%s"
881-
% (texcommand, e.output))
873+
cbook._check_and_log_subprocess(
874+
[texcommand, "-interaction=nonstopmode", "-halt-on-error",
875+
"figure.tex"], cwd=tmpdir)
882876

883877
# copy file contents to target
884878
with open(fname_pdf, "rb") as fh_src:
@@ -1101,21 +1095,10 @@ def close(self):
11011095

11021096
def _run_latex(self):
11031097
texcommand = rcParams["pgf.texsystem"]
1104-
cmdargs = [
1105-
texcommand,
1106-
"-interaction=nonstopmode",
1107-
"-halt-on-error",
1108-
os.path.basename(self._fname_tex),
1109-
]
1110-
try:
1111-
subprocess.check_output(
1112-
cmdargs, stderr=subprocess.STDOUT, cwd=self._tmpdir
1113-
)
1114-
except subprocess.CalledProcessError as e:
1115-
raise RuntimeError(
1116-
"%s was not able to process your file.\n\nFull log:\n%s"
1117-
% (texcommand, e.output.decode('utf-8')))
1118-
1098+
cbook._check_and_log_subprocess(
1099+
[texcommand, "-interaction=nonstopmode", "-halt-on-error",
1100+
os.path.basename(self._fname_tex)],
1101+
cwd=self._tmpdir)
11191102
# copy file contents to target
11201103
shutil.copyfile(self._fname_pdf, self._outputfile)
11211104

lib/matplotlib/backends/backend_ps.py

Lines changed: 22 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,34 +1421,12 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
14211421
latexfile = latexfile.replace("\\", "/")
14221422
# Replace ~ so Latex does not think it is line break
14231423
latexfile = latexfile.replace("~", "\\string~")
1424-
command = ["latex", "-interaction=nonstopmode", '"%s"' % latexfile]
1425-
_log.debug('%s', command)
1426-
try:
1427-
report = subprocess.check_output(command, cwd=tmpdir,
1428-
stderr=subprocess.STDOUT)
1429-
except subprocess.CalledProcessError as exc:
1430-
raise RuntimeError(
1431-
('LaTeX was not able to process the following '
1432-
'file:\n%s\n\n'
1433-
'Here is the full report generated by LaTeX:\n%s '
1434-
'\n\n' % (latexfile,
1435-
exc.output.decode("utf-8"))))
1436-
_log.debug(report)
1437-
1438-
command = ['dvips', '-q', '-R0', '-o', os.path.basename(psfile),
1439-
os.path.basename(dvifile)]
1440-
_log.debug(command)
1441-
try:
1442-
report = subprocess.check_output(command, cwd=tmpdir,
1443-
stderr=subprocess.STDOUT)
1444-
except subprocess.CalledProcessError as exc:
1445-
raise RuntimeError(
1446-
('dvips was not able to process the following '
1447-
'file:\n%s\n\n'
1448-
'Here is the full report generated by dvips:\n%s '
1449-
'\n\n' % (dvifile,
1450-
exc.output.decode("utf-8"))))
1451-
_log.debug(report)
1424+
1425+
cbook._check_and_log_subprocess(
1426+
["latex", "-interaction=nonstopmode", '"%s"' % latexfile])
1427+
cbook._check_and_log_subprocess(
1428+
['dvips', '-q', '-R0', '-o', os.path.basename(psfile),
1429+
os.path.basename(dvifile)], cwd=tmpdir)
14521430
os.remove(epsfile)
14531431
shutil.move(psfile, tmpfile)
14541432

@@ -1493,18 +1471,11 @@ def gs_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
14931471
else:
14941472
device_name = "pswrite"
14951473

1496-
command = [str(gs_exe), "-dBATCH", "-dNOPAUSE", "-r%d" % dpi,
1497-
"-sDEVICE=%s" % device_name, paper_option,
1498-
"-sOutputFile=%s" % psfile, tmpfile]
1499-
_log.debug(command)
1500-
try:
1501-
report = subprocess.check_output(command, stderr=subprocess.STDOUT)
1502-
except subprocess.CalledProcessError as exc:
1503-
raise RuntimeError(
1504-
('ghostscript was not able to process your image.\n'
1505-
'Here is the full report generated by ghostscript:\n%s '
1506-
'\n\n' % exc.output.decode("utf-8")))
1507-
_log.debug(report)
1474+
cbook._check_and_log_subprocess(
1475+
[gs_exe, "-dBATCH", "-dNOPAUSE", "-r%d" % dpi,
1476+
"-sDEVICE=%s" % device_name, paper_option,
1477+
"-sOutputFile=%s" % psfile, tmpfile])
1478+
15081479
os.remove(tmpfile)
15091480
shutil.move(psfile, tmpfile)
15101481

@@ -1535,35 +1506,18 @@ def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
15351506

15361507
# Pass options as `-foo#bar` instead of `-foo=bar` to keep Windows happy
15371508
# (https://www.ghostscript.com/doc/9.22/Use.htm#MS_Windows).
1538-
command = ["ps2pdf",
1539-
"-dAutoFilterColorImages#false",
1540-
"-dAutoFilterGrayImages#false",
1541-
"-dAutoRotatePages#false",
1542-
"-sGrayImageFilter#FlateEncode",
1543-
"-sColorImageFilter#FlateEncode",
1544-
"-dEPSCrop" if eps else "-sPAPERSIZE#%s" % ptype,
1545-
tmpfile, pdffile]
1546-
_log.debug(command)
1547-
1548-
try:
1549-
report = subprocess.check_output(command, stderr=subprocess.STDOUT)
1550-
except subprocess.CalledProcessError as exc:
1551-
raise RuntimeError(
1552-
('ps2pdf was not able to process your image.\n'
1553-
'Here is the full report generated by ps2pdf:\n%s '
1554-
'\n\n' % exc.output.decode("utf-8")))
1555-
_log.debug(report)
1509+
cbook._check_and_log_subprocess(
1510+
["ps2pdf",
1511+
"-dAutoFilterColorImages#false",
1512+
"-dAutoFilterGrayImages#false",
1513+
"-dAutoRotatePages#false",
1514+
"-sGrayImageFilter#FlateEncode",
1515+
"-sColorImageFilter#FlateEncode",
1516+
"-dEPSCrop" if eps else "-sPAPERSIZE#%s" % ptype,
1517+
tmpfile, pdffile])
1518+
cbook._check_and_log_subprocess(
1519+
["pdftops", "-paper", "match", "-level2", pdffile, psfile])
15561520

1557-
command = ["pdftops", "-paper", "match", "-level2", pdffile, psfile]
1558-
_log.debug(command)
1559-
try:
1560-
report = subprocess.check_output(command, stderr=subprocess.STDOUT)
1561-
except subprocess.CalledProcessError as exc:
1562-
raise RuntimeError(
1563-
('pdftops was not able to process your image.\n'
1564-
'Here is the full report generated by pdftops:\n%s '
1565-
'\n\n' % exc.output.decode("utf-8")))
1566-
_log.debug(report)
15671521
os.remove(tmpfile)
15681522
shutil.move(psfile, tmpfile)
15691523

lib/matplotlib/cbook/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,3 +2075,19 @@ def _unmultipled_rgba8888_to_premultiplied_argb32(rgba8888):
20752075
if alpha8.min() != 0xff:
20762076
np.multiply(rgb24, alpha8 / 0xff, out=rgb24, casting="unsafe")
20772077
return argb32
2078+
2079+
2080+
def _check_and_log_subprocess(self, command, **kwargs):
2081+
_log.debug(command)
2082+
try:
2083+
report = subprocess.check_output(
2084+
command, stderr=subprocess.STDOUT, **kwargs)
2085+
except subprocess.CalledProcessError as exc:
2086+
raise RuntimeError(
2087+
'The command\n'
2088+
' {}\n'
2089+
'failed and generated the following output:\n'
2090+
'{}'
2091+
.format(command, exc.output.decode('utf-8')))
2092+
_log.debug(report)
2093+
return report

0 commit comments

Comments
 (0)