emerge --info: show free ram/swap, bug #451048
authorZac Medico <zmedico@gentoo.org>
Wed, 16 Jan 2013 14:11:55 +0000 (06:11 -0800)
committerZac Medico <zmedico@gentoo.org>
Wed, 16 Jan 2013 14:11:55 +0000 (06:11 -0800)
pym/_emerge/actions.py
pym/portage/util/_get_vm_info.py [new file with mode: 0644]

index ac9a60fc0bf067640ce2f64723b4c0c2234ff5d7..589680a51af4ec5963dbb304a41397c873dde69f 100644 (file)
@@ -25,6 +25,7 @@ portage.proxy.lazyimport.lazyimport(globals(),
        'portage.dbapi._similar_name_search:similar_name_search',
        'portage.debug',
        'portage.news:count_unread_news,display_news_notifications',
+       'portage.util._get_vm_info:get_vm_info',
        '_emerge.chk_updated_cfg_files:chk_updated_cfg_files',
        '_emerge.help:help@emerge_help',
        '_emerge.post_emerge:display_news_notification,post_emerge',
@@ -1488,6 +1489,18 @@ def action_info(settings, trees, myopts, myfiles):
        append(header_width * "=")
        append("System uname: %s" % (platform.platform(aliased=1),))
 
+       vm_info = get_vm_info()
+       if "ram.total" in vm_info:
+               line = "%-9s %10d total" % ("KiB Mem:", vm_info["ram.total"] / 1024)
+               if "ram.free" in vm_info:
+                       line += ",%10d free" % (vm_info["ram.free"] / 1024,)
+               append(line)
+       if "swap.total" in vm_info:
+               line = "%-9s %10d total" % ("KiB Swap:", vm_info["swap.total"] / 1024)
+               if "swap.free" in vm_info:
+                       line += ",%10d free" % (vm_info["swap.free"] / 1024,)
+               append(line)
+
        lastSync = portage.grabfile(os.path.join(
                settings["PORTDIR"], "metadata", "timestamp.chk"))
        if lastSync:
diff --git a/pym/portage/util/_get_vm_info.py b/pym/portage/util/_get_vm_info.py
new file mode 100644 (file)
index 0000000..e8ad938
--- /dev/null
@@ -0,0 +1,80 @@
+# Copyright 2013 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+
+import os
+import platform
+import subprocess
+
+from portage import _unicode_decode
+
+def get_vm_info():
+
+       vm_info = {}
+
+       if platform.system() == 'Linux':
+               try:
+                       proc = subprocess.Popen(["free"],
+                               stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+               except OSError:
+                       pass
+               else:
+                       output = _unicode_decode(proc.communicate()[0])
+                       if proc.wait() == os.EX_OK:
+                               for line in output.splitlines():
+                                       line = line.split()
+                                       if len(line) < 2:
+                                               continue
+                                       if line[0] == "Mem:":
+                                               try:
+                                                       vm_info["ram.total"] = int(line[1]) * 1024
+                                               except ValueError:
+                                                       pass
+                                               if len(line) > 3:
+                                                       try:
+                                                               vm_info["ram.free"] = int(line[3]) * 1024
+                                                       except ValueError:
+                                                               pass
+                                       elif line[0] == "Swap:":
+                                               try:
+                                                       vm_info["swap.total"] = int(line[1]) * 1024
+                                               except ValueError:
+                                                       pass
+                                               if len(line) > 3:
+                                                       try:
+                                                               vm_info["swap.free"] = int(line[3]) * 1024
+                                                       except ValueError:
+                                                               pass
+
+       else:
+
+               try:
+                       proc = subprocess.Popen(["sysctl", "-a"],
+                               stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+               except OSError:
+                       pass
+               else:
+                       output = _unicode_decode(proc.communicate()[0])
+                       if proc.wait() == os.EX_OK:
+                               for line in output.splitlines():
+                                       line = line.split(":", 1)
+                                       if len(line) != 2:
+                                               continue
+                                       line[1] = line[1].strip()
+                                       if line[0] == "hw.physmem":
+                                               try:
+                                                       vm_info["ram.total"] = int(line[1])
+                                               except ValueError:
+                                                       pass
+                                       elif line[0] == "vm.swap_total":
+                                               try:
+                                                       vm_info["swap.total"] = int(line[1])
+                                               except ValueError:
+                                                       pass
+                                       elif line[0] == "Free Memory Pages":
+                                               if line[1][-1] == "K":
+                                                       try:
+                                                               vm_info["ram.free"] = int(line[1][:-1]) * 1024
+                                                       except ValueError:
+                                                               pass
+
+       return vm_info