43
43
import glob
44
44
import os
45
45
import shutil
46
+ from matplotlib .compat .subprocess import subprocess
46
47
import sys
47
48
import warnings
48
49
@@ -398,30 +399,29 @@ def make_dvi(self, tex, fontsize):
398
399
if DEBUG or not os .path .exists (dvifile ):
399
400
texfile = self .make_tex (tex , fontsize )
400
401
outfile = basefile + '.output'
401
- command = self ._get_shell_cmd (
402
- 'cd "%s"' % self .texcache ,
403
- 'latex -interaction=nonstopmode %s > "%s"' %
404
- (os .path .split (texfile )[- 1 ], outfile ))
402
+ command = ["latex" , "-interaction=nonstopmode" ,
403
+ os .path .split (texfile )[- 1 ]]
405
404
mpl .verbose .report (command , 'debug' )
406
405
with Locked (self .texcache ):
407
- exit_status = os .system (command )
408
- try :
409
- with open (outfile ) as fh :
410
- report = fh .read ()
411
- except IOError :
412
- report = 'No latex error report available.'
413
- try :
414
- os .stat (dvifile )
415
- exists = True
416
- except OSError :
417
- exists = False
418
- if exit_status or not exists :
419
- raise RuntimeError (
420
- ('LaTeX was not able to process the following '
421
- 'string:\n %s\n Here is the full report generated by '
422
- 'LaTeX: \n \n ' % repr (tex .encode ('unicode_escape' )) +
423
- report ))
424
- else :
406
+ try :
407
+ with open (outfile , "w" ) as fout :
408
+ subprocess .check_call (command ,
409
+ cwd = self .texcache ,
410
+ stdout = fout ,
411
+ stderr = subprocess .STDOUT )
412
+ except subprocess .CalledProcessError as exc :
413
+ try :
414
+ with open (outfile ) as fh :
415
+ report = fh .read ()
416
+ except IOError :
417
+ report = 'No latex error report available.'
418
+ raise RuntimeError (
419
+ ('LaTeX was not able to process the following '
420
+ 'string:\n %s\n LaTeX failed with: %s\n '
421
+ 'Here is the full report generated by LaTeX: '
422
+ '\n \n ' % (exc .output ,
423
+ repr (tex .encode ('unicode_escape' ))) +
424
+ report ))
425
425
mpl .verbose .report (report , 'debug' )
426
426
for fname in glob .glob (basefile + '*' ):
427
427
if fname .endswith ('dvi' ):
@@ -452,25 +452,34 @@ def make_dvi_preview(self, tex, fontsize):
452
452
not os .path .exists (baselinefile )):
453
453
texfile = self .make_tex_preview (tex , fontsize )
454
454
outfile = basefile + '.output'
455
- command = self ._get_shell_cmd (
456
- 'cd "%s"' % self .texcache ,
457
- 'latex -interaction=nonstopmode %s > "%s"' %
458
- (os .path .split (texfile )[- 1 ], outfile ))
455
+ command = ["latex" , "-interaction=nonstopmode" ,
456
+ os .path .split (texfile )[- 1 ]]
459
457
mpl .verbose .report (command , 'debug' )
460
- exit_status = os .system (command )
458
+ try :
459
+ with open (outfile , "w" ) as fout :
460
+ subprocess .check_call (command ,
461
+ cwd = self .texcache ,
462
+ stdout = fout ,
463
+ stderr = subprocess .STDOUT )
464
+ except subprocess .CalledProcessError as exc :
465
+ try :
466
+ with open (outfile ) as fh :
467
+ report = fh .read ()
468
+ except IOError :
469
+ report = 'No latex error report available.'
470
+ raise RuntimeError (
471
+ ('LaTeX was not able to process the following '
472
+ 'string:\n %s\n LaTeX failed with: %s\n '
473
+ 'Here is the full report generated by LaTeX: '
474
+ '\n \n ' % (exc .output ,
475
+ repr (tex .encode ('unicode_escape' ))) +
476
+ report ))
461
477
try :
462
478
with open (outfile ) as fh :
463
479
report = fh .read ()
464
-
465
480
except IOError :
466
481
report = 'No latex error report available.'
467
- if exit_status :
468
- raise RuntimeError (
469
- ('LaTeX was not able to process the following '
470
- 'string:\n %s\n Here is the full report generated by '
471
- 'LaTeX: \n \n ' % repr (tex )) + report )
472
- else :
473
- mpl .verbose .report (report , 'debug' )
482
+ mpl .verbose .report (report , 'debug' )
474
483
475
484
# find the box extent information in the latex output
476
485
# file and store them in ".baseline" file
@@ -506,25 +515,35 @@ def make_png(self, tex, fontsize, dpi):
506
515
if DEBUG or not os .path .exists (pngfile ):
507
516
dvifile = self .make_dvi (tex , fontsize )
508
517
outfile = basefile + '.output'
509
- command = self ._get_shell_cmd (
510
- 'cd "%s"' % self .texcache ,
511
- 'dvipng -bg Transparent -D %s -T tight -o "%s" "%s" > "%s"' %
512
- (dpi , os .path .split (pngfile )[- 1 ],
513
- os .path .split (dvifile )[- 1 ], outfile ))
518
+ command = ["dvipng" , "-bg" , "Transparent" , "-D" , str (dpi ),
519
+ "-T" , "tight" , "-o" , os .path .split (pngfile )[- 1 ],
520
+ os .path .split (dvifile )[- 1 ]]
514
521
mpl .verbose .report (command , 'debug' )
515
- exit_status = os .system (command )
522
+ try :
523
+ with open (outfile , "w" ) as fout :
524
+ subprocess .check_call (command ,
525
+ cwd = self .texcache ,
526
+ stdout = fout ,
527
+ stderr = subprocess .STDOUT )
528
+ except subprocess .CalledProcessError as exc :
529
+ try :
530
+ with open (outfile ) as fh :
531
+ report = fh .read ()
532
+ except IOError :
533
+ report = 'No dvipng error report available.'
534
+ raise RuntimeError (
535
+ ('dvipng was not able to process the following '
536
+ 'string:\n %s\n dvipng failed with: %s\n '
537
+ 'Here is the full report generated by dvipng: '
538
+ '\n \n ' % (exc .output ,
539
+ repr (tex .encode ('unicode_escape' ))) +
540
+ report ))
516
541
try :
517
542
with open (outfile ) as fh :
518
543
report = fh .read ()
519
544
except IOError :
520
545
report = 'No dvipng error report available.'
521
- if exit_status :
522
- raise RuntimeError (
523
- 'dvipng was not able to process the following '
524
- 'file:\n %s\n Here is the full report generated by '
525
- 'dvipng: \n \n ' % dvifile + report )
526
- else :
527
- mpl .verbose .report (report , 'debug' )
546
+ mpl .verbose .report (report , 'debug' )
528
547
try :
529
548
os .remove (outfile )
530
549
except OSError :
@@ -544,21 +563,25 @@ def make_ps(self, tex, fontsize):
544
563
if DEBUG or not os .path .exists (psfile ):
545
564
dvifile = self .make_dvi (tex , fontsize )
546
565
outfile = basefile + '.output'
547
- command = self ._get_shell_cmd (
548
- 'cd "%s"' % self .texcache ,
549
- 'dvips -q -E -o "%s" "%s" > "%s"' %
550
- (os .path .split (psfile )[- 1 ],
551
- os .path .split (dvifile )[- 1 ], outfile ))
566
+ command = ["dvips" , "-q" , "-E" , "-o" , os .path .split (psfile )[- 1 ],
567
+ os .path .split (dvifile )[- 1 ]]
552
568
mpl .verbose .report (command , 'debug' )
553
- exit_status = os .system (command )
554
- with open (outfile ) as fh :
555
- if exit_status :
569
+ try :
570
+ with open (outfile , "w" ) as fout :
571
+ subprocess .check_call (command ,
572
+ cwd = self .texcache ,
573
+ stdout = fout ,
574
+ stderr = subprocess .STDOUT )
575
+ except subprocess .CalledProcessError as exc :
576
+ with open (outfile ) as fh :
556
577
raise RuntimeError (
557
- 'dvipng was not able to process the flowing '
558
- 'file:\n %s\n Here is the full report generated by '
559
- 'dvipng: \n \n ' % dvifile + fh .read ())
560
- else :
561
- mpl .verbose .report (fh .read (), 'debug' )
578
+ ('dvipng was not able to process the following '
579
+ 'file:\n %s\n dvipng failed with: %s\n '
580
+ 'Here is the full report generated by dvipng: '
581
+ '\n \n ' % (dvifile , exc .output ) +
582
+ fh .read ()))
583
+ with open (outfile ) as fh :
584
+ mpl .verbose .report (fh .read (), 'debug' )
562
585
os .remove (outfile )
563
586
564
587
return psfile
0 commit comments