forked from SublimeText/LaTeXTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkpsewhich.py
61 lines (52 loc) · 1.75 KB
/
kpsewhich.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from __future__ import print_function
import subprocess
from subprocess import Popen, PIPE
import os
import sublime
if sublime.version() < '3000':
_ST3 = False
else:
_ST3 = True
__all__ = ['kpsewhich']
def get_texpath():
settings = sublime.load_settings('LaTeXTools.sublime-settings')
platform_settings = settings.get(sublime.platform())
texpath = platform_settings['texpath']
if not _ST3:
return os.path.expandvars(texpath).encode(sys.getfilesystemencoding())
else:
return os.path.expandvars(texpath)
def kpsewhich(filename, file_format=None):
# build command
command = ['kpsewhich']
if file_format is not None:
command.append('-format=%s' % (file_format))
command.append(filename)
# setup the environment to run the subprocess in
texpath = get_texpath() or os.environ['PATH']
env = dict(os.environ)
env['PATH'] = texpath
try:
# Windows-specific adjustments
startupinfo = None
shell = False
if sublime.platform() == 'windows':
# ensure console window doesn't show
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
shell = True
print('Running %s' % (' '.join(command)))
p = Popen(
command,
stdout=PIPE,
stdin=PIPE,
startupinfo=startupinfo,
shell=shell,
env=env
)
path = p.communicate()[0].decode('utf-8').rstrip()
if p.returncode == 0:
return path
except OSError:
sublime.error_message('Could not run kpsewhich. Please ensure that your texpath setting is configured correctly in the LaTeXTools settings.')
return None