| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import os
- import subprocess
- def sudo_getsize(path):
- """Retourne la taille d'un fichier via sudo stat si non accessible directement."""
- try:
- return os.path.getsize(path)
- except OSError:
- result = subprocess.run(
- ["sudo", "stat", "--format=%s", path],
- capture_output=True, text=True,
- )
- if result.returncode == 0:
- try:
- return int(result.stdout.strip())
- except ValueError:
- pass
- return 0
- def sudo_getmtime(path):
- """Retourne le mtime d'un fichier via sudo stat si non accessible directement."""
- try:
- return os.path.getmtime(path)
- except OSError:
- result = subprocess.run(
- ["sudo", "stat", "--format=%Y", path],
- capture_output=True, text=True,
- )
- if result.returncode == 0:
- try:
- return float(result.stdout.strip())
- except ValueError:
- pass
- return 0.0
- def sudo_listdir(directory):
- """Liste les fichiers d'un répertoire via sudo find si non accessible directement."""
- try:
- return os.listdir(directory)
- except OSError:
- result = subprocess.run(
- ["sudo", "find", directory, "-maxdepth", "1", "-mindepth", "1",
- "-printf", "%f\n"],
- capture_output=True, text=True,
- )
- if result.returncode == 0:
- return [f for f in result.stdout.strip().split("\n") if f]
- return []
|