Skip to content

Commit 10a981a

Browse files
authored
Merge pull request #11888 from anntzer/subprocess-helper
Factor out a subprocess log-and-check helper.
2 parents 753d330 + c954f34 commit 10a981a

File tree

3 files changed

+47
-92
lines changed

3 files changed

+47
-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"], _log, 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+
_log, 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: 23 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,34 +1421,13 @@ 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+
_log, cwd=tmpdir)
1428+
cbook._check_and_log_subprocess(
1429+
['dvips', '-q', '-R0', '-o', os.path.basename(psfile),
1430+
os.path.basename(dvifile)], _log, cwd=tmpdir)
14521431
os.remove(epsfile)
14531432
shutil.move(psfile, tmpfile)
14541433

@@ -1493,18 +1472,11 @@ def gs_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
14931472
else:
14941473
device_name = "pswrite"
14951474

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)
1475+
cbook._check_and_log_subprocess(
1476+
[gs_exe, "-dBATCH", "-dNOPAUSE", "-r%d" % dpi,
1477+
"-sDEVICE=%s" % device_name, paper_option,
1478+
"-sOutputFile=%s" % psfile, tmpfile], _log)
1479+
15081480
os.remove(tmpfile)
15091481
shutil.move(psfile, tmpfile)
15101482

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

15361508
# Pass options as `-foo#bar` instead of `-foo=bar` to keep Windows happy
15371509
# (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)
1510+
cbook._check_and_log_subprocess(
1511+
["ps2pdf",
1512+
"-dAutoFilterColorImages#false",
1513+
"-dAutoFilterGrayImages#false",
1514+
"-dAutoRotatePages#false",
1515+
"-sGrayImageFilter#FlateEncode",
1516+
"-sColorImageFilter#FlateEncode",
1517+
"-dEPSCrop" if eps else "-sPAPERSIZE#%s" % ptype,
1518+
tmpfile, pdffile], _log)
1519+
cbook._check_and_log_subprocess(
1520+
["pdftops", "-paper", "match", "-level2", pdffile, psfile], _log)
15561521

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)
15671522
os.remove(tmpfile)
15681523
shutil.move(psfile, tmpfile)
15691524

lib/matplotlib/cbook/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import os
2323
from pathlib import Path
2424
import re
25+
import subprocess
2526
import sys
2627
import time
2728
import traceback
@@ -2075,3 +2076,19 @@ def _unmultipled_rgba8888_to_premultiplied_argb32(rgba8888):
20752076
if alpha8.min() != 0xff:
20762077
np.multiply(rgb24, alpha8 / 0xff, out=rgb24, casting="unsafe")
20772078
return argb32
2079+
2080+
2081+
def _check_and_log_subprocess(command, logger, **kwargs):
2082+
logger.debug(command)
2083+
try:
2084+
report = subprocess.check_output(
2085+
command, stderr=subprocess.STDOUT, **kwargs)
2086+
except subprocess.CalledProcessError as exc:
2087+
raise RuntimeError(
2088+
'The command\n'
2089+
' {}\n'
2090+
'failed and generated the following output:\n'
2091+
'{}'
2092+
.format(command, exc.output.decode('utf-8')))
2093+
logger.debug(report)
2094+
return report

0 commit comments

Comments
 (0)