Auto-detect firmware files in CWD for firmware update prompts

Instead of requiring a manual path entry, scan the current working
directory for files with common firmware extensions and present them
as a numbered list (most recently modified first). The last option
always allows entering a custom path, and the manual prompt is used
as a fallback if no matching files are found.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-17 17:28:47 -04:00
parent 4e0a17429b
commit c11208199d

View File

@@ -680,6 +680,63 @@ def _update_fabric_fw(password: str, ip: str, iom: str, fw_path: str) -> bool:
return True
# ── Firmware file selection helper ────────────────────────────────────────────
def _prompt_fw_file(label: str) -> str:
"""
Scan the current working directory for firmware files and let the user
pick one by number, or enter a custom path as the last option.
Files are sorted most-recently-modified first.
"""
cwd = os.getcwd()
FW_EXTS = {".bin", ".img", ".fw", ".hex", ".zip", ".tar", ".tgz", ".gz"}
try:
candidates = sorted(
[f for f in os.listdir(cwd)
if not f.startswith(".")
and os.path.isfile(os.path.join(cwd, f))
and os.path.splitext(f)[1].lower() in FW_EXTS],
key=lambda f: os.path.getmtime(os.path.join(cwd, f)),
reverse=True,
)
except OSError:
candidates = []
print()
if candidates:
info(f"Firmware files found in {cwd}:")
for i, fname in enumerate(candidates, 1):
sz = os.path.getsize(os.path.join(cwd, fname))
print(f" {_c(C.BOLD, str(i))} {fname} {_c(C.DIM, f'({sz // 1024} KB)')}")
custom_idx = len(candidates) + 1
print(f" {_c(C.BOLD, str(custom_idx))} Enter a custom file path")
print()
while True:
choice = prompt(f"Select {label} [1-{custom_idx}]")
if choice.isdigit():
idx = int(choice)
if 1 <= idx <= len(candidates):
path = os.path.join(cwd, candidates[idx - 1])
sz = os.path.getsize(path)
ok(f"Selected: {candidates[idx - 1]} ({sz // 1024} KB)")
return path
if idx == custom_idx:
break
warn(f"Please enter a number between 1 and {custom_idx}.")
else:
info(f"No firmware files found in {cwd}.")
# Manual path entry
while True:
path = prompt(f"Path to {label}")
if os.path.isfile(path):
sz = os.path.getsize(path)
ok(f"File: {path} ({sz // 1024} KB)")
return path
warn(f"File not found: {path}")
# ── Firmware Update Workflow ──────────────────────────────────────────────────
def firmware_update_workflow():
"""
@@ -746,24 +803,10 @@ def firmware_update_workflow():
fabric_fw_path = ""
if update_iom:
print()
while True:
iom_fw_path = prompt("Path to IOM firmware file")
if os.path.isfile(iom_fw_path):
sz = os.path.getsize(iom_fw_path)
ok(f"File: {iom_fw_path} ({sz // 1024} KB)")
break
warn(f"File not found: {iom_fw_path}")
iom_fw_path = _prompt_fw_file("IOM firmware file")
if update_fabric:
print()
while True:
fabric_fw_path = prompt("Path to Fabric Card firmware file")
if os.path.isfile(fabric_fw_path):
sz = os.path.getsize(fabric_fw_path)
ok(f"File: {fabric_fw_path} ({sz // 1024} KB)")
break
warn(f"File not found: {fabric_fw_path}")
fabric_fw_path = _prompt_fw_file("Fabric Card firmware file")
print()
warn("For HA systems: update the passive IOM first.")