+
+
+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:
+ progress("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
+
+
+# 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