File size: 5,246 Bytes
498f51d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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()