File size: 12,396 Bytes
7270c96 |
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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
#!/bin/bash
# سكريبت صيانة نظام الحارس التلقائي
# Auto Guardian System Maintenance Script
# =========================================
# إعداداتColors
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"
# دالة تنظيف ملفات Python المؤقتة
clean_pycache() {
print_info "جاري تنظيف ملفات Python المؤقتة..."
local count=0
# حذف مجلدات __pycache__
while IFS= read -r -d '' dir; do
rm -rf "$dir"
print_info "حُذف: $dir"
((count++))
done < <(find . -type d -name "__pycache__" -print0 2>/dev/null)
# حذف ملفات .pyc
while IFS= read -r -d '' file; do
rm -f "$file"
((count++))
done < <(find . -name "*.pyc" -print0 2>/dev/null)
# حذف ملفات .pyo
while IFS= read -r -d '' file; do
rm -f "$file"
((count++))
done < <(find . -name "*.pyo" -print0 2>/dev/null)
# حذف مجلدات .pytest_cache
rm -rf .pytest_cache/ 2>/dev/null
((count++))
# حذف مجلدات .hypothesis
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
# حذف تقارير pytest
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
# ملفات .egg
rm -rf *.egg 2>/dev/null
# ملفات Wheel
rm -rf *.whl 2>/dev/null
print_success "تم تنظيف ملفات البناء"
}
# دالة تنظيف ملفات التخزين المؤقت
clean_cache() {
print_info "جاري تنظيف ملفات التخزين المؤقت..."
# حذف ذاكرة التخزين المؤقت لـ pip
pip cache purge 2>/dev/null || true
# حذف مجلدات ذاكرة التخزين المؤقت
rm -rf ~/.cache/pip 2>/dev/null || true
# حذف ذاكرة التخزين المؤقت لـ mypy
rm -rf .mypy_cache/ 2>/dev/null || true
# حذف ذاكرة التخزين المؤقت لـ ruff
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
}
# دالة التحقق من حالة Git
check_git_status() {
print_info "جاري التحقق من حالة Git..."
# التحقق من وجود 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 "جاري التحقق من التبعيات..."
# التحقق من Python
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)
# حذف ملفات .swp
while IFS= read -r -d '' file; do
rm -f "$file"
((count++))
done < <(find . -name "*.swp" -print0 2>/dev/null)
# حذف ملفات .swo
while IFS= read -r -d '' file; do
rm -f "$file"
((count++))
done < <(find . -name "*.swo" -print0 2>/dev/null)
# حذف ملفات .tmp
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 ""
|