Last active
May 25, 2021 21:17
-
-
Save matthewfl/bc37c8ac1434720d278806fbd19317fe to your computer and use it in GitHub Desktop.
Get a notification when latexmk fails
This file contains 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 | |
import subprocess | |
import sys | |
def main(): | |
proc = subprocess.Popen(['latexmk', '-pvc'] + sys.argv[1:], | |
stdin=subprocess.PIPE, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.STDOUT, | |
bufsize=0, | |
universal_newlines=False) | |
out = '' | |
while True: | |
li = proc.stdout.read(1) | |
if not li: | |
if proc.poll() is not None: | |
sys.exit(1) | |
continue | |
li = li.decode('utf-8', 'ignore') | |
out += li | |
out = out[-2000:] | |
if out.endswith('\n? '): | |
# then we have failed, so log an error | |
out = '\n'.join(out.split('\n')[-10:-1]) | |
print('send notification') | |
subprocess.Popen(['notify-send', '-t', '10000', '-u', 'critical', | |
'Latex compile failed', out]) | |
# make it continue to compile | |
if sys.argv[0].endswith('r'): | |
proc.stdin.write(b'R\n') | |
else: | |
proc.stdin.write(b'X\n') | |
sys.stdout.write(li) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On OSX use the following instead of
notify-send