Source

Gentoo-count_pkg_in_overlays / count_pkg_in_ovrly.py

Full commit
#!/usr/bin/env python
### imports ###
from subprocess import call

# First create the list of installed packages
print "# eix -I > instl"
call("eix -I > instl", shell=True)

# First load the list of installed packages as a list of lines
print "# count packages in overlays"
f = open("instl", "r")
instl = f.read().splitlines()
f.close()

#: A list of numbers for overlays
overlays = ["[" + str(i) + "]" for i in range(100)]
#: The counts for each overlay. 
counts = {}
#: The corresponding names
names = {}

# Now just count the number of times, the overlay number appers in the data. 
for number in overlays: 
	for line in instl: 
		if number in line: 
			# if we don't yet have an entry for the overlay, 
			# we create one with 0! 
			# reason: every overlay appears again at the end. 
			if not number in counts: 
				counts[number] = 0
			else: 
				counts[number] += 1

# Now we clean out empty entries from counts. 
for i in counts: 
	if not counts[i]: 
		del counts[i]

# And parse the names
# For this we just grab the first appearance of the number in the 
# reversed list of lines, but only for the actually existing overlays. 
instl.reverse()
for number in counts: 
	for line in instl: 
		if number in line: 
			names[number] = line
			break

# finally sort the overlays by the number of appearances and print it
# put counts in a list we can sort by the number of appearances. 
overlay_list = []
for number in counts: 
	overlay_list.append((counts[number], number))

overlay_list.sort()
# Print the highest count first
overlay_list.reverse()

# now print the result
for count, number in overlay_list: 
	print count, names[number]