plotPopconData.py 2.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#!/usr/bin/env python
#
# print the popcon data as a graph with x = pkgnr, y=popcon-value
#

from optparse import OptionParser
import glob
from heapq import heappush, heappop
import gd
from math import log, log10, floor
  
def gatherPopconData(menudir):
    popcon = {}
    for dentry in glob.glob("%s/*.desktop" % menudir):
        pkgname = None
        pop = None
        for line in open(dentry):
            if line.startswith("X-AppInstall-Package"):
                pkgname = line.strip().split("=")[1]
            elif line.startswith("X-AppInstall-Popcon="):
                pop = int(line.strip().split("=")[1])
            if pkgname != None and pop != None:
                popcon[pkgname] = pop
    return popcon

def plotPopcon(popcon, outfile):
    log_basis = 10
    y_scale = 10
    popcon_max = max(popcon.values())
    size = (len(popcon), 5*y_scale)
    heap = []
    # get a sorted version
    for pkg in popcon:
        heappush(heap, (popcon[pkg], pkg))
    # now print the ranking
    ranks = { 1: 0,
              2: 0,
              3: 0,
              4: 0,
              5: 0,
              6: 0}
    while heap:
        (pop, pkg) = heappop(heap)
        rank = 0
        #rank = int((5 * pop / popcon_max))
        if pop > 0:
            rank = 5*log(pop,log_basis)/log(popcon_max, log_basis)
        ranks[int(rank)+1] += 1
    for i in ranks:
        print i,ranks[i]

    # allocate image data
    img = gd.image(size)
    img.colorAllocate((255,255,255))
    # draw it
    i = 0
    last_rank = 0
    heap = []
    # get a sorted version
    for pkg in popcon:
        heappush(heap, (popcon[pkg], pkg))
    while heap:
        (pop, pkg) = heappop(heap)
        rank = 1
        if pop > 0:
            #pop = int(log(pop,log_basis))+1
            rank = int(5*log(pop,log_basis)/log(popcon_max, log_basis))+1
        img.line((i-1, size[1]-(last_rank*y_scale)),
                 (i, size[1]-(rank*y_scale)),
                 1)
        i+=1
        last_rank = rank
    img.writePng(outfile)

if __name__ == "__main__":
    parser = OptionParser()
    parser.add_option("-d", "--direcroty", dest="dir",
                      default="../menu-data",
                      help="directory for the menu-data")
    parser.add_option("-f", "--file", dest="file",
                      default="popcon.png",
                      help="filename of the output plot")
    (options, args) = parser.parse_args()
    popcon = gatherPopconData(options.dir)
    plotPopcon(popcon, options.file)