adding simple package search
author“Alexandr <“popov2al@gmail.com”>
Tue, 31 Mar 2009 09:19:58 +0000 (13:19 +0400)
committer“Alexandr <“popov2al@gmail.com”>
Tue, 31 Mar 2009 09:19:58 +0000 (13:19 +0400)
src/pkgsearch.py [new file with mode: 0644]

diff --git a/src/pkgsearch.py b/src/pkgsearch.py
new file mode 100644 (file)
index 0000000..56c253b
--- /dev/null
@@ -0,0 +1,32 @@
+#!/usr/bin/python
+# -*-coding: utf-8 -*-
+# vim: sw=4 ts=4 expandtab ai
+
+import apt_pkg
+from heapq import nlargest
+
+def pkggetter(cache):
+    for pkg in cache.Packages:
+        # pkg is from a list of packages, sorted by name.
+        if pkg.CurrentState == apt_pkg.CurStateInstalled:
+           yield [version.InstalledSize for version in pkg.VersionList][0], pkg.Name
+
+def size_convert(size):
+        """Return string with file size in b or Kb or Mb or Gb or Tb."""
+        for i, unit in enumerate(['%d b', '%.1f Kb', '%.2f Mb', '%.3f Gb', '%.4f Tb']):
+            if size < 1024**(i+1):
+                return unit % (size/1024.**i)
+        return '>1024 Tb'
+
+def main():
+    """The main function."""
+    apt_pkg.InitConfig()
+    apt_pkg.InitSystem()
+    
+    cache = apt_pkg.GetCache()
+    count = 50
+    for size, packg in nlargest(count, pkggetter(cache)):
+        print packg, size_convert(size)
+
+if __name__ == "__main__":
+    main()