Paste Code
Paste Blends
Paste Images
#!/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
  1. #!/usr/bin/env python
  2.  
  3. ##Copyright (C) 2010 Louis Taylor  
  4. ## _    ___ _____                                                              
  5. ##| |  | _ \_   _|                                                            
  6. ##| |__|  _/ | |                                                              
  7. ##|____|_|   |_| *programus optimus est*                                      
  8. ##                                                                            
  9. ##This program is free software: you can redistribute it and/or modify        
  10. ##it under the terms of the GNU General Public License as published by        
  11. ##the Free Software Foundation, either version 3 of the License, or            
  12. ##(at your option) any later version.                                          
  13. ##                                                                            
  14. ##This program is distributed in the hope that it will be useful,              
  15. ##but WITHOUT ANY WARRANTY; without even the implied warranty of              
  16. ##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                
  17. ##GNU General Public License for more details.                                
  18. ##                                                                            
  19. ##You should have received a copy of the GNU General Public License            
  20. ##along with this program.  If not, see http://www.gnu.org/licenses/gpl-3.0.txt  
  21.  
  22. import os, subprocess, time
  23.  
  24. def compile(latexfile, pdfdir='/tmp', rmlog=False, movepdf=False):
  25.     '''compiles latex document, saves it to the directory
  26.    specified in *pdfdir* and returns any error messages it can find'''
  27.     path = os.path.split(latexfile)
  28.     texfile = path[1]
  29.     directory = path[0]
  30.    
  31.     rawname = os.path.splitext(texfile)[0]
  32.    
  33.     logfile = os.path.splitext(texfile)[0]+'.log'
  34.     logpath = os.path.join(pdfdir, logfile)
  35.    
  36.     pdffile = os.path.splitext(texfile)[0]+'.pdf'
  37.     pdfpath = os.path.join(pdfdir, pdffile)
  38.    
  39.     os.chdir(directory) #change cwd to the dir to place all latex files
  40.    
  41.     os.system('bibtex %s' % rawname)
  42.    
  43.     cmd = ['pdflatex', '--halt-on-error', '-output-directory', pdfdir, texfile]
  44.     latex = subprocess.Popen(cmd, shell=False,
  45.                             stdin=subprocess.PIPE,
  46.                             stdout=subprocess.PIPE,
  47.                             stderr=subprocess.PIPE)
  48.     message = latex.communicate()[0] #get the stdout, latex.communicate()[1] gives stderr
  49.     #set the data to be returned on success
  50.     returned = ['success', pdfpath]
  51.  
  52.     if '\n!' in message:
  53.         name = False
  54.         ln = False
  55.         error_msg = ''
  56.         error_ln = ''
  57.         quote = ''
  58.        
  59.         for line in message.split('\n'):
  60.             if line == '':
  61.                 pass #pass if the line is blank
  62.            
  63.             elif line[0] == '!' and not name:
  64.                 #find error message line
  65.                 name = True
  66.                 error_msg = line.replace('!', '').strip()
  67.                                         #remove all whitespace around string
  68.                
  69.             elif line[0] == 'l' and name and not ln:
  70.                 #find line number and quote.
  71.                 #only come to this line if:
  72.                 #the-- first character is an 'l', there has just been an error
  73.                 #line and there has not been a line number before.
  74.                 ln = True
  75.                 string = line.split(' ')
  76.                 error_ln = string[0].replace('l.', '')
  77.                 quote = line.replace(string[0], '')
  78.                
  79.             else:
  80.                 pass
  81.        
  82.         #set the data to be returned on error
  83.         returned = ['error', error_msg, error_ln, quote, pdffile]
  84.        
  85.     if rmlog:
  86.         try:
  87.             os.remove(logpath)
  88.         except OSError:
  89.             pass
  90.        
  91.     if movepdf != False:
  92.         #bit of a quick hack, use cp to do the work:
  93.         os.system('cp "%s" "%s"' % (pdfpath, movepdf))
  94.        
  95.     #lastly return error codes ect.
  96.     return returned
  97.  
  98. if __name__ == '__main__':
  99.     #small self test
  100.     error = compile('/home/louis/Desktop/tex/text.tex', '/home/louis/Desktop/out', True, movepdf='/home/louis/Desktop/tex/text.pdf')
  101.     print error
go to heaven