Skip to content

Instantly share code, notes, and snippets.

@ynniv
Created September 2, 2009 20:32
Show Gist options
  • Save ynniv/179943 to your computer and use it in GitHub Desktop.
Save ynniv/179943 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2.6
## MacPorts needless dependency cleaning. Edit 'keep' first!!!
#
## requires python >= 2.6!
#
## Vincent Fiano <ynniv@ynniv.com>
# FIXME: the packages that you care about go here!
keep = ['libidl', 'graphviz', 'bash-completion']
from subprocess import Popen,PIPE
def shellExec(*args):
return Popen(args, stdout=PIPE).communicate()[0].split('\n')
installed = [l.split(' ')[2] for l in shellExec("port", "installed")[1:-1]]
def dependentsOf(p):
lines = shellExec("port", "dependents", p)
if len(lines) > 0 and "has no dependents" in lines[0]:
return []
return [l.split(' ')[0] for l in lines[:-1]]
deptree = dict([[p, dependentsOf(p)] for p in installed])
def prune(p):
if p in deptree: del(deptree[p])
for k in deptree.keys():
try:
deptree[k].remove(p)
except ValueError:
pass # they say this is more "pythonic" than checking first
def remove(p):
print shellExec("sudo", "port", "uninstall", p)
prune(p)
def findIndependents():
return [p for p in deptree.keys() if len(deptree[p]) == 0]
toRemove = []
for p in keep:
del(deptree[p])
independents = findIndependents()
while (len(independents) > 0):
toRemove.extend(independents)
[prune(p) for p in independents]
independents = findIndependents()
print "independents: %s" % independents
if len(toRemove) > 0:
print "remove %s?" % ' '.join(toRemove)
answer = ''
while not answer in ["yes", "no"]:
print "type 'yes' or 'no'"
answer = raw_input()
if answer == "yes":
[remove(p) for p in toRemove]
else:
print "nice and tidy!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment