Spaces:
Running
Running
| import subprocess | |
| import sys | |
| def install_packages(): | |
| print("تحديث pip...") | |
| subprocess.check_call([sys.executable, "-m", "pip", "install", "--upgrade", "pip", "--user"]) | |
| print("تثبيت الحزم من requirements.txt...") | |
| subprocess.check_call([sys.executable, "-m", "pip", "install", "--user", "-r", "requirements.txt"]) | |
| if __name__ == "__main__": | |
| try: | |
| install_packages() | |
| print("تم تثبيت الحزم بنجاح.") | |
| except subprocess.CalledProcessError as e: | |
| print(f"حدث خطأ أثناء التثبيت: {e}") | |
| import subprocess | |
| import sys | |
| from pathlib import Path | |
| import platform | |
| from datetime import datetime | |
| try: | |
| from colorama import init, Fore | |
| except ImportError: | |
| subprocess.check_call([sys.executable, "-m", "pip", "install", "colorama", "--user"]) | |
| from colorama import init, Fore | |
| init(autoreset=True) | |
| LOG_FILE = "install_log.txt" | |
| def log(message): | |
| with open(LOG_FILE, "a", encoding="utf-8") as f: | |
| f.write(f"[{datetime.now()}] {message}\n") | |
| def print_and_log(message, color=Fore.WHITE): | |
| print(color + message) | |
| log(message) | |
| def install_packages(): | |
| try: | |
| print_and_log("📢 نظام التشغيل: " + platform.system(), Fore.CYAN) | |
| print_and_log("🔄 جاري تحديث pip...", Fore.YELLOW) | |
| subprocess.check_call([sys.executable, "-m", "pip", "install", "--upgrade", "pip", "--user"]) | |
| requirements_path = Path("requirements.txt") | |
| if not requirements_path.exists(): | |
| print_and_log("⚠️ ملف requirements.txt غير موجود!", Fore.RED) | |
| return | |
| print_and_log("📦 جاري تثبيت الحزم من requirements.txt...", Fore.YELLOW) | |
| subprocess.check_call([sys.executable, "-m", "pip", "install", "--user", "-r", str(requirements_path)]) | |
| print_and_log("✅ تم تثبيت الحزم بنجاح.", Fore.GREEN) | |
| except subprocess.CalledProcessError as e: | |
| print_and_log(f"❌ حدث خطأ أثناء التثبيت: {e}", Fore.RED) | |
| except Exception as e: | |
| print_and_log(f"⚠️ خطأ غير متوقع: {e}", Fore.RED) | |
| if __name__ == "__main__": | |
| install_packages() | |
| import subprocess | |
| import sys | |
| import os | |
| import socket | |
| from pathlib import Path | |
| from datetime import datetime | |
| import platform | |
| try: | |
| from colorama import init, Fore, Style | |
| except ImportError: | |
| subprocess.call([sys.executable, "-m", "pip", "install", "colorama", "--user"]) | |
| from colorama import init, Fore, Style | |
| init(autoreset=True) | |
| LOG_FILE = "install_log.txt" | |
| VENV_DIR = Path("env") | |
| REQUIREMENTS_FILE = Path("requirements.txt") | |
| def log(message): | |
| with open(LOG_FILE, "a", encoding="utf-8") as f: | |
| f.write(f"[{datetime.now()}] {message}\n") | |
| def print_and_log(message, color=Fore.WHITE): | |
| print(color + message) | |
| log(message) | |
| def check_internet(host="8.8.8.8", port=53, timeout=3): | |
| try: | |
| socket.setdefaulttimeout(timeout) | |
| socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port)) | |
| return True | |
| except Exception: | |
| return False | |
| def create_virtual_env(): | |
| if not VENV_DIR.exists(): | |
| print_and_log("📦 إنشاء بيئة افتراضية...", Fore.YELLOW) | |
| subprocess.check_call([sys.executable, "-m", "venv", str(VENV_DIR)]) | |
| else: | |
| print_and_log("✅ البيئة الافتراضية موجودة مسبقًا.", Fore.CYAN) | |
| def activate_venv_command(): | |
| if platform.system() == "Windows": | |
| return str(VENV_DIR / "Scripts" / "python.exe") | |
| else: | |
| return str(VENV_DIR / "bin" / "python") | |
| def install_requirements(python_exec): | |
| if not REQUIREMENTS_FILE.exists(): | |
| print_and_log("❗ ملف requirements.txt غير موجود!", Fore.RED) | |
| return | |
| print_and_log("📥 تثبيت الحزم المطلوبة...", Fore.YELLOW) | |
| subprocess.check_call([python_exec, "-m", "pip", "install", "--upgrade", "pip"]) | |
| subprocess.check_call([python_exec, "-m", "pip", "install", "-r", str(REQUIREMENTS_FILE)]) | |
| def run_post_install_script(): | |
| script = Path("post_install.py") | |
| if script.exists(): | |
| print_and_log("🚀 تشغيل سكربت post_install.py...", Fore.GREEN) | |
| subprocess.call([activate_venv_command(), str(script)]) | |
| else: | |
| print_and_log("ℹ️ لا يوجد سكربت إضافي للتشغيل.", Fore.BLUE) | |
| def main(): | |
| print_and_log("⚙️ تشغيل Smart Installer...", Fore.MAGENTA) | |
| print_and_log(f"🖥️ نظام التشغيل: {platform.system()} {platform.release()}", Fore.CYAN) | |
| if not check_internet(): | |
| print_and_log("❌ لا يوجد اتصال بالإنترنت!", Fore.RED) | |
| return | |
| try: | |
| create_virtual_env() | |
| python_exec = activate_venv_command() | |
| install_requirements(python_exec) | |
| run_post_install_script() | |
| print_and_log("✅ تم التثبيت بنجاح!", Fore.GREEN) | |
| except subprocess.CalledProcessError as e: | |
| print_and_log(f"💥 خطأ أثناء تنفيذ أمر: {e}", Fore.RED) | |
| except Exception as ex: | |
| print_and_log(f"🚨 استثناء غير متوقع: {ex}", Fore.RED) | |
| if __name__ == "__main__": | |
| main() | |