]> git.lyx.org Git - lyx.git/blobdiff - lib/scripts/lyxpreview_tools.py
iucr.layout: use Item_Environment for a style as proposed by Jürgen
[lyx.git] / lib / scripts / lyxpreview_tools.py
index 546ed24248aede74c992766bea14f7cac213e750..4b18227746b037351a93694305c744d803c22e4b 100644 (file)
@@ -1,4 +1,3 @@
-#! /usr/bin/env python
 
 # file lyxpreview_tools.py
 # This file is part of LyX, the document processor.
@@ -36,6 +35,7 @@ path = os.environ["PATH"].split(os.pathsep)
 extlist = ['']
 if "PATHEXT" in os.environ:
     extlist += os.environ["PATHEXT"].split(os.pathsep)
+extlist.append('.py')
 
 use_win32_modules = 0
 if os.name == "nt":
@@ -99,6 +99,9 @@ def find_exe(candidates):
                     # have found it). Return just the basename to avoid
                     # problems when the path to the executable contains
                     # spaces.
+                    if full_path.lower().endswith('.py'):
+                        return command.replace(prog, '"%s" "%s"'
+                            % (sys.executable, full_path))
                     return command
 
     return None
@@ -112,14 +115,21 @@ def find_exe_or_terminate(candidates):
     return exe
 
 
-def run_command_popen(cmd):
+def run_command_popen(cmd, stderr2stdout):
     if os.name == 'nt':
         unix = False
     else:
         unix = True
-    pipe = subprocess.Popen(cmd, shell=unix, close_fds=unix, stdin=subprocess.PIPE, \
-        stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
-    cmd_stdout = pipe.communicate()[0]
+    if stderr2stdout:
+        pipe = subprocess.Popen(cmd, shell=unix, close_fds=unix, stdin=subprocess.PIPE, \
+            stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
+        cmd_stdout = pipe.communicate()[0]
+    else:
+        pipe = subprocess.Popen(cmd, shell=unix, close_fds=unix, stdin=subprocess.PIPE, \
+            stdout=subprocess.PIPE, universal_newlines=True)
+        (cmd_stdout, cmd_stderr) = pipe.communicate()
+        if cmd_stderr:
+            sys.stderr.write(cmd_stderr)
     cmd_status = pipe.returncode
 
     global debug
@@ -178,12 +188,12 @@ def run_command_win32(cmd):
     return 0, data
 
 
-def run_command(cmd):
+def run_command(cmd, stderr2stdout = True):
     progress("Running %s" % cmd)
     if use_win32_modules:
         return run_command_win32(cmd)
     else:
-        return run_command_popen(cmd)
+        return run_command_popen(cmd, stderr2stdout)
 
 
 def get_version_info():
@@ -353,3 +363,39 @@ def string_in_file(string, infile):
             return True
     f.close()
     return False
+
+
+# Returns a list of indexes of pages giving errors extracted from the latex log
+def check_latex_log(log_file):
+
+    error_re = re.compile("^! ")
+    snippet_re = re.compile("^Preview: Snippet ")
+    data_re = re.compile("([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)")
+
+    found_error = False
+    error_pages = []
+
+    try:
+        for line in open(log_file, 'r').readlines():
+            if not found_error:
+                match = error_re.match(line)
+                if match != None:
+                    found_error = True
+                continue
+            else:
+                match = snippet_re.match(line)
+                if match == None:
+                    continue
+
+                found_error = False
+                match = data_re.search(line)
+                if match == None:
+                    error("Unexpected data in %s\n%s" % (log_file, line))
+
+                error_pages.append(int(match.group(1)))
+
+    except:
+        warning('check_latex_log: Unable to open "%s"' % log_file)
+        warning(`sys.exc_type` + ',' + `sys.exc_value`)
+
+    return error_pages