mor.cmd (6147B)
1 @echo off 2 setlocal 3 set mor_version=0.7 4 set root_dir=%cd%\out 5 for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a" 6 7 rem default values 8 set /a is_logi=0 9 set config_file=requirements.ini 10 set MOR_EXTS_TAR=.tar.gz,.tgz,.zip 11 12 if "%~1" == "" if not exist %config_file% goto print_usage 13 goto :main 14 15 :logi 16 if %is_logi% equ 1 echo %* 17 goto :eof 18 19 :print_usage 20 echo Usage: mor [ -c requirements.ini] [-d] [-Dvar1=value1 ...] [[@]target1, ...] 21 goto :eof 22 23 :read_ini <config_file.ini> 24 setlocal EnableDelayedExpansion 25 set /a section_count=0 26 set current_section=[ 27 28 rem locally remove env variables starting with '[' 29 set [ 2>nul && for /f "usebackq delims== tokens=1" %%l in ( `set [` ) do ( 30 set %%l= 31 ) 32 33 for /f "eol=; usebackq delims==] tokens=1,*" %%a in (%~1) do ( 34 set tok=%%~a 35 set val=%%~b 36 if "!tok:~0,1!" == "[" ( 37 set current_section=!tok! 38 call :logi #!current_section! 39 ) else ( 40 set MOR_V_ 2>NUL >NUL 41 if not ERRORLEVEL 1 ( 42 for /f "usebackq delims== tokens=1*" %%s in (`set MOR_V_`) do ( 43 set varname=%%s 44 set varname=!varname:MOR_V_=! 45 call set val=%%val:$!varname!=%%t%% 46 ) 47 ) 48 if "!current_section:~1,1!" == "$" ( 49 if "!tok!"=="/" ( 50 set root_dir=!val! 51 ) else ( 52 set MOR_V_!tok!=!val! 53 ) 54 ) else ( 55 set key=!current_section![!tok!] 56 set !key!=!val! 57 call :logi # [!tok!] "!val!" 58 ) 59 ) 60 ) 61 62 set wtargets=%targets% 63 :MOR_TARGETS_START 64 for /f "tokens=1*" %%a in ("%wtargets%") do ( 65 set target=%%~a 66 67 set [!target![ 2>NUL >NUL 68 if ERRORLEVEL 1 ( 69 echo ^> Cannot find target '%%~a' 70 exit /b 1 71 ) 72 73 echo !target!: 74 for /f "usebackq tokens=1,2* delims=[]=" %%l in (`set [!target![`) do ( 75 if "!target:~0,1!"=="#" ( 76 echo ^> Target definitions ^(i.e #targets^) cannot be invoked 77 exit /b 1 78 ) else if "!target:~0,1!"=="@" ( 79 set section=%%~l 80 set section=!section:~1! 81 call :parse_target !section! %%~m %%~n 82 if ERRORLEVEL 1 exit /b !ERRORLEVEL! 83 ) else ( 84 call :prime_download %%~l %%~m %%~n 85 if ERRORLEVEL 1 exit /b !ERRORLEVEL! 86 ) 87 ) 88 89 set wtargets=%%b 90 if not [!wtargets!] == [] goto :MOR_TARGETS_START 91 ) 92 endlocal DisableDelayedExpansion 93 goto :eof 94 95 :parse_target <section> <target_definition> <value> 96 setlocal EnableDelayedExpansion 97 set section=%1 98 set wsections=![#%section%[/]! 99 :MOR_PARSE_TARGET_START 100 for /f "usebackq tokens=1* delims= " %%a in ('!wsections!') do ( 101 set [%%a[ 2>NUL >NUL 102 if ERRORLEVEL 1 ( 103 echo ^> Cannot find target '%%~a' 104 exit /b 1 105 ) 106 107 for /f "usebackq tokens=1,2,3* delims=[]=" %%e in (`set [%%a[`) do ( 108 if "%%~f"=="%~2-%~3" ( 109 if not defined %%~f_done ( 110 set %%~f_done=1 111 call :prime_download %%e %%~f %%~g || goto :eof 112 exit /b 0 113 ) 114 ) 115 ) 116 117 set wsections=%%b 118 if not [%%b] == [] goto :MOR_PARSE_TARGET_START 119 ) 120 echo ^> Cannot find key '%~2-%~3' 121 exit /b 1 122 endlocal DisableDelayedExpansion 123 goto :eof 124 125 :prime_download <section> <target> <url> 126 setlocal EnableDelayedExpansion 127 set current_target_dir="!root_dir!\%1" 128 shift 129 call :logi Current Target Dir: !current_target_dir! 130 if not exist "!current_target_dir!" mkdir "!current_target_dir!" 131 :MOR_KEY_VAULE 132 if "%~1" == "" goto :eof 133 rem TODO Check if this for loop is really necessary 134 for %%i in (%2) do set ext=%%~xi 135 where /q curl 136 if ERRORLEVEL 1 ( 137 call :download_archive !current_target_dir! %1 %2 !ext! 138 if ERRORLEVEL 1 exit /b !ERRORLEVEL! 139 ) else ( 140 call :download_archive_curl !current_target_dir! %1 %2 !ext! 141 if ERRORLEVEL 1 exit /b !ERRORLEVEL! 142 ) 143 144 145 for %%x in (%MOR_EXTS_TAR%) do ( 146 if "!ext!"=="%%x" ( 147 call :unzip_archive !current_target_dir! %1 !ext! 148 if ERRORLEVEL 1 exit /b !ERRORLEVEL! 149 goto :MOR_AFTER_EXTRACT 150 ) 151 ) 152 153 :MOR_AFTER_EXTRACT 154 shift 155 shift 156 goto :MOR_KEY_VAULE 157 endlocal 158 goto :eof 159 160 :unzip_archive <download_dir> <file_name> <file_extension> 161 setlocal 162 echo ^| [%~2%~3] %~1\ 163 tar xzf "%~1\%~2%~3" -C "%~1" 164 exit /b %ERRORLEVEL% 165 endlocal 166 goto :eof 167 168 :download_archive_curl <download_dir> <file_name> <url> <file_extension> 169 setlocal 170 echo v [%2] %3 171 curl -Lf "%~3" -o "%~1\%~2%~4" 2>>mor.log 172 exit /b %ERRORLEVEL% 173 endlocal 174 goto :eof 175 176 :download_archive <download_dir> <file_name> <url> <file_extension> 177 setlocal 178 echo v [%2] %3 179 for /f "usebackq" %%i in (`bitsadmin /rawreturn /create "mor:%2"`) do ( 180 set job_id=%%i 181 bitsadmin /rawreturn /addfile "%%i" %3 "%~1\%2%4" >>mor.log 182 bitsadmin /setsecurityflags "%%i" 0x0000 >>mor.log 183 bitsadmin /setpriority "%%i" HIGH >>mor.log 184 bitsadmin /setnoprogresstimeout "%%i" 30 >>mor.log 185 bitsadmin /resume "%%i" >>mor.log 186 :MOR_DOWNLOAD_START 187 if "%job_id%" == "" ( 188 timeout /t 2 >nul 189 goto :MOR_DOWNLOAD_START 190 ) 191 for /f %%f in ('bitsadmin /rawreturn /getstate %job_id%') do ( 192 set dstate=%%f 193 ) 194 goto :BITS_%dstate% || ( 195 goto :BITS_TRANSFERRING 196 ) 197 198 :BITS_CANCELED 199 :BITS_SUSPENDED 200 :BITS_TRANSIENT_ERROR 201 :BITS_ERROR 202 :BITS_Unable 203 bitsadmin /rawreturn /cancel %job_id% >>mor.log 204 setlocal DisableDelayedExpansion 205 exit /b 1 206 goto :eof 207 :BITS_TRANSFERRING 208 :BITS_CONNECTING 209 for /f %%b in ('bitsadmin /rawreturn /getbytestransferred "%job_id%"') do ( 210 <nul set /p"=%%b!CR!" 211 ) 212 :BITS_CONNECTING 213 :BITS_ACKNOWLEDGED 214 :BITS_QUEUED 215 :BITS_Wait 216 :BITS_Wait2 217 :BITS_ERWait 218 timeout /t 1 >nul 219 goto:MOR_DOWNLOAD_START 220 :BITS_TRANSFERRED 221 222 bitsadmin /rawreturn /complete "%job_id%" >>mor.log 223 ) 224 endlocal 225 goto :eof 226 227 :main 228 setlocal EnableDelayedExpansion 229 :parse 230 set arg=%~1 231 if "%~1" == "" goto :MOR_MAIN_CONTINUE 232 if "%~1" == "-v" ( 233 echo mor v%mor_version% 234 goto :eof 235 ) else if "%arg%" == "-c" ( 236 set config_file="%~2" 237 call :logi -c !config_file! 238 shift 239 ) else if "%arg%" == "-d" ( 240 set /a is_logi=1 241 ) else if "%arg%" == "-D" ( 242 set d=%~2 243 call :logi -D !d!=%~3 244 shift 245 shift 246 ) else if "%arg:~0,2%" == "-D" ( 247 set arg=%~1 248 set d=%arg:~2% 249 call :logi -D!d!=%~2 250 shift 251 ) else if "%arg%" == "-" ( 252 echo mor: invalid argument '-' 253 ) else ( 254 set targets=%targets% %~1 255 ) 256 if "%arg:~0,1%" == "=" echo "= command" 257 258 shift 259 goto :parse 260 :MOR_MAIN_CONTINUE 261 call :read_ini "%config_file%" 262 if ERRORLEVEL 1 ( 263 echo ^^^! Error 264 exit /b !ERRORLEVEL! 265 ) 266 endlocal DisableDelayedExpansion 267 goto :eof 268 269 endlocal