7
7
import glob , math , md5 , os , shutil , sys , time
8
8
def _fn_name (): return sys ._getframe (1 ).f_code .co_name
9
9
10
- from subprocess import Popen , STDOUT , PIPE
11
10
from tempfile import gettempdir
12
11
from cStringIO import StringIO
13
12
from matplotlib import verbose , __version__ , rcParams , get_data_path
@@ -32,9 +31,12 @@ def _fn_name(): return sys._getframe(1).f_code.co_name
32
31
import binascii
33
32
import re
34
33
34
+ if sys .platform .startswith ('win' ): cmd_split = '&'
35
+ else : cmd_split = ';'
36
+
35
37
backend_version = 'Level II'
36
38
37
- debugPS = 0
39
+ debugPS = 1
38
40
39
41
papersize = {'letter' : (8.5 ,11 ),
40
42
'legal' : (8.5 ,14 ),
@@ -1012,7 +1014,7 @@ def print_figure(self, outfile, dpi=72, facecolor='w', edgecolor='w',
1012
1014
title = outfile
1013
1015
1014
1016
# 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 ())
1016
1018
fh = file (tmpfile , 'w' )
1017
1019
1018
1020
# find the appropriate papertype
@@ -1144,7 +1146,10 @@ def _print_figure_tex(self, outfile, dpi, facecolor, edgecolor, orientation,
1144
1146
title = outfile
1145
1147
1146
1148
# 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 ())
1148
1153
fh = file (tmpfile , 'w' )
1149
1154
1150
1155
self .figure .dpi .set (72 ) # ignore the dpi kwarg
@@ -1248,6 +1253,7 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, paperWidth, paperHeight,
1248
1253
epsfile = tmpfile + '.eps'
1249
1254
shutil .move (tmpfile , epsfile )
1250
1255
latexfile = tmpfile + '.tex'
1256
+ outfile = tmpfile + '.output'
1251
1257
latexh = file (latexfile , 'w' )
1252
1258
dvifile = tmpfile + '.dvi'
1253
1259
psfile = tmpfile + '.ps'
@@ -1269,38 +1275,38 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, paperWidth, paperHeight,
1269
1275
\end{figure}
1270
1276
\end{document}
1271
1277
""" % (font_preamble , paperWidth , paperHeight , paperWidth , paperHeight ,
1272
- '\n ' .join (psfrags ), angle , os . path . split ( epsfile )[ - 1 ] )
1278
+ '\n ' .join (psfrags ), angle , epsfile )
1273
1279
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 )
1300
1305
os .remove (epsfile )
1301
1306
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 )
1304
1310
1305
1311
1306
1312
def gs_distill (tmpfile , eps = False , ptype = 'letter' , bbox = None ):
@@ -1310,25 +1316,24 @@ def gs_distill(tmpfile, eps=False, ptype='letter', bbox=None):
1310
1316
operators. The output is low-level, converting text to outlines.
1311
1317
"""
1312
1318
paper = '-sPAPERSIZE=%s' % ptype
1313
- outputfile = tmpfile + '.ps'
1319
+ psfile = tmpfile + '.ps'
1320
+ outfile = tmpfile + '.output'
1314
1321
dpi = rcParams ['ps.distiller.res' ]
1315
1322
if sys .platform == 'win32' : gs_exe = 'gswin32c'
1316
1323
else : gs_exe = 'gs'
1317
1324
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 )
1323
1329
if exit_status : raise RuntimeError ('ghostscript was not able to process \
1324
1330
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 )
1330
1335
os .remove (tmpfile )
1331
- shutil .move (outputfile , tmpfile )
1336
+ shutil .move (psfile , tmpfile )
1332
1337
if eps :
1333
1338
pstoeps (tmpfile , bbox )
1334
1339
@@ -1342,28 +1347,27 @@ def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None):
1342
1347
"""
1343
1348
pdffile = tmpfile + '.pdf'
1344
1349
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 )
1367
1371
os .remove (tmpfile )
1368
1372
shutil .move (psfile , tmpfile )
1369
1373
if eps :
@@ -1377,24 +1381,22 @@ def get_bbox(tmpfile, bbox):
1377
1381
Use ghostscript's bbox device to find the center of the bounding box. Return
1378
1382
an appropriately sized bbox centered around that point. A bit of a hack.
1379
1383
"""
1384
+ outfile = tmpfile + '.output'
1380
1385
if sys .platform == 'win32' : gs_exe = 'gswin32c'
1381
1386
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 ()
1396
1393
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 )
1398
1400
l , b , r , t = [float (i ) for i in bbox_info .split ()[- 4 :]]
1399
1401
1400
1402
# this is a hack to deal with the fact that ghostscript does not return the
0 commit comments