1 # file lyxpreview_tools.py
2 # This file is part of LyX, the document processor.
3 # Licence details can be found in the file COPYING.
6 # Full author contact details are available in file CREDITS
8 # and with much help testing the code under Windows from
9 # Paul A. Rubin, rubin@msu.edu.
11 # A repository of the following functions, used by the lyxpreview2xyz scripts.
12 # copyfileobj, error, find_exe, find_exe_or_terminate, make_texcolor,
13 # progress, run_command, run_latex, warning
15 # Requires python 2.4 or later (subprocess module).
17 import os, re, subprocess, sys, tempfile
20 # Control the output to stdout
24 # Known flavors of latex and bibtex
25 bibtex_commands = ("bibtex", "bibtex8", "biber")
26 latex_commands = ("latex", "pplatex", "platex", "latex2e")
27 pdflatex_commands = ("pdflatex", "xelatex", "lualatex")
29 # Pre-compiled regular expressions
30 latex_file_re = re.compile(r"\.tex$")
32 # PATH and PATHEXT environment variables
33 path = os.environ["PATH"].split(os.pathsep)
35 if "PATHEXT" in os.environ:
36 extlist += os.environ["PATHEXT"].split(os.pathsep)
52 sys.stderr.write("Consider installing the PyWin extension modules " \
53 "if you're irritated by windows appearing briefly.\n")
57 def progress(message):
60 sys.stdout.write("Progress: %s\n" % message)
64 sys.stderr.write("Warning: %s\n" % message)
68 sys.stderr.write("Error: %s\n" % message)
72 def make_texcolor(hexcolor, graphics):
73 # Test that the input string contains 6 hexadecimal chars.
74 hexcolor_re = re.compile(b"^[0-9a-fA-F]{6}$")
75 if not hexcolor_re.match(hexcolor):
76 error("Cannot convert color '%s'" % hexcolor)
78 red = float(int(hexcolor[0:2], 16)) / 255.0
79 green = float(int(hexcolor[2:4], 16)) / 255.0
80 blue = float(int(hexcolor[4:6], 16)) / 255.0
83 return b"%f,%f,%f" % (red, green, blue)
85 return b"rgb %f %f %f" % (red, green, blue)
88 def find_exe(candidates):
91 for command in candidates:
92 prog = command.split()[0]
93 for directory in path:
95 full_path = os.path.join(directory, prog + ext)
96 if os.access(full_path, os.X_OK):
97 # The thing is in the PATH already (or we wouldn't
98 # have found it). Return just the basename to avoid
99 # problems when the path to the executable contains
101 if full_path.lower().endswith('.py'):
102 return command.replace(prog, '"%s" "%s"'
103 % (sys.executable, full_path))
109 def find_exe_or_terminate(candidates):
110 exe = find_exe(candidates)
112 error("Unable to find executable from '%s'" % " ".join(candidates))
117 def run_command_popen(cmd, stderr2stdout):
123 pipe = subprocess.Popen(cmd, shell=unix, close_fds=unix, stdin=subprocess.PIPE, \
124 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
125 cmd_stdout = pipe.communicate()[0]
127 pipe = subprocess.Popen(cmd, shell=unix, close_fds=unix, stdin=subprocess.PIPE, \
128 stdout=subprocess.PIPE, universal_newlines=True)
129 (cmd_stdout, cmd_stderr) = pipe.communicate()
131 sys.stderr.write(cmd_stderr)
132 cmd_status = pipe.returncode
136 sys.stdout.write(cmd_stdout)
137 return cmd_status, cmd_stdout
140 def run_command_win32(cmd):
141 sa = win32security.SECURITY_ATTRIBUTES()
142 sa.bInheritHandle = True
143 stdout_r, stdout_w = win32pipe.CreatePipe(sa, 0)
145 si = win32process.STARTUPINFO()
146 si.dwFlags = (win32process.STARTF_USESTDHANDLES
147 | win32process.STARTF_USESHOWWINDOW)
148 si.wShowWindow = win32con.SW_HIDE
149 si.hStdOutput = stdout_w
151 process, thread, pid, tid = \
152 win32process.CreateProcess(None, cmd, None, None, True,
157 # Must close the write handle in this process, or ReadFile will hang.
160 # Read the pipe until we get an error (including ERROR_BROKEN_PIPE,
161 # which is okay because it happens when child process ends).
166 hr, buffer = win32file.ReadFile(stdout_r, 4096)
167 if hr != winerror.ERROR_IO_PENDING:
170 except pywintypes.error as e:
171 if e.args[0] != winerror.ERROR_BROKEN_PIPE:
178 # Everything is okay --- the called process has closed the pipe.
179 # For safety, check that the process ended, then pick up its exit code.
180 win32event.WaitForSingleObject(process, win32event.INFINITE)
181 if win32process.GetExitCodeProcess(process):
186 sys.stdout.write(data)
190 def run_command(cmd, stderr2stdout = True):
191 progress("Running %s" % cmd)
192 if use_win32_modules:
193 return run_command_win32(cmd)
195 return run_command_popen(cmd, stderr2stdout)
198 def copyfileobj(fsrc, fdst, rewind=0, length=16*1024):
199 """copy data from file-like object fsrc to file-like object fdst"""
205 buf = fsrc.read(length)
211 def write_metrics_info(metrics_info, metrics_file):
212 metrics = open(metrics_file, 'w')
213 for metric in metrics_info:
214 metrics.write("Snippet %s %f\n" % metric)
218 # Reads a .tex files and create an identical file but only with
219 # pages whose index is in pages_to_keep
220 def filter_pages(source_path, destination_path, pages_to_keep):
221 def_re = re.compile(b"(\\\\newcommandx|\\\\renewcommandx|\\\\global\\\\long\\\\def)(\\[a-zA-Z]+)(.+)")
222 source_file = open(source_path, "rb")
223 destination_file = open(destination_path, "wb")
228 for line in source_file:
229 # We found a new page
230 if line.startswith(b"\\begin{preview}"):
232 # If the page index isn't in pages_to_keep we don't copy it
233 skip_page = page_index not in pages_to_keep
236 match = def_re.match(line)
238 definecmd = match.group(1)
239 macroname = match.group(2)
240 if not macroname in macros:
241 macros.append(macroname)
242 if definecmd == b"\\renewcommandx":
243 line = line.replace(definecmd, b"\\newcommandx")
244 destination_file.write(line)
246 # End of a page, we reset the skip_page bool
247 if line.startswith(b"\\end{preview}"):
250 destination_file.close()
253 # Joins two metrics list, that is a list of tuple (page_index, metric)
254 # new_page_indexes contains the original page number of the pages in new_metrics
255 # e.g. new_page_indexes[3] == 14 means that the 4th item in new_metrics is the 15th in the original counting
256 # original_bitmap and destination_bitmap are file name models used to rename the new files
257 # e.g. image_new%d.png and image_%d.png
258 def join_metrics_and_rename(original_metrics, new_metrics, new_page_indexes, original_bitmap, destination_bitmap):
260 for (index, metric) in new_metrics:
261 # If the file exists we rename it
262 if os.path.isfile(original_bitmap % (index)):
263 os.rename(original_bitmap % (index), destination_bitmap % new_page_indexes[index-1])
265 # Extract the original page index
266 index = new_page_indexes[index-1]
267 # Goes through the array until the end is reached or the correct index is found
268 while legacy_index < len(original_metrics) and original_metrics[legacy_index][0] < index:
271 # Add or update the metric for this page
272 if legacy_index < len(original_metrics) and original_metrics[legacy_index][0] == index:
273 original_metrics[legacy_index] = (index, metric)
275 original_metrics.insert(legacy_index, (index, metric))
278 def run_latex(latex, latex_file, bibtex = None):
280 latex_status, latex_stdout = run_tex(latex, latex_file)
283 return latex_status, latex_stdout
285 # The aux and log output file names
286 aux_file = latex_file_re.sub(".aux", latex_file)
287 log_file = latex_file_re.sub(".log", latex_file)
289 # Run bibtex/latex if necessary
290 progress("Checking if a bibtex run is necessary")
291 if string_in_file(r"\bibdata", aux_file):
292 bibtex_status, bibtex_stdout = run_tex(bibtex, aux_file)
293 latex_status, latex_stdout = run_tex(latex, latex_file)
294 # Rerun latex if necessary
295 progress("Checking if a latex rerun is necessary")
296 if string_in_file("Warning: Citation", log_file):
297 latex_status, latex_stdout = run_tex(latex, latex_file)
299 return latex_status, latex_stdout
302 def run_tex(tex, tex_file):
303 tex_call = '%s "%s"' % (tex, tex_file)
305 tex_status, tex_stdout = run_command(tex_call)
307 progress("Warning: %s had problems compiling %s" \
308 % (os.path.basename(tex), tex_file))
309 return tex_status, tex_stdout
312 def string_in_file(string, infile):
313 if not os.path.isfile(infile):
315 f = open(infile, 'rb')
316 for line in f.readlines():
317 if string.encode() in line:
324 # Returns a list of indexes of pages giving errors extracted from the latex log
325 def check_latex_log(log_file):
327 error_re = re.compile(b"^! ")
328 snippet_re = re.compile(b"^Preview: Snippet ")
329 data_re = re.compile(b"([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)")
335 for line in open(log_file, 'rb').readlines():
337 match = error_re.match(line)
342 match = snippet_re.match(line)
347 match = data_re.search(line)
349 error("Unexpected data in %s\n%s" % (log_file, line))
351 error_pages.append(int(match.group(1)))
354 warning('check_latex_log: Unable to open "%s"' % log_file)
355 warning(repr(sys.exc_info()[0]) + ',' + repr(sys.exc_info()[1]))