How to remove ppa using python apt module?
I can add a ppa using it, but I cannot remove it. I cannot find the correct syntax for removing ppa from sources.list. Here's my code:
import aptsources.sourceslist as s
repo = ('deb', 'http://ppa.launchpad.net/danielrichter2007/grub-customizer/ubuntu', 'xenial', ['main'])
sources = s.SourcesList()
sources.add(repo)
sources.save()
#doesn't work
sources.remove(repo)
I tried to read the documents I found here , but I still cannot find the format to callsources.remove(repo)
The text SourcesList.remove()
reads remove(source_entry)
, indicating that it needs an object SourceEntry
. However, it sources.add()
returns an object SourceEntry
:
import aptsources.sourceslist as sl
sources = sl.SourcesList()
entry = sources.add('deb', 'mirror://mirrors.ubuntu.com/mirrors.txt', 'xenial', ['main'])
print(type(entry))
Outputs:
<class 'aptsources.sourceslist.SourceEntry'>
To delete an entry:
sources.remove(entry)
sources.save()
You can also disable it (which will leave a commented out entry in sources.list
:
entry.set_enabled(False)
sources.save()
I am using this for deletion for now.
import fileinput
filename = '/etc/apt/sources.list'
word = 'grub-customizer'
n = ""
remove = fileinput.input(filename, inplace=1)
for line in remove:
if word in line:
line = n
line.strip()
print line,
remove.close()