feat(btreset): python -> bash

This commit is contained in:
2025-11-22 22:03:21 +08:00
parent 0983d5b058
commit 374a3ef071

View File

@@ -7,34 +7,21 @@
... ...
}: }:
let let
btreset = pkgs.writeScriptBin "btreset" '' btreset = pkgs.writeShellScriptBin "btreset" ''
#!${lib.getExe pkgs.python3} LOCKFILE="/tmp/.btreseted"
import subprocess
import os
import sys
SYM="BT" SYM="BT"
def action(line: str) -> bool: if [ -f "$LOCKFILE" ] && [ "$1" != "-f" ]; then
if line.find(SYM) == -1: exit 0
return False fi
temp = line.split(" ")
bus = temp[1]
device = temp[3][:-1]
subprocess.run(["${lib.getExe' pkgs.usbutils "usbreset"}", f"{bus}/{device}"])
return True
if __name__ == "__main__": ${lib.getExe' pkgs.usbutils "lsusb"} | grep "$SYM" | while read -r line; do
if os.path.exists("/tmp/.btreseted") and len(sys.argv) == 1 and "-f" not in sys.argv[1:]: bus=$(echo "$line" | awk '{print $2}')
exit(0) dev=$(echo "$line" | awk '{print $4}' | tr -d ':')
res_byte = subprocess.check_output("/run/current-system/sw/bin/lsusb") ${lib.getExe' pkgs.usbutils "usbreset"} "$bus/$dev"
res = res_byte.decode()
lst = res.split("\n")
if any(tuple(map(action, lst))): touch "$LOCKFILE"
with open("/tmp/.btreseted", "w"): done
...
''; '';
in in
{ {