sync
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
# core/commands/rm.py
|
||||
import fnmatch
|
||||
import posixpath
|
||||
from core.connection import BuzzerError
|
||||
from core.commands.ls import get_file_tree
|
||||
|
||||
@@ -21,10 +23,37 @@ def _try_rm(conn, path, is_dir=False):
|
||||
print(f" ❌ Fehler bei {path}: {e}")
|
||||
|
||||
def execute(conn, path: str, recursive: bool = False):
|
||||
"""Löscht eine Datei oder ein Verzeichnis."""
|
||||
"""Löscht eine Datei, ein Verzeichnis oder löst Wildcards (*) auf."""
|
||||
|
||||
# 1. Wildcard-Behandlung (z.B. /lfs/a/* oder *.wav)
|
||||
if '*' in path or '?' in path:
|
||||
dirname, pattern = posixpath.split(path)
|
||||
if not dirname:
|
||||
dirname = "/"
|
||||
|
||||
print(f"Suche nach Dateien passend zu '{pattern}' in '{dirname}'...")
|
||||
tree = get_file_tree(conn, target_path=dirname, recursive=False)
|
||||
|
||||
# Fehler beim Verzeichnis-Lesen abfangen
|
||||
if len(tree) == 1 and tree[0].get("type") == "E":
|
||||
print(f"❌ Verzeichnis '{dirname}' nicht gefunden.")
|
||||
return
|
||||
|
||||
# Filtern mit fnmatch (funktioniert wie in der Linux-Shell)
|
||||
matches = [node for node in tree if node.get("type") == "F" and fnmatch.fnmatch(node["name"], pattern)]
|
||||
|
||||
if not matches:
|
||||
print(f"Keine passenden Dateien für '{path}' gefunden.")
|
||||
return
|
||||
|
||||
for match in matches:
|
||||
_try_rm(conn, match["path"], is_dir=False)
|
||||
|
||||
return # Fertig mit Wildcard-Löschen
|
||||
|
||||
# 2. Rekursives Löschen (-r)
|
||||
if recursive:
|
||||
print(f"Sammle Dateibaum für rekursives Löschen von '{path}'...")
|
||||
# Lade den kompletten Baum ab diesem Pfad
|
||||
tree = get_file_tree(conn, target_path=path, recursive=True)
|
||||
|
||||
if len(tree) == 1 and tree[0].get("type") == "E":
|
||||
@@ -36,7 +65,7 @@ def execute(conn, path: str, recursive: bool = False):
|
||||
else:
|
||||
_delete_recursive(conn, tree)
|
||||
|
||||
# Am Ende das eigentliche Ziel löschen (die Datei oder den nun leeren Ordner)
|
||||
# 3. Standard-Löschen (Einzeldatei oder am Ende der Rekursion der leere Ordner)
|
||||
try:
|
||||
conn.send_command(f"rm {path}")
|
||||
print(f"🗑️ '{path}' erfolgreich gelöscht.")
|
||||
|
||||
Reference in New Issue
Block a user