summaryrefslogtreecommitdiff
path: root/support/asymptote/__main__.py
blob: 5c6f67a5effb4dd64aede2c24e91720f5bf112cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import os, sys, shutil
from lib import util


def _asymptote(in_path, out_path, asymptote_dir, working_dir):
	util.command([os.environ['ASYMPTOTE'], '-f', 'pdf', '-o', out_path, in_path], set_env = { 'ASYMPTOTE_DIR': asymptote_dir }, working_dir = working_dir)


def main(in_path, out_path):
	_, out_suffix = os.path.splitext(out_path)
	
	if out_suffix == '.pdf':
		with util.TemporaryDirectory() as temp_dir:
			absolute_in_path = os.path.abspath(in_path)
			temp_out_path = os.path.join(temp_dir, 'out.pdf')
			
			# Asymptote creates A LOT of temp files (presumably when invoking LaTeX) and leaves some of them behind. Thus we run asymptote in a temporary directory.
			_asymptote(absolute_in_path, 'out', os.path.dirname(absolute_in_path), temp_dir)
			
			shutil.copyfile(temp_out_path, out_path)
	else:
		raise Exception('Unknown file type: {}'.format(out_suffix))


try:
	main(*sys.argv[1:])
except util.UserError as e:
	print 'Error:', e
	sys.exit(1)
except KeyboardInterrupt:
	sys.exit(2)