]> git.lyx.org Git - lyx.git/blobdiff - lib/scripts/lyxpreview_tools.py
whitespace.
[lyx.git] / lib / scripts / lyxpreview_tools.py
index 8408b3a08fd854219c06cf3605e66397cb3dacb4..2012927c05003e662621a29ecf2bd75cb33a01d3 100644 (file)
@@ -1,4 +1,3 @@
-#! /usr/bin/env python
 
 # file lyxpreview_tools.py
 # This file is part of LyX, the document processor.
@@ -116,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
@@ -162,7 +168,7 @@ def run_command_win32(cmd):
             if hr != winerror.ERROR_IO_PENDING:
                 data = data + buffer
 
-        except pywintypes.error, e:
+        except pywintypes.error as e:
             if e.args[0] != winerror.ERROR_BROKEN_PIPE:
                 error = 1
             break
@@ -182,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():
@@ -266,11 +272,13 @@ def write_metrics_info(metrics_info, metrics_file):
 # Reads a .tex files and create an identical file but only with
 # pages whose index is in pages_to_keep
 def filter_pages(source_path, destination_path, pages_to_keep):
+    def_re = re.compile(r"(\\newcommandx|\\renewcommandx|\\global\\long\\def)(\\[a-zA-Z]+)(.+)")
     source_file = open(source_path, "r")
     destination_file = open(destination_path, "w")
 
     page_index = 0
     skip_page = False
+    macros = []
     for line in source_file:
         # We found a new page
         if line.startswith("\\begin{preview}"):
@@ -279,6 +287,14 @@ def filter_pages(source_path, destination_path, pages_to_keep):
             skip_page = page_index not in pages_to_keep
 
         if not skip_page:
+            match = def_re.match(line)
+            if match != None:
+                definecmd = match.group(1)
+                macroname = match.group(2)
+                if not macroname in macros:
+                    macros.append(macroname)
+                    if definecmd == "\\renewcommandx":
+                        line = line.replace(definecmd, "\\newcommandx")
             destination_file.write(line)
 
         # End of a page, we reset the skip_page bool
@@ -342,7 +358,7 @@ def run_tex(tex, tex_file):
 
     tex_status, tex_stdout = run_command(tex_call)
     if tex_status:
-        warning("%s had problems compiling %s" \
+        progress("Warning: %s had problems compiling %s" \
             % (os.path.basename(tex), tex_file))
     return tex_status, tex_stdout
 
@@ -357,3 +373,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(repr(sys.exc_info()[0]) + ',' + repr(sys.exc_info()[1]))
+
+    return error_pages