Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A Python tool that downgrades packages to not use an undesired repository in debian-based systems (think ppa-purge, except ppa-purge failed me in this case). Right now hardcoded to get a faulty sparkleshare repo out. Warning: incomplete and perhaps hacky. It worked for the purpose I needed it to, YMMV.
#!/usr/bin/env python
#The MIT License (MIT)
#Copyright © 2014 Ryan Pavlik <ryan.pavlik@gmail.com>
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the “Software”), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.
import apt
cache = apt.Cache()
cache.open(None)
main_pkg = cache["sparkleshare"]
current_ver = main_pkg.installed
print(current_ver)
def get_external_origins(origins):
return filter(lambda origin: origin.archive != "now", origins)
external_origins = get_external_origins(current_ver.origins)
if len(external_origins) > 1:
print("Ambiguous main package: current version installable from multiple origins:")
print(external_origins)
bad_origin = external_origins[0]
#print("Targeting the following bad origin:")
#print(bad_origin)
class BadOrigin:
def __init__(self, bad_origin):
self.bad = bad_origin
self.origin_string = bad_origin.origin
def is_usable_version(self, ver):
acceptable_origins = filter(lambda origin: origin.origin != self.origin_string, get_external_origins(ver.origins))
return len(acceptable_origins) > 0
def get_usable_versions(self, pkg):
return filter(lambda ver: self.is_usable_version(ver), pkg.versions)
def is_pkg_from_here(self, pkg):
if pkg.is_installed:
ver = pkg.installed
external_origins = get_external_origins(ver.origins)
return (len(external_origins) == 1 and external_origins[0].origin == self.origin_string)
return False
bad = BadOrigin(external_origins[0])
print(bad.is_pkg_from_here(main_pkg))
for pkg in cache:
if pkg.installed and pkg.candidate.downloadable and bad.is_pkg_from_here(pkg):
alternate_ver = max(bad.get_usable_versions(pkg))
print("{!s}: {!s} -> {!s}".format(pkg.name, pkg.installed.version, alternate_ver.version))
pkg.candidate = alternate_ver
pkg.mark_install(auto_fix=False, auto_inst = False, from_user=not pkg.is_auto_installed)
print(cache.get_changes())
#import apt_pkg
#apt_pkg.config.set("APT::Get::Simulate","true")
#apt_pkg.config.set("dir::cache","/tmp")
cache.commit(install_progress=apt.progress.base.InstallProgress())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment