Skip to content

Commit 70b6e79

Browse files
committed
updates the esp32 build
1 parent 9b543da commit 70b6e79

File tree

2 files changed

+256
-187
lines changed

2 files changed

+256
-187
lines changed

builder/__init__.py

Lines changed: 103 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import subprocess
44
import threading
55
import random
6+
import queue
67

78
_windows_env = None
89

10+
911
def setup_windows_build():
1012

1113
global _windows_env
@@ -242,26 +244,25 @@ def _busy_spinner(evnt):
242244
wait = random.randint(10, 100) * 0.001
243245

244246

245-
def spawn(cmd_, out_to_screen=True, spinner=False, env=None, cmpl=False):
246-
if env is None:
247-
env = os.environ
247+
def _convert_line(lne):
248+
try:
249+
lne = lne.decode('utf-8')
250+
except UnicodeDecodeError:
251+
for char in lne:
252+
if 32 <= char <= 125 or char in (b'\r', b'\n'):
253+
continue
248254

249-
if isinstance(cmd_[0], str):
250-
cmd_ = [cmd_[:]]
255+
lne = lne.replace(char, b'')
256+
lne = lne.decode('utf-8')
251257

252-
cmd_ = ' && '.join(' '.join(c) for c in cmd_)
258+
return lne
253259

254-
p = subprocess.Popen(
255-
cmd_,
256-
stdout=subprocess.PIPE,
257-
stderr=subprocess.PIPE,
258-
shell=True,
259-
env=env
260-
)
261260

262-
if not sys.platform.startswith('win'):
263-
os.set_blocking(p.stdout.fileno(), False)
264-
os.set_blocking(p.stderr.fileno(), False)
261+
def process_output(myproc, out_to_screen, spinner, cmpl, out_queue): #output-consuming thread
262+
line = b''
263+
err_line = b''
264+
265+
last_line_len = -1
265266

266267
event = threading.Event()
267268

@@ -272,36 +273,23 @@ def spawn(cmd_, out_to_screen=True, spinner=False, env=None, cmpl=False):
272273
else:
273274
t = None
274275

275-
output_buffer = []
276-
last_line_len = -1
276+
while True:
277+
if myproc.poll() is not None:
278+
break
277279

278-
def _convert_line(lne):
279-
try:
280-
lne = lne.decode('utf-8')
281-
except UnicodeDecodeError:
282-
for char in lne:
283-
if 32 <= char <= 125 or char in (b'\r', b'\n'):
284-
continue
285-
286-
lne = lne.replace(char, b'')
287-
lne = lne.decode('utf-8')
288-
289-
return lne
290-
291-
try:
292-
while p.poll() is None:
293-
line = p.stdout.readline()
294-
if line is not None and line.strip():
280+
# --- extract line using read(1)
281+
out = myproc.stdout.read(1)
282+
while out:
283+
line += out
284+
if out == b'\n':
295285
line = _convert_line(line.strip())
296-
output_buffer.append(line)
286+
out_queue.put(line)
297287

298288
if not spinner and out_to_screen:
299-
if (
300-
cmpl and
301-
(line.startswith('[') or line.startswith('--')) and
302-
last_line_len != -1
303-
):
304-
sys.stdout.write('\r')
289+
if cmpl and (line.startswith('[') or line.startswith('--')):
290+
if last_line_len != -1:
291+
sys.stdout.write('\r')
292+
305293
if len(line) < last_line_len:
306294
padding = ' ' * (last_line_len - len(line))
307295
else:
@@ -315,57 +303,101 @@ def _convert_line(lne):
315303

316304
sys.stdout.flush()
317305

318-
line = p.stderr.readline()
319-
while line is not None and line.strip():
320-
if not spinner and out_to_screen and cmpl and last_line_len != -1:
321-
sys.stdout.write('\n')
322-
sys.stdout.flush()
323-
last_line_len = -1
306+
line = b''
324307

325-
line = _convert_line(line.strip())
326-
output_buffer.append(line)
308+
out = myproc.stdout.read(1)
309+
310+
out = myproc.stderr.read(1)
327311

312+
while out:
313+
if not spinner and out_to_screen and cmpl and last_line_len != -1:
314+
sys.stdout.write('\n')
315+
sys.stdout.flush()
316+
last_line_len = -1
317+
318+
err_line += out
319+
if out == b'\n':
320+
err_line = _convert_line(err_line.strip())
321+
out_queue.put(err_line)
328322
if out_to_screen and not spinner:
329-
sys.stderr.write(line + '\n')
323+
sys.stderr.write(err_line + '\n')
330324
sys.stderr.flush()
331325

332-
line = p.stderr.readline()
326+
out = myproc.stderr.read(1)
333327

334-
except KeyboardInterrupt:
335-
if t is not None:
336-
event.set()
337-
t.join()
328+
if t is not None:
329+
event.set()
330+
t.join()
338331

339-
print()
340-
print(output_buffer)
332+
sys.stdout.write('\n')
333+
sys.stdout.flush()
334+
335+
elif out_to_screen and cmpl and last_line_len != -1:
336+
sys.stdout.write('\n')
337+
sys.stdout.flush()
341338

342-
if not p.stdout.closed:
343-
p.stdout.close()
344339

345-
if not p.stderr.closed:
346-
p.stderr.close()
340+
myprocess = subprocess.Popen('myprogram.exe', stdout=subprocess.PIPE) #output-producing process
347341

348-
raise
342+
343+
def spawn(cmd_, out_to_screen=True, spinner=False, env=None, cmpl=False):
344+
if env is None:
345+
env = os.environ
346+
347+
if isinstance(cmd_[0], str):
348+
cmd_ = [cmd_[:]]
349+
350+
cmd_ = ' && '.join(' '.join(c) for c in cmd_)
351+
352+
que = queue.Queue()
353+
354+
p = subprocess.Popen(
355+
cmd_,
356+
stdout=subprocess.PIPE,
357+
stderr=subprocess.PIPE,
358+
shell=True,
359+
env=env
360+
)
361+
362+
proc_thread = threading.Thread(
363+
target=process_output,
364+
args=(myprocess, out_to_screen, spinner, cmpl, que)
365+
)
366+
367+
proc_thread.start()
368+
369+
output_buffer = []
370+
371+
while proc_thread and proc_thread.is_alive(): # wait for thread to finish
372+
try:
373+
line = que.get_nowait() # or q.get(timeout=.1)
374+
output_buffer.append(line)
375+
except queue.Empty:
376+
pass
377+
378+
try:
379+
proc_thread.join(1)
380+
except: # NOQA
381+
break
382+
383+
try:
384+
line = que.get_nowait() # or q.get(timeout=.1)
385+
output_buffer.append(line)
386+
except queue.Empty:
387+
pass
349388

350389
if not p.stdout.closed:
351390
p.stdout.close()
352391

353392
if not p.stderr.closed:
354393
p.stderr.close()
355394

356-
if t is not None:
357-
event.set()
358-
t.join()
359-
360-
sys.stdout.write('\n')
361-
sys.stdout.flush()
362-
363395
output_buffer = '\n'.join(output_buffer)
364396

365397
if out_to_screen:
366398
if spinner:
367399
print(output_buffer)
368-
elif cmpl and last_line_len != -1:
400+
elif cmpl:
369401
sys.stdout.write('\n')
370402
sys.stdout.flush()
371403

0 commit comments

Comments
 (0)