File size: 2,668 Bytes
15fbd84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Verifies app.py imports cleanly under file-layout problems that have actually
broken this Space:

  A  integrations/ folder missing entirely (HF web uploader skips folders)
  B  integrations/ present but without __init__.py
  C  integrations/ complete
  D  app started from a different working directory

Uses the REAL gradio (a verified dependency) and stubs only model loading, so
nothing downloads. Never launches a server — that is test_app.py's job.
"""
import os
import shutil
import subprocess
import sys
import tempfile

SRC = os.path.dirname(os.path.abspath(__file__))
GREEN, RED, OFF = "\033[92m", "\033[91m", "\033[0m"

HARNESS = '''
import sys
sys.path.insert(0, sys.argv[2])
import pipeline as _pl
_pl.HausaVoiceAIPipeline._load_asr      = lambda self: None
_pl.HausaVoiceAIPipeline._load_asr_fast = lambda self: None
_pl.HausaVoiceAIPipeline._load_nllb     = lambda self: None
_pl.HausaVoiceAIPipeline._load_tts      = lambda self: None
import runpy
runpy.run_path(sys.argv[1], run_name="__not_main__")
print("IMPORT_OK")
'''


def scenario(name, setup, cwd=None):
    tmp = tempfile.mkdtemp()
    for f in os.listdir(SRC):
        if f.endswith(".py"):
            shutil.copy(os.path.join(SRC, f), tmp)
    setup(tmp)
    harness = os.path.join(tmp, "_h.py")
    with open(harness, "w") as fh:
        fh.write(HARNESS)
    r = subprocess.run(
        [sys.executable, harness, os.path.join(tmp, "app.py"), tmp],
        capture_output=True, text=True, cwd=cwd or tmp)
    ok = "IMPORT_OK" in r.stdout
    tag = f"{GREEN}BOOTS{OFF}" if ok else f"{RED}CRASH{OFF}"
    print(f"  [{tag}] {name}")
    if not ok:
        tail = (r.stderr or r.stdout).strip().splitlines()
        print("         " + (tail[-1] if tail else "no output"))
    shutil.rmtree(tmp, ignore_errors=True)
    return ok


def none(tmp):
    pass


def no_init(tmp):
    d = os.path.join(tmp, "integrations")
    os.makedirs(d, exist_ok=True)
    for f in ("crm.py", "sip.py", "whatsapp.py"):
        shutil.copy(os.path.join(SRC, "integrations", f), d)


def full(tmp):
    d = os.path.join(tmp, "integrations")
    os.makedirs(d, exist_ok=True)
    for f in os.listdir(os.path.join(SRC, "integrations")):
        if f.endswith(".py"):
            shutil.copy(os.path.join(SRC, "integrations", f), d)


print("\n== Space startup scenarios ==")
res = [
    scenario("A: integrations/ missing entirely", none),
    scenario("B: integrations/ without __init__.py", no_init),
    scenario("C: integrations/ complete", full),
    scenario("D: different working directory", full, cwd="/"),
]
print(f"\n  {sum(res)}/{len(res)} scenarios boot cleanly")
sys.exit(0 if all(res) else 1)