#!/usr/bin/env python
##Copyright (C) 2010 Louis Taylor ## _ ___ _____ ##| | | _ \_ _| ##| |__| _/ | | ##|____|_| |_| *programus optimus est* ## ##This program is free software: you can redistribute it and/or modify ##it under the terms of the GNU General Public License as published by ##the Free Software Foundation, either version 3 of the License, or ##(at your option) any later version. ## ##This program is distributed in the hope that it will be useful, ##but WITHOUT ANY WARRANTY; without even the implied warranty of ##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ##GNU General Public License for more details. ## ##You should have received a copy of the GNU General Public License ##along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.txt
import os, subprocess, time
def compile(latexfile, pdfdir='/tmp', rmlog=False, movepdf=False): '''compiles latex document, saves it to the directory specified in *pdfdir* and returns any error messages it can find''' path = os.path.split(latexfile) texfile = path[1] directory = path[0] rawname = os.path.splitext(texfile)[0] logfile = os.path.splitext(texfile)[0]+'.log' logpath = os.path.join(pdfdir, logfile) pdffile = os.path.splitext(texfile)[0]+'.pdf' pdfpath = os.path.join(pdfdir, pdffile) os.chdir(directory) #change cwd to the dir to place all latex files os.system('bibtex %s' % rawname) cmd = ['pdflatex', '--halt-on-error', '-output-directory', pdfdir, texfile] latex = subprocess.Popen(cmd, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) message = latex.communicate()[0] #get the stdout, latex.communicate()[1] gives stderr #set the data to be returned on success returned = ['success', pdfpath]
if '\n!' in message: name = False ln = False error_msg = '' error_ln = '' quote = '' for line in message.split('\n'): if line == '': pass #pass if the line is blank elif line[0] == '!' and not name: #find error message line name = True error_msg = line.replace('!', '').strip() #remove all whitespace around string elif line[0] == 'l' and name and not ln: #find line number and quote. #only come to this line if: #the-- first character is an 'l', there has just been an error #line and there has not been a line number before. ln = True string = line.split(' ') error_ln = string[0].replace('l.', '') quote = line.replace(string[0], '') else: pass #set the data to be returned on error returned = ['error', error_msg, error_ln, quote, pdffile] if rmlog: try: os.remove(logpath) except OSError: pass if movepdf != False: #bit of a quick hack, use cp to do the work: os.system('cp "%s" "%s"' % (pdfpath, movepdf)) #lastly return error codes ect. return returned
if __name__ == '__main__': #small self test error = compile('/home/louis/Desktop/tex/text.tex', '/home/louis/Desktop/out', True, movepdf='/home/louis/Desktop/tex/text.pdf') print error
#!/usr/bin/env python ##Copyright (C) 2010 Louis Taylor ## _ ___ _____ ##| | | _ \_ _| ##| |__| _/ | | ##|____|_| |_| *programus optimus est* ## ##This program is free software: you can redistribute it and/or modify ##it under the terms of the GNU General Public License as published by ##the Free Software Foundation, either version 3 of the License, or ##(at your option) any later version. ## ##This program is distributed in the hope that it will be useful, ##but WITHOUT ANY WARRANTY; without even the implied warranty of ##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ##GNU General Public License for more details. ## ##You should have received a copy of the GNU General Public License ##along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.txt import os, subprocess, time def compile(latexfile, pdfdir='/tmp', rmlog=False, movepdf=False): '''compiles latex document, saves it to the directory specified in *pdfdir* and returns any error messages it can find''' path = os.path.split(latexfile) texfile = path[1] directory = path[0] rawname = os.path.splitext(texfile)[0] logfile = os.path.splitext(texfile)[0]+'.log' logpath = os.path.join(pdfdir, logfile) pdffile = os.path.splitext(texfile)[0]+'.pdf' pdfpath = os.path.join(pdfdir, pdffile) os.chdir(directory) #change cwd to the dir to place all latex files os.system('bibtex %s' % rawname) cmd = ['pdflatex', '--halt-on-error', '-output-directory', pdfdir, texfile] latex = subprocess.Popen(cmd, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) message = latex.communicate()[0] #get the stdout, latex.communicate()[1] gives stderr #set the data to be returned on success returned = ['success', pdfpath] if '\n!' in message: name = False ln = False error_msg = '' error_ln = '' quote = '' for line in message.split('\n'): if line == '': pass #pass if the line is blank elif line[0] == '!' and not name: #find error message line name = True error_msg = line.replace('!', '').strip() #remove all whitespace around string elif line[0] == 'l' and name and not ln: #find line number and quote. #only come to this line if: #the-- first character is an 'l', there has just been an error #line and there has not been a line number before. ln = True string = line.split(' ') error_ln = string[0].replace('l.', '') quote = line.replace(string[0], '') else: pass #set the data to be returned on error returned = ['error', error_msg, error_ln, quote, pdffile] if rmlog: try: os.remove(logpath) except OSError: pass if movepdf != False: #bit of a quick hack, use cp to do the work: os.system('cp "%s" "%s"' % (pdfpath, movepdf)) #lastly return error codes ect. return returned if __name__ == '__main__': #small self test error = compile('/home/louis/Desktop/tex/text.tex', '/home/louis/Desktop/out', True, movepdf='/home/louis/Desktop/tex/text.pdf') print error
|