|
| 1 | +import sys |
| 2 | +import subprocess |
1 | 3 | import glob
|
2 | 4 | import os
|
3 | 5 |
|
4 | 6 | from .tinytex_download import download_tinytex
|
5 | 7 |
|
6 | 8 | # 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 | + |
8 | 20 |
|
9 | 21 | def get_tinytex_path(base="."):
|
10 | 22 | global __tinytex_path
|
@@ -69,5 +81,52 @@ def _check_file(dir, prefix):
|
69 | 81 | if os.path.splitext(s)[0] == prefix and os.path.isfile(os.path.join(dir, s)):
|
70 | 82 | return True
|
71 | 83 | 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