Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -643,7 +643,174 @@ def internal_error(error):
|
|
| 643 |
"error": "Internal server error",
|
| 644 |
"status": "error"
|
| 645 |
}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 646 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 647 |
if __name__ == '__main__':
|
| 648 |
logger.info("🚀 Starting Flask application...")
|
| 649 |
logger.info(f"📦 Essential Modules Available: {essential_modules_available}")
|
|
|
|
| 643 |
"error": "Internal server error",
|
| 644 |
"status": "error"
|
| 645 |
}), 500
|
| 646 |
+
import os
|
| 647 |
+
import sys
|
| 648 |
+
import importlib.util
|
| 649 |
+
import threading
|
| 650 |
+
import time
|
| 651 |
+
import logging
|
| 652 |
+
from pathlib import Path
|
| 653 |
|
| 654 |
+
logger = logging.getLogger(__name__)
|
| 655 |
+
|
| 656 |
+
def run_all_python_files():
|
| 657 |
+
"""
|
| 658 |
+
تشغيل جميع ملفات بايثون في المجلد الحالي
|
| 659 |
+
"""
|
| 660 |
+
current_dir = Path(__file__).parent
|
| 661 |
+
python_files = []
|
| 662 |
+
|
| 663 |
+
# البحث عن جميع ملفات .py في المجلد الحالي
|
| 664 |
+
for file_path in current_dir.glob("*.py"):
|
| 665 |
+
if file_path.name != "__init__.py" and file_path.name != Path(__file__).name:
|
| 666 |
+
python_files.append(file_path)
|
| 667 |
+
|
| 668 |
+
logger.info(f"📁 وجد {len(python_files)} ملف بايثون في المجلد الحالي")
|
| 669 |
+
|
| 670 |
+
# تشغيل الملفات
|
| 671 |
+
for file_path in python_files:
|
| 672 |
+
try:
|
| 673 |
+
run_python_file(file_path)
|
| 674 |
+
except Exception as e:
|
| 675 |
+
logger.error(f"❌ خطأ في تشغيل {file_path.name}: {e}")
|
| 676 |
+
|
| 677 |
+
def run_python_file(file_path):
|
| 678 |
+
"""
|
| 679 |
+
تشغيل ملف بايثون معين
|
| 680 |
+
"""
|
| 681 |
+
try:
|
| 682 |
+
file_name = file_path.name
|
| 683 |
+
logger.info(f"🚀 جاري تشغيل: {file_name}")
|
| 684 |
+
|
| 685 |
+
# تحميل المواصفات والوحدة
|
| 686 |
+
spec = importlib.util.spec_from_file_location(file_path.stem, file_path)
|
| 687 |
+
module = importlib.util.module_from_spec(spec)
|
| 688 |
+
|
| 689 |
+
# تشغيل الملف
|
| 690 |
+
spec.loader.exec_module(module)
|
| 691 |
+
|
| 692 |
+
logger.info(f"✅ تم تشغيل {file_name} بنجاح")
|
| 693 |
+
return module
|
| 694 |
+
|
| 695 |
+
except Exception as e:
|
| 696 |
+
logger.error(f"❌ فشل تشغيل {file_path.name}: {e}")
|
| 697 |
+
return None
|
| 698 |
+
|
| 699 |
+
def run_python_files_in_threads():
|
| 700 |
+
"""
|
| 701 |
+
تشغيل جميع ملفات بايثون في خيوط منفصلة
|
| 702 |
+
"""
|
| 703 |
+
current_dir = Path(__file__).parent
|
| 704 |
+
python_files = []
|
| 705 |
+
|
| 706 |
+
# البحث عن جميع ملفات .py في المجلد الحالي
|
| 707 |
+
for file_path in current_dir.glob("*.py"):
|
| 708 |
+
if file_path.name != "__init__.py" and file_path.name != Path(__file__).name:
|
| 709 |
+
python_files.append(file_path)
|
| 710 |
+
|
| 711 |
+
logger.info(f"🧵 تشغيل {len(python_files)} ملف في خيوط منفصلة")
|
| 712 |
+
|
| 713 |
+
threads = []
|
| 714 |
+
for file_path in python_files:
|
| 715 |
+
thread = threading.Thread(
|
| 716 |
+
target=run_python_file,
|
| 717 |
+
args=(file_path,),
|
| 718 |
+
name=f"Thread-{file_path.stem}",
|
| 719 |
+
daemon=True
|
| 720 |
+
)
|
| 721 |
+
thread.start()
|
| 722 |
+
threads.append(thread)
|
| 723 |
+
logger.info(f"🔧 بدأ تشغيل: {file_path.name}")
|
| 724 |
+
|
| 725 |
+
return threads
|
| 726 |
+
|
| 727 |
+
def run_python_files_with_main():
|
| 728 |
+
"""
|
| 729 |
+
تشغيل فقط الملفات التي تحتوي على دالة main()
|
| 730 |
+
"""
|
| 731 |
+
current_dir = Path(__file__).parent
|
| 732 |
+
files_with_main = []
|
| 733 |
+
|
| 734 |
+
for file_path in current_dir.glob("*.py"):
|
| 735 |
+
if file_path.name != "__init__.py" and file_path.name != Path(__file__).name:
|
| 736 |
+
try:
|
| 737 |
+
# فحص الملف لمعرفة إذا كان يحتوي على دالة main
|
| 738 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
| 739 |
+
content = f.read()
|
| 740 |
+
if 'def main(' in content or 'if __name__ == "__main__"' in content:
|
| 741 |
+
files_with_main.append(file_path)
|
| 742 |
+
except Exception as e:
|
| 743 |
+
logger.warning(f"⚠️ لا يمكن قراءة {file_path.name}: {e}")
|
| 744 |
+
|
| 745 |
+
logger.info(f"🎯 وجد {len(files_with_main)} ملف يحتوي على دالة main")
|
| 746 |
+
|
| 747 |
+
for file_path in files_with_main:
|
| 748 |
+
try:
|
| 749 |
+
module = run_python_file(file_path)
|
| 750 |
+
|
| 751 |
+
# إذا كان الملف يحتوي على دالة main، قم باستدعائها
|
| 752 |
+
if hasattr(module, 'main'):
|
| 753 |
+
logger.info(f"🔧 استدعاء main() في {file_path.name}")
|
| 754 |
+
module.main()
|
| 755 |
+
|
| 756 |
+
except Exception as e:
|
| 757 |
+
logger.error(f"❌ خطأ في استدعاء main() لـ {file_path.name}: {e}")
|
| 758 |
+
|
| 759 |
+
def run_specific_files(file_patterns):
|
| 760 |
+
"""
|
| 761 |
+
تشغيل ملفات محددة بناء على أنماط
|
| 762 |
+
"""
|
| 763 |
+
current_dir = Path(__file__).parent
|
| 764 |
+
matched_files = []
|
| 765 |
+
|
| 766 |
+
for pattern in file_patterns:
|
| 767 |
+
for file_path in current_dir.glob(pattern):
|
| 768 |
+
if file_path.is_file() and file_path.suffix == '.py':
|
| 769 |
+
matched_files.append(file_path)
|
| 770 |
+
|
| 771 |
+
logger.info(f"🎯 تشغيل {len(matched_files)} ملف مطابق للأنماط")
|
| 772 |
+
|
| 773 |
+
for file_path in matched_files:
|
| 774 |
+
run_python_file(file_path)
|
| 775 |
+
|
| 776 |
+
return matched_files
|
| 777 |
+
|
| 778 |
+
def monitor_and_restart_files():
|
| 779 |
+
"""
|
| 780 |
+
مراقبة الملفات وإعادة تشغيلها عند التعديل
|
| 781 |
+
"""
|
| 782 |
+
current_dir = Path(__file__).parent
|
| 783 |
+
file_timestamps = {}
|
| 784 |
+
|
| 785 |
+
while True:
|
| 786 |
+
try:
|
| 787 |
+
for file_path in current_dir.glob("*.py"):
|
| 788 |
+
if file_path.name != "__init__.py" and file_path.name != Path(__file__).name:
|
| 789 |
+
current_mtime = file_path.stat().st_mtime
|
| 790 |
+
|
| 791 |
+
if file_path not in file_timestamps:
|
| 792 |
+
# أول مرة نرى الملف، قم بتشغيله
|
| 793 |
+
file_timestamps[file_path] = current_mtime
|
| 794 |
+
threading.Thread(
|
| 795 |
+
target=run_python_file,
|
| 796 |
+
args=(file_path,),
|
| 797 |
+
daemon=True
|
| 798 |
+
).start()
|
| 799 |
+
elif current_mtime > file_timestamps[file_path]:
|
| 800 |
+
# الملف تم تعديله، أعد تشغيله
|
| 801 |
+
logger.info(f"🔄 إعادة تشغيل {file_path.name} (تم التعديل)")
|
| 802 |
+
file_timestamps[file_path] = current_mtime
|
| 803 |
+
threading.Thread(
|
| 804 |
+
target=run_python_file,
|
| 805 |
+
args=(file_path,),
|
| 806 |
+
daemon=True
|
| 807 |
+
).start()
|
| 808 |
+
|
| 809 |
+
time.sleep(5) # فحص كل 5 ثواني
|
| 810 |
+
|
| 811 |
+
except Exception as e:
|
| 812 |
+
logger.error(f"❌ خطأ في المراقبة: {e}")
|
| 813 |
+
time.sleep(10)
|
| 814 |
if __name__ == '__main__':
|
| 815 |
logger.info("🚀 Starting Flask application...")
|
| 816 |
logger.info(f"📦 Essential Modules Available: {essential_modules_available}")
|