From e2f65b1ba753de3d3995d7e3b755738dba461fbe Mon Sep 17 00:00:00 2001 From: Jules Date: Tue, 23 Jun 2026 08:37:41 +0200 Subject: [PATCH] USB rebind via sudo tee (permission sysfs requiert root) Co-Authored-By: Claude Opus 4.6 --- backend/main.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/backend/main.py b/backend/main.py index a6d0dc3..965a9eb 100644 --- a/backend/main.py +++ b/backend/main.py @@ -124,8 +124,9 @@ def _usb_reset_canon(): def _usb_rebind_canon(): - """Unbind/rebind USB Canon — simule un debranchement/rebranchement physique.""" - import glob + """Unbind/rebind USB Canon — simule un debranchement/rebranchement physique. + Necessite sudoers NOPASSWD pour tee sur /sys/bus/usb/drivers/usb/{un,}bind.""" + import glob, subprocess, time try: for f in glob.glob("/sys/bus/usb/devices/*/idVendor"): if open(f).read().strip() == "04a9": @@ -133,12 +134,15 @@ def _usb_rebind_canon(): __import__("os.path", fromlist=["dirname"]).dirname(f) ) log.info(f"USB rebind Canon: unbind {dev_id}") - with open("/sys/bus/usb/drivers/usb/unbind", "w") as fh: - fh.write(dev_id) - import time + subprocess.run( + ["sudo", "tee", "/sys/bus/usb/drivers/usb/unbind"], + input=dev_id.encode(), stdout=subprocess.DEVNULL, timeout=5, + ) time.sleep(2) - with open("/sys/bus/usb/drivers/usb/bind", "w") as fh: - fh.write(dev_id) + subprocess.run( + ["sudo", "tee", "/sys/bus/usb/drivers/usb/bind"], + input=dev_id.encode(), stdout=subprocess.DEVNULL, timeout=5, + ) log.info(f"USB rebind Canon: bind {dev_id}") time.sleep(3) return True