|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Telegram Panel CLI Launcher |
| 4 | +# Smart script to manage virtual environment and launch interactive CLI |
| 5 | + |
| 6 | +set -e # Exit on error |
| 7 | + |
| 8 | +# Colors for output |
| 9 | +RED='\033[0;31m' |
| 10 | +GREEN='\033[0;32m' |
| 11 | +YELLOW='\033[1;33m' |
| 12 | +BLUE='\033[0;34m' |
| 13 | +NC='\033[0m' # No Color |
| 14 | + |
| 15 | +# Get the directory where the script is located |
| 16 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 17 | +cd "$SCRIPT_DIR" |
| 18 | + |
| 19 | +VENV_DIR="$SCRIPT_DIR/venv" |
| 20 | +VENV_PYTHON="$VENV_DIR/bin/python" |
| 21 | +VENV_ACTIVATE="$VENV_DIR/bin/activate" |
| 22 | +REQUIREMENTS_FILE="$SCRIPT_DIR/requirements.txt" |
| 23 | +CLI_SCRIPT="$SCRIPT_DIR/interactive_cli.py" |
| 24 | + |
| 25 | +# Function to print colored messages |
| 26 | +print_info() { |
| 27 | + echo -e "${BLUE}[INFO]${NC} $1" |
| 28 | +} |
| 29 | + |
| 30 | +print_success() { |
| 31 | + echo -e "${GREEN}[SUCCESS]${NC} $1" |
| 32 | +} |
| 33 | + |
| 34 | +print_warning() { |
| 35 | + echo -e "${YELLOW}[WARNING]${NC} $1" |
| 36 | +} |
| 37 | + |
| 38 | +print_error() { |
| 39 | + echo -e "${RED}[ERROR]${NC} $1" |
| 40 | +} |
| 41 | + |
| 42 | +# Check if virtual environment is already active |
| 43 | +check_venv_active() { |
| 44 | + if [[ -n "$VIRTUAL_ENV" ]]; then |
| 45 | + print_info "Virtual environment is already active: $VIRTUAL_ENV" |
| 46 | + return 0 |
| 47 | + else |
| 48 | + return 1 |
| 49 | + fi |
| 50 | +} |
| 51 | + |
| 52 | +# Check if venv directory exists |
| 53 | +check_venv_exists() { |
| 54 | + if [[ -d "$VENV_DIR" ]] && [[ -f "$VENV_PYTHON" ]]; then |
| 55 | + print_info "Virtual environment found at: $VENV_DIR" |
| 56 | + return 0 |
| 57 | + else |
| 58 | + return 1 |
| 59 | + fi |
| 60 | +} |
| 61 | + |
| 62 | +# Activate existing virtual environment |
| 63 | +activate_venv() { |
| 64 | + print_info "Activating virtual environment..." |
| 65 | + if [[ -f "$VENV_ACTIVATE" ]]; then |
| 66 | + source "$VENV_ACTIVATE" |
| 67 | + print_success "Virtual environment activated" |
| 68 | + return 0 |
| 69 | + else |
| 70 | + print_error "Activation script not found: $VENV_ACTIVATE" |
| 71 | + return 1 |
| 72 | + fi |
| 73 | +} |
| 74 | + |
| 75 | +# Create new virtual environment |
| 76 | +create_venv() { |
| 77 | + print_info "Creating new virtual environment..." |
| 78 | + |
| 79 | + # Check if python3 is available |
| 80 | + if ! command -v python3 &> /dev/null; then |
| 81 | + print_error "python3 is not installed or not in PATH" |
| 82 | + exit 1 |
| 83 | + fi |
| 84 | + |
| 85 | + # Get python version |
| 86 | + PYTHON_VERSION=$(python3 --version 2>&1 | awk '{print $2}') |
| 87 | + print_info "Using Python $PYTHON_VERSION" |
| 88 | + |
| 89 | + # Create venv |
| 90 | + if python3 -m venv "$VENV_DIR"; then |
| 91 | + print_success "Virtual environment created successfully" |
| 92 | + return 0 |
| 93 | + else |
| 94 | + print_error "Failed to create virtual environment" |
| 95 | + exit 1 |
| 96 | + fi |
| 97 | +} |
| 98 | + |
| 99 | +# Install requirements |
| 100 | +install_requirements() { |
| 101 | + print_info "Installing requirements..." |
| 102 | + |
| 103 | + if [[ ! -f "$REQUIREMENTS_FILE" ]]; then |
| 104 | + print_warning "Requirements file not found: $REQUIREMENTS_FILE" |
| 105 | + print_warning "Skipping requirements installation" |
| 106 | + return 0 |
| 107 | + fi |
| 108 | + |
| 109 | + # Check if pip is available |
| 110 | + if ! command -v pip &> /dev/null && ! "$VENV_PYTHON" -m pip --version &> /dev/null; then |
| 111 | + print_error "pip is not available" |
| 112 | + exit 1 |
| 113 | + fi |
| 114 | + |
| 115 | + # Upgrade pip first |
| 116 | + print_info "Upgrading pip..." |
| 117 | + "$VENV_PYTHON" -m pip install --upgrade pip --quiet |
| 118 | + |
| 119 | + # Install requirements |
| 120 | + print_info "Installing packages from requirements.txt..." |
| 121 | + if "$VENV_PYTHON" -m pip install -r "$REQUIREMENTS_FILE" --quiet; then |
| 122 | + print_success "Requirements installed successfully" |
| 123 | + return 0 |
| 124 | + else |
| 125 | + print_error "Failed to install requirements" |
| 126 | + exit 1 |
| 127 | + fi |
| 128 | +} |
| 129 | + |
| 130 | +# Check if CLI script exists |
| 131 | +check_cli_script() { |
| 132 | + if [[ ! -f "$CLI_SCRIPT" ]]; then |
| 133 | + print_error "CLI script not found: $CLI_SCRIPT" |
| 134 | + exit 1 |
| 135 | + fi |
| 136 | +} |
| 137 | + |
| 138 | +# Main execution |
| 139 | +main() { |
| 140 | + print_info "Telegram Panel CLI Launcher" |
| 141 | + print_info "==========================" |
| 142 | + |
| 143 | + # Check if CLI script exists |
| 144 | + check_cli_script |
| 145 | + |
| 146 | + # Check if venv is already active |
| 147 | + if check_venv_active; then |
| 148 | + print_success "Using active virtual environment" |
| 149 | + # Update VENV_PYTHON to use active venv's python |
| 150 | + if [[ -f "$VIRTUAL_ENV/bin/python" ]]; then |
| 151 | + VENV_PYTHON="$VIRTUAL_ENV/bin/python" |
| 152 | + fi |
| 153 | + else |
| 154 | + # Check if venv exists |
| 155 | + if check_venv_exists; then |
| 156 | + # Activate existing venv |
| 157 | + if ! activate_venv; then |
| 158 | + print_error "Failed to activate virtual environment" |
| 159 | + exit 1 |
| 160 | + fi |
| 161 | + else |
| 162 | + # Create new venv |
| 163 | + if ! create_venv; then |
| 164 | + exit 1 |
| 165 | + fi |
| 166 | + |
| 167 | + # Activate new venv |
| 168 | + if ! activate_venv; then |
| 169 | + print_error "Failed to activate newly created virtual environment" |
| 170 | + exit 1 |
| 171 | + fi |
| 172 | + |
| 173 | + # Install requirements |
| 174 | + install_requirements |
| 175 | + fi |
| 176 | + fi |
| 177 | + |
| 178 | + # Verify Python is available |
| 179 | + if ! command -v python &> /dev/null && ! command -v python3 &> /dev/null; then |
| 180 | + print_error "Python is not available" |
| 181 | + exit 1 |
| 182 | + fi |
| 183 | + |
| 184 | + # Use python from venv if available, otherwise use system python |
| 185 | + if [[ -n "$VIRTUAL_ENV" ]] && [[ -f "$VENV_PYTHON" ]]; then |
| 186 | + PYTHON_CMD="$VENV_PYTHON" |
| 187 | + elif command -v python3 &> /dev/null; then |
| 188 | + PYTHON_CMD="python3" |
| 189 | + elif command -v python &> /dev/null; then |
| 190 | + PYTHON_CMD="python" |
| 191 | + else |
| 192 | + print_error "Python interpreter not found" |
| 193 | + exit 1 |
| 194 | + fi |
| 195 | + |
| 196 | + print_info "Using Python: $($PYTHON_CMD --version 2>&1)" |
| 197 | + print_info "Python path: $(which $PYTHON_CMD)" |
| 198 | + |
| 199 | + # Launch CLI |
| 200 | + print_info "Launching Interactive CLI..." |
| 201 | + print_info "==========================" |
| 202 | + echo "" |
| 203 | + |
| 204 | + # Execute CLI script |
| 205 | + exec "$PYTHON_CMD" "$CLI_SCRIPT" "$@" |
| 206 | +} |
| 207 | + |
| 208 | +# Run main function |
| 209 | +main "$@" |
| 210 | + |
0 commit comments