| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- 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_exists(path):
- """Vérifie l'existence d'un fichier via sudo si non accessible directement."""
- if os.path.exists(path):
- return True
- result = subprocess.run(
- ["sudo", "stat", path],
- capture_output=True,
- )
- return result.returncode == 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 []
|