Update big_maniger_gita.py

This commit is contained in:
2025-08-25 21:11:50 +00:00
parent 4591bd63f1
commit fd0550139d

View File

@@ -1,15 +1,12 @@
import os,shutil import os, shutil
import time import time
import subprocess import subprocess
import json import json
from datetime import datetime from datetime import datetime
import psutil import psutil
import stat
# اطلاعات Gitea شما # اطلاعات Gitea شما
# نام کاربری: pythonkoft
# توکن شخصی: 2f135dee41b069f92413dd3f234e12cb8c20a96a
# آدرس ریپازیتوری: http://188.245.173.247:3080/pythonkoft/ea_shadow.git
REPO_DIR = "bot_ea_db" REPO_DIR = "bot_ea_db"
GIT_REPO = "http://pythonkoft:2f135dee41b069f92413dd3f234e12cb8c20a96a@188.245.173.247:3080/pythonkoft/ea_shadow.git" GIT_REPO = "http://pythonkoft:2f135dee41b069f92413dd3f234e12cb8c20a96a@188.245.173.247:3080/pythonkoft/ea_shadow.git"
LOG_FILE = "log.json" LOG_FILE = "log.json"
@@ -27,11 +24,19 @@ def write_log(event, message):
with open(LOG_FILE, "w", encoding="utf-8") as f: with open(LOG_FILE, "w", encoding="utf-8") as f:
json.dump([log_entry], f, ensure_ascii=False, indent=2) json.dump([log_entry], f, ensure_ascii=False, indent=2)
else: else:
with open(LOG_FILE, "r+", encoding="utf-8") as f: try:
logs = json.load(f) with open(LOG_FILE, "r+", encoding="utf-8") as f:
logs.append(log_entry) try:
f.seek(0) logs = json.load(f)
json.dump(logs, f, ensure_ascii=False, indent=2) except json.JSONDecodeError:
f.seek(0)
f.truncate()
logs = []
logs.append(log_entry)
f.seek(0)
json.dump(logs, f, ensure_ascii=False, indent=2)
except Exception as e:
print("خطا در نوشتن لاگ:", e)
def kill_old_process(): def kill_old_process():
if not os.path.exists(PID_FILE): if not os.path.exists(PID_FILE):
@@ -80,7 +85,62 @@ def load_last_commit():
return f.read().strip() return f.read().strip()
return None return None
def clean_gitconfig():
home_dir = os.path.expanduser("~")
gitconfig_path = os.path.join(home_dir, ".gitconfig")
if not os.path.exists(gitconfig_path):
return
try:
with open(gitconfig_path, "r", encoding="utf-8") as f:
lines = f.readlines()
new_lines = []
skip_next = False
for line in lines:
if line.strip().startswith('[credential "http://188.245.173.247:3080"]'):
skip_next = True
continue
if skip_next:
if line.strip().startswith("provider"):
skip_next = False
continue
new_lines.append(line)
# رفع مشکل Read-Only ویندوز
if not os.access(gitconfig_path, os.W_OK):
os.chmod(gitconfig_path, stat.S_IWRITE)
with open(gitconfig_path, "w", encoding="utf-8") as f:
f.writelines(new_lines)
write_log("cleanup", ".gitconfig پاکسازی شد.")
except Exception as e:
write_log("error", f"خطا در پاکسازی .gitconfig: {e}")
def clear_git_credentials():
# حذف credential helper
subprocess.run(["git", "config", "--global", "--unset", "credential.helper"], capture_output=True)
# پاک کردن فایل‌هایی که اسمشون git داره توی Home
home_dir = os.path.expanduser("~")
for fname in os.listdir(home_dir):
if fname.lower().startswith(".git"):
try:
file_path = os.path.join(home_dir, fname)
if not os.access(file_path, os.W_OK):
os.chmod(file_path, stat.S_IWRITE)
os.remove(file_path)
write_log("cleanup", f"فایل {fname} از Home پاک شد.")
except Exception as e:
write_log("error", f"پاک کردن {fname} نشد: {e}")
def clone_or_update_repo(): def clone_or_update_repo():
clean_gitconfig()
clear_git_credentials()
if os.path.exists(REPO_DIR): if os.path.exists(REPO_DIR):
try: try:
result = subprocess.run( result = subprocess.run(
@@ -107,7 +167,7 @@ def clone_or_update_repo():
def run_main_bat(): def run_main_bat():
try: try:
shutil.copy(os.path.join(os.getcwd(),'.env'),os.path.join(os.getcwd(),'bot_ea_db')) shutil.copy(os.path.join(os.getcwd(), '.env'), os.path.join(os.getcwd(), 'bot_ea_db'))
process = subprocess.Popen( process = subprocess.Popen(
["cmd.exe", "/c", "main.bat"], ["cmd.exe", "/c", "main.bat"],
cwd=REPO_DIR, cwd=REPO_DIR,
@@ -123,27 +183,53 @@ def run_main_bat():
def main_loop(): def main_loop():
while True: while True:
print('1') try:
clone_or_update_repo() print('1')
print('2') clone_or_update_repo()
latest_commit = get_latest_commit() print('2')
print('3') latest_commit = get_latest_commit()
saved_commit = load_last_commit() print('3')
print('4') saved_commit = load_last_commit()
if latest_commit and latest_commit != saved_commit: print('4')
print('5') if latest_commit and latest_commit != saved_commit:
write_log("update", f"کمیت جدید: {latest_commit}") print('5')
print('6') write_log("update", f"کمیت جدید: {latest_commit}")
kill_old_process() print('6')
print('7') kill_old_process()
run_main_bat() print('7')
print('8') run_main_bat()
save_last_commit(latest_commit) print('8')
else: save_last_commit(latest_commit)
print(("check", "تغییری نبود.")) else:
write_log("check", "تغییری نبود.") print(("check", "تغییری نبود."))
print(60) write_log("check", "تغییری نبود.")
time.sleep(60) print(60)
time.sleep(60)
except Exception as e:
print('EROR MSG :>>> ', e)
def clean_initial_files():
# پاک کردن log.json
if os.path.exists(LOG_FILE):
try:
os.remove(LOG_FILE)
print(f"{LOG_FILE} پاک شد.")
except Exception as e:
print(f"پاک کردن {LOG_FILE} نشد:", e)
# پاک کردن PID_FILE
if os.path.exists(PID_FILE):
try:
os.remove(PID_FILE)
print(f"{PID_FILE} پاک شد.")
except Exception as e:
print(f"پاک کردن {PID_FILE} نشد:", e)
if os.path.exists('last_commit.txt'):
try:
os.remove('last_commit.txt')
print(f"{PID_FILE} پاک شد.")
except Exception as e:
print(f"پاک کردن {PID_FILE} نشد:", e)
if __name__ == "__main__": if __name__ == "__main__":
# پاکسازی اولیه قبل از حلقه
clean_initial_files()
main_loop() main_loop()