Skip to content

Commit

Permalink
Better dfu-util output handling
Browse files Browse the repository at this point in the history
  • Loading branch information
benlye committed Apr 14, 2021
1 parent af08a9c commit 4efdb36
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions octoprint_firmwareupdater/methods/dfuutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import os
import sarge

DFUUTIL_ERASING = "Erase "
DFUUTIL_WRITING = "Download "
DFUUTIL_ERASING = "Erase"
DFUUTIL_WRITING = "Downloading"
DFUUTIL_NOACCESS = "Cannot open DFU device"
DFUUTIL_NODEVICE = "No DFU capable USB device available"

Expand Down Expand Up @@ -41,15 +41,16 @@ def _flash_dfuutil(self, firmware=None, printer_port=None, **kwargs):

import sarge
self._logger.info(u"Running '{}' in {}".format(dfuutil_command, working_dir))
self._send_status("progress", subtype="writing")
self._console_logger.info(dfuutil_command)
try:
p = sarge.run(dfuutil_command, cwd=working_dir, async_=True, stdout=sarge.Capture(buffer_size=1), stderr=sarge.Capture(buffer_size=1))
p.wait_events()

while p.returncode is None:
output = p.stderr.read(timeout=0.2).decode('utf-8')
if not output:
output = p.stdout.read(timeout=0.2).decode('utf-8')
error = p.stderr.read(timeout=0.2).decode('utf-8')

if not output and not error:
p.commands[0].poll()
continue

Expand All @@ -64,7 +65,13 @@ def _flash_dfuutil(self, firmware=None, printer_port=None, **kwargs):
elif DFUUTIL_WRITING in line:
self._logger.info(u"Writing memory...")
self._send_status("progress", subtype="writing")
elif DFUUTIL_NOACCESS in line:

for line in error.split("\n"):
if line.endswith("\r"):
line = line[:-1]
self._console_logger.info(u"> {}".format(line))

if DFUUTIL_NOACCESS in line:
raise FlashException("Cannot access DFU device")
elif DFUUTIL_NODEVICE in line:
raise FlashException("No DFU device found")
Expand Down

0 comments on commit 4efdb36

Please sign in to comment.