Skip to content

Instantly share code, notes, and snippets.

@joneskoo
Created December 1, 2011 05:19
Show Gist options
  • Save joneskoo/1413908 to your computer and use it in GitHub Desktop.
Save joneskoo/1413908 to your computer and use it in GitHub Desktop.
Run latex in a automatically destroyed temporary directory
#!/usr/bin/env python3
# Joonas Kuorilehto 2011
import os.path
from subprocess import Popen, PIPE
import shutil
from contextlib import contextmanager
from tempfile import mkdtemp
@contextmanager
def TemporaryDirectory(*args, **kw):
'''
Wrap a temporary directory for use in the with statement
Usage:
with TemporaryDirectory() as tmpdir:
pass # Create files in temporary directory
# Temporary directory is automatically deleted along its contents
'''
try:
tmpdir = mkdtemp(*args, **kw)
yield tmpdir
finally:
shutil.rmtree(tmpdir) # delete directory
def execute(command):
process = Popen(command, stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
rv = process.returncode
return rv
with TemporaryDirectory() as tmpdir:
command = ['/usr/local/texlive/2010/bin/x86_64-darwin/pdflatex', tmpdir]
rv = execute(command)
print("Return value was {}".format(rv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment