Jean-Marc,
attached is a patch to make the script a little more robust in the face of
exceptions raised by a failure to open a file. It also improves the
quality of comments in the code and in diagnostic messages. Functionality
is otherwise unchanged.
OK to apply?
--
AngusIndex: lib/ChangeLog
===
RCS file: /usr/local/lyx/cvsroot/lyx-devel/lib/ChangeLog,v
retrieving revision 1.363.2.75
diff -u -p -r1.363.2.75 ChangeLog
--- lib/ChangeLog 9 Jun 2004 12:35:49 - 1.363.2.75
+++ lib/ChangeLog 14 Jul 2004 10:20:56 -
@@ -1,3 +1,10 @@
+2004-07-14 Angus Leeming <[EMAIL PROTECTED]>
+
+ * scripts/lyxpreview2ppm.py: improve the commentary, both in
+ comments in the file and in diagnostics output on error.
+ Use try-except blocks to catch exceptions raised by a failure
+ to open a file.
+
2004-06-09 Angus Leeming <[EMAIL PROTECTED]>
* scripts/lyxpreview2bitmap.sh: removed.
Index: lib/scripts/lyxpreview2ppm.py
===
RCS file: /usr/local/lyx/cvsroot/lyx-devel/lib/scripts/Attic/lyxpreview2ppm.py,v
retrieving revision 1.1.2.1
diff -u -p -r1.1.2.1 lyxpreview2ppm.py
--- lib/scripts/lyxpreview2ppm.py 9 Jun 2004 12:35:49 - 1.1.2.1
+++ lib/scripts/lyxpreview2ppm.py 14 Jul 2004 10:20:58 -
@@ -9,32 +9,17 @@
# Full author contact details are available in file CREDITS
# with much advice from members of the preview-latex project:
-# David Kastrup, [EMAIL PROTECTED] and
-# Jan-Åke Larsson, [EMAIL PROTECTED]
-
+# David Kastrup, [EMAIL PROTECTED] and
+# Jan-Åke Larsson, [EMAIL PROTECTED]
# and with much help testing the code under Windows from
-# Paul A. Rubin, [EMAIL PROTECTED]
+# Paul A. Rubin, [EMAIL PROTECTED]
# This script takes a LaTeX file and generates a collection of
# ppm image files, one per previewed snippet.
-
-# Pre-requisites:
-# * A latex executable;
-# * preview.sty;
-# * dvips;
-# * gs;
-# * pnmcrop (optional).
-
-# preview.sty is part of the preview-latex project
-# http://preview-latex.sourceforge.net/
-
-# preview.sty can alternatively be obtained from
-# CTAN/support/preview-latex/
-
# Example usage:
-# lyxpreview2bitmap.py 0lyxpreview.tex 128 ppm
+# lyxpreview2ppm.py 0lyxpreview.tex 128 ppm
-# This script takes three arguments:
+# The script takes three arguments:
# TEXFILE: the name of the .tex file to be converted.
# SCALEFACTOR: a scale factor, used to ascertain the resolution of the
#generated image which is then passed to gs.
@@ -48,6 +33,18 @@
# * a file BASE.metrics, containing info needed by LyX to position
# the images correctly on the screen.
+# The script uses several external programs and files:
+# * A latex executable;
+# * preview.sty;
+# * dvips;
+# * gs;
+# * pnmcrop (optional).
+
+# preview.sty is part of the preview-latex project
+# http://preview-latex.sourceforge.net/
+# Alternatively, it can be obtained from
+# CTAN/support/preview-latex/
+
import glob, os, re, string, sys
import pipes, tempfile
@@ -65,11 +62,11 @@ if os.name == "nt":
import winerror
except:
sys.stderr.write("Consider installing the PyWin extension modules "\
- "if you're irritated by the gs window being shown\n")
+ "if you're irritated by windows appearing briefly.\n")
use_win32_modules = 0
-# Pre-compiled regular expressions.
+# Pre-compiled regular expression.
latex_file_re = re.compile("\.tex$")
@@ -78,6 +75,10 @@ def usage(prog_name):
% prog_name
+def warning(message):
+sys.stderr.write(message + '\n')
+
+
def error(message):
sys.stderr.write(message + '\n')
sys.exit(1)
@@ -151,7 +152,7 @@ def run_command_win32(cmd):
if error:
return -2, ""
-# Everything okay - called process has closed the pipe.
+# Everything is okay --- the called process has closed the pipe.
# For safety, check that the process ended, then pick up its exit code.
win32event.WaitForSingleObject(process, win32event.INFINITE)
if win32process.GetExitCodeProcess(process):
@@ -173,11 +174,18 @@ def extract_metrics_info(log_file, metri
log_re = re.compile("Preview: [ST]")
success = 0
-for line in open(log_file, 'r').readlines():
-match = log_re.match(line)
-if match != None:
-success = 1
-metrics.write("%s\n" % line)
+try:
+for line in open(log_file, 'r').readlines():
+match = log_re.match(line)
+if match != None:
+success = 1
+metrics.write("%s\n" % line)
+
+except:
+# Unable to open the file, but do nothing here because
+# the calling function will act on the value of 'success'.
+warning('Warning in extract_metrics_info! Unable to open "%s"' % log_file)
+warning(`sys.exc_type` + ',' + `sys.exc_value`)
return success
@@ -193,32