Last active
November 5, 2015 17:06
-
-
Save MoritzMaxeiner/77f3087cac49631ece12 to your computer and use it in GitHub Desktop.
Python 3 wrapper around latexmk for building in a temporary directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
from subprocess import Popen, PIPE | |
from os import write, getcwd | |
from tempfile import TemporaryDirectory | |
from shutil import copy | |
from pathlib import Path | |
from glob import glob | |
from signal import signal, SIGINT | |
from sys import exit | |
import click | |
@click.command() | |
@click.option("output_dir", "-o", type=click.Path(exists=True), help="Output directory", default=getcwd()) | |
@click.argument("latexmk_args", nargs=-1) | |
def latex2pdf(output_dir, latexmk_args): | |
with TemporaryDirectory() as build_directory: | |
def save_output(signal=None, frame=None): | |
for generated_pdf in glob(str(Path(build_directory) / Path("*.pdf"))): | |
copy(generated_pdf, output_dir) | |
if signal: | |
exit(0) | |
signal(SIGINT, save_output) | |
latexmk = Popen(["latexmk", "-outdir=" + build_directory, "-pdf"] + list(latexmk_args), stdout = PIPE) | |
for char in iter(lambda: latexmk.stdout.read(1), b''): | |
write(1, char) | |
save_output() | |
if __name__ == "__main__": | |
latex2pdf() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Data center access limited to data center technicians and approved GitHub staff..