From db60b5af298562d577533a126226c56a6738e9d0 Mon Sep 17 00:00:00 2001 From: Shohan Dutta Roy Date: Wed, 5 Jun 2024 11:42:28 +0530 Subject: [PATCH 1/3] feat: Add scripts to run the app after build --- .github/workflows/release-and-publish.yml | 69 +++++++++++++++++++++-- build_scripts/test_app_run_macos.sh | 55 ++++++++++++++++++ build_scripts/test_app_run_windows.bat | 51 +++++++++++++++++ openadapt/entrypoint.py | 16 ++++-- 4 files changed, 181 insertions(+), 10 deletions(-) create mode 100755 build_scripts/test_app_run_macos.sh create mode 100644 build_scripts/test_app_run_windows.bat diff --git a/.github/workflows/release-and-publish.yml b/.github/workflows/release-and-publish.yml index fcbf949a3..64c9f20c3 100644 --- a/.github/workflows/release-and-publish.yml +++ b/.github/workflows/release-and-publish.yml @@ -93,10 +93,72 @@ jobs: name: OpenAdapt path: OpenAdapt.zip + test_on_macos: + name: Test on macOS + runs-on: macos-latest + needs: [build-macos-executables] + outputs: + macos_build_status: ${{ steps.test_on_macos.outputs.status }} + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Download macOS executable + uses: actions/download-artifact@v4 + with: + name: OpenAdapt.app + path: dist/ + - name: Run app + id: test_on_macos + run: | + ./build_scripts/test_app_run_macos.sh + EXIT_CODE=$? + if [ $EXIT_CODE -ne 0 ]; then + echo "macos_build_status=failed" >> $GITHUB_OUTPUT + fi + + test_on_windows: + name: Test on Windows + runs-on: windows-latest + needs: [build-windows-executables] + outputs: + windows_build_status: ${{ steps.test_on_windows.outputs.windows_build_status }} + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Download Windows executable + uses: actions/download-artifact@v4 + with: + name: OpenAdapt + path: dist/ + - name: Run app + id: test_on_windows + shell: powershell + run: | + ./build_scripts/test_app_run_windows.bat + if ($LastExitCode -ne 0) { + "windows_build_status=failed" | Out-File -FilePath $env:GITHUB_OUTPUT -Append + } + + consolidate_tests: + name: Consolidate test results + runs-on: ubuntu-latest + needs: [test_on_windows, test_on_macos] + steps: + - name: Consolidate test results + id: consolidate_test_results + run: | + if [ "${{ needs.test_on_windows.outputs.windows_build_status }}" = "failed" ] || [ "${{ needs.test_on_macos.outputs.macos_build_status }}" = "failed" ]; then + echo "Error: Tests failed" + exit 1 + fi + release: runs-on: ubuntu-latest - needs: [check_last_commit_author, build-macos-executables, build-windows-executables] - if: ${{ needs.check_last_commit_author.outputs.skip_ci != 'true' }} + needs: [consolidate_tests] concurrency: release permissions: id-token: write @@ -141,8 +203,7 @@ jobs: publish: name: Publish to PyPI - needs: [check_last_commit_author, release] - if: ${{ needs.check_last_commit_author.outputs.skip_ci != 'true' }} + needs: [release] runs-on: ubuntu-latest steps: - name: Checkout repository diff --git a/build_scripts/test_app_run_macos.sh b/build_scripts/test_app_run_macos.sh new file mode 100755 index 000000000..62f1e45a4 --- /dev/null +++ b/build_scripts/test_app_run_macos.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +# unzip the app + +ZIPFILE_PATH="$(pwd)/dist/OpenAdapt.app.zip" +unzip -o "$ZIPFILE_PATH" -d "$(pwd)/dist" + +APP_PATH="$(pwd)/dist/OpenAdapt.app/Contents/MacOS/OpenAdapt.app" + +# print current directory +echo "Current directory: $(pwd)" +echo "App path: $APP_PATH" + +# Run the app +open "$APP_PATH" + +# Allow some time for the application to launch +sleep 30 + +# Verify that the executable exists +if [ -z "$APP_PATH" ]; then + echo "Error: Could not find executable in $APP_PATH" + exit 1 +fi + +# Get the process IDs +PIDS=$(pgrep -f "$APP_PATH") + +# Verify that the process IDs were found +if [ -z "$PIDS" ]; then + echo "Error: Could not find process IDs for $APP_PATH" + exit 1 +fi + +# Variable to track if any process is still running +ALL_PROCESSES_RUNNING=true + +# Check if the processes are still running +for PID in $PIDS; do + if ! ps -p $PID > /dev/null; then + echo "Process $PID is not running" + ALL_PROCESSES_RUNNING=false + break + fi +done + +# Set the exit code variable based on the processes' status +if [ "$ALL_PROCESSES_RUNNING" = true ]; then + EXIT_CODE=0 +else + EXIT_CODE=1 +fi + +echo "Exit code: $EXIT_CODE" +exit $EXIT_CODE diff --git a/build_scripts/test_app_run_windows.bat b/build_scripts/test_app_run_windows.bat new file mode 100644 index 000000000..7a5aab608 --- /dev/null +++ b/build_scripts/test_app_run_windows.bat @@ -0,0 +1,51 @@ +@echo off + +REM Unzip the distribution +set "ZIP_PATH=%cd%\dist\OpenAdapt.zip" +7z x %ZIP_PATH% -o%cd%\dist + +REM Path to the .exe file +set "APP_PATH=%cd%\dist\OpenAdapt\OpenAdapt.exe" + +REM Run the app +start %APP_PATH% + +REM Allow some time for the application to launch +ping -n 30 127.0.0.1 >nul + +REM Verify that the executable exists +if not exist "%APP_PATH%" ( + echo Error: Could not find executable in %APP_PATH% + exit /b 1 +) + +REM Get the process IDs +for /f "tokens=2" %%i in ('tasklist /fi "imagename eq OpenAdapt.exe" /nh') do ( + set "PID=%%i" +) + +REM Verify that the process ID was found +if not defined PID ( + echo Error: Could not find process IDs for %APP_PATH% + exit /b 1 +) + +REM Variable to track if any process is still running +set "ALL_PROCESSES_RUNNING=true" + +REM Check if the processes are still running +tasklist /fi "pid eq %PID%" /nh | find /i "OpenAdapt.exe" >nul +if errorlevel 1 ( + echo Process %PID% is not running + set "ALL_PROCESSES_RUNNING=false" +) + +REM Set the exit code variable based on the processes' status +if "%ALL_PROCESSES_RUNNING%"=="true" ( + set "EXIT_CODE=0" +) else ( + set "EXIT_CODE=1" +) + +echo Exit code: %EXIT_CODE% +exit /b %EXIT_CODE% diff --git a/openadapt/entrypoint.py b/openadapt/entrypoint.py index 3b1f503dc..cfb9036a2 100644 --- a/openadapt/entrypoint.py +++ b/openadapt/entrypoint.py @@ -7,17 +7,21 @@ multiprocessing.freeze_support() -from openadapt.alembic.context_loader import load_alembic_context -from openadapt.app import tray from openadapt.build_utils import redirect_stdout_stderr -from openadapt.config import print_config def run_openadapt() -> None: """Run OpenAdapt.""" - print_config() - load_alembic_context() - tray._run() + try: + from openadapt.alembic.context_loader import load_alembic_context + from openadapt.app import tray + from openadapt.config import print_config + + print_config() + load_alembic_context() + tray._run() + except Exception: + pass if __name__ == "__main__": From 4d11b59f8f73f948c59dac398e479a25f2825b04 Mon Sep 17 00:00:00 2001 From: Shohan Dutta Roy Date: Tue, 11 Jun 2024 12:20:35 +0530 Subject: [PATCH 2/3] feat: Log error in a file --- openadapt/entrypoint.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/openadapt/entrypoint.py b/openadapt/entrypoint.py index cfb9036a2..2a72a65fa 100644 --- a/openadapt/entrypoint.py +++ b/openadapt/entrypoint.py @@ -6,8 +6,9 @@ # This needs to be called before any code that uses multiprocessing multiprocessing.freeze_support() +from datetime import datetime -from openadapt.build_utils import redirect_stdout_stderr +from openadapt.build_utils import get_root_dir_path, redirect_stdout_stderr def run_openadapt() -> None: @@ -20,8 +21,10 @@ def run_openadapt() -> None: print_config() load_alembic_context() tray._run() - except Exception: - pass + except Exception as exc: + data_dir = get_root_dir_path() + with open(data_dir / "error.log", "a") as f: + f.write(f"{datetime.now()}: {exc}\n") if __name__ == "__main__": From 1089d01e6f0f770c7f6a0403a7be6eaf70fcedb6 Mon Sep 17 00:00:00 2001 From: Shohan Dutta Roy Date: Mon, 17 Jun 2024 13:27:35 +0530 Subject: [PATCH 3/3] chore: Add TODO for future additions of logging --- openadapt/entrypoint.py | 1 + 1 file changed, 1 insertion(+) diff --git a/openadapt/entrypoint.py b/openadapt/entrypoint.py index 2a72a65fa..d957bb060 100644 --- a/openadapt/entrypoint.py +++ b/openadapt/entrypoint.py @@ -23,6 +23,7 @@ def run_openadapt() -> None: tray._run() except Exception as exc: data_dir = get_root_dir_path() + # TODO: log all exceptions to a file with open(data_dir / "error.log", "a") as f: f.write(f"{datetime.now()}: {exc}\n")