-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.bat
166 lines (156 loc) · 5.64 KB
/
run.bat
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
@echo off
setlocal enabledelayedexpansion
cd /d "%~dp0"
set NAME=EasyWallpaperInfo
set PROJECTFOLDER=main
set RUN=%PROJECTFOLDER%\EasyWallpaperInfo.pyw
set requirementsFile=%PROJECTFOLDER%\requirements.txt
set VENV_PATH=.venv
set ACTIVATE_PATH=%VENV_PATH%\Scripts\activate
set CURRENT_DIR=%CD%
set LAST_DIR_FILE=%PROJECTFOLDER%\last_known_directory.txt
set refrenv=%PROJECTFOLDER%\refrenv.bat
set PYTHON_DOWNLOAD_URL=https://www.python.org/ftp/python/3.12.4/python-3.12.4-amd64.exe
:: Display provided argument if any
if not "%~1"=="" (
echo Open with: "%~1"
)
:: Check if Python is installed
pip --version >nul 2>&1 && (set python_installed=true) || (set python_installed=false)
if not "%python_installed%"=="true" (
echo Python is required to run this program.
set /p "userinp=Do you want to install Python (y/n)?: "
if /i "!userinp!"=="y" (
echo Downloading Python...
winget install python --accept-package-agreements
if errorlevel 1 (
echo Download failed using winget method. Trying to download manually...
:bothwingetfailed
if not exist "%PROJECTFOLDER%\python_installer.exe" (
echo Downloading Python from %PYTHON_DOWNLOAD_URL%
curl %PYTHON_DOWNLOAD_URL% -o %PROJECTFOLDER%\python_installer.exe
if not exist "%PROJECTFOLDER%\python_installer.exe" (
echo Download failed using curl. Trying with the powershell method
powershell -Command "& {Invoke-WebRequest -Uri '%PYTHON_DOWNLOAD_URL%' -OutFile '%PROJECTFOLDER%\python_installer.exe'}"
if errorlevel 1 (
echo Failed to install Python. Please install Python manually and try again.
pause
exit /b
)
)
)
echo Installing Python. Please wait...
start /wait %PROJECTFOLDER%\python_installer.exe /quiet InstallAllUsers=1 PrependPath=1 Include_test=0
if errorlevel 1 (
echo Failed to install Python. Please install Python manually and try again.
pause
exit /b
)
del %PROJECTFOLDER%\python_installer.exe
goto python_ok
)
) else (
echo Python is required to run this program. Please install Python and try again.
pause
exit /b
)
:python_ok
echo Python installed successfully.
:: Refresh the environment variables
call %refrenv%
set python_installed=true
)
:: Check if virtual environment exists
IF NOT EXIST %VENV_PATH% (
echo Creating virtual environment...
echo %CURRENT_DIR% > "%LAST_DIR_FILE%"
python -m venv %VENV_PATH%
) ELSE (
goto check_python
)
:: Check if Python exists at the path stored in the virtual environment
set count_python=0
:check_python
%VENV_PATH%\Scripts\python.exe --version >nul 2>&1
if %errorlevel% neq 0 (
set /a count_python+=1
echo Python not found at the path stored in the virtual environment.
echo Recreating virtual environment...
rmdir /s /q %VENV_PATH%
python -m venv %VENV_PATH%
if !count_python! lss 3 (
goto check_python
) else (
echo Something went wrong. Please try again.
pause
exit /b
)
)
:: Check if the current directory has changed since the last run
if exist "%LAST_DIR_FILE%" (
set /p last_dir=<"%LAST_DIR_FILE%"
cd "%CURRENT_DIR%" && (
if not exist "!last_dir!" (
echo Current directory: %CURRENT_DIR%
echo Last known directory: !last_dir!
echo Looks like the directory of the program has been changed.
echo It is hihgly recommended to recreate the virtual environment.
set /p "recreate_venv=Recreate the virtual environment (y/n)?: "
if /i "!recreate_venv!"=="y" (
echo Recreating virtual environment...
rmdir /s /q %VENV_PATH%
python -m venv %VENV_PATH%
:: Update the last directory file
echo %CURRENT_DIR% > "%LAST_DIR_FILE%"
)
)
)
) else (
:: Create the last directory file if it doesn't exist
echo %CURRENT_DIR% > "%LAST_DIR_FILE%"
)
:: Activate the virtual environment
set count_activate=0
:activate
echo Activating virtual environment...
call %ACTIVATE_PATH%
if errorlevel 1 (
set /a count_activate+=1
echo Failed to activate virtual environment.
echo Recreating virtual environment...
rmdir /s /q %VENV_PATH%
python -m venv %VENV_PATH%
if !count_activate! lss 3 (
goto activate
) else (
echo Failed to activate virtual environment. Please try again.
pause
exit /b
)
)
:: Get the list of installed packages using pip freeze
echo Checking the requirements...
for /f "tokens=1,* delims==" %%i in ('pip freeze --all') do (
set installed[%%i]=%%j
)
:: Compare with the requirements from the requirements.txt file
for /f "tokens=1,* delims==" %%i in (%requirementsFile%) do (
if not "!installed[%%i]!"=="%%j" (
echo Installing package: %%i==%%j
pip install %%i==%%j --upgrade --quiet
if errorlevel 1 (
echo Failed to install %%i==%%j. Please check your internet connection and try again.
pause
exit /b
)
)
)
:: Run the program
echo Starting %NAME%...
start /B "" "%VENV_PATH%\Scripts\pythonw.exe" %RUN% %* > nul 2>&1
if errorlevel 1 (
echo Failed to start %NAME%. Please try again.
pause
exit /b
)
exit /b