@@ -23,6 +23,7 @@ def _fn_name(): return sys._getframe(1).f_code.co_name
23
23
24
24
from matplotlib .cbook import is_string_like , get_realpath_and_stat , \
25
25
is_writable_file_like , maxdict , file_requires_unicode
26
+ from matplotlib .compat .subprocess import subprocess
26
27
from matplotlib .figure import Figure
27
28
28
29
from matplotlib .font_manager import findfont , is_opentype_cff_font , get_font
@@ -83,8 +84,7 @@ def gs_version(self):
83
84
pass
84
85
85
86
from matplotlib .compat .subprocess import Popen , PIPE
86
- s = Popen (self .gs_exe + " --version" ,
87
- shell = True , stdout = PIPE )
87
+ s = Popen ([self .gs_exe , "--version" ], stdout = PIPE )
88
88
pipe , stderr = s .communicate ()
89
89
if six .PY3 :
90
90
ver = pipe .decode ('ascii' )
@@ -1469,27 +1469,32 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
1469
1469
command = '%s cd "%s" && latex -interaction=nonstopmode "%s" > "%s"' \
1470
1470
% (precmd , tmpdir , latexfile , outfile )
1471
1471
verbose .report (command , 'debug' )
1472
- exit_status = os .system (command )
1473
-
1474
- with io .open (outfile , 'rb' ) as fh :
1475
- if exit_status :
1476
- raise RuntimeError ('LaTeX was not able to process your file:\
1477
- \n Here is the full report generated by LaTeX: \n \n %s'% fh .read ())
1478
- else :
1472
+ try :
1473
+ output = subprocess .check_output (command , shell = True ,
1474
+ stderr = subprocess .STDOUT )
1475
+ except subprocess .CalledProcessError as exc :
1476
+ with io .open (outfile , 'rb' ) as fh :
1477
+ raise RuntimeError ('LaTeX was not able to process your file: '
1478
+ 'Here is the full report generated by LaTeX'
1479
+ '\n \n %s' % (fh .read ()))
1480
+ else :
1481
+ with io .open (outfile , 'rb' ) as fh :
1479
1482
verbose .report (fh .read (), 'debug' )
1480
1483
os .remove (outfile )
1481
1484
1482
1485
command = '%s cd "%s" && dvips -q -R0 -o "%s" "%s" > "%s"' % (precmd , tmpdir ,
1483
1486
os .path .split (psfile )[- 1 ], os .path .split (dvifile )[- 1 ], outfile )
1484
1487
verbose .report (command , 'debug' )
1485
- exit_status = os .system (command )
1486
-
1487
- with io .open (outfile , 'rb' ) as fh :
1488
- if exit_status :
1489
- raise RuntimeError ('dvips was not able to \
1490
- process the following file:\n %s\n Here is the full report generated by dvips: \
1491
- \n \n '% dvifile + fh .read ())
1492
- else :
1488
+ try :
1489
+ output = subprocess .check_output (command , shell = True ,
1490
+ stderr = subprocess .STDOUT )
1491
+ except subprocess .CalledProcessError as exc :
1492
+ with io .open (outfile , 'rb' ) as fh :
1493
+ raise RuntimeError ('dvips was not able to process the following '
1494
+ 'file:\n %s\n Here is the full report generated '
1495
+ 'by dvips: \n \n ' % dvifile + fh .read ())
1496
+ else :
1497
+ with io .open (outfile , 'rb' ) as fh :
1493
1498
verbose .report (fh .read (), 'debug' )
1494
1499
os .remove (outfile )
1495
1500
os .remove (epsfile )
@@ -1528,30 +1533,27 @@ def gs_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
1528
1533
outfile = tmpfile + '.output'
1529
1534
dpi = rcParams ['ps.distiller.res' ]
1530
1535
1531
-
1532
1536
gs_exe = ps_backend_helper .gs_exe
1533
1537
if ps_backend_helper .supports_ps2write : # gs version >= 9
1534
1538
device_name = "ps2write"
1535
1539
else :
1536
1540
device_name = "pswrite"
1537
1541
1538
- command = '%s -dBATCH -dNOPAUSE -r%d -sDEVICE=%s %s -sOutputFile="%s" \
1539
- "%s" > "%s"'% (gs_exe , dpi , device_name ,
1540
- paper_option , psfile , tmpfile , outfile )
1541
-
1542
+ command = [gs_exe , "-dBATCH" , "-dNOPAUSE" , "-r%d" % dpi ,
1543
+ "-sDEVICE=%s" % device_name , paper_option ,
1544
+ "-sOutputFile=%s" % psfile , tmpfile ]
1542
1545
verbose .report (command , 'debug' )
1543
- exit_status = os .system (command )
1544
-
1545
- with io .open (outfile , 'rb' ) as fh :
1546
- if exit_status :
1547
- output = fh .read ()
1548
- m = "\n " .join (["ghostscript was not able to process your image." ,
1549
- "Here is the full report generated by ghostscript:" ,
1550
- "" ,
1551
- "%s" ])
1552
- # use % to prevent problems with bytes
1553
- raise RuntimeError (m % output )
1554
- else :
1546
+ try :
1547
+ with open (outfile , "w" ) as fout :
1548
+ subprocess .check_call (command ,
1549
+ stdout = fout , stderr = subprocess .STDOUT )
1550
+ except subprocess .CalledProcessError as exc :
1551
+ with io .open (outfile , 'rb' ) as fh :
1552
+ raise RuntimeError ('ghostscript was not able to process your '
1553
+ 'image.\n Here is the full report generated by '
1554
+ 'ghostscript: %s\n \n ' % fh .read ())
1555
+ else :
1556
+ with io .open (outfile , 'rb' ) as fh :
1555
1557
verbose .report (fh .read (), 'debug' )
1556
1558
os .remove (outfile )
1557
1559
os .remove (tmpfile )
@@ -1584,33 +1586,55 @@ def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
1584
1586
psfile = tmpfile + '.ps'
1585
1587
outfile = tmpfile + '.output'
1586
1588
1587
- if eps : paper_option = "-dEPSCrop"
1588
- else : paper_option = "-sPAPERSIZE=%s" % ptype
1589
-
1590
- command = 'ps2pdf -dAutoFilterColorImages=false \
1591
- -dAutoFilterGrayImages=false -sGrayImageFilter=FlateEncode \
1592
- -sColorImageFilter=FlateEncode %s "%s" "%s" > "%s"'% \
1593
- (paper_option , tmpfile , pdffile , outfile )
1594
- if sys .platform == 'win32' : command = command .replace ('=' , '#' )
1595
- verbose .report (command , 'debug' )
1596
- exit_status = os .system (command )
1597
- with io .open (outfile , 'rb' ) as fh :
1598
- if exit_status :
1599
- raise RuntimeError ('ps2pdf was not able to process your \
1600
- image.\n Here is the report generated by ghostscript:\n \n ' + fh .read ())
1589
+ if eps :
1590
+ paper_option = "-dEPSCrop"
1591
+ else :
1592
+ if sys .platform == "win32" :
1593
+ paper_option = "-sPAPERSIZE#%s" % ptype
1601
1594
else :
1595
+ paper_option = "-sPAPERSIZE=%s" % ptype
1596
+
1597
+ if sys .platform == "win32" :
1598
+ command = ["ps2pdf" , "-dAutoFilterColorImages#false" ,
1599
+ "-dAutoFilterGrayImages#false" ,
1600
+ "-sGrayImageFilter#FlateEncode" ,
1601
+ "-sColorImageFilter#FlateEncode" , paper_option , tmpfile ,
1602
+ pdffile ]
1603
+ else :
1604
+ command = ["ps2pdf" , "-dAutoFilterColorImages=false" ,
1605
+ "-dAutoFilterGrayImages=false" ,
1606
+ "-sGrayImageFilter=FlateEncode" ,
1607
+ "-sColorImageFilter=FlateEncode" , paper_option , tmpfile ,
1608
+ pdffile ]
1609
+ verbose .report (command , 'debug' )
1610
+
1611
+ try :
1612
+ with open (outfile , "w" ) as fout :
1613
+ subprocess .check_call (command ,
1614
+ stdout = fout , stderr = subprocess .STDOUT )
1615
+ except subprocess .CalledProcessError as exc :
1616
+ with io .open (outfile , 'rb' ) as fh :
1617
+ raise RuntimeError ('ps2pdf was not able to process your image.\n '
1618
+ 'Here is the report generated by ps2pdf: '
1619
+ '%s\n \n ' % fh .read ())
1620
+ else :
1621
+ with io .open (outfile , 'rb' ) as fh :
1602
1622
verbose .report (fh .read (), 'debug' )
1603
1623
os .remove (outfile )
1604
- command = 'pdftops -paper match -level2 "%s" "%s" > "%s"' % \
1605
- (pdffile , psfile , outfile )
1606
- verbose .report (command , 'debug' )
1607
- exit_status = os .system (command )
1608
1624
1609
- with io .open (outfile , 'rb' ) as fh :
1610
- if exit_status :
1611
- raise RuntimeError ('pdftops was not able to process your \
1612
- image.\n Here is the full report generated by pdftops: \n \n ' + fh .read ())
1613
- else :
1625
+ command = ["pdftops" , "-paper" , "match" , "-level2" , pdffile , psfile ]
1626
+ verbose .report (command , 'debug' )
1627
+ try :
1628
+ with open (outfile , "w" ) as fout :
1629
+ subprocess .check_call (command ,
1630
+ stdout = fout , stderr = subprocess .STDOUT )
1631
+ except subprocess .CalledProcessError as exc :
1632
+ with io .open (outfile , 'rb' ) as fh :
1633
+ raise RuntimeError ('pdftops was not able to process your image.\n '
1634
+ 'Here is the full report generated by pdftops: '
1635
+ '%s\n \n ' % fh .read ())
1636
+ else :
1637
+ with io .open (outfile , 'rb' ) as fh :
1614
1638
verbose .report (fh .read (), 'debug' )
1615
1639
os .remove (outfile )
1616
1640
os .remove (tmpfile )
0 commit comments