]> git.lyx.org Git - lyx.git/blobdiff - lib/scripts/lyxpreview_tools.py
Capitalize layout tag
[lyx.git] / lib / scripts / lyxpreview_tools.py
index 355ddba985122fda220087d77174b5d71403c3bf..0f595e2bf35cd7b7c2c0089f2c858bc412be5c7e 100644 (file)
@@ -1,4 +1,3 @@
-#! /usr/bin/env python
 
 # file lyxpreview_tools.py
 # This file is part of LyX, the document processor.
 
 # A repository of the following functions, used by the lyxpreview2xyz scripts.
 # copyfileobj, error, find_exe, find_exe_or_terminate, make_texcolor, mkstemp,
-# run_command, warning
+# progress, run_command, run_latex, warning
 
-import os, re, string, sys, tempfile
+# Requires python 2.4 or later (subprocess module).
+
+import os, re, string, subprocess, sys, tempfile
+
+
+# Control the output to stdout
+debug = False
+verbose = False
+
+# Known flavors of latex and bibtex
+bibtex_commands = ("bibtex", "bibtex8", "biber")
+latex_commands = ("latex", "pplatex", "platex", "latex2e")
+pdflatex_commands = ("pdflatex", "xelatex", "lualatex")
+
+# Pre-compiled regular expressions
+latex_file_re = re.compile(r"\.tex$")
+
+# PATH and PATHEXT environment variables
+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":
@@ -34,12 +55,18 @@ if os.name == "nt":
         use_win32_modules = 0
 
 
+def progress(message):
+    global verbose
+    if verbose:
+        sys.stdout.write("Progress: %s\n" % message)
+
+
 def warning(message):
-    sys.stderr.write(message + '\n')
+    sys.stderr.write("Warning: %s\n" % message)
 
 
 def error(message):
-    sys.stderr.write(message + '\n')
+    sys.stderr.write("Error: %s\n" % message)
     sys.exit(1)
 
 
@@ -59,12 +86,11 @@ def make_texcolor(hexcolor, graphics):
         return "rgb %f %f %f" % (red, green, blue)
 
 
-def find_exe(candidates, path):
-    extlist = ['']
-    if "PATHEXT" in os.environ:
-        extlist = extlist + os.environ["PATHEXT"].split(os.pathsep)
+def find_exe(candidates):
+    global extlist, path
 
-    for prog in candidates:
+    for command in candidates:
+        prog = command.split()[0]
         for directory in path:
             for ext in extlist:
                 full_path = os.path.join(directory, prog + ext)
@@ -73,24 +99,42 @@ def find_exe(candidates, path):
                     # have found it). Return just the basename to avoid
                     # problems when the path to the executable contains
                     # spaces.
-                    return os.path.basename(full_path)
+                    if full_path.lower().endswith('.py'):
+                        return command.replace(prog, '"%s" "%s"'
+                            % (sys.executable, full_path))
+                    return command
 
     return None
 
 
-def find_exe_or_terminate(candidates, path):
-    exe = find_exe(candidates, path)
+def find_exe_or_terminate(candidates):
+    exe = find_exe(candidates)
     if exe == None:
         error("Unable to find executable from '%s'" % string.join(candidates))
 
     return exe
 
 
-def run_command_popen(cmd):
-    handle = os.popen(cmd, 'r')
-    cmd_stdout = handle.read()
-    cmd_status = handle.close()
-
+def run_command_popen(cmd, stderr2stdout):
+    if os.name == 'nt':
+        unix = False
+    else:
+        unix = True
+    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
+    if debug:
+        sys.stdout.write(cmd_stdout)
     return cmd_status, cmd_stdout
 
 
@@ -138,14 +182,18 @@ def run_command_win32(cmd):
     if win32process.GetExitCodeProcess(process):
         return -3, ""
 
-    return None, data
+    global debug
+    if debug:
+        sys.stdout.write(data)
+    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():
@@ -269,3 +317,49 @@ def join_metrics_and_rename(original_metrics, new_metrics, new_page_indexes, ori
             original_metrics[legacy_index] = (index, metric)
         else:
             original_metrics.insert(legacy_index, (index, metric))
+
+
+def run_latex(latex, latex_file, bibtex = None):
+    # Run latex
+    latex_status, latex_stdout = run_tex(latex, latex_file)
+
+    if bibtex is None:
+        return latex_status, latex_stdout
+
+    # The aux and log output file names
+    aux_file = latex_file_re.sub(".aux", latex_file)
+    log_file = latex_file_re.sub(".log", latex_file)
+
+    # Run bibtex/latex if necessary
+    progress("Checking if a bibtex run is necessary")
+    if string_in_file(r"\bibdata", aux_file):
+        bibtex_status, bibtex_stdout = run_tex(bibtex, aux_file)
+        latex_status, latex_stdout = run_tex(latex, latex_file)
+    # Rerun latex if necessary
+    progress("Checking if a latex rerun is necessary")
+    if string_in_file("Warning: Citation", log_file):
+        latex_status, latex_stdout = run_tex(latex, latex_file)
+
+    return latex_status, latex_stdout
+
+
+def run_tex(tex, tex_file):
+    tex_call = '%s "%s"' % (tex, tex_file)
+
+    tex_status, tex_stdout = run_command(tex_call)
+    if tex_status:
+        warning("%s had problems compiling %s" \
+            % (os.path.basename(tex), tex_file))
+    return tex_status, tex_stdout
+
+
+def string_in_file(string, infile):
+    if not os.path.isfile(infile):
+        return False
+    f = open(infile, 'r')
+    for line in f.readlines():
+        if string in line:
+            f.close()
+            return True
+    f.close()
+    return False