diff --git a/doc/make.py b/doc/make.py index dd1a4878c3b3..378cbb6973b3 100755 --- a/doc/make.py +++ b/doc/make.py @@ -172,13 +172,9 @@ def latex(): raise SystemExit("Building LaTeX failed.") # Produce pdf. - os.chdir('build/latex') - # Call the makefile produced by sphinx... - if os.system('make'): - raise SystemExit("Rendering LaTeX failed.") - - os.chdir('../..') + if subprocess.call("make", cwd="build/latex"): + raise SystemExit("Rendering LaTeX failed with.") else: print('latex build has not been tested on windows') @@ -195,13 +191,9 @@ def texinfo(): raise SystemExit("Building Texinfo failed.") # Produce info file. - os.chdir('build/texinfo') - # Call the makefile produced by sphinx... - if os.system('make'): - raise SystemExit("Rendering Texinfo failed.") - - os.chdir('../..') + if subprocess.call("make", cwd="build/texinfo"): + raise SystemExit("Rendering Texinfo failed with.") else: print('texinfo build has not been tested on windows') diff --git a/examples/pylab_examples/mathtext_examples.py b/examples/pylab_examples/mathtext_examples.py index bee528de214d..eac9670bcdc5 100644 --- a/examples/pylab_examples/mathtext_examples.py +++ b/examples/pylab_examples/mathtext_examples.py @@ -3,7 +3,7 @@ """ from __future__ import print_function import matplotlib.pyplot as plt -import os +import subprocess import sys import re import gc @@ -120,6 +120,6 @@ def doall(): fd.write("\\end{document}\n") fd.close() - os.system("pdflatex mathtext_examples.ltx") + subprocess.call(["pdflatex", "mathtext_examples.ltx"]) else: doall() diff --git a/examples/pylab_examples/movie_demo.py b/examples/pylab_examples/movie_demo.py index 0d8e61338257..7e9368f459f0 100644 --- a/examples/pylab_examples/movie_demo.py +++ b/examples/pylab_examples/movie_demo.py @@ -2,7 +2,7 @@ from __future__ import print_function -import os +import subprocess import matplotlib.pyplot as plt import numpy as np @@ -22,8 +22,8 @@ files.append(fname) print('Making movie animation.mpg - this may take a while') -os.system("mencoder 'mf://_tmp*.png' -mf type=png:fps=10 -ovc lavc -lavcopts vcodec=wmv2 -oac copy -o animation.mpg") -#os.system("convert _tmp*.png animation.mng") +subprocess.call("mencoder 'mf://_tmp*.png' -mf type=png:fps=10 -ovc lavc " + "-lavcopts vcodec=wmv2 -oac copy -o animation.mpg", shell=True) # cleanup for fname in files: diff --git a/examples/pylab_examples/stix_fonts_demo.py b/examples/pylab_examples/stix_fonts_demo.py index 81bd73575cdc..bbc6828339d0 100644 --- a/examples/pylab_examples/stix_fonts_demo.py +++ b/examples/pylab_examples/stix_fonts_demo.py @@ -1,6 +1,6 @@ from __future__ import unicode_literals -import os +import subprocess import sys import re import gc @@ -53,6 +53,6 @@ def doall(): fd.write("\\end{document}\n") fd.close() - os.system("pdflatex stix_fonts_examples.ltx") + subprocess.call(["pdflatex", "stix_fonts_examples.ltx"]) else: doall() diff --git a/examples/tests/backend_driver.py b/examples/tests/backend_driver.py index e084e032f9d8..83b768644071 100644 --- a/examples/tests/backend_driver.py +++ b/examples/tests/backend_driver.py @@ -337,19 +337,16 @@ def report_all_missing(directories): ) -try: - import subprocess - - def run(arglist): - try: - ret = subprocess.call(arglist) - except KeyboardInterrupt: - sys.exit() - else: - return ret -except ImportError: - def run(arglist): - os.system(' '.join(arglist)) +from matplotlib.compat import subprocess + + +def run(arglist): + try: + ret = subprocess.call(arglist) + except KeyboardInterrupt: + sys.exit() + else: + return ret def drive(backend, directories, python=['python'], switches=[]): @@ -420,7 +417,7 @@ def drive(backend, directories, python=['python'], switches=[]): ret = run(program + [tmpfile_name] + switches) end_time = time.time() print("%s %s" % ((end_time - start_time), ret)) - #os.system('%s %s %s' % (python, tmpfile_name, ' '.join(switches))) + # subprocess.call([python, tmpfile_name] + switches) os.remove(tmpfile_name) if ret: failures.append(fullpath) diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index 167eff74167d..72d9d6f0f39f 100644 --- a/lib/matplotlib/backends/backend_ps.py +++ b/lib/matplotlib/backends/backend_ps.py @@ -8,21 +8,19 @@ import six from six.moves import StringIO -import glob, math, os, shutil, sys, time, datetime +import glob, os, shutil, sys, time, datetime def _fn_name(): return sys._getframe(1).f_code.co_name import io -from hashlib import md5 - from tempfile import mkstemp from matplotlib import verbose, __version__, rcParams, checkdep_ghostscript -from matplotlib._pylab_helpers import Gcf from matplotlib.afm import AFM from matplotlib.backend_bases import RendererBase, GraphicsContextBase,\ FigureManagerBase, FigureCanvasBase from matplotlib.cbook import is_string_like, get_realpath_and_stat, \ is_writable_file_like, maxdict, file_requires_unicode +from matplotlib.compat.subprocess import subprocess from matplotlib.figure import Figure from matplotlib.font_manager import findfont, is_opentype_cff_font, get_font @@ -30,7 +28,6 @@ def _fn_name(): return sys._getframe(1).f_code.co_name from matplotlib.ttconv import convert_ttf_to_ps from matplotlib.mathtext import MathTextParser from matplotlib._mathtext_data import uni2type1 -from matplotlib.text import Text from matplotlib.path import Path from matplotlib import _path from matplotlib.transforms import Affine2D @@ -69,8 +66,8 @@ def gs_exe(self): if gs_exe is None: gs_exe = 'gs' - self._cached["gs_exe"] = gs_exe - return gs_exe + self._cached["gs_exe"] = str(gs_exe) + return str(gs_exe) @property def gs_version(self): @@ -83,8 +80,7 @@ def gs_version(self): pass from matplotlib.compat.subprocess import Popen, PIPE - s = Popen(self.gs_exe + " --version", - shell=True, stdout=PIPE) + s = Popen([self.gs_exe, "--version"], stdout=PIPE) pipe, stderr = s.communicate() if six.PY3: ver = pipe.decode('ascii') @@ -474,8 +470,6 @@ def draw_image(self, gc, x, y, im, transform=None): self._pswriter.write(ps) def _convert_path(self, path, transform, clip=False, simplify=None): - ps = [] - last_points = None if clip: clip = (0.0, 0.0, self.width * 72.0, self.height * 72.0) @@ -516,8 +510,6 @@ def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None) """ if debugPS: self._pswriter.write('% draw_markers \n') - write = self._pswriter.write - if rgbFace: if rgbFace[0]==rgbFace[1] and rgbFace[0]==rgbFace[2]: ps_color = '%1.3f setgray' % rgbFace[0] @@ -885,7 +877,6 @@ def _draw_ps(self, ps, gc, rgbFace, fill=True, stroke=True, command=None): write("grestore\n") - class GraphicsContextPS(GraphicsContextBase): def get_capstyle(self): return {'butt':0, @@ -901,6 +892,7 @@ def shouldstroke(self): return (self.get_linewidth() > 0.0 and (len(self.get_rgb()) <= 3 or self.get_rgb()[3] != 0.0)) + def new_figure_manager(num, *args, **kwargs): FigureClass = kwargs.pop('FigureClass', Figure) thisFig = FigureClass(*args, **kwargs) @@ -915,6 +907,7 @@ def new_figure_manager_given_figure(num, figure): manager = FigureManagerPS(canvas, num) return manager + class FigureCanvasPS(FigureCanvasBase): _renderer_class = RendererPS @@ -1412,12 +1405,13 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble, epsfile = tmpfile+'.eps' shutil.move(tmpfile, epsfile) latexfile = tmpfile+'.tex' - outfile = tmpfile+'.output' dvifile = tmpfile+'.dvi' psfile = tmpfile+'.ps' - if orientation=='landscape': angle = 90 - else: angle = 0 + if orientation == 'landscape': + angle = 90 + else: + angle = 0 if rcParams['text.latex.unicode']: unicode_preamble = """\\usepackage{ucs} @@ -1458,40 +1452,39 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble, "rcParam.", 'helpful') raise - # the split drive part of the command is necessary for windows users with - # multiple - if sys.platform == 'win32': precmd = '%s &&'% os.path.splitdrive(tmpdir)[0] - else: precmd = '' - #Replace \\ for / so latex does not think there is a function call + # Replace \\ for / so latex does not think there is a function call latexfile = latexfile.replace("\\", "/") # Replace ~ so Latex does not think it is line break latexfile = latexfile.replace("~", "\\string~") - command = '%s cd "%s" && latex -interaction=nonstopmode "%s" > "%s"'\ - %(precmd, tmpdir, latexfile, outfile) + command = [str("latex"), "-interaction=nonstopmode", + '"%s"' % latexfile] verbose.report(command, 'debug') - exit_status = os.system(command) - - with io.open(outfile, 'rb') as fh: - if exit_status: - raise RuntimeError('LaTeX was not able to process your file:\ - \nHere is the full report generated by LaTeX: \n\n%s'% fh.read()) - else: - verbose.report(fh.read(), 'debug') - os.remove(outfile) - - command = '%s cd "%s" && dvips -q -R0 -o "%s" "%s" > "%s"'%(precmd, tmpdir, - os.path.split(psfile)[-1], os.path.split(dvifile)[-1], outfile) + 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")))) + verbose.report(report, 'debug') + + command = [str('dvips'), '-q', '-R0', '-o', os.path.basename(psfile), + os.path.basename(dvifile)] verbose.report(command, 'debug') - exit_status = os.system(command) - - with io.open(outfile, 'rb') as fh: - if exit_status: - raise RuntimeError('dvips was not able to \ - process the following file:\n%s\nHere is the full report generated by dvips: \ - \n\n'% dvifile + fh.read()) - else: - verbose.report(fh.read(), 'debug') - os.remove(outfile) + 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")))) + verbose.report(report, 'debug') os.remove(epsfile) shutil.move(psfile, tmpfile) @@ -1521,39 +1514,32 @@ def gs_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False): operators. The output is low-level, converting text to outlines. """ - if eps: paper_option = "-dEPSCrop" - else: paper_option = "-sPAPERSIZE=%s" % ptype + if eps: + paper_option = "-dEPSCrop" + else: + paper_option = "-sPAPERSIZE=%s" % ptype psfile = tmpfile + '.ps' - outfile = tmpfile + '.output' dpi = rcParams['ps.distiller.res'] - gs_exe = ps_backend_helper.gs_exe if ps_backend_helper.supports_ps2write: # gs version >= 9 device_name = "ps2write" else: device_name = "pswrite" - command = '%s -dBATCH -dNOPAUSE -r%d -sDEVICE=%s %s -sOutputFile="%s" \ - "%s" > "%s"'% (gs_exe, dpi, device_name, - paper_option, psfile, tmpfile, outfile) - + command = [str(gs_exe), "-dBATCH", "-dNOPAUSE", "-r%d" % dpi, + "-sDEVICE=%s" % device_name, paper_option, + "-sOutputFile=%s" % psfile, tmpfile] verbose.report(command, 'debug') - exit_status = os.system(command) - - with io.open(outfile, 'rb') as fh: - if exit_status: - output = fh.read() - m = "\n".join(["ghostscript was not able to process your image.", - "Here is the full report generated by ghostscript:", - "", - "%s"]) - # use % to prevent problems with bytes - raise RuntimeError(m % output) - else: - verbose.report(fh.read(), 'debug') - os.remove(outfile) + 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"))) + verbose.report(report, 'debug') os.remove(tmpfile) shutil.move(psfile, tmpfile) @@ -1582,37 +1568,48 @@ def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False): """ pdffile = tmpfile + '.pdf' psfile = tmpfile + '.ps' - outfile = tmpfile + '.output' - if eps: paper_option = "-dEPSCrop" - else: paper_option = "-sPAPERSIZE=%s" % ptype - - command = 'ps2pdf -dAutoFilterColorImages=false \ --dAutoFilterGrayImages=false -sGrayImageFilter=FlateEncode \ --sColorImageFilter=FlateEncode %s "%s" "%s" > "%s"'% \ -(paper_option, tmpfile, pdffile, outfile) - if sys.platform == 'win32': command = command.replace('=', '#') - verbose.report(command, 'debug') - exit_status = os.system(command) - with io.open(outfile, 'rb') as fh: - if exit_status: - raise RuntimeError('ps2pdf was not able to process your \ -image.\nHere is the report generated by ghostscript:\n\n' + fh.read()) + if eps: + paper_option = "-dEPSCrop" + else: + if sys.platform == "win32": + paper_option = "-sPAPERSIZE#%s" % ptype else: - verbose.report(fh.read(), 'debug') - os.remove(outfile) - command = 'pdftops -paper match -level2 "%s" "%s" > "%s"'% \ - (pdffile, psfile, outfile) + paper_option = "-sPAPERSIZE=%s" % ptype + + if sys.platform == "win32": + command = [str("ps2pdf"), "-dAutoFilterColorImages#false", + "-dAutoFilterGrayImages#false", + "-sGrayImageFilter#FlateEncode", + "-sColorImageFilter#FlateEncode", paper_option, tmpfile, + pdffile] + else: + command = [str("ps2pdf"), "-dAutoFilterColorImages=false", + "-dAutoFilterGrayImages=false", + "-sGrayImageFilter=FlateEncode", + "-sColorImageFilter=FlateEncode", paper_option, tmpfile, + pdffile] verbose.report(command, 'debug') - exit_status = os.system(command) - with io.open(outfile, 'rb') as fh: - if exit_status: - raise RuntimeError('pdftops was not able to process your \ -image.\nHere is the full report generated by pdftops: \n\n' + fh.read()) - else: - verbose.report(fh.read(), 'debug') - os.remove(outfile) + 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"))) + verbose.report(report, 'debug') + + command = [str("pdftops"), "-paper", "match", "-level2", pdffile, psfile] + verbose.report(command, 'debug') + 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"))) + verbose.report(report, 'debug') os.remove(tmpfile) shutil.move(psfile, tmpfile) @@ -1622,6 +1619,7 @@ def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False): for fname in glob.glob(tmpfile+'.*'): os.remove(fname) + def get_bbox_header(lbrt, rotated=False): """ return a postscript header stringfor the given bbox lbrt=(l, b, r, t). @@ -1629,7 +1627,7 @@ def get_bbox_header(lbrt, rotated=False): """ l, b, r, t = lbrt - if rotated: + if rotated: rotate = "%.2f %.2f translate\n90 rotate" % (l+r, 0) else: rotate = "" @@ -1643,16 +1641,18 @@ def get_bbox_header(lbrt, rotated=False): # find the bounding box, as the required bounding box is alread known. def get_bbox(tmpfile, bbox): """ - Use ghostscript's bbox device to find the center of the bounding box. Return - an appropriately sized bbox centered around that point. A bit of a hack. + Use ghostscript's bbox device to find the center of the bounding box. + Return an appropriately sized bbox centered around that point. A bit of a + hack. """ - outfile = tmpfile + '.output' gs_exe = ps_backend_helper.gs_exe - command = '%s -dBATCH -dNOPAUSE -sDEVICE=bbox "%s"' %\ - (gs_exe, tmpfile) + command = [gs_exe, "-dBATCH", "-dNOPAUSE", "-sDEVICE=bbox" "%s" % tmpfile] verbose.report(command, 'debug') - stdin, stdout, stderr = os.popen3(command) + p = subprocess.Popen(command, stdin=subprocess.PIPE, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + close_fds=True) + (stdout, stderr) = (p.stdout, p.stderr) verbose.report(stdout.read(), 'debug-annoying') bbox_info = stderr.read() verbose.report(bbox_info, 'helpful') @@ -1661,7 +1661,7 @@ def get_bbox(tmpfile, bbox): bbox_info = bbox_found.group() else: raise RuntimeError('Ghostscript was not able to extract a bounding box.\ -Here is the Ghostscript output:\n\n%s'% bbox_info) +Here is the Ghostscript output:\n\n%s' % bbox_info) l, b, r, t = [float(i) for i in bbox_info.split()[-4:]] # this is a hack to deal with the fact that ghostscript does not return the diff --git a/lib/matplotlib/texmanager.py b/lib/matplotlib/texmanager.py index 5ad793057468..8eae5fcc51a6 100644 --- a/lib/matplotlib/texmanager.py +++ b/lib/matplotlib/texmanager.py @@ -54,7 +54,7 @@ from matplotlib import rcParams from matplotlib._png import read_png from matplotlib.cbook import mkdirs, Locked -from matplotlib.compat.subprocess import Popen, PIPE, STDOUT +from matplotlib.compat.subprocess import subprocess, Popen, PIPE, STDOUT import matplotlib.dviread as dviread import re @@ -263,18 +263,6 @@ def get_custom_preamble(self): """returns a string containing user additions to the tex preamble""" return '\n'.join(rcParams['text.latex.preamble']) - def _get_shell_cmd(self, *args): - """ - On windows, changing directories can be complicated by the presence of - multiple drives. get_shell_cmd deals with this issue. - """ - if sys.platform == 'win32': - command = ['%s' % os.path.splitdrive(self.texcache)[0]] - else: - command = [] - command.extend(args) - return ' && '.join(command) - def make_tex(self, tex, fontsize): """ Generate a tex file to render the tex string at a specific font size @@ -327,7 +315,7 @@ def make_tex(self, tex, fontsize): def make_tex_preview(self, tex, fontsize): """ Generate a tex file to render the tex string at a specific - font size. It uses the preview.sty to determin the dimension + font size. It uses the preview.sty to determine the dimension (width, height, descent) of the output. returns the file name @@ -397,31 +385,21 @@ def make_dvi(self, tex, fontsize): if DEBUG or not os.path.exists(dvifile): texfile = self.make_tex(tex, fontsize) - outfile = basefile + '.output' - command = self._get_shell_cmd( - 'cd "%s"' % self.texcache, - 'latex -interaction=nonstopmode %s > "%s"' % - (os.path.split(texfile)[-1], outfile)) + command = [str("latex"), "-interaction=nonstopmode", + os.path.basename(texfile)] mpl.verbose.report(command, 'debug') with Locked(self.texcache): - exit_status = os.system(command) - try: - with open(outfile) as fh: - report = fh.read() - except IOError: - report = 'No latex error report available.' - try: - os.stat(dvifile) - exists = True - except OSError: - exists = False - if exit_status or not exists: - raise RuntimeError( - ('LaTeX was not able to process the following ' - 'string:\n%s\nHere is the full report generated by ' - 'LaTeX: \n\n' % repr(tex.encode('unicode_escape')) + - report)) - else: + try: + report = subprocess.check_output(command, + cwd=self.texcache, + stderr=subprocess.STDOUT) + except subprocess.CalledProcessError as exc: + raise RuntimeError( + ('LaTeX was not able to process the following ' + 'string:\n%s\n\n' + 'Here is the full report generated by LaTeX:\n%s ' + '\n\n' % (repr(tex.encode('unicode_escape')), + exc.output.decode("utf-8")))) mpl.verbose.report(report, 'debug') for fname in glob.glob(basefile + '*'): if fname.endswith('dvi'): @@ -451,30 +429,25 @@ def make_dvi_preview(self, tex, fontsize): if (DEBUG or not os.path.exists(dvifile) or not os.path.exists(baselinefile)): texfile = self.make_tex_preview(tex, fontsize) - outfile = basefile + '.output' - command = self._get_shell_cmd( - 'cd "%s"' % self.texcache, - 'latex -interaction=nonstopmode %s > "%s"' % - (os.path.split(texfile)[-1], outfile)) + command = [str("latex"), "-interaction=nonstopmode", + os.path.basename(texfile)] mpl.verbose.report(command, 'debug') - exit_status = os.system(command) try: - with open(outfile) as fh: - report = fh.read() - - except IOError: - report = 'No latex error report available.' - if exit_status: + report = subprocess.check_output(command, + cwd=self.texcache, + stderr=subprocess.STDOUT) + except subprocess.CalledProcessError as exc: raise RuntimeError( ('LaTeX was not able to process the following ' - 'string:\n%s\nHere is the full report generated by ' - 'LaTeX: \n\n' % repr(tex)) + report) - else: - mpl.verbose.report(report, 'debug') + 'string:\n%s\n\n' + 'Here is the full report generated by LaTeX:\n%s ' + '\n\n' % (repr(tex.encode('unicode_escape')), + exc.output.decode("utf-8")))) + mpl.verbose.report(report, 'debug') # find the box extent information in the latex output # file and store them in ".baseline" file - m = TexManager._re_vbox.search(report) + m = TexManager._re_vbox.search(report.decode("utf-8")) with open(basefile + '.baseline', "w") as fh: fh.write(" ".join(m.groups())) @@ -505,30 +478,22 @@ def make_png(self, tex, fontsize, dpi): # see get_rgba for a discussion of the background if DEBUG or not os.path.exists(pngfile): dvifile = self.make_dvi(tex, fontsize) - outfile = basefile + '.output' - command = self._get_shell_cmd( - 'cd "%s"' % self.texcache, - 'dvipng -bg Transparent -D %s -T tight -o "%s" "%s" > "%s"' % - (dpi, os.path.split(pngfile)[-1], - os.path.split(dvifile)[-1], outfile)) + command = [str("dvipng"), "-bg", "Transparent", "-D", str(dpi), + "-T", "tight", "-o", os.path.basename(pngfile), + os.path.basename(dvifile)] mpl.verbose.report(command, 'debug') - exit_status = os.system(command) try: - with open(outfile) as fh: - report = fh.read() - except IOError: - report = 'No dvipng error report available.' - if exit_status: + report = subprocess.check_output(command, + cwd=self.texcache, + stderr=subprocess.STDOUT) + except subprocess.CalledProcessError as exc: raise RuntimeError( - 'dvipng was not able to process the following ' - 'file:\n%s\nHere is the full report generated by ' - 'dvipng: \n\n' % dvifile + report) - else: - mpl.verbose.report(report, 'debug') - try: - os.remove(outfile) - except OSError: - pass + ('dvipng was not able to process the following ' + 'string:\n%s\n\n' + 'Here is the full report generated by dvipng:\n%s ' + '\n\n' % (repr(tex.encode('unicode_escape')), + exc.output.decode("utf-8")))) + mpl.verbose.report(report, 'debug') return pngfile @@ -543,23 +508,22 @@ def make_ps(self, tex, fontsize): if DEBUG or not os.path.exists(psfile): dvifile = self.make_dvi(tex, fontsize) - outfile = basefile + '.output' - command = self._get_shell_cmd( - 'cd "%s"' % self.texcache, - 'dvips -q -E -o "%s" "%s" > "%s"' % - (os.path.split(psfile)[-1], - os.path.split(dvifile)[-1], outfile)) + command = [str("dvips"), "-q", "-E", "-o", + os.path.basename(psfile), + os.path.basename(dvifile)] mpl.verbose.report(command, 'debug') - exit_status = os.system(command) - with open(outfile) as fh: - if exit_status: - raise RuntimeError( - 'dvipng was not able to process the flowing ' - 'file:\n%s\nHere is the full report generated by ' - 'dvipng: \n\n' % dvifile + fh.read()) - else: - mpl.verbose.report(fh.read(), 'debug') - os.remove(outfile) + try: + report = subprocess.check_output(command, + cwd=self.texcache, + stderr=subprocess.STDOUT) + except subprocess.CalledProcessError as exc: + raise RuntimeError( + ('dvips was not able to process the following ' + 'string:\n%s\n\n' + 'Here is the full report generated by dvips:\n%s ' + '\n\n' % (repr(tex.encode('unicode_escape')), + exc.output.decode("utf-8")))) + mpl.verbose.report(report, 'debug') return psfile diff --git a/tools/subset.py b/tools/subset.py index 97441c2cfa47..7a17cc22bd14 100644 --- a/tools/subset.py +++ b/tools/subset.py @@ -28,6 +28,7 @@ import sys import getopt import os +import subprocess import struct def log_namelist(nam, unicode): @@ -145,7 +146,7 @@ def subset_font_raw(font_in, font_out, unicodes, opts): if pe: print('Generate("' + font_out + '")', file=pe) pe.close() - os.system("fontforge -script " + pe_fn) + subprocess.call(["fontforge", "-script", pe_fn]) else: font.generate(font_out, flags = flags) font.close()