suphe commited on
Commit
559359f
·
verified ·
1 Parent(s): 7e0990c

Create setup/updater.py

Browse files
Files changed (1) hide show
  1. setup/updater.py +71 -0
setup/updater.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ setup/updater.py -- سكربت مستقل (مو أمر بوت)
4
+
5
+ يشتغل قبل تشغيل البوت. ما يستورد telethon ولا tython.
6
+ الاستخدام:
7
+ python3 setup/updater.py <requirements_قديم> <requirements_جديد>
8
+ python3 setup/updater.py <requirements>
9
+ الأول: ينصّب بس المكتبات الموجودة بالجديد وناقصة من القديم.
10
+ الثاني: ينصّب كل شي بالملف.
11
+ مهم: يطلع دائماً بكود 0 حتى لو صار خطأ، حتى ما يوقف التنصيب.
12
+ """
13
+ import os
14
+ import re
15
+ import subprocess
16
+ import sys
17
+
18
+
19
+ def pkg_name(line: str) -> str:
20
+ line = line.split("#", 1)[0].strip()
21
+ if not line or line.startswith("-"):
22
+ return ""
23
+ return re.split(r"[<>=!~\[;\s@]", line, maxsplit=1)[0].lower().replace("_", "-")
24
+
25
+
26
+ def read_reqs(path: str) -> dict:
27
+ out = {}
28
+ if not os.path.isfile(path):
29
+ return out
30
+ with open(path, encoding="utf-8", errors="ignore") as f:
31
+ for raw in f:
32
+ name = pkg_name(raw)
33
+ if name:
34
+ out[name] = raw.split("#", 1)[0].strip()
35
+ return out
36
+
37
+
38
+ def pip_install(specs: list) -> None:
39
+ for spec in specs:
40
+ cmd = [sys.executable, "-m", "pip", "install", "--no-cache-dir", spec]
41
+ try:
42
+ r = subprocess.run(cmd, capture_output=True, text=True, timeout=600)
43
+ print(("OK " if r.returncode == 0 else "FAIL ") + spec)
44
+ except Exception as e: # noqa: BLE001
45
+ print(f"FAIL {spec} ({e})")
46
+
47
+
48
+ def main() -> None:
49
+ args = sys.argv[1:]
50
+ if not args:
51
+ print("updater: لا توجد ملفات requirements .. تخطي")
52
+ return
53
+ if len(args) == 1:
54
+ new = read_reqs(args[0])
55
+ todo = list(new.values())
56
+ else:
57
+ old, new = read_reqs(args[0]), read_reqs(args[1])
58
+ todo = [spec for name, spec in new.items() if name not in old]
59
+ if not todo:
60
+ print("updater: لا توجد مكتبات جديدة .. تخطي")
61
+ return
62
+ print(f"updater: تنصيب {len(todo)} مكتبة ناقصة")
63
+ pip_install(todo)
64
+
65
+
66
+ if __name__ == "__main__":
67
+ try:
68
+ main()
69
+ except Exception as e: # noqa: BLE001
70
+ print(f"updater: خطأ تم تجاهله: {e}")
71
+ sys.exit(0)