PC & IT SUPPORT MADE EASY FORUM
Would you like to react to this message? Create an account in a few clicks or log in to continue.

bashrc function to auto installs missing linux programs and missing python modules!

Go down

bashrc function to auto installs missing linux programs and missing python modules! Empty bashrc function to auto installs missing linux programs and missing python modules!

Post by jamied_uk 24th September 2024, 14:56

bashrc function to auto installs missing linux programs and missing python modules! (even auto re runs the script that caused the auto install)!

to edit your bashrc file


Code:
sudo gedit ~/.bashrc



Code:
# Function to handle missing commands and install them
command_not_found_handle() {
    local cmd=$1
    # Check if the command is available
    if ! command -v "$cmd" &> /dev/null; then
        echo "Command '$cmd' not found. Installing..."
        sudo apt-get update -y
        sudo apt-get install -y "$cmd"
        # Check if the installation was successful
        if command -v "$cmd" &> /dev/null; then
            echo "Command '$cmd' installed successfully."
        else
            echo "Failed to install '$cmd'."
        fi
    else
        # If the command exists, handle it normally
        "$@"
    fi
}

# Override the default command_not_found_handle function
if [ -n "$BASH_VERSION" ]; then
    command_not_found_handle() {
        local cmd=$1
        if ! command -v "$cmd" &> /dev/null; then
            echo "Command '$cmd' not found. Installing..."
            sudo apt-get update -y
            sudo apt-get install -y "$cmd"
            if command -v "$cmd" &> /dev/null; then
                echo "Command '$cmd' installed successfully."
            else
                echo "Failed to install '$cmd'."
            fi
        else
            "$@"
        fi
    }
fi


# Function to handle missing Python modules and install them
pip_command_not_found_handle() {
    local cmd=$1

    echo "Attempting to install missing module '$cmd'..."

    # Activate the virtual environment if it exists
    if [ -d "venv" ]; then
        source venv/bin/activate
        echo "Using virtual environment: venv"
    else
        python -m venv myenv
        source myenv/bin/activate 
        
    fi

    # Attempt to install the missing package
    if pip install "$cmd"; then
        echo "Module '$cmd' installed successfully."
    else
        echo "Failed to install '$cmd'. Please install it manually."
        return 1
    fi
}

# Override the python command to automatically handle missing modules
python() {
    output=$(command python "$@" 2>&1)  # Capture the output and error of the Python command

    # Check for "ModuleNotFoundError" in the output
    if echo "$output" | grep -q "ModuleNotFoundError"; then
        local missing_module=$(echo "$output" | grep -oP "No module named '\K[^']+")

        if [ -n "$missing_module" ]; then
            echo "Module '$missing_module' not found! Attempting to install it..."
            pip_command_not_found_handle "$missing_module"

            # Retry the script after attempting to install the missing module
            echo "Retrying to run the Python command..."
            output=$(command python "$@")  # Run the command again
        fi
    fi

    # Output the result or error
    echo "$output"
}

# Ensure the function works with a script argument as well
alias runpy="python"
jamied_uk
jamied_uk
Admin

Posts : 3053
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum