File size: 2,974 Bytes
672ebf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<<<ФАЙЛ: setup_checks.py>>>
# FILE: setup_checks.py
import sys
import subprocess
import pkg_resources
import importlib
import traceback
from pathlib import Path

REQUIREMENTS_FILE = Path("requirements.txt")

def check_python_version():
    if sys.version_info < (3, 10):
        print(f"[ERROR] Python 3.10+ требуется, текущая версия: {sys.version}")
        return False
    print(f"[OK] Python версия {sys.version_info.major}.{sys.version_info.minor}")
    return True

def check_dependencies():
    print("[INFO] Проверка зависимостей из requirements.txt...")
    if not REQUIREMENTS_FILE.exists():
        print("[ERROR] requirements.txt не найден!")
        return False

    with REQUIREMENTS_FILE.open("r", encoding="utf-8") as f:
        reqs = [line.strip() for line in f if line.strip() and not line.startswith("#")]

    ok = True
    for r in reqs:
        try:
            pkg_resources.require(r)
            print(f"[OK] {r}")
        except pkg_resources.DistributionNotFound:
            print(f"[ERROR] Пакет не установлен: {r}")
            ok = False
        except pkg_resources.VersionConflict as e:
            print(f"[ERROR] Конфликт версии: {e}")
            ok = False
    return ok

def run_static_analysis():
    print("[INFO] Запуск flake8 и mypy...")
    ok = True
    try:
        subprocess.run(["flake8", "--max-line-length=120"], check=True)
        subprocess.run(["mypy", "."], check=True)
    except subprocess.CalledProcessError:
        ok = False
    return ok

def smoke_test_app():
    print("[INFO] Выполняем smoke-test app.py...")
    ok = True
    try:
        app_module = importlib.import_module("app")
        if not hasattr(app_module, "mock_predict"):
            print("[ERROR] В app.py отсутствует mock_predict()")
            ok = False
        else:
            test_result = app_module.mock_predict("Тест")
            if not isinstance(test_result, str):
                print("[ERROR] mock_predict() вернул не строку")
                ok = False
            else:
                print("[OK] mock_predict() успешно работает")
    except Exception:
        print("[ERROR] Ошибка при импорте app.py:")
        traceback.print_exc()
        ok = False
    return ok

def main():
    success = True
    if not check_python_version():
        success = False
    if not check_dependencies():
        success = False
    if not run_static_analysis():
        print("[WARNING] Статический анализ выявил проблемы")
    if not smoke_test_app():
        success = False

    if success:
        print("[SUCCESS] Все проверки пройдены")
        sys.exit(0)
    else:
        print("[FAIL] Обнаружены критические ошибки")
        sys.exit(1)

if __name__ == "__main__":
    main()