2011-03-23 12:18:13 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Remove the given apt repository.
|
|
|
|
#
|
|
|
|
# Exit with:
|
|
|
|
# 0: if it worked
|
|
|
|
# 1: if not
|
|
|
|
# 2: on other error
|
|
|
|
|
2012-01-16 23:50:12 +00:00
|
|
|
import os
|
2011-03-23 12:18:13 +00:00
|
|
|
import sys
|
|
|
|
from aptsources import distro, sourceslist
|
|
|
|
from softwareproperties import ppa
|
|
|
|
from softwareproperties.SoftwareProperties import SoftwareProperties
|
|
|
|
|
|
|
|
|
2012-01-16 23:50:12 +00:00
|
|
|
def remove_if_empty(file_name):
|
|
|
|
with open(file_name, 'r') as f:
|
|
|
|
if f.read().strip():
|
|
|
|
return
|
|
|
|
os.unlink(file_name)
|
|
|
|
|
2011-03-23 12:18:13 +00:00
|
|
|
def remove_repository(repository):
|
|
|
|
#print 'repository:', repository
|
|
|
|
codename = distro.get_distro().codename
|
|
|
|
#print 'codename:', codename
|
|
|
|
(line, file) = ppa.expand_ppa_line(repository.strip(), codename)
|
|
|
|
#print 'line:', line
|
|
|
|
#print 'file:', file
|
2012-01-16 23:50:12 +00:00
|
|
|
deb_source_entry = sourceslist.SourceEntry(line, file)
|
|
|
|
src_source_entry = sourceslist.SourceEntry('deb-src{}'.format(line[3:]), file)
|
2011-03-23 12:18:13 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
sp = SoftwareProperties()
|
2012-01-16 23:50:12 +00:00
|
|
|
sp.remove_source(deb_source_entry)
|
|
|
|
try:
|
|
|
|
# If there's a deb-src entry, remove that too
|
|
|
|
sp.remove_source(src_source_entry)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
remove_if_empty(file)
|
2011-03-23 12:18:13 +00:00
|
|
|
return True
|
|
|
|
except ValueError:
|
|
|
|
print >> sys.stderr, "Error: '%s' doesn't exists in a sourcelist file" % line
|
|
|
|
return False
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if (len(sys.argv) != 2):
|
|
|
|
print >> sys.stderr, 'Error: need a repository as argument'
|
|
|
|
sys.exit(2)
|
|
|
|
repository = sys.argv[1]
|
|
|
|
if remove_repository(repository):
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
sys.exit(1)
|