100 lines
2.1 KiB
Batchfile
100 lines
2.1 KiB
Batchfile
@echo off
|
|
setlocal
|
|
setlocal enabledelayedexpansion
|
|
|
|
set tools_dir=%~dp0
|
|
set repo_dir=%~dp0..
|
|
set venv_dir=%~dp0..\.venv
|
|
set requirements_txt=
|
|
set force=0
|
|
|
|
:parse_args
|
|
if "%~1"=="" goto parsing_done
|
|
if "%~1"=="-d" goto dir_flag
|
|
if "%~1"=="--dir" goto dir_flag
|
|
if "%~1"=="-r" goto requirements_flag
|
|
if "%~1"=="--requirements" goto requirements_flag
|
|
if "%~1"=="-f" goto force_flag
|
|
if "%~1"=="--force" goto force_flag
|
|
if "%~1"=="-h" goto print_help_and_exit 0
|
|
if "%~1"=="--help" goto print_help_and_exit 0
|
|
|
|
:positional_arg
|
|
if "%requirements_txt%"=="" (
|
|
set "requirements_txt=%~1"
|
|
shift
|
|
goto parse_args
|
|
) else (
|
|
echo Unexpected argument "%~1"
|
|
goto print_help_and_exit 1
|
|
)
|
|
|
|
:dir_flag
|
|
if "%~2"=="" (
|
|
echo Virtual environment dir [-d,--dir] must be non-empty
|
|
goto print_help_and_exit 1
|
|
)
|
|
set venv_dir="%~2"
|
|
shift
|
|
shift
|
|
goto parse_args
|
|
|
|
:requirements_flag
|
|
if "%~2"=="" (
|
|
echo Requirements file [-r,--requirements] must be non-empty
|
|
goto print_help_and_exit 1
|
|
)
|
|
set requirements_txt="%~2"
|
|
shift
|
|
shift
|
|
goto parse_args
|
|
|
|
:force_flag
|
|
set force=1
|
|
shift
|
|
goto parse_args
|
|
|
|
:parsing_done
|
|
if "!requirements_txt!"=="" (
|
|
set "requirements_txt=%repo_dir%\requirements.txt"
|
|
)
|
|
if exist %venv_dir% (
|
|
if "!force!"=="1" (
|
|
echo Removing python virtual environment
|
|
rmdir /S /Q %venv_dir%
|
|
goto create_venv
|
|
) else (
|
|
goto install_requirements
|
|
)
|
|
)
|
|
|
|
:create_venv
|
|
echo Creating python virtual environment
|
|
@call python -m venv %venv_dir%
|
|
@call %venv_dir%\Scripts\python -m pip install --upgrade pip
|
|
goto install_requirements
|
|
|
|
:install_requirements
|
|
echo Installing requirements '!requirements_txt!'
|
|
@call %venv_dir%\Scripts\pip install -r !requirements_txt!
|
|
goto END
|
|
|
|
:print_help_and_exit
|
|
echo Usage: ensure_venv.cmd [OPTIONS] requirements
|
|
echo.
|
|
echo Positionals:
|
|
echo requirements The requirements file (defaults to 'requirements.txt')
|
|
echo.
|
|
echo Options:
|
|
echo -d,--dir The virtual environment dir
|
|
echo -r,--requirements FILE The requirements file (defaults to 'requirements.txt')
|
|
echo -h,--help Print this help message and exit
|
|
echo.
|
|
EXIT /B %~1
|
|
|
|
:END
|
|
EXIT /B %ERRORLEVEL%
|
|
|
|
:ERROR
|
|
EXIT /B 1
|