Skip to content

Instantly share code, notes, and snippets.

@Luzifer
Created November 12, 2012 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Luzifer/4060817 to your computer and use it in GitHub Desktop.
Save Luzifer/4060817 to your computer and use it in GitHub Desktop.
Upstart daemon to autogenerate LaTeX documents added to Dropbox
import os, time, subprocess, sys
from datetime import datetime
my_path = os.path.realpath(__file__)
my_version = os.path.getmtime(my_path)
startdir = '/home/luzifer/Dropbox/tex-convert'
gentex_command = '/home/luzifer/bin/gentex'
def log(string):
print '[%s] %s' % (datetime.now().strftime('%Y-%m-%d %H:%M:%S'), string)
while True:
dirpath = os.path.realpath(os.path.expanduser(startdir))
index = {}
for dirname, directories, files in os.walk(dirpath):
for filename in files:
if filename.find('.tex') == -1:
continue
fullpath = os.path.join(dirname, filename)
index[fullpath] = {
'mtime': os.path.getmtime(fullpath)
}
pdfpath = fullpath.replace('.tex', '.pdf')
if os.path.exists(pdfpath):
index[fullpath]['pdfexists'] = True
index[fullpath]['pdftime'] = os.path.getmtime(pdfpath)
else:
index[fullpath]['pdfexists'] = False
logpath = fullpath.replace('.tex', '.log')
if os.path.exists(logpath):
index[fullpath]['logtime'] = os.path.getmtime(logpath)
index[fullpath]['logexists'] = True
else:
index[fullpath]['logexists'] = False
for texfile in index.keys():
if index[texfile]['pdfexists'] and index[texfile]['pdftime'] > index[texfile]['mtime']:
continue
if index[texfile]['logexists'] and index[texfile]['logtime'] > index[texfile]['mtime']:
continue
log('Need rendering of %s' % texfile)
os.chdir(os.path.dirname(texfile))
subprocess.check_output('%s "%s"' % (gentex_command, texfile), shell=True)
if my_version < os.path.getmtime(my_path):
log('Restarting to refresh version.')
sys.exit(0)
time.sleep(3)
#!/bin/bash
while [ $# -gt 0 ]; do
texfile=$1
name=$(basename $texfile .tex)
basedir=$(dirname $texfile)
cd $basedir
for i in $(seq 1 3); do
pdflatex -interaction batchmode $texfile
if ! [ $? -eq 0 ]; then
echo "Build failed. See $name.log"
for file in $name.aux $name.toc $name.out; do
rm -f $basedir/$file
done
exit 0
fi
done
for file in $name.aux $name.log $name.toc $name.out; do
rm -f $basedir/$file
done
shift
done
start on runlevel [2345]
stop on runlevel [016]
respawn
exec su luzifer -c 'python /home/luzifer/Dropbox/tex-convert/worker.py'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment