56c253b54810c7a9559b110808b2b2ae4815b35d
[findit] / src / pkgsearch.py
1 #!/usr/bin/python
2 # -*-coding: utf-8 -*-
3 # vim: sw=4 ts=4 expandtab ai
4
5 import apt_pkg
6 from heapq import nlargest
7
8 def pkggetter(cache):
9     for pkg in cache.Packages:
10         # pkg is from a list of packages, sorted by name.
11         if pkg.CurrentState == apt_pkg.CurStateInstalled:
12            yield [version.InstalledSize for version in pkg.VersionList][0], pkg.Name
13
14 def size_convert(size):
15         """Return string with file size in b or Kb or Mb or Gb or Tb."""
16         for i, unit in enumerate(['%d b', '%.1f Kb', '%.2f Mb', '%.3f Gb', '%.4f Tb']):
17             if size < 1024**(i+1):
18                 return unit % (size/1024.**i)
19         return '>1024 Tb'
20
21 def main():
22     """The main function."""
23     apt_pkg.InitConfig()
24     apt_pkg.InitSystem()
25     
26     cache = apt_pkg.GetCache()
27     count = 50
28     for size, packg in nlargest(count, pkggetter(cache)):
29         print packg, size_convert(size)
30
31 if __name__ == "__main__":
32     main()