77import glob , math , md5 , os , shutil , sys , time
88def _fn_name (): return sys ._getframe (1 ).f_code .co_name
99
10- from subprocess import Popen , STDOUT , PIPE
1110from tempfile import gettempdir
1211from cStringIO import StringIO
1312from matplotlib import verbose , __version__ , rcParams , get_data_path
@@ -32,9 +31,12 @@ def _fn_name(): return sys._getframe(1).f_code.co_name
3231import binascii
3332import re
3433
34+ if sys .platform .startswith ('win' ): cmd_split = '&'
35+ else : cmd_split = ';'
36+
3537backend_version = 'Level II'
3638
37- debugPS = 0
39+ debugPS = 1
3840
3941papersize = {'letter' : (8.5 ,11 ),
4042 'legal' : (8.5 ,14 ),
@@ -1012,7 +1014,7 @@ def print_figure(self, outfile, dpi=72, facecolor='w', edgecolor='w',
10121014 title = outfile
10131015
10141016 # write to a temp file, we'll move it to outfile when done
1015- tmpfile = os .path .join (gettempdir (), md5 .md5 (outfile ).hexdigest ())
1017+ tmpfile = os .path .join (gettempdir (), md5 .md5 (basename ).hexdigest ())
10161018 fh = file (tmpfile , 'w' )
10171019
10181020 # find the appropriate papertype
@@ -1144,7 +1146,10 @@ def _print_figure_tex(self, outfile, dpi, facecolor, edgecolor, orientation,
11441146 title = outfile
11451147
11461148 # write to a temp file, we'll move it to outfile when done
1147- tmpfile = os .path .join (gettempdir (), md5 .md5 (outfile ).hexdigest ())
1149+ if sys .platform == 'win32' :
1150+ tmpfile = md5 .md5 (basename ).hexdigest ()
1151+ else :
1152+ tmpfile = os .path .join (gettempdir (), md5 .md5 (basename ).hexdigest ())
11481153 fh = file (tmpfile , 'w' )
11491154
11501155 self .figure .dpi .set (72 ) # ignore the dpi kwarg
@@ -1248,6 +1253,7 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, paperWidth, paperHeight,
12481253 epsfile = tmpfile + '.eps'
12491254 shutil .move (tmpfile , epsfile )
12501255 latexfile = tmpfile + '.tex'
1256+ outfile = tmpfile + '.output'
12511257 latexh = file (latexfile , 'w' )
12521258 dvifile = tmpfile + '.dvi'
12531259 psfile = tmpfile + '.ps'
@@ -1269,38 +1275,38 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, paperWidth, paperHeight,
12691275\end{figure}
12701276\end{document}
12711277""" % (font_preamble , paperWidth , paperHeight , paperWidth , paperHeight ,
1272- '\n ' .join (psfrags ), angle , os . path . split ( epsfile )[ - 1 ] )
1278+ '\n ' .join (psfrags ), angle , epsfile )
12731279 latexh .close ()
1274-
1275- command = 'cd "%s"; latex -interaction=nonstopmode "%s"' % (gettempdir (), latexfile )
1276- verbose .report (command , 'debug-annoying' )
1277- process = Popen ([command ], shell = True , stdin = PIPE , stdout = PIPE ,
1278- stderr = STDOUT )
1279- exit_status = process .wait ()
1280- if exit_status : raise RuntimeError ('LaTeX was not able to process \
1281- your image.\n Here is the full report generated by LaTeX: \
1282- \n \n ' + process .stdout .read ())
1283- else : verbose .report (process .stdout .read (), 'debug-annoying' )
1284- ## stdin, stdout, stderr = os.popen3(command)
1285- ## verbose.report(stdout.read(), 'debug-annoying')
1286- ## verbose.report(stderr.read(), 'helpful')
1287- ## shutil.move(tmpfile, os.path.split(tmpfile)[-1])
1288- command = 'cd "%s"; dvips -o "%s" "%s"' % (gettempdir (), psfile , dvifile )
1289- verbose .report (command , 'debug-annoying' )
1290- process = Popen ([command ], shell = True , stdin = PIPE , stdout = PIPE ,
1291- stderr = STDOUT )
1292- exit_status = process .wait ()
1293- if exit_status : raise RuntimeError ('dvips was not able to process \
1294- your image.\n Here is the full report generated by dvips: \
1295- \n \n ' + process .stdout .read ())
1296- else : verbose .report (process .stdout .read (), 'debug-annoying' )
1297- ## stdin, stdout, stderr = os.popen3(command)
1298- ## verbose.report(stdout.read(), 'debug-annoying')
1299- ## verbose.report(stderr.read(), 'helpful')
1280+ if sys .platform == 'win32' : outputdir = '.'
1281+ else : outputdir = gettempdir ()
1282+ command = 'latex -interaction=nonstopmode -output-directory="%s" "%s"\
1283+ > "%s"'% (outputdir , latexfile , outfile )
1284+ verbose .report (command , 'debug' )
1285+ exit_status = os .system (command )
1286+ fh = file (outfile )
1287+ if exit_status :
1288+ raise RuntimeError ('LaTeX was not able to process your file:\
1289+ \n Here is the full report generated by LaTeX: \n \n %s'% fh .read ())
1290+ else : verbose .report (fh .read (), 'debug' )
1291+ fh .close ()
1292+ os .remove (outfile )
1293+
1294+ command = 'dvips -q -R0 -o "%s" "%s" > "%s"' % \
1295+ (psfile , dvifile , outfile )
1296+ verbose .report (command , 'debug' )
1297+ exit_status = os .system (command )
1298+ fh = file (outfile )
1299+ if exit_status : raise RuntimeError ('dvips was not able to \
1300+ process the following file:\n %s\n Here is the full report generated by dvips: \
1301+ \n \n '% dvifile + fh .read ())
1302+ else : verbose .report (fh .read (), 'debug' )
1303+ fh .close ()
1304+ os .remove (outfile )
13001305 os .remove (epsfile )
13011306 shutil .move (psfile , tmpfile )
1302- for fname in glob .glob (tmpfile + '.*' ):
1303- os .remove (fname )
1307+ if not debugPS :
1308+ for fname in glob .glob (tmpfile + '.*' ):
1309+ os .remove (fname )
13041310
13051311
13061312def gs_distill (tmpfile , eps = False , ptype = 'letter' , bbox = None ):
@@ -1310,25 +1316,24 @@ def gs_distill(tmpfile, eps=False, ptype='letter', bbox=None):
13101316 operators. The output is low-level, converting text to outlines.
13111317 """
13121318 paper = '-sPAPERSIZE=%s' % ptype
1313- outputfile = tmpfile + '.ps'
1319+ psfile = tmpfile + '.ps'
1320+ outfile = tmpfile + '.output'
13141321 dpi = rcParams ['ps.distiller.res' ]
13151322 if sys .platform == 'win32' : gs_exe = 'gswin32c'
13161323 else : gs_exe = 'gs'
13171324 command = '%s -dBATCH -dNOPAUSE -r%d -sDEVICE=pswrite %s -sOutputFile="%s" \
1318- "%s"'% (gs_exe , dpi , paper , outputfile , tmpfile )
1319- verbose .report (command , 'debug-annoying' )
1320- process = Popen ([command ], shell = True , stdin = PIPE , stdout = PIPE ,
1321- stderr = STDOUT )
1322- exit_status = process .wait ()
1325+ "%s" > "%s"'% (gs_exe , dpi , paper , psfile , tmpfile , outfile )
1326+ verbose .report (command , 'debug' )
1327+ exit_status = os .system (command )
1328+ fh = file (outfile )
13231329 if exit_status : raise RuntimeError ('ghostscript was not able to process \
13241330 your image.\n Here is the full report generated by ghostscript: \
1325- \n \n ' + process .stdout .read ())
1326- else : verbose .report (process .stdout .read (), 'debug-annoying' )
1327- ## stdin, stdout, stderr = os.popen3(command)
1328- ## verbose.report(stdout.read(), 'debug-annoying')
1329- ## verbose.report(stderr.read(), 'helpful')
1331+ \n \n '% dvifile + fh .read ())
1332+ else : verbose .report (fh .read (), 'debug' )
1333+ fh .close ()
1334+ os .remove (outfile )
13301335 os .remove (tmpfile )
1331- shutil .move (outputfile , tmpfile )
1336+ shutil .move (psfile , tmpfile )
13321337 if eps :
13331338 pstoeps (tmpfile , bbox )
13341339
@@ -1342,28 +1347,27 @@ def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None):
13421347 """
13431348 pdffile = tmpfile + '.pdf'
13441349 psfile = tmpfile + '.ps'
1345- command = 'ps2pdf -sPAPERSIZE=%s "%s" "%s"' % (ptype , tmpfile , pdffile )
1346- verbose .report (command , 'debug-annoying' )
1347- process = Popen ([command ], shell = True , stdin = PIPE , stdout = PIPE ,
1348- stderr = STDOUT )
1349- exit_status = process .wait ()
1350- if exit_status :
1351- raise RuntimeError ('ps2pdf was not able to process your image.\n \
1352- Here is the report generated by ghostscript:\n \n ' + process .stdout .read ())
1353- else : verbose .report (process .stdout .read (), 'debug-annoying' )
1354- ## stdin, stderr = os.popen4(command)
1355- ## verbose.report(stderr.read(), 'helpful')
1356- command = 'pdftops -paper match -level2 "%s" "%s"' % (pdffile , psfile )
1357- verbose .report (command , 'debug-annoying' )
1358- process = Popen ([command ], shell = True , stdin = PIPE , stdout = PIPE ,
1359- stderr = STDOUT )
1360- exit_status = process .wait ()
1361- if exit_status : raise RuntimeError ('pdftops was not able to process \
1362- your image.\n Here is the full report generated by pdftops: \
1363- \n \n ' + process .stdout .read ())
1364- else : verbose .report (process .stdout .read (), 'debug-annoying' )
1365- ## stdin, stderr = os.popen4(command)
1366- ## verbose.report(stderr.read(), 'helpful')
1350+ outfile = tmpfile + '.output'
1351+ command = 'ps2pdf -sPAPERSIZE=%s "%s" "%s" > "%s"' % \
1352+ (ptype , tmpfile , pdffile , outfile )
1353+ verbose .report (command , 'debug' )
1354+ exit_status = os .system (command )
1355+ fh = file (outfile )
1356+ if exit_status : raise RuntimeError ('ps2pdf was not able to process your \
1357+ image.\n \Here is the report generated by ghostscript:\n \n ' + fh .read ())
1358+ else : verbose .report (fh .read (), 'debug' )
1359+ fh .close ()
1360+ os .remove (outfile )
1361+ command = 'pdftops -paper match -level2 "%s" "%s" > "%s"' % \
1362+ (pdffile , psfile , outfile )
1363+ verbose .report (command , 'debug' )
1364+ exit_status = os .system (command )
1365+ fh = file (outfile )
1366+ if exit_status : raise RuntimeError ('pdftops was not able to process your \
1367+ image.\n Here is the full report generated by pdftops: \n \n ' + fh .read ())
1368+ else : verbose .report (fh .read (), 'debug' )
1369+ fh .close ()
1370+ os .remove (outfile )
13671371 os .remove (tmpfile )
13681372 shutil .move (psfile , tmpfile )
13691373 if eps :
@@ -1377,24 +1381,22 @@ def get_bbox(tmpfile, bbox):
13771381 Use ghostscript's bbox device to find the center of the bounding box. Return
13781382 an appropriately sized bbox centered around that point. A bit of a hack.
13791383 """
1384+ outfile = tmpfile + '.output'
13801385 if sys .platform == 'win32' : gs_exe = 'gswin32c'
13811386 else : gs_exe = 'gs'
1382- command = '%s -dBATCH -dNOPAUSE -sDEVICE=bbox "%s"' % (gs_exe , tmpfile )
1383- verbose .report (command , 'debug-annoying' )
1384- process = Popen ([command ], shell = True , stdin = PIPE , stdout = PIPE ,
1385- stderr = STDOUT )
1386- exit_status = process .wait ()
1387- if exit_status : raise RuntimeError ('ghostscript was not able to determine \
1388- the bounding box for your image.\n Here is the full report generated by \
1389- ghostscript: \n \n ' + process .stdout .read ())
1390- else :
1391- bbox_info = process .stdout .read ()
1392- verbose .report (bbox_info , 'debug' )
1393- ## stdin, stdout, stderr = os.popen3(command)
1394- ## verbose.report(stdout.read(), 'debug-annoying')
1395- ## bbox_info = stderr.read()
1387+ command = '%s -dBATCH -dNOPAUSE -sDEVICE=bbox "%s"' % \
1388+ (gs_exe , tmpfile )
1389+ verbose .report (command , 'debug' )
1390+ stdin , stdout , stderr = os .popen3 (command )
1391+ verbose .report (stdout .read (), 'debug-annoying' )
1392+ bbox_info = stderr .read ()
13961393 verbose .report (bbox_info , 'helpful' )
1397- bbox_info = re .search ('%%HiResBoundingBox: .*' , bbox_info ).group ()
1394+ bbox_found = re .search ('%%HiResBoundingBox: .*' , bbox_info )
1395+ if bbox_found :
1396+ bbox_info = bbox_found .group ()
1397+ else :
1398+ raise RuntimeError ('Ghostscript was not able to extract a bounding box.\
1399+ Here is the Ghostscript output:\n \n %s'% bbox_info )
13981400 l , b , r , t = [float (i ) for i in bbox_info .split ()[- 4 :]]
13991401
14001402 # this is a hack to deal with the fact that ghostscript does not return the
0 commit comments