From fffe4ddd00f993928310836482223c6b7264f7f5 Mon Sep 17 00:00:00 2001 From: theovit Date: Mon, 4 Sep 2023 21:43:42 -0500 Subject: [PATCH] Added multi-python check, colors, and run menu Added the ability to run the script with multiple versions of python installed and selected the proper version for the virtual environment. Added pretty colors. create a menu out of the run.bat that allows you to install the The prerequisites run the bot or open a command prompt within the installed environment. --- .gitignore | 3 +- Install.bat | 245 +++++++++++++++++++++++++++++++++++----------------- run.bat | 76 ++++++++++++++-- 3 files changed, 237 insertions(+), 87 deletions(-) diff --git a/.gitignore b/.gitignore index 41bdd5e..72b35d1 100644 --- a/.gitignore +++ b/.gitignore @@ -244,4 +244,5 @@ video_creation/data/videos.json video_creation/data/envvars.txt config.toml -*.exe \ No newline at end of file +*.exe +video_creation/data/videos.json diff --git a/Install.bat b/Install.bat index f867c37..188dd0f 100644 --- a/Install.bat +++ b/Install.bat @@ -3,97 +3,149 @@ setlocal enabledelayedexpansion set VENV_DIR=.venv set "requirements_file=requirements.txt" :main +set "wrong_python=0" cls -:: Check if Python version 3.10.x is installed and create/activate a virtual environment. -python --version 2>NUL | findstr /R "3.10" > NUL -if %errorlevel% NEQ 0 ( - powershell write-host -fore Red It looks like python version 3.10.x isn't installed. Please refer to the documentation for help. - powershell write-host -fore Red https://reddit-video-maker-bot.netlify.app/docs/prerequisites -) else ( - powershell write-host -fore Green Python 3.10 is installed and running. - :activate - if exist "%VENV_DIR%" ( - echo Activating virtual environment... - call "%VENV_DIR%\Scripts\activate.bat" - ) else ( - echo Creating virtual environment... - python -m venv %VENV_DIR% - goto activate - ) - python.exe -m pip install --upgrade pip >NUL - if %errorlevel% equ 0 ( - powershell write-host -fore Green Pip is up-to-date. +if exist "%VENV_DIR%" ( + call:cecho red "Previous virtual environment detected. May cause issues if not deleted." + echo  + set /p "choice=Would you like to remove this virtual environment? [Y/N]:" + echo  + set "choice=!choice:~0,1!" + set "choice=!choice:~0,1!" + if /i "!choice!"=="Y" ( + rmdir /s /q %VENV_DIR% + ) else if /i "!choice!"=="N" ( + call:cecho red "You have chosen not to remove the current virtual environment. Please note this may cause issues with the installation." ) else ( - powershell write-host -fore Green Pip has been upgraded to the latest version. - ) - :: Check if requirements.txt exists - if not exist %requirements_file% ( - powershell write-host -fore Red %requirements_file% does not exist. - exit /b 1 - ) - :: Loop through each line in requirements.txt and check if the package is installed - echo Checking dependencies... - for /f "tokens=1 delims=~=" %%a in (%requirements_file%) do ( - pip show %%a >nul 2>&1 - if errorlevel 1 ( - powershell write-host -fore Red %%a is not installed. - set "missing=1" - ) + call:cecho red "Invalid choice. Please enter Y or N." + goto :again ) - :: Check if any packages are missing - if defined missing ( - powershell write-host -fore Yellow One or more required packages are missing. - :ask_for_choice - powershell write-host -fore Yellow Do you want to install them? [Y/N]: - set /p choice= - set "choice=!choice:~0,1!" - set "choice=!choice:~0,1!" - if /i "!choice!"=="Y" ( - pip install -r "%requirements_file%" - if errorlevel 1 ( - powershell write-host -fore Red Installation failed. - goto :ask_for_choice - ) else ( - powershell write-host -fore Green Installation successful. +) +cls +python --version >nul 2>&1 +if %errorlevel% EQU 0 ( + :: Find the location of the Python executable + for /f %%i in ('where python') do ( + set "python_path_check=%%i" + echo !python_path_check! + !python_path_check! --version 2>NUL | findstr /R "3.10" > NUL + if %errorlevel% EQU 0 ( + echo python_path_check | findstr /R "Python310" > NUL + if %errorlevel% EQU 0 ( + set "standalone=!python_path_check!" + ) + echo python_path_check | findstr /R "WindowsApps" > NUL + if %errorlevel% EQU 0 ( + set "WindowsApps=!python_path_check!" ) - ) else if /i "!choice!"=="N" ( - echo You chose not to install missing packages. - goto again ) else ( - powershell write-host -fore Red Invalid choice. Please enter Y or N. - goto :ask_for_choice + set "wrong_python=1" ) - ) else ( - powershell write-host -fore Green All required packages are installed. ) - :playwright_check - pip show playwright >nul 2>&1 - if %errorlevel% equ 0 ( - powershell write-host -fore Green Playwright is installed. - ) else ( - echo Installing Playwright... - python -m playwright install - python -m playwright install-deps - goto playwright_check + if NOT "!standalone!"=="" ( + echo Using standalone Python + set "python_path=!standalone!" + goto python310 + ) else if NOT "!WindowsApps!"=="" ( + echo Using WindowsStore Python + set "python_path=!WindowsApps!" + goto python310 + ) + if !wrong_python! EQU 1 ( + goto wrong_python_display + ) +) else ( + goto wrong_python_display +) +:python310 +call:cecho green "Python 3.10.x is installed and running." +:activate +if exist "%VENV_DIR%" ( + echo Activating virtual environment... + call "%VENV_DIR%\Scripts\activate.bat" +) else ( + echo Creating virtual environment... + !python_path! -m venv %VENV_DIR% + goto activate +) +:: Upgrading pip +python -m pip install --upgrade pip >NUL +if %errorlevel% equ 0 ( + call:cecho green "Pip is up-to-date." +) else ( + call:cecho green "Pip has been upgraded to the latest version." +) +:: Check if requirements.txt exists +if not exist %requirements_file% ( + call:cecho red "%requirements_file% does not exist." + exit /b 1 +) +:: Loop through each line in requirements.txt and check if the package is installed +echo Checking dependencies... +for /f "tokens=1 delims=~=" %%a in (%requirements_file%) do ( + pip show %%a >nul 2>&1 + if errorlevel 1 ( + call:cecho red "%%a is not installed." + ::echo %%a is not installed. + set "missing=1" ) - :runbot? - powershell write-host -fore Yellow Would you like to run the bot now? [Y/N]: - set /p choice= +) +:: Check if any packages are missing +if defined missing ( + call:cecho yellow "One or more required packages are missing." + :ask_for_choice + echo  + set /p "choice=Do you want to install them? [Y/N]:" + echo  set "choice=!choice:~0,1!" set "choice=!choice:~0,1!" if /i "!choice!"=="Y" ( - call run.bat - goto :exit + pip install -r "%requirements_file%" + if errorlevel 1 ( + call:cecho red "Installation failed." + goto :ask_for_choice + ) else ( + call:cecho green "Installation successful." + ) ) else if /i "!choice!"=="N" ( - goto :again + echo You chose not to install missing packages. + goto again ) else ( - powershell write-host -fore Red Invalid choice. Please enter Y or N. - goto :runbot? + call:cecho red "Invalid choice. Please enter Y or N." + goto :ask_for_choice ) +) else ( + call:cecho green "All required packages are installed." +) +:playwright_check +pip show playwright >nul 2>&1 +if %errorlevel% equ 0 ( + call:cecho green "Playwright is installed." +) else ( + echo Installing Playwright... + python -m playwright install + python -m playwright install-deps + goto playwright_check +) +:runbot? +echo  +set /p "choice=Would you like to run the bot now? [Y/N]:" +echo  +set "choice=!choice:~0,1!" +set "choice=!choice:~0,1!" +if /i "!choice!"=="Y" ( + call run.bat + goto :exit +) else if /i "!choice!"=="N" ( + goto :again +) else ( + call:cecho red "Invalid choice. Please enter Y or N." + goto :runbot? ) :again -powershell write-host -fore Yellow Do you want to run the script again? [Y/N]: -set /p choice= +echo  +set /p "choice=Do you want to run the script again? [Y/N]:" +echo  set "choice=!choice:~0,1!" set "choice=!choice:~0,1!" if /i "!choice!"=="Y" ( @@ -101,10 +153,47 @@ if /i "!choice!"=="Y" ( ) else if /i "!choice!"=="N" ( goto :exit ) else ( - powershell write-host -fore Red Invalid choice. Please enter Y or N. + call:cecho red "Invalid choice. Please enter Y or N." goto :again ) endlocal :exit -echo Press any key to exit ... -pause >nul \ No newline at end of file +echo Press any key to Return to menu... +pause >nul +call run.bat +cmd /k + + +:wrong_python_display +call:cecho red "It looks like python version 3.10.x is not installed. Please refer to the documentation for help." +call:cecho red "https://reddit-video-maker-bot.netlify.app/docs/prerequisites" +call:cecho yellow "Control Click to install Python: https://apps.microsoft.com/store/detail/python-310/9PJPW5LDXLZ5" +goto again + + +EXIT /B %ERRORLEVEL% +:cecho +setlocal enabledelayedexpansion +set "color=%~1" +set "text=%~2" +if !color! == red ( + set "colored_text=%text%" +) +if !color! == green ( + set "colored_text=%text%" +) +if !color! == yellow ( + set "colored_text=%text%" +) +if !color! == blue ( + set "colored_text=%text%" +) +if !color! == magenta ( + set "colored_text=%text%" +) +if !color! == cyan ( + set "colored_text=%text%" +) +echo !colored_text! +endlocal +EXIT /B 0 \ No newline at end of file diff --git a/run.bat b/run.bat index f9b2aef..293de34 100644 --- a/run.bat +++ b/run.bat @@ -1,6 +1,38 @@ @echo off set VENV_DIR=.venv - +setlocal enabledelayedexpansion +:menu +cls +echo ######## MENU ######## +echo 1. Install +echo 2. Run +echo 3. Open CMD (.vevn) +echo ###################### +echo  +set /p "choice=Please make a selection :" +echo  + if /i "!choice!"=="1" ( + call install.bat + ) else if /i "!choice!"=="2" ( + goto run + ) else if /i "!choice!"=="3" ( + if exist "%VENV_DIR%" ( + cls + echo Activating virtual environment... + call "%VENV_DIR%\Scripts\activate.bat" + cmd /k + ) else ( + cls + call:cecho red "No virtual environment detected" + call:cecho red "Please select Install from the menu" + pause + goto menu + ) + )else ( + cls + goto menu + ) +:run if exist "%VENV_DIR%" ( echo Activating virtual environment... call "%VENV_DIR%\Scripts\activate.bat" @@ -11,13 +43,13 @@ if exist "%VENV_DIR%" ( pause >nul exit ) - ) else ( - setlocal enabledelayedexpansion :again - powershell write-host -fore Yellow It does not look like the prerequisites for the bot had been installed. - powershell write-host -fore Yellow Would you like to do that now? [Y/N]: - set /p choice= + cls + call:cecho yellow "It does not look like the prerequisites for the bot had been installed." + echo  + set /p "choice=Would you like to do that now? [Y/N]:" + echo  set "choice=!choice:~0,1!" set "choice=!choice:~0,1!" if /i "!choice!"=="Y" ( @@ -26,8 +58,36 @@ if exist "%VENV_DIR%" ( exit ) else ( cls - powershell write-host -fore Red Invalid choice. Please enter Y or N. + call:cecho red "Invalid choice. Please enter Y or N." goto :again ) - endlocal ) +endlocal + + +EXIT /B %ERRORLEVEL% +:cecho +setlocal enabledelayedexpansion +set "color=%~1" +set "text=%~2" +if !color! == red ( + set "colored_text=%text%" +) +if !color! == green ( + set "colored_text=%text%" +) +if !color! == yellow ( + set "colored_text=%text%" +) +if !color! == blue ( + set "colored_text=%text%" +) +if !color! == magenta ( + set "colored_text=%text%" +) +if !color! == cyan ( + set "colored_text=%text%" +) +echo !colored_text! +endlocal +EXIT /B 0 \ No newline at end of file