Skip to content

Commit 3268f5d

Browse files
NicklasTegnerNicklasTegner
NicklasTegner
authored and
NicklasTegner
committed
Added tmglr command runner and update command as a proof
1 parent 8f7807e commit 3268f5d

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
*.zip
2+
*.tar.gz
3+
*.tgz
4+
tinytex/*
5+
6+
17
# Byte-compiled / optimized / DLL files
28
__pycache__/
39
*.py[cod]

pytinytex/__init__.py

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1+
import sys
2+
import subprocess
13
import glob
24
import os
35

46
from .tinytex_download import download_tinytex
57

68
# Global cache
7-
__tinytex_path = None
9+
__tinytex_path = getattr(os.environ, "PYTINYTEX_TINYTEX", None)
10+
11+
def update(package="-all"):
12+
path = get_tinytex_path()
13+
try:
14+
code, stdout,stderr = _run_tlmgr_command(["update", package], path, False)
15+
return True
16+
except RuntimeError:
17+
raise
18+
return False
19+
820

921
def get_tinytex_path(base="."):
1022
global __tinytex_path
@@ -69,5 +81,52 @@ def _check_file(dir, prefix):
6981
if os.path.splitext(s)[0] == prefix and os.path.isfile(os.path.join(dir, s)):
7082
return True
7183
except FileNotFoundError:
72-
raise RuntimeError("Unable to resolve TinyTeX path.")
73-
return False
84+
raise RuntimeError("Unable to resolve path.")
85+
return False
86+
87+
def _get_file(dir, prefix):
88+
try:
89+
for s in os.listdir(dir):
90+
if os.path.splitext(s)[0] == prefix and os.path.isfile(os.path.join(dir, s)):
91+
return os.path.join(dir, s)
92+
except FileNotFoundError:
93+
raise RuntimeError("Unable to find {}.".format(prefix))
94+
95+
def _run_tlmgr_command(args, path, machine_readable=True):
96+
if machine_readable:
97+
if "--machine-readable" not in args:
98+
args.insert(0, "--machine-readable")
99+
tlmgr_executable = _get_file(path, "tlmgr")
100+
args.insert(0, tlmgr_executable)
101+
new_env = os.environ.copy()
102+
creation_flag = 0x08000000 if sys.platform == "win32" else 0 # set creation flag to not open TinyTeX in new console on windows
103+
p = subprocess.Popen(
104+
args,
105+
stdout=subprocess.PIPE,
106+
stderr=subprocess.PIPE,
107+
env=new_env,
108+
creationflags=creation_flag)
109+
# something else than 'None' indicates that the process already terminated
110+
if not (p.returncode is None):
111+
raise RuntimeError(
112+
'TLMGR died with exitcode "%s" before receiving input: %s' % (p.returncode,
113+
p.stderr.read())
114+
)
115+
116+
stdout, stderr = p.communicate()
117+
118+
try:
119+
stdout = stdout.decode("utf-8")
120+
except UnicodeDecodeError:
121+
raise RuntimeError("Unable to decode stdout from TinyTeX")
122+
123+
try:
124+
stderr = stderr.decode("utf-8")
125+
except UnicodeDecodeError:
126+
raise RuntimeError("Unable to decode stderr from TinyTeX")
127+
128+
if stderr == "" and p.returncode == 0:
129+
return p.returncode, stdout, stderr
130+
else:
131+
raise RuntimeError("TLMGR died with the following error:\n{0}".format(stderr.strip()))
132+
return p.returncode, stdout, stderr

0 commit comments

Comments
 (0)