|
|
#!/bin/bash |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GREEN='\033[0;32m' |
|
|
BLUE='\033[0;34m' |
|
|
YELLOW='\033[1;33m' |
|
|
RED='\033[0;31m' |
|
|
CYAN='\033[0;36m' |
|
|
NC='\033[0m' |
|
|
|
|
|
|
|
|
LOG_FILE="logs/maintenance.log" |
|
|
DATA_DIR="data" |
|
|
LOGS_DIR="logs" |
|
|
|
|
|
|
|
|
print_info() { |
|
|
echo -e "${BLUE}[INFO]${NC} $1" |
|
|
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [INFO] $1" >> "$LOG_FILE" |
|
|
} |
|
|
|
|
|
print_success() { |
|
|
echo -e "${GREEN}[SUCCESS]${NC} $1" |
|
|
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [SUCCESS] $1" >> "$LOG_FILE" |
|
|
} |
|
|
|
|
|
print_warning() { |
|
|
echo -e "${YELLOW}[WARNING]${NC} $1" |
|
|
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [WARNING] $1" >> "$LOG_FILE" |
|
|
} |
|
|
|
|
|
print_error() { |
|
|
echo -e "${RED}[ERROR]${NC} $1" |
|
|
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [ERROR] $1" >> "$LOG_FILE" |
|
|
} |
|
|
|
|
|
print_header() { |
|
|
echo "" |
|
|
echo "==============================================" |
|
|
echo " نظام الحارس التلقائي - الصيانة" |
|
|
echo "==============================================" |
|
|
echo "" |
|
|
} |
|
|
|
|
|
|
|
|
mkdir -p "$(dirname "$LOG_FILE")" "$DATA_DIR" "$LOGS_DIR" |
|
|
|
|
|
|
|
|
clean_pycache() { |
|
|
print_info "جاري تنظيف ملفات Python المؤقتة..." |
|
|
|
|
|
local count=0 |
|
|
|
|
|
|
|
|
while IFS= read -r -d '' dir; do |
|
|
rm -rf "$dir" |
|
|
print_info "حُذف: $dir" |
|
|
((count++)) |
|
|
done < <(find . -type d -name "__pycache__" -print0 2>/dev/null) |
|
|
|
|
|
|
|
|
while IFS= read -r -d '' file; do |
|
|
rm -f "$file" |
|
|
((count++)) |
|
|
done < <(find . -name "*.pyc" -print0 2>/dev/null) |
|
|
|
|
|
|
|
|
while IFS= read -r -d '' file; do |
|
|
rm -f "$file" |
|
|
((count++)) |
|
|
done < <(find . -name "*.pyo" -print0 2>/dev/null) |
|
|
|
|
|
|
|
|
rm -rf .pytest_cache/ 2>/dev/null |
|
|
((count++)) |
|
|
|
|
|
|
|
|
rm -rf .hypothesis/ 2>/dev/null |
|
|
((count++)) |
|
|
|
|
|
print_success "تم تنظيف $count عنصر" |
|
|
} |
|
|
|
|
|
|
|
|
clean_logs() { |
|
|
print_info "جاري تنظيف السجلات القديمة (أقدم من 14 يوماً)..." |
|
|
|
|
|
local count=0 |
|
|
local size_freed=0 |
|
|
|
|
|
|
|
|
while IFS= read -r -d '' file; do |
|
|
local file_size=$(stat -f%z "$file" 2>/dev/null || stat -c%s "$file" 2>/dev/null || echo 0) |
|
|
size_freed=$((size_freed + file_size)) |
|
|
rm -f "$file" |
|
|
print_info "حُذف: $(basename "$file")" |
|
|
((count++)) |
|
|
done < <(find "$LOGS_DIR" -name "*.log" -mtime +14 -print0 2>/dev/null) |
|
|
|
|
|
if [ $count -gt 0 ]; then |
|
|
print_success "حُذفت $count ملف سجل ($(echo "scale=2; $size_freed/1048576" | bc) MB)" |
|
|
else |
|
|
print_info "لا توجد ملفات سجل قديمة للحذف" |
|
|
fi |
|
|
} |
|
|
|
|
|
|
|
|
clean_tests() { |
|
|
print_info "جاري تنظيف ملفات الاختبار..." |
|
|
|
|
|
|
|
|
rm -rf htmlcov/ .coverage .coverage.* 2>/dev/null |
|
|
local count=1 |
|
|
|
|
|
|
|
|
rm -rf pytest-results/ 2>/dev/null || true |
|
|
|
|
|
print_success "تم تنظيف ملفات الاختبار" |
|
|
} |
|
|
|
|
|
|
|
|
clean_build() { |
|
|
print_info "جاري تنظيف ملفات البناء..." |
|
|
|
|
|
|
|
|
rm -rf build/ dist/ *.egg-info/ 2>/dev/null |
|
|
local count=3 |
|
|
|
|
|
|
|
|
rm -rf *.egg 2>/dev/null |
|
|
|
|
|
|
|
|
rm -rf *.whl 2>/dev/null |
|
|
|
|
|
print_success "تم تنظيف ملفات البناء" |
|
|
} |
|
|
|
|
|
|
|
|
clean_cache() { |
|
|
print_info "جاري تنظيف ملفات التخزين المؤقت..." |
|
|
|
|
|
|
|
|
pip cache purge 2>/dev/null || true |
|
|
|
|
|
|
|
|
rm -rf ~/.cache/pip 2>/dev/null || true |
|
|
|
|
|
|
|
|
rm -rf .mypy_cache/ 2>/dev/null || true |
|
|
|
|
|
|
|
|
rm -rf .ruff_cache/ 2>/dev/null || true |
|
|
|
|
|
print_success "تم تنظيف ملفات التخزين المؤقت" |
|
|
} |
|
|
|
|
|
|
|
|
check_disk_space() { |
|
|
print_info "جاري التحقق من مساحة القرص..." |
|
|
|
|
|
local disk_usage=$(df -h . | tail -1 | awk '{print $5}' | sed 's/%//') |
|
|
local disk_available=$(df -h . | tail -1 | awk '{print $4}') |
|
|
|
|
|
echo "" |
|
|
echo "معلومات مساحة القرص:" |
|
|
echo "--------------------" |
|
|
df -h . | tail -1 |
|
|
echo "" |
|
|
|
|
|
if [ "$disk_usage" -gt 80 ]; then |
|
|
print_warning "تحذير: استخدام القرص مرتفع ($disk_usage%)" |
|
|
print_info "المساحة المتاحة: $disk_available" |
|
|
else |
|
|
print_success "مساحة القرص كافية (사용량: $disk_usage%)" |
|
|
fi |
|
|
} |
|
|
|
|
|
|
|
|
check_git_status() { |
|
|
print_info "جاري التحقق من حالة Git..." |
|
|
|
|
|
|
|
|
if ! command -v git &> /dev/null; then |
|
|
print_warning "Git غير مثبت" |
|
|
return |
|
|
fi |
|
|
|
|
|
|
|
|
cd "$(dirname "$0")/.." 2>/dev/null |
|
|
|
|
|
if [ -d ".git" ]; then |
|
|
|
|
|
echo "" |
|
|
echo "الفروع المحلية:" |
|
|
git branch --color=never 2>/dev/null | head -5 |
|
|
|
|
|
|
|
|
local status=$(git status --short 2>/dev/null | wc -l) |
|
|
if [ "$status" -gt 0 ]; then |
|
|
print_warning "توجد تغييرات غير ملتزم بها ($status ملف)" |
|
|
else |
|
|
print_success "المستودع نظيف" |
|
|
fi |
|
|
|
|
|
|
|
|
git fetch origin 2>/dev/null |
|
|
|
|
|
local behind=$(git rev-list --count HEAD..origin/main 2>/dev/null || echo 0) |
|
|
if [ "$behind" -gt 0 ]; then |
|
|
print_info "يوجد تحديث متاح ($behind commits behind)" |
|
|
else |
|
|
print_success "المستودع محدث" |
|
|
fi |
|
|
else |
|
|
print_info "المستودع ليس Git" |
|
|
fi |
|
|
} |
|
|
|
|
|
|
|
|
check_dependencies() { |
|
|
print_info "جاري التحقق من التبعيات..." |
|
|
|
|
|
|
|
|
if command -v python3 &> /dev/null; then |
|
|
print_success "Python مثبت: $(python3 --version)" |
|
|
else |
|
|
print_error "Python غير مثبت!" |
|
|
fi |
|
|
|
|
|
|
|
|
echo "" |
|
|
echo "حالة الحزم:" |
|
|
echo "----------" |
|
|
|
|
|
for pkg in requests pyyaml pytest; do |
|
|
if python3 -c "import $pkg" 2>/dev/null; then |
|
|
print_success "$pkg ✓" |
|
|
else |
|
|
print_warning "$pkg ✗ (غير مثبت)" |
|
|
fi |
|
|
done |
|
|
} |
|
|
|
|
|
|
|
|
generate_status_report() { |
|
|
print_info "جاري إنشاء تقرير الحالة..." |
|
|
|
|
|
local report_file="logs/status_report_$(date +%Y%m%d_%H%M%S).txt" |
|
|
|
|
|
{ |
|
|
echo "=============================================" |
|
|
echo " تقرير حالة نظام الحارس التلقائي" |
|
|
echo "=============================================" |
|
|
echo "" |
|
|
echo "التاريخ: $(date)" |
|
|
echo "" |
|
|
echo "بيئة التشغيل:" |
|
|
echo "------------" |
|
|
echo "Python: $(python3 --version 2>&1)" |
|
|
echo "النظام: $(uname -a)" |
|
|
echo "" |
|
|
echo "مساحة القرص:" |
|
|
echo "-----------" |
|
|
df -h . | tail -1 |
|
|
echo "" |
|
|
echo "حالة Git:" |
|
|
echo "--------" |
|
|
git status --short 2>/dev/null || echo "غير متاح" |
|
|
echo "" |
|
|
echo "=============================================" |
|
|
} > "$report_file" |
|
|
|
|
|
print_success "تم إنشاء التقرير: $report_file" |
|
|
} |
|
|
|
|
|
|
|
|
fix_permissions() { |
|
|
print_info "جاري إصلاح أذونات الملفات..." |
|
|
|
|
|
|
|
|
chmod +x scripts/*.sh 2>/dev/null || true |
|
|
|
|
|
|
|
|
chmod 600 config/*.yaml 2>/dev/null || true |
|
|
|
|
|
|
|
|
find . -type d -exec chmod 755 {} \; 2>/dev/null || true |
|
|
|
|
|
print_success "تم إصلاح الأذونات" |
|
|
} |
|
|
|
|
|
|
|
|
clean_temp() { |
|
|
print_info "جاري حذف الملفات المؤقتة..." |
|
|
|
|
|
local count=0 |
|
|
|
|
|
|
|
|
while IFS= read -r -d '' file; do |
|
|
rm -f "$file" |
|
|
((count++)) |
|
|
done < <(find . -name "*~" -print0 2>/dev/null) |
|
|
|
|
|
|
|
|
while IFS= read -r -d '' file; do |
|
|
rm -f "$file" |
|
|
((count++)) |
|
|
done < <(find . -name "*.swp" -print0 2>/dev/null) |
|
|
|
|
|
|
|
|
while IFS= read -r -d '' file; do |
|
|
rm -f "$file" |
|
|
((count++)) |
|
|
done < <(find . -name "*.swo" -print0 2>/dev/null) |
|
|
|
|
|
|
|
|
rm -f *.tmp 2>/dev/null |
|
|
rm -f tmp/* 2>/dev/null || true |
|
|
|
|
|
print_success "تم حذف $count ملف مؤقت" |
|
|
} |
|
|
|
|
|
|
|
|
show_help() { |
|
|
echo "" |
|
|
echo "استخدام: $0 [الخيار]" |
|
|
echo "" |
|
|
echo "خيارات الصيانة:" |
|
|
echo "" |
|
|
echo " all - تشغيل جميع عمليات الصيانة" |
|
|
echo " pycache - تنظيف ملفات Python المؤقتة" |
|
|
echo " logs - تنظيف السجلات القديمة" |
|
|
echo " tests - تنظيف ملفات الاختبار" |
|
|
echo " build - تنظيف ملفات البناء" |
|
|
echo " cache - تنظيف ملفات التخزين المؤقت" |
|
|
echo " temp - حذف الملفات المؤقتة" |
|
|
echo " permissions - إصلاح أذونات الملفات" |
|
|
echo " disk - التحقق من مساحة القرص" |
|
|
echo " git - التحقق من حالة Git" |
|
|
echo " deps - التحقق من التبعيات" |
|
|
echo " report - إنشاء تقرير الحالة" |
|
|
echo " help - عرض هذه المساعدة" |
|
|
echo "" |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print_header |
|
|
|
|
|
|
|
|
case "${1:-all}" in |
|
|
all) |
|
|
echo "جاري تشغيل جميع عمليات الصيانة..." |
|
|
echo "" |
|
|
|
|
|
clean_pycache |
|
|
echo "" |
|
|
|
|
|
clean_logs |
|
|
echo "" |
|
|
|
|
|
clean_tests |
|
|
echo "" |
|
|
|
|
|
clean_build |
|
|
echo "" |
|
|
|
|
|
clean_cache |
|
|
echo "" |
|
|
|
|
|
clean_temp |
|
|
echo "" |
|
|
|
|
|
fix_permissions |
|
|
echo "" |
|
|
|
|
|
check_disk_space |
|
|
echo "" |
|
|
|
|
|
generate_status_report |
|
|
;; |
|
|
|
|
|
pycache) |
|
|
clean_pycache |
|
|
;; |
|
|
|
|
|
logs) |
|
|
clean_logs |
|
|
;; |
|
|
|
|
|
tests) |
|
|
clean_tests |
|
|
;; |
|
|
|
|
|
build) |
|
|
clean_build |
|
|
;; |
|
|
|
|
|
cache) |
|
|
clean_cache |
|
|
;; |
|
|
|
|
|
temp) |
|
|
clean_temp |
|
|
;; |
|
|
|
|
|
permissions) |
|
|
fix_permissions |
|
|
;; |
|
|
|
|
|
disk) |
|
|
check_disk_space |
|
|
;; |
|
|
|
|
|
git) |
|
|
check_git_status |
|
|
;; |
|
|
|
|
|
deps) |
|
|
check_dependencies |
|
|
;; |
|
|
|
|
|
report) |
|
|
generate_status_report |
|
|
;; |
|
|
|
|
|
help|--help|-h) |
|
|
show_help |
|
|
;; |
|
|
|
|
|
*) |
|
|
print_error "خيار غير معروف: $1" |
|
|
show_help |
|
|
exit 1 |
|
|
;; |
|
|
esac |
|
|
|
|
|
echo "" |
|
|
print_success "اكتمل العمل بنجاح!" |
|
|
echo "" |
|
|
|