From 35e251d0bdd213274d0faf569a1eaf75ab690296 Mon Sep 17 00:00:00 2001 From: theovit Date: Mon, 11 Sep 2023 04:12:30 -0500 Subject: [PATCH] Created install.bat and updated run.bat this script automates the setup of a Python development environment, checks for Python version compatibility, and handles virtual environment creation and package installations. It also provides options for user interaction and error handling. 1. The script starts with the "@echo off" command to disable command echoing and make the output cleaner. 2. It enables delayed variable expansion using "setlocal enabledelayedexpansion." 3. Several variables are initialized, including: - `VENV_DIR` for the virtual environment directory. - `wrong_version` and `WinAliases` to track Python version and Windows aliases, respectively. - `python_path_exe` and `python_path_app` to store the paths of Python executables. 4. The script checks for the existence of a previous virtual environment (`%VENV_DIR%`) and prompts the user to remove it if it exists. 5. It attempts to locate the Python executable by using the `where python` command and checks the Python version. It also identifies whether it's a Windows Store Python (which runs slower) or a standalone version. 6. Based on the Python executable found or the user's choice, it sets the `python_path` variable accordingly. 7. If Python 3.10.x is found and running, it displays a message indicating that Python is installed. 8. The script then checks for an existing virtual environment. If it exists, it activates it; otherwise, it creates one. 9. It upgrades `pip` to the latest version. 10. It installs Python packages specified in `requirements.txt`. 11. It checks if Playwright is installed. If not, it installs Playwright dependencies. 12. The script offers the option to run the installation script again or exit. 13. If Python 3.10.x is not found, it displays an error message and provides links to documentation and Python download pages. 14. The script defines a function called `cecho` to display colored text. 15. Finally, it exits the script with the appropriate error level. --- .gitignore | 3 +- Install.bat | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++++ run.bat | 117 ++++++++++++++++++++++++++++++++-- 3 files changed, 289 insertions(+), 7 deletions(-) create mode 100644 Install.bat 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 new file mode 100644 index 0000000..4fadbb8 --- /dev/null +++ b/Install.bat @@ -0,0 +1,176 @@ +@echo off +setlocal enabledelayedexpansion +:: Set variables to zero or null +set VENV_DIR=.venv +:main +set wrong_version=0 +set WinAliases=0 +set python_path_exe=nul +set python_path_app=nul +cls +:: Checks if there is a preexisting virtual environment and prompts to remove it +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 ( + call:cecho red "Invalid choice. Please enter Y or N." + goto :again + ) +) +cls +:: Find the location of the Python executable +for /f %%i in ('where python') do ( + set "python_path_check=%%i" + !python_path_check! --version 2>nul + if !errorlevel! EQU 9009 ( + set WinAliases=1 + ) else ( + !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 python_path_exe=!python_path_check! + ) + echo !python_path_check! | findstr /R ."WindowsApps". > NUL + if !errorlevel! EQU 0 ( + set python_path_app=!python_path_check! + ) + ) else ( + set wrong_version=1 + ) + ) +) +:: Set the default python to the stand alone version +if NOT !python_path_exe! EQU nul ( + echo Using !python_path_exe! + set python_path=!python_path_exe! +) else if NOT !python_path_app! EQU nul ( + :: Ask the user if they would like to install the standalone version of python + :winask + cls + call:cecho cyan "It looks like you are running the Windows store version of python." + call:cecho cyan "This version of python is known to run slower than the standalone version." + echo  + set /p "choice=Would you like to be directed to the install page? [Y/N]:" + echo  + set "choice=!choice:~0,1!" + set "choice=!choice:~0,1!" + if /i "!choice!"=="Y" ( + cls + goto :install_python + ) else if /i "!choice!"=="N" ( + echo You have chosen to continue with the Windows App store version of python. + pause + ) else ( + call:cecho red "Invalid choice. Please enter Y or N." + goto :winask + ) + set python_path=!python_path_app! +) else if !wrong_version! EQU 1 ( + goto wrong_python_display +) else if !WinAliases! EQU 1 ( + goto wrong_python_display +) + + +:python_installed +call:cecho green "Python 3.10.x is installed and running." +:activate +:: Check the there is a existing virtual environment and it not creates one +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." +) +:: Installing requirements + +call:cecho yellow "This process could take a long time please be patient" +pause +pip install -r requirements.txt +:playwright_check +pip show playwright >nul 2>&1 +if !errorlevel! equ 0 ( + call:cecho green "Playwright Dependencies installed." +) else ( + echo Installing Playwright Dependencies... + python -m playwright install + python -m playwright install-deps + goto playwright_check +) + +:again +echo  +set /p "choice=Do you want to run the install script again? [Y/N]:" +echo  +set "choice=!choice:~0,1!" +set "choice=!choice:~0,1!" +if /i "!choice!"=="Y" ( + goto :main +) else if /i "!choice!"=="N" ( + goto :exit +) else ( + call:cecho red "Invalid choice. Please enter Y or N." + goto :again +) + +:exit +echo Press any key to Return to menu... +pause >nul +call run.bat +cmd /k + + +:wrong_python_display +echo It looks like python version 3.10.x is not installed. Please refer to the documentation for help. +:install_python +call:cecho yellow "Control Click to open links in browser:" +echo Documentation: https://reddit-video-maker-bot.netlify.app/docs/prerequisites +echo Windows Store Python: https://apps.microsoft.com/store/detail/python-310/9PJPW5LDXLZ5 +echo Python site: (Recommended) https://www.python.org/ftp/python/3.10.11/python-3.10.11-amd64.exe +goto again + + +EXIT /B !ERRORLEVEL! +:: "Function" That allows coloring of text with call:cecho Text_color "Text to be displayed" +:cecho +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 38bbeb7..0f3296c 100644 --- a/run.bat +++ b/run.bat @@ -1,15 +1,120 @@ @echo off set VENV_DIR=.venv - +setlocal enabledelayedexpansion +:menu +cls +echo ######## MENU ######## +echo 1. Install +echo 2. Run +echo 3. Open CMD (.vevn) +echo 4. Launch Web GUI (WIP)  +echo 5. Exit  +echo ###################### +echo  +set /p "choice=Please make a selection :" +echo  + if /i "!choice!"=="1" ( + .venv\Scripts\deactivate.bat + 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 if /i "!choice!"=="4" ( + if exist "%VENV_DIR%" ( + echo Activating virtual environment... + call "%VENV_DIR%\Scripts\activate.bat" + echo Launching Web Graphical User Interface + start python GUI.py + ) else ( + cls + call:cecho red "No virtual environment detected" + call:cecho red "Please select Install from the menu" + pause + goto menu + ) + ) else if /i "!choice!"=="5" ( + cmd /k + )else ( + cls + goto menu + ) +goto:menu +:run if exist "%VENV_DIR%" ( echo Activating virtual environment... call "%VENV_DIR%\Scripts\activate.bat" + echo Running Python script... + python main.py + if errorlevel 1 ( + echo An error occurred. Press any key to exit. + pause >nul + exit + ) +) else ( + :again + 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" ( + call install.bat + ) else if /i "!choice!"=="N" ( + exit + ) else ( + cls + call:cecho red "Invalid choice. Please enter Y or N." + goto :again + ) ) +endlocal -echo Running Python script... -python main.py -if errorlevel 1 ( - echo An error occurred. Press any key to exit. - pause >nul +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 + + +EXIT /B %ERRORLEVEL% +:venv +if exist "%VENV_DIR%" ( + echo Activating virtual environment... + call "%VENV_DIR%\Scripts\activate.bat" +EXIT /B 0 \ No newline at end of file