2
2
import sys
3
3
import os
4
4
import platform
5
+ import logging
5
6
6
7
from .tinytex_download import download_tinytex , DEFAULT_TARGET_FOLDER # noqa
7
8
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
+
8
19
# Global cache
9
20
__tinytex_path = None
10
21
11
22
def update (package = "-all" ):
12
23
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 )
19
25
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 )
20
33
21
34
def get_tinytex_path (base = None ):
22
35
if __tinytex_path :
@@ -84,28 +97,31 @@ def _run_tlmgr_command(args, path, machine_readable=True, interactive=False):
84
97
creation_flag = 0x08000000 if sys .platform == "win32" else 0 # set creation flag to not open TinyTeX in new console on windows
85
98
86
99
try :
100
+ logger .debug (f"Running command: { args } " )
87
101
return asyncio .run (_run_command (* args , stdin = interactive , env = new_env , creationflags = creation_flag ))
88
102
except Exception :
89
103
raise
90
104
91
105
async def read_stdout (process , output_buffer ):
92
106
"""Read lines from process.stdout and print them."""
107
+ logger .debug (f"Reading stdout from process { process .pid } " )
93
108
try :
94
109
while True :
95
110
line = await process .stdout .readline ()
96
111
if not line : # EOF reached
97
112
break
98
113
line = line .decode ('utf-8' ).rstrip ()
99
114
output_buffer .append (line )
115
+ logger .info (line )
100
116
except Exception as e :
101
- print ( "Error in read_stdout:" , e )
117
+ logger . error ( f "Error in read_stdout: { e } " )
102
118
finally :
103
119
process ._transport .close ()
104
120
return await process .wait ()
105
121
106
-
107
122
async def send_stdin (process ):
108
123
"""Read user input from sys.stdin and send it to process.stdin."""
124
+ logger .debug (f"Sending stdin to process { process .pid } " )
109
125
loop = asyncio .get_running_loop ()
110
126
try :
111
127
while True :
@@ -116,7 +132,7 @@ async def send_stdin(process):
116
132
process .stdin .write (user_input .encode ('utf-8' ))
117
133
await process .stdin .drain ()
118
134
except Exception as e :
119
- print ( "Error in send_stdin:" , e )
135
+ logger . error ( f "Error in send_stdin: { e } " )
120
136
finally :
121
137
if process .stdin :
122
138
process ._transport .close ()
@@ -142,14 +158,15 @@ async def _run_command(*args, stdin=False, **kwargs):
142
158
try :
143
159
if stdin :
144
160
# Wait for both tasks to complete.
161
+ logger .debug ("Waiting for stdout and stdin tasks to complete" )
145
162
await asyncio .gather (stdout_task , stdin_task )
146
163
else :
147
164
# Wait for the stdout task to complete.
165
+ logger .debug ("Waiting for stdout task to complete" )
148
166
await stdout_task
149
167
# Return the process return code.
150
168
exit_code = await process .wait ()
151
169
except KeyboardInterrupt :
152
- print ("\n KeyboardInterrupt detected, terminating subprocess..." )
153
170
process .terminate () # Gracefully terminate the subprocess.
154
171
exit_code = await process .wait ()
155
172
finally :
@@ -161,6 +178,3 @@ async def _run_command(*args, stdin=False, **kwargs):
161
178
if exit_code != 0 :
162
179
raise RuntimeError (f"Error running command: { captured_output } " )
163
180
return exit_code , captured_output
164
-
165
-
166
- return process .returncode
0 commit comments