|
#!/bin/bash |
|
|
|
# Define color codes |
|
GREEN='\033[1;32m' # Info |
|
YELLOW='\033[1;33m' # Warning |
|
RED='\033[1;31m' # Error |
|
NC='\033[0m' # No Color |
|
|
|
# Helper functions to print messages |
|
info() { echo -e "${GREEN}[INFO]${NC} $1"; } |
|
warn() { echo -e "${YELLOW}[WARNING]${NC} $1"; } |
|
error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; } |
|
|
|
# Function to display help |
|
print_help() { |
|
echo -e "Usage: ./setup_root_env.sh [OPTIONS]" |
|
echo -e "" |
|
echo -e "Options:" |
|
echo -e " -h, --help Show this help message and exit" |
|
echo -e " -e, --env ENV_NAME Specify the name of the Conda environment to create (default: root-env)" |
|
echo -e " -p, --packages PACKAGES Additional packages to install (comma-separated)" |
|
echo -e " -c, --channels CHANNELS Conda channels to use (comma-separated, default: conda-forge)" |
|
echo -e " -v, --python-version VERSION Specify the Python version to use (default: 3.9)" |
|
} |
|
|
|
# Default values |
|
ENV_NAME="root-env" |
|
PACKAGES="" |
|
CHANNELS="conda-forge" |
|
PYTHON_VERSION="3.9" |
|
|
|
# Parse arguments |
|
while [[ $# -gt 0 ]]; do |
|
case $1 in |
|
-h|--help) |
|
print_help |
|
exit 0 |
|
;; |
|
-e|--env) |
|
ENV_NAME="$2" |
|
shift 2 |
|
;; |
|
-p|--packages) |
|
PACKAGES="$2" |
|
shift 2 |
|
;; |
|
-c|--channels) |
|
CHANNELS="$2" |
|
shift 2 |
|
;; |
|
-v|--python-version) |
|
PYTHON_VERSION="$2" |
|
shift 2 |
|
;; |
|
*) |
|
error "Unknown option: $1. Use -h or --help for usage." |
|
;; |
|
esac |
|
done |
|
|
|
# Check if Conda is installed |
|
if ! command -v conda &> /dev/null; then |
|
error "Conda is not installed. Please install Conda or Miniconda and try again." |
|
fi |
|
|
|
# Convert comma-separated lists to space-separated lists |
|
IFS=',' read -r -a CHANNEL_LIST <<< "$CHANNELS" |
|
CHANNEL_ARGS="" |
|
for CHANNEL in "${CHANNEL_LIST[@]}"; do |
|
CHANNEL_ARGS+="-c $CHANNEL " |
|
done |
|
|
|
IFS=',' read -r -a PACKAGE_LIST <<< "$PACKAGES" |
|
|
|
# Step 1: Remove existing environment if requested |
|
if conda info --envs | grep -qE "^\s*$ENV_NAME\s"; then |
|
warn "Environment '$ENV_NAME' already exists." |
|
read -p "Do you want to remove it and create a fresh environment? (y/N): " choice |
|
if [[ "$choice" =~ ^[Yy]$ ]]; then |
|
info "Removing existing environment '$ENV_NAME'..." |
|
conda remove -n "$ENV_NAME" --all -y || error "Failed to remove environment '$ENV_NAME'." |
|
info "Environment '$ENV_NAME' removed successfully." |
|
else |
|
info "Exiting without making changes." |
|
exit 0 |
|
fi |
|
fi |
|
|
|
# Step 2: Create a new environment |
|
info "Creating new Conda environment '$ENV_NAME' with ROOT and Python $PYTHON_VERSION..." |
|
CREATE_CMD="conda create -n \"$ENV_NAME\" python=\"$PYTHON_VERSION\" root ${PACKAGE_LIST[@]} ${CHANNEL_ARGS} -y" |
|
eval $CREATE_CMD || error "Failed to create environment '$ENV_NAME'." |
|
|
|
# Step 3: Activate the environment |
|
info "Activating the environment..." |
|
|
|
# Ensure Conda is initialized |
|
CONDA_BASE=$(conda info --base) # Get the base directory of Conda |
|
source "$CONDA_BASE/etc/profile.d/conda.sh" || error "Failed to initialize Conda." |
|
|
|
# Activate the environment |
|
conda activate "$ENV_NAME" || error "Failed to activate environment '$ENV_NAME'." |
|
|
|
|
|
# Step 4: Install Jupyter kernel |
|
info "Installing Jupyter kernel..." |
|
conda install ipykernel -y || error "Failed to install Jupyter kernel." |
|
python -m ipykernel install --user --name="$ENV_NAME" --display-name "Python ($ENV_NAME)" || error "Failed to register Jupyter kernel." |
|
|
|
# Step 5: Create activation and deactivation scripts |
|
info "Setting up environment variables for ROOT..." |
|
|
|
# Create activation script |
|
ACTIVATE_SCRIPT="$CONDA_PREFIX/etc/conda/activate.d/env_vars.sh" |
|
mkdir -p "$(dirname "$ACTIVATE_SCRIPT")" |
|
cat > "$ACTIVATE_SCRIPT" <<EOF |
|
export ROOTSYS=\$CONDA_PREFIX |
|
export LD_LIBRARY_PATH=\$CONDA_PREFIX/lib:\$LD_LIBRARY_PATH |
|
export PYTHONPATH=\$CONDA_PREFIX/lib:\$PYTHONPATH |
|
EOF |
|
|
|
# Create deactivation script |
|
DEACTIVATE_SCRIPT="$CONDA_PREFIX/etc/conda/deactivate.d/env_vars.sh" |
|
mkdir -p "$(dirname "$DEACTIVATE_SCRIPT")" |
|
cat > "$DEACTIVATE_SCRIPT" <<EOF |
|
unset ROOTSYS |
|
unset LD_LIBRARY_PATH |
|
unset PYTHONPATH |
|
EOF |
|
|
|
info "Environment variable scripts created successfully." |
|
|
|
# Step 6: Test PyROOT |
|
info "Testing PyROOT setup..." |
|
python -c "import ROOT; print(ROOT.__version__)" &> /dev/null |
|
if [[ $? -eq 0 ]]; then |
|
info "PyROOT is working correctly." |
|
else |
|
error "PyROOT is not working. Please check the installation or environment setup." |
|
fi |
|
|
|
info "Environment '$ENV_NAME' has been successfully set up!" |
|
info "You can now use it with VSCode or Jupyter Notebook. Select the 'Python ($ENV_NAME)' kernel." |