Просмотр исходного кода

fix: streamline script execution by removing temporary file usage in update function

Manuel 6 месяцев назад
Родитель
Сommit
987c3cd70c
1 измененных файлов с 12 добавлено и 19 удалено
  1. 12 19
      utils/updateSquidStats.py

+ 12 - 19
utils/updateSquidStats.py

@@ -1,7 +1,6 @@
 import os
 import shutil
 import subprocess
-import tempfile
 
 import requests
 from dotenv import load_dotenv
@@ -22,27 +21,21 @@ def updateSquidStats():
         if proxy_url:
             proxies = {"http": proxy_url, "https": proxy_url}
         try:
-            with tempfile.NamedTemporaryFile(
-                delete=False, mode="w+b", suffix=".sh"
-            ) as tmp_script:
-                tmp_script_path = tmp_script.name
-                response = requests.get(
-                    "https://github.com/kaelthasmanu/SquidStats/releases/download/2.3.1/install.sh",
-                    proxies=proxies,
-                    timeout=30,
-                )
-                response.raise_for_status()
-                tmp_script.write(response.content)
-            subprocess.run([shutil.which("chmod") or "chmod", "+x", tmp_script_path])  # noqa: S603
-            subprocess.run(
-                [shutil.which("bash") or "bash", tmp_script_path, "--update"], env=env
-            )  # noqa: S603
-            os.unlink(tmp_script_path)
+            response = requests.get(
+                "https://github.com/kaelthasmanu/SquidStats/releases/download/2.3.1/install.sh",
+                proxies=proxies,
+                timeout=30,
+            )
+            response.raise_for_status()
+            sh_bin = "/bin/sh" if os.path.exists("/bin/sh") else shutil.which("sh")
+            if not sh_bin:
+                logger.error("sh no encontrado en el sistema")
+                return False
+            args = [sh_bin, "-s", "--update"]
+            subprocess.run(args, input=response.content, env=env, check=True)  # noqa: S603
             return True
         except Exception:
             logger.exception("Error descargando el script de actualización")
-            if "tmp_script_path" in locals() and os.path.exists(tmp_script_path):
-                os.unlink(tmp_script_path)
             return False
 
     except Exception: