File size: 3,938 Bytes
6ea020b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import shutil
import glob

def package():
    # --- CONFIGURATION ---
    PORT = 8000
    # ---------------------

    dist_dir = "NexLab_Published"
    if os.path.exists(dist_dir):
        shutil.rmtree(dist_dir)
    os.makedirs(dist_dir)

    # 1. Copy application directories
    include_dirs = ['app', 'routes', 'templates', 'static', 'uploads', 'tmp', 'device_interfacing', 'migrations', 'tools']
    # Removed lab_database.db from include_files to prevent overwriting production data
    include_files = [
        'main.py', 'models.py', 'database.py', 'requirements.txt',
        'update_system.bat', 'System_Update_Guide.md', '.env'
    ]

    for d in include_dirs:
        if os.path.exists(d):
            shutil.copytree(d, os.path.join(dist_dir, d), dirs_exist_ok=True,
                           ignore=shutil.ignore_patterns('__pycache__', '*.pyc', '*.pyo'))
        else:
            os.makedirs(os.path.join(dist_dir, d), exist_ok=True)

    for f in include_files:
        if os.path.exists(f):
            shutil.copy2(f, os.path.join(dist_dir, f))

    # 2. Create Start Script
    start_bat_content = f"""@echo off
set "VENV_DIR=%~dp0venv"

REM Load environment variables from .env
if exist "%~dp0.env" (
    for /F "tokens=1,2 delims==" %%A in ('type "%~dp0.env"') do (
        echo %%A | findstr /r "^#" >nul 2>&1
        if errorlevel 1 (
            if not "%%A"=="" set "%%A=%%B"
        )
    )
)

if not exist "%VENV_DIR%" (
    echo [ERROR] Virtual environment not found. Please run 'install_requirements.bat' first.
    pause
    exit /b
)
echo Starting NexLab LIS Server...
echo Access this system at: http://localhost:%PORT%
echo.
REM Start CBC Device Watcher in a separate window
echo Starting CBC Device Watcher...
start "NexLab CBC Device Watcher" "%VENV_DIR%\\\\Scripts\\\\python.exe" device_interfacing\\\\device_watcher.py
"%VENV_DIR%\\\\Scripts\\\\python.exe" -m uvicorn main:app --host 0.0.0.0 --port %PORT%
pause
"""
    with open(os.path.join(dist_dir, "start_nexlab.bat"), "w") as f:
        f.write(start_bat_content)

    # 2b. Create standalone Device Watcher script
    watcher_bat_content = """@echo off
set "VENV_DIR=%~dp0venv"
if not exist "%VENV_DIR%" (
    echo [ERROR] Virtual environment not found. Please run 'install_requirements.bat' first.
    pause
    exit /b
)
echo =====================================================
echo    NexLab CBC Device Watcher
echo =====================================================
echo.
"%VENV_DIR%\\\\Scripts\\\\python.exe" device_interfacing\\\\device_watcher.py
pause
"""
    with open(os.path.join(dist_dir, "start_device_watcher.bat"), "w") as f:
        f.write(watcher_bat_content)

    # 3. Create Install Script
    install_bat_content = """@echo off
echo Preparing NexLab Environment...
where python >nul 2>&1
if %errorlevel% neq 0 (
    echo [ERROR] Python not found! Please install Python 3.10+ first.
    pause
    exit /b
)
if not exist "venv" (
    python -m venv venv
)
echo Installing dependencies...
"venv\\\\Scripts\\\\pip.exe" install -r requirements.txt
"venv\\\\Scripts\\\\python.exe" -m playwright install chromium
echo Setup Complete! Run 'start_nexlab.bat'
pause
"""
    with open(os.path.join(dist_dir, "install_requirements.bat"), "w") as f:
        f.write(install_bat_content)

    # 4. Create Auto-Start Script
    autostart_bat_content = """@echo off
set "SCRIPT_PATH=%~dp0start_nexlab.bat"
set "SHORTCUT_PATH=%APPDATA%\\\\Microsoft\\\\Windows\\\\Start Menu\\\\Programs\\\\Startup\\\\NexLab_LIS.lnk"
powershell "$s=(New-Object -ComObject WScript.Shell).CreateShortcut('%SHORTCUT_PATH%');$s.TargetPath='%SCRIPT_PATH%';$s.WorkingDirectory='%~dp0';$s.WindowStyle=7;$s.Save()"
echo Added to Startup!
pause
"""
    with open(os.path.join(dist_dir, "add_to_startup.bat"), "w") as f:
        f.write(autostart_bat_content)

if __name__ == "__main__":
    package()
    print("Packaging finished successfully.")