Ubuntu – Automatically disable a ppa

12.04aptppa

Is there a way to automatically delete or disable a ppa that does not work anymore?

When I do apt-get update I get a lot of error message regarding some ppas that cant be found.

I have disabled them by hand for now, but is there a way to disable them automatically? Since I have many ppas it is a lot of work to search and disable them by hand.

Best Answer

You can use a script to disable/enable a ppa very easily. Save the script as toggle_ppa.sh and put it in /usr/local/bin

#!/bin/bash
#
# toggle_ppa.sh
#
# DESCRIPTION:  Detects if a PPA is active/inactive and deactivates/activates it
#               on user confirmation.

SOURCEDIRECTORY=/etc/apt/sources.list.d
PPA="$1"     
if [ -z "$PPA" ]
then
    echo "Error: Please provide a PPA name to toggle between activation/deactivation"
    echo "The PPA name should be formatted as it appears on launchpad, e.g.:
"$0" ppa:webupd8team/y-ppa-manager"
    exit 1
fi

## Root privileges

if [ "$(whoami)" != "root" ]; then
  echo "Error: This script needs root privileges. Restarting..."
  sudo "$0" "$1"
  exit
fi

### MAIN

SOURCELIST_NOPFX="${PPA#*:}" #remove 'ppa:' prefix
SOURCELIST="${SOURCELIST_NOPFX////-}"-$(lsb_release -cs) #replace all slashes with dashes, include release
SOURCEFILE="$SOURCEDIRECTORY"/"$SOURCELIST".list #compose sources list path

if [ -e "$SOURCEFILE" ]
then
    echo "Processing $SOURCEFILE..."
    SOURCE_COMMENTED=$(grep "^\#deb\ " "$SOURCEFILE") #check if sources line is commented
    if [ -z "$SOURCE_COMMENTED" ]
    then
        echo "$PPA is active. Going to deactivate. Proceed? [ y/n ]"
        read ANSWER
        if [ $ANSWER == "y" ]
        then
            sed -i "s/^deb\-src/\#deb\-src/" $SOURCEFILE
            sed -i "s/^deb\ http/\#deb\ http/" $SOURCEFILE
            echo "Updating package index files..."
            sudo apt-get update
            echo "Done."
        else
            echo "Aborted."
            exit 0
        fi
    else
        echo "$PPA is inactive. Going to activate. Proceed? [ y/n ]"
        read ANSWER
        if [ $ANSWER == "y" ]
        then
            sed -i "s/^\#deb\-src/deb\-src/" $SOURCEFILE
            sed -i "s/^\#deb\ http/deb\ http/" $SOURCEFILE
            echo "Updating package index files..."
            sudo apt-get update
            echo "Done."
        else
            echo "Aborted."
            exit 0
        fi
    fi
else
    echo "Error: Source file at $SOURCEFILE for $PPA does not exist. Please check PPA name."
    exit 0
fi

Usage

sudo toggle_ppa.sh <full-ppa-name>

Example

sudo toggle_ppa.sh ppa:webupd8team/java

How it works

When any ppa repo is deactivated using Software center (GUI), it actually puts a # in front of all the lines inside corresponding ppa .list file at /etc/apt/sources.list.d/. Usually most of the .list files contain two lines that begins with deb http and deb-src, a few have only one. The above script is doing the same thing using sed. First it checks the ppa .list (input by user) is present or not. Then put or remove a # in front the lines accordingly.

The code acts in a very interactive manner. When someone runs this along with ppa name as its argument, it will display the PPA's current status and what the code is going to do on successful execution. Then it will ask permission of the user. Only if the user inputs 'y' to confirm the code will change the status of the PPA and activate/deactivate it. It will immediately abort if the user puts an 'n' for no.

Note I proposed this code in this answered. glutanimate helped a lot to improve the code.

Related Question