]> git.lyx.org Git - lyx.git/blob - lib/scripts/lyxpreview_tools.py
Create Chapter 6 Bullets in Additional.lyx and move the bullet section into it; this...
[lyx.git] / lib / scripts / lyxpreview_tools.py
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.
4
5 # author Angus Leeming
6 # Full author contact details are available in file CREDITS
7
8 # and with much help testing the code under Windows from
9 #   Paul A. Rubin, rubin@msu.edu.
10
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
14
15 # Requires python 2.4 or later (subprocess module).
16
17 import os, re, subprocess, sys, tempfile
18
19
20 # Control the output to stdout
21 debug = False
22 verbose = False
23
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")
28
29 # Pre-compiled regular expressions
30 latex_file_re = re.compile(r"\.tex$")
31
32 # PATH and PATHEXT environment variables
33 path = os.environ["PATH"].split(os.pathsep)
34 extlist = ['']
35 if "PATHEXT" in os.environ:
36     extlist += os.environ["PATHEXT"].split(os.pathsep)
37 extlist.append('.py')
38
39 use_win32_modules = 0
40 if os.name == "nt":
41     use_win32_modules = 1
42     try:
43         import pywintypes
44         import win32con
45         import win32event
46         import win32file
47         import win32pipe
48         import win32process
49         import win32security
50         import winerror
51     except:
52         sys.stderr.write("Consider installing the PyWin extension modules " \
53                          "if you're irritated by windows appearing briefly.\n")
54         use_win32_modules = 0
55
56
57 def progress(message):
58     global verbose
59     if verbose:
60         sys.stdout.write("Progress: %s\n" % message)
61
62
63 def warning(message):
64     sys.stderr.write("Warning: %s\n" % message)
65
66
67 def error(message):
68     sys.stderr.write("Error: %s\n" % message)
69     sys.exit(1)
70
71
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)
77
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
81
82     if graphics:
83         return b"%f,%f,%f" % (red, green, blue)
84     else:
85         return b"rgb %f %f %f" % (red, green, blue)
86
87
88 def find_exe(candidates):
89     global extlist, path
90
91     for command in candidates:
92         prog = command.split()[0]
93         for directory in path:
94             for ext in extlist:
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
100                     # spaces.
101                     if full_path.lower().endswith('.py'):
102                         return command.replace(prog, '"%s" "%s"'
103                             % (sys.executable, full_path))
104                     return command
105
106     return None
107
108
109 def find_exe_or_terminate(candidates):
110     exe = find_exe(candidates)
111     if exe == None:
112         error("Unable to find executable from '%s'" % " ".join(candidates))
113
114     return exe
115
116
117 def run_command_popen(cmd, stderr2stdout):
118     if os.name == 'nt':
119         unix = False
120     else:
121         unix = True
122     if 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]
126     else:
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()
130         if cmd_stderr:
131             sys.stderr.write(cmd_stderr)
132     cmd_status = pipe.returncode
133
134     global debug
135     if debug:
136         sys.stdout.write(cmd_stdout)
137     return cmd_status, cmd_stdout
138
139
140 def run_command_win32(cmd):
141     sa = win32security.SECURITY_ATTRIBUTES()
142     sa.bInheritHandle = True
143     stdout_r, stdout_w = win32pipe.CreatePipe(sa, 0)
144
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
150
151     process, thread, pid, tid = \
152              win32process.CreateProcess(None, cmd, None, None, True,
153                                         0, None, None, si)
154     if process == None:
155         return -1, ""
156
157     # Must close the write handle in this process, or ReadFile will hang.
158     stdout_w.Close()
159
160     # Read the pipe until we get an error (including ERROR_BROKEN_PIPE,
161     # which is okay because it happens when child process ends).
162     data = ""
163     error = 0
164     while 1:
165         try:
166             hr, buffer = win32file.ReadFile(stdout_r, 4096)
167             if hr != winerror.ERROR_IO_PENDING:
168                 data = data + buffer
169
170         except pywintypes.error as e:
171             if e.args[0] != winerror.ERROR_BROKEN_PIPE:
172                 error = 1
173             break
174
175     if error:
176         return -2, ""
177
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):
182         return -3, ""
183
184     global debug
185     if debug:
186         sys.stdout.write(data)
187     return 0, data
188
189
190 def run_command(cmd, stderr2stdout = True):
191     progress("Running %s" % cmd)
192     if use_win32_modules:
193         return run_command_win32(cmd)
194     else:
195         return run_command_popen(cmd, stderr2stdout)
196
197
198 def copyfileobj(fsrc, fdst, rewind=0, length=16*1024):
199     """copy data from file-like object fsrc to file-like object fdst"""
200     if rewind:
201         fsrc.flush()
202         fsrc.seek(0)
203
204     while 1:
205         buf = fsrc.read(length)
206         if not buf:
207             break
208         fdst.write(buf)
209
210
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)
215     metrics.close()
216
217
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")
224
225     page_index = 0
226     skip_page = False
227     macros = []
228     for line in source_file:
229         # We found a new page
230         if line.startswith(b"\\begin{preview}"):
231             page_index += 1
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
234
235         if not skip_page:
236             match = def_re.match(line)
237             if match != None:
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)
245
246         # End of a page, we reset the skip_page bool
247         if line.startswith(b"\\end{preview}"):
248             skip_page = False
249
250     destination_file.close()
251     source_file.close()
252
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):
259     legacy_index = 0
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])
264
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:
269             legacy_index += 1
270
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)
274         else:
275             original_metrics.insert(legacy_index, (index, metric))
276
277
278 def run_latex(latex, latex_file, bibtex = None):
279     # Run latex
280     latex_status, latex_stdout = run_tex(latex, latex_file)
281
282     if bibtex is None:
283         return latex_status, latex_stdout
284
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)
288
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|Reference)", log_file):
297         latex_status, latex_stdout = run_tex(latex, latex_file)
298
299     return latex_status, latex_stdout
300
301
302 def run_tex(tex, tex_file):
303     tex_call = '%s "%s"' % (tex, tex_file)
304
305     tex_status, tex_stdout = run_command(tex_call)
306     if tex_status:
307         progress("Warning: %s had problems compiling %s" \
308             % (os.path.basename(tex), tex_file))
309     return tex_status, tex_stdout
310
311
312 def string_in_file(string, infile):
313     if not os.path.isfile(infile):
314         return False
315     string_re = re.compile(string.encode())
316     f = open(infile, 'rb')
317     for line in f.readlines():
318         if string_re.search(line):
319             f.close()
320             return True
321     f.close()
322     return False
323
324
325 # Returns a list of indexes of pages giving errors extracted from the latex log
326 def check_latex_log(log_file):
327
328     error_re = re.compile(b"^! ")
329     snippet_re = re.compile(b"^Preview: Snippet ")
330     data_re = re.compile(b"([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)")
331
332     found_error = False
333     error_pages = []
334
335     try:
336         for line in open(log_file, 'rb').readlines():
337             if not found_error:
338                 match = error_re.match(line)
339                 if match != None:
340                     found_error = True
341                 continue
342             else:
343                 match = snippet_re.match(line)
344                 if match == None:
345                     continue
346
347                 found_error = False
348                 match = data_re.search(line)
349                 if match == None:
350                     error("Unexpected data in %s\n%s" % (log_file, line))
351
352                 error_pages.append(int(match.group(1)))
353
354     except:
355         warning('check_latex_log: Unable to open "%s"' % log_file)
356         warning(repr(sys.exc_info()[0]) + ',' + repr(sys.exc_info()[1]))
357
358     return error_pages