This commit is contained in:
2025-06-09 19:34:56 +09:00
parent 02a10ca391
commit cdc4e209c7
15 changed files with 490 additions and 1 deletions

View File

@@ -0,0 +1,58 @@
import subprocess
import yaml
from datetime import datetime
import logging
from pathlib import Path
# Настройка логирования
log_dir = Path("logs")
log_dir.mkdir(exist_ok=True)
log_file = log_dir / f"commands_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log"
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler(log_file, encoding='utf-8'),
logging.StreamHandler()
]
)
def run_command(command_str: str) -> tuple[str, str, int]:
"""Выполняет команду и возвращает stdout, stderr, return_code"""
try:
result = subprocess.run(command_str, shell=True, capture_output=True, text=True)
return result.stdout, result.stderr, result.returncode
except Exception as e:
return "", str(e), -1
def main(config_path="commands.yaml"):
# Загрузка конфигурации
try:
with open(config_path, encoding="utf-8") as f:
config = yaml.safe_load(f)
except Exception as e:
logging.error(f"Ошибка при загрузке YAML: {e}")
return
for item in config.get("commands", []):
name = item.get("name", "Unnamed")
command = item.get("command")
logging.info(f"⏳ Выполняется команда: {name}{command}")
stdout, stderr, code = run_command(command)
if code == 0:
logging.info(f"✅ Успешно: {name}")
else:
logging.error(f"❌ Ошибка ({code}): {name}")
logging.info(f"🔎 STDOUT:\n{stdout.strip()}")
if stderr.strip():
logging.warning(f"⚠️ STDERR:\n{stderr.strip()}")
logging.info("-" * 80)
logging.info("📝 Все команды завершены. Логи: %s", log_file)
if __name__ == "__main__":
main()