Add --local and --no-env options to bootstrap scripts

- Add --no-env flag to skip virtual environment creation
- Add --local flag to install with --user (user-local installation)
- Update both bash and PowerShell scripts

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
nvaldez
2026-10-21 23:55:53 +02:00
parent 59c7fd73da
commit c38579fb1e
2 changed files with 195 additions and 69 deletions
+93 -13
View File
@@ -6,7 +6,7 @@
# source bootstrap
#
# Windows (PowerShell): dot-source bootstrap.ps1 instead
# . .\bootstrap.ps1 [--env name] [--python 3.x.x] [-c|--clean]
# . .\bootstrap.ps1 [--env name] [--python 3.x.x] [-c|--clean] [--no-env] [--local]
#
# What it does:
# 1. Installs pyenv and required system dependencies if needed
@@ -15,12 +15,18 @@
# 4. Activates the virtual environment
# 5. Installs the project in development mode with all dependencies
#
# With --clean/-c option:
# - Deletes existing environment before creating a new one
# Options:
# --pyenv Force reinstall pyenv
# -c, --clean Delete existing environment before creating a new one
# --env NAME Use a custom environment name (default: venv)
# --python VER Use a specific Python version (default: 3.11.8)
# --no-env Skip virtual environment creation, install to current Python
# --local Install package with --user flag (user-local installation)
#
# Usage:
# source bootstrap [--pyenv] [-c|--clean] [--env custom_name] [--python 3.x.x]
# source bootstrap [--pyenv] [-c|--clean] [--env custom_name] [--python 3.x.x] [--no-env] [--local]
# ex. source bootstrap --pyenv -c --env my_env --python 3.11.8
# ex. source bootstrap --no-env --local
# =============================================
# VARIABLES
@@ -29,6 +35,8 @@ PYTHON_VERSION="3.11.8" # Default Python version - change as needed
export PYENV_NAME="venv" # Default environment name
FORCE_PYENV_INSTALL=false # Flag to force pyenv reinstallation
CLEAN_ENVIRONMENT=false # Flag to delete and recreate environment
NO_ENVIRONMENT=false # Flag to skip virtual environment creation
LOCAL_INSTALL=false # Flag to install with --user
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Scripts root directory
# =============================================
@@ -61,8 +69,8 @@ check_python_installed() {
return 1
fi
# If pyenv is available, check if the specific version is installed
if command -v pyenv &> /dev/null; then
# If pyenv is available and we're using an environment, check if the specific version is installed
if [ "$NO_ENVIRONMENT" = false ] && command -v pyenv &> /dev/null; then
if ! pyenv versions | grep -q "$PYTHON_VERSION"; then
echo_status "Python $PYTHON_VERSION is not available in pyenv. Installing it now..."
if ! pyenv install "$PYTHON_VERSION"; then
@@ -77,6 +85,12 @@ check_python_installed() {
}
initialize_pyenv() {
# Skip if we're not using an environment
if [ "$NO_ENVIRONMENT" = true ]; then
echo_status "Skipping pyenv initialization (--no-env mode)"
return 0
fi
# Set up pyenv environment variables
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
@@ -141,7 +155,7 @@ install_pyenv_dependencies() {
echo_warning "You may need to install them manually"
return 1
}
elif [ "$(uname)" = "Darwin" ]; then
elif [ "$(unname)" = "Darwin" ]; then
echo_status "Detected macOS - ensuring required tools are available"
if ! command -v brew &> /dev/null; then
echo_warning "Homebrew (brew) not found. You may need to install pyenv dependencies manually."
@@ -169,6 +183,11 @@ install_pyenv() {
}
check_and_install_pyenv() {
# Skip if we're not using an environment
if [ "$NO_ENVIRONMENT" = true ]; then
return 0
fi
# Check if pyenv is already available
if command -v pyenv &> /dev/null && [ "$FORCE_PYENV_INSTALL" = false ]; then
echo_status "pyenv is already available"
@@ -203,6 +222,10 @@ check_and_install_pyenv() {
}
clean_environment() {
if [ "$NO_ENVIRONMENT" = true ]; then
return 0
fi
if pyenv virtualenvs | grep -q "$PYENV_NAME"; then
echo_status "Deleting existing virtual environment $PYENV_NAME..."
pyenv uninstall -f "$PYENV_NAME" >/dev/null 2>&1 || {
@@ -226,6 +249,11 @@ install_python_version() {
}
check_environment_exists() {
# Skip if we're not using an environment
if [ "$NO_ENVIRONMENT" = true ]; then
return 0
fi
# Clean if requested
if [ "$CLEAN_ENVIRONMENT" = true ]; then
clean_environment || return 1
@@ -251,6 +279,12 @@ check_environment_exists() {
}
activate_environment() {
# Skip if we're not using an environment
if [ "$NO_ENVIRONMENT" = true ]; then
echo_status "Skipping environment activation (--no-env mode)"
return 0
fi
echo_status "Activating $PYENV_NAME..."
# Try pyenv activate first
@@ -308,7 +342,24 @@ install_requirements() {
# Check if pyproject.toml exists in the current directory
if [ -f "$SCRIPT_DIR/pyproject.toml" ]; then
echo_status "Installing package in development mode from pyproject.toml..."
(cd "$SCRIPT_DIR" && pip install -e ".[dev]" --break-system-packages) || {
# Build the pip install command
local pip_cmd="pip install -e ."
# Add dev extras if not in --no-env mode
if [ "$NO_ENVIRONMENT" = false ]; then
pip_cmd="$pip_cmd[dev]"
fi
# Add --user flag if --local is specified
if [ "$LOCAL_INSTALL" = true ]; then
pip_cmd="$pip_cmd --user"
fi
# Add --break-system-packages for safety
pip_cmd="$pip_cmd --break-system-packages"
(cd "$SCRIPT_DIR" && eval "$pip_cmd") || {
echo_error "Failed to install package and dependencies from pyproject.toml"
return 1
}
@@ -361,9 +412,17 @@ while [[ $# -gt 0 ]]; do
return 1
fi
;;
--no-env)
NO_ENVIRONMENT=true
shift
;;
--local)
LOCAL_INSTALL=true
shift
;;
*)
echo_error "Unknown option: $1"
echo "Usage: source bootstrap [--pyenv] [-c|--clean] [--env custom_name] [--python 3.x.x]"
echo "Usage: source bootstrap [--pyenv] [-c|--clean] [--env custom_name] [--python 3.x.x] [--no-env] [--local]"
return 1
;;
esac
@@ -381,30 +440,42 @@ if [ "$FORCE_PYENV_INSTALL" = true ]; then
echo_status "Forcing pyenv reinstallation"
fi
if [ "$NO_ENVIRONMENT" = true ]; then
echo_status "Running in --no-env mode (no virtual environment will be created)"
fi
if [ "$LOCAL_INSTALL" = true ]; then
echo_status "Running in --local mode (will install with --user flag)"
fi
# 1. Check if Python is installed
if ! check_python_installed; then
return 1
fi
# 2. Check/install pyenv
# 2. Check/install pyenv (skip if --no-env)
if [ "$NO_ENVIRONMENT" = false ]; then
if ! check_and_install_pyenv; then
return 1
fi
# 3. Initialize pyenv
# 3. Initialize pyenv (skip if --no-env)
if ! initialize_pyenv; then
return 1
fi
# 4. Check/create virtual environment
# 4. Check/create virtual environment (skip if --no-env)
if ! check_environment_exists; then
return 1
fi
# 5. Activate the environment
# 5. Activate the environment (skip if --no-env)
if ! activate_environment; then
return 1
fi
else
echo_status "Using current Python environment (no venv)"
fi
# 6. Upgrade pip
upgrade_pip
@@ -414,11 +485,20 @@ install_requirements
# Final verification
echo_status "Bootstrap completed successfully!"
if [ "$NO_ENVIRONMENT" = false ]; then
echo_status "Virtual environment '$PYENV_NAME' is active"
else
echo_status "Using current Python environment"
fi
echo_status "Python: $(python --version) ($(which python))"
echo_status "Pip: $(pip --version)"
echo_status ""
echo_status "To verify installation, try:"
echo_status " agent-cost-estimator --help"
if [ "$NO_ENVIRONMENT" = false ]; then
echo_status " python -m pytest tests/"
fi
echo_status ""
echo_status "Happy coding!"
+48 -2
View File
@@ -7,10 +7,19 @@
# . .\bootstrap.ps1
# . .\bootstrap.ps1 --env ace_env
# . .\bootstrap.ps1 --env ace_env --python 3.11.8 -c
# . .\bootstrap.ps1 --no-env --local
#
# Creates a repo-local venv at .venvs\<name> with the Python launcher (py)
# or python on PATH. Does not install pyenv. --pyenv is accepted so the
# Linux flag still parses; on Windows it is a no-op notice.
#
# Options:
# --pyenv Ignored on Windows (pyenv not supported)
# -c, --clean Delete existing environment before creating a new one
# --env NAME Use a custom environment name (default: venv)
# --python VER Use a specific Python version (default: current)
# --no-env Skip virtual environment creation, install to current Python
# --local Install package with --user flag (user-local installation)
#Requires -Version 5.1
@@ -97,6 +106,8 @@ $Clean = $false
$EnvName = "venv"
$PythonVersion = ""
$Pyenv = $false
$NoEnv = $false
$LocalInstall = $false
$bootstrapArgs = @($args)
for ($i = 0; $i -lt $bootstrapArgs.Count; $i++) {
$token = [string]$bootstrapArgs[$i]
@@ -124,9 +135,15 @@ for ($i = 0; $i -lt $bootstrapArgs.Count; $i++) {
$i++
$PythonVersion = [string]$bootstrapArgs[$i]
}
elseif ($token -eq "--no-env") {
$NoEnv = $true
}
elseif ($token -eq "--local") {
$LocalInstall = $true
}
else {
Write-AceError "Unknown option: $token"
Write-AceError "Usage: . .\bootstrap.ps1 [--pyenv] [-c|--clean] [--env custom_name] [--python 3.x.x]"
Write-AceError "Usage: . .\bootstrap.ps1 [--pyenv] [-c|--clean] [--env custom_name] [--python 3.x.x] [--no-env] [--local]"
Remove-AceBootstrapFunctions
return
}
@@ -143,6 +160,14 @@ if ($Pyenv) {
Write-AceWarning "Windows bootstrap uses a local venv under .venvs\. It does not install pyenv."
}
if ($NoEnv) {
Write-AceStatus "Running in --no-env mode (no virtual environment will be created)"
}
if ($LocalInstall) {
Write-AceStatus "Running in --local mode (will install with --user flag)"
}
$repoRoot = $PSScriptRoot
if (-not $repoRoot) {
$repoRoot = (Get-Location).Path
@@ -157,6 +182,7 @@ catch {
# A GPO can refuse the process override. RemoteSigned still runs local scripts.
}
if (-not $NoEnv) {
$pythonExe = Resolve-AcePython -Version $PythonVersion
if (-not $pythonExe) {
Write-AceError "Python is not installed, or version '$PythonVersion' is not available to the py launcher."
@@ -207,6 +233,10 @@ if (-not $env:VIRTUAL_ENV) {
Remove-AceBootstrapFunctions
return
}
}
else {
Write-AceStatus "Using current Python environment (no venv)"
}
if ($env:PYTEST_ADDOPTS) {
$sanitized = $env:PYTEST_ADDOPTS -replace '--no-summary', '' -replace '(^|\s)-S(\s|$)', ' '
@@ -246,7 +276,14 @@ if ($LASTEXITCODE -ne 0) {
Write-AceStatus "Installing package and dependencies from pyproject.toml..."
Push-Location $repoRoot
try {
& python -m pip install -e ".[dev]"
$pipArgs = "-e ."
if (-not $NoEnv) {
$pipArgs += "[dev]"
}
if ($LocalInstall) {
$pipArgs += " --user"
}
& python -m pip install $pipArgs
if ($LASTEXITCODE -ne 0) {
Write-AceError "Failed to install package and dependencies from pyproject.toml"
Remove-AceBootstrapFunctions
@@ -263,13 +300,22 @@ $pythonVersionText = (& python --version 2>&1 | Out-String).Trim()
$pipVersionText = (& python -m pip --version 2>&1 | Out-String).Trim()
Write-AceStatus "Bootstrap completed successfully!"
if (-not $NoEnv) {
Write-AceStatus "Virtual environment '$EnvName' is active"
}
else {
Write-AceStatus "Using current Python environment"
}
Write-AceStatus "Python: $pythonVersionText ($pythonWhere)"
Write-AceStatus "Pip: $pipVersionText"
Write-AceStatus ""
Write-AceStatus "To verify installation, try:"
Write-AceStatus " agent-cost-estimator --help"
if (-not $NoEnv) {
Write-AceStatus " python -m pytest tests/"
}
Write-AceStatus ""
Write-AceStatus "Happy coding!"