Skip to content

Commit f8209c1

Browse files
committed
added help and shell + logging
1 parent 2dcf813 commit f8209c1

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

pytinytex/__init__.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,34 @@
22
import sys
33
import os
44
import platform
5+
import logging
56

67
from .tinytex_download import download_tinytex, DEFAULT_TARGET_FOLDER # noqa
78

9+
logger = logging.getLogger("pytinytex")
10+
formatter = logging.Formatter('%(message)s')
11+
12+
# create a console handler and set the level to debug
13+
ch = logging.StreamHandler()
14+
ch.setFormatter(formatter)
15+
logger.addHandler(ch)
16+
17+
18+
819
# Global cache
920
__tinytex_path = None
1021

1122
def update(package="-all"):
1223
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
24+
return _run_tlmgr_command(["update", package], path, False)
1925

26+
def help():
27+
path = get_tinytex_path()
28+
return _run_tlmgr_command(["help"], path, False)
29+
30+
def shell():
31+
path = get_tinytex_path()
32+
return _run_tlmgr_command(["shell"], path, False, True)
2033

2134
def get_tinytex_path(base=None):
2235
if __tinytex_path:
@@ -84,28 +97,31 @@ def _run_tlmgr_command(args, path, machine_readable=True, interactive=False):
8497
creation_flag = 0x08000000 if sys.platform == "win32" else 0 # set creation flag to not open TinyTeX in new console on windows
8598

8699
try:
100+
logger.debug(f"Running command: {args}")
87101
return asyncio.run(_run_command(*args, stdin=interactive, env=new_env, creationflags=creation_flag))
88102
except Exception:
89103
raise
90104

91105
async def read_stdout(process, output_buffer):
92106
"""Read lines from process.stdout and print them."""
107+
logger.debug(f"Reading stdout from process {process.pid}")
93108
try:
94109
while True:
95110
line = await process.stdout.readline()
96111
if not line: # EOF reached
97112
break
98113
line = line.decode('utf-8').rstrip()
99114
output_buffer.append(line)
115+
logger.info(line)
100116
except Exception as e:
101-
print("Error in read_stdout:", e)
117+
logger.error(f"Error in read_stdout: {e}")
102118
finally:
103119
process._transport.close()
104120
return await process.wait()
105121

106-
107122
async def send_stdin(process):
108123
"""Read user input from sys.stdin and send it to process.stdin."""
124+
logger.debug(f"Sending stdin to process {process.pid}")
109125
loop = asyncio.get_running_loop()
110126
try:
111127
while True:
@@ -116,7 +132,7 @@ async def send_stdin(process):
116132
process.stdin.write(user_input.encode('utf-8'))
117133
await process.stdin.drain()
118134
except Exception as e:
119-
print("Error in send_stdin:", e)
135+
logger.error(f"Error in send_stdin: {e}")
120136
finally:
121137
if process.stdin:
122138
process._transport.close()
@@ -142,14 +158,15 @@ async def _run_command(*args, stdin=False, **kwargs):
142158
try:
143159
if stdin:
144160
# Wait for both tasks to complete.
161+
logger.debug("Waiting for stdout and stdin tasks to complete")
145162
await asyncio.gather(stdout_task, stdin_task)
146163
else:
147164
# Wait for the stdout task to complete.
165+
logger.debug("Waiting for stdout task to complete")
148166
await stdout_task
149167
# Return the process return code.
150168
exit_code = await process.wait()
151169
except KeyboardInterrupt:
152-
print("\nKeyboardInterrupt detected, terminating subprocess...")
153170
process.terminate() # Gracefully terminate the subprocess.
154171
exit_code = await process.wait()
155172
finally:
@@ -161,6 +178,3 @@ async def _run_command(*args, stdin=False, **kwargs):
161178
if exit_code != 0:
162179
raise RuntimeError(f"Error running command: {captured_output}")
163180
return exit_code, captured_output
164-
165-
166-
return process.returncode

tests/test_tinytex_runner.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import os
2+
import pytest
3+
4+
import pytinytex
5+
from .utils import download_tinytex, TINYTEX_DISTRIBUTION # noqa
6+
7+
def test_run_help(download_tinytex): # noqa
8+
exit_code, output = pytinytex.help()
9+
assert exit_code == 0
10+
assert "TeX Live" in output
11+

0 commit comments

Comments
 (0)