Skip to content

Commit 04ec75e

Browse files
Try and upload ASAP instead of hard-waiting 10sec (#610)
Fixes #609 and a niggling slowness in uploads. Try to find the drive in a loop for 10 seconds, instead of only checking once after 10 seconds. Avoid 100% CPU usage while waiting for Pico drive
1 parent 420d669 commit 04ec75e

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

tools/uf2conv.py

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,21 @@ def get_drives():
262262
for d in os.listdir(rootpath):
263263
drives.append(os.path.join(rootpath, d))
264264

265+
if (len(drives) == 0) and (sys.platform == "linux"):
266+
globexpr = "/dev/disk/by-id/usb-RPI_RP2*-part1"
267+
rpidisk = glob.glob(globexpr)
268+
if len(rpidisk) > 0:
269+
try:
270+
cmd = ["udisksctl", "mount", "--block-device", os.path.realpath(rpidisk[0])]
271+
proc_out = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
272+
if proc_out.returncode == 0:
273+
stdoutput = proc_out.stdout.decode("UTF-8")
274+
match = re.search(r'Mounted\s+.*\s+at\s+([^\.\r\n]*)', stdoutput)
275+
if match:
276+
drives = [match.group(1)]
277+
except Exception as ex:
278+
print("Exception executing udisksctl. Exception: {}".format(ex))
279+
# If it fails, no problem since it was a heroic attempt
265280

266281
def has_info(d):
267282
try:
@@ -336,8 +351,6 @@ def error(msg):
336351
ser.dtr = False
337352
except:
338353
print("Caught exception during reset!")
339-
# Probably should be smart and check for device appearance or something
340-
time.sleep(10)
341354
except:
342355
pass
343356
if args.list:
@@ -368,33 +381,11 @@ def error(msg):
368381
if args.output == None:
369382
args.output = "flash." + ext
370383
else:
371-
drives = get_drives()
372-
if (len(drives) == 0) and (sys.platform == "linux"):
373-
globexpr = "/dev/disk/by-id/usb-RPI_RP2*-part1"
374-
rpidisk = glob.glob(globexpr)
375-
if len(rpidisk) == 0:
376-
print("Unable to find disk by ID using expression: {}".format(globexpr))
377-
else:
378-
try:
379-
cmd = ["udisksctl", "mount", "--block-device", os.path.realpath(rpidisk[0])]
380-
proc_out = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
381-
if proc_out.returncode == 0:
382-
stdoutput = proc_out.stdout.decode("UTF-8")
383-
match = re.search(r'Mounted\s+.*\s+at\s+([^\.\r\n]*)', stdoutput)
384-
if match is None:
385-
print("Warn: {} did not print mount point. Attempting to locate mounted drive in file system. StdOut={}".format(cmd[0], stdoutput))
386-
drives = get_drives()
387-
else:
388-
drives = [match.group(1)]
389-
else:
390-
print("Error executing command {}. Return Code: {} Std Output: {} StdError: {}".format(" ".join(cmd),
391-
proc_out.returncode,
392-
proc_out.stdout.decode("UTF-8"),
393-
proc_out.stderr.decode("UTF-8")))
394-
395-
except Exception as ex:
396-
print("Exception executing udisksctl. Exception: {}".format(ex))
397-
# If it fails, no problem since it was a heroic attempt
384+
now = time.time()
385+
drives = []
386+
while (time.time() - now < 10.0) and (len(drives) == 0):
387+
time.sleep(0.5) # Avoid 100% CPU use while waiting for drive to appear
388+
drives = get_drives()
398389

399390
if args.output:
400391
write_file(args.output, outbuf)

0 commit comments

Comments
 (0)