[pypy-svn] r58036 - pypy/build/doc/tool

hpk at codespeak.net hpk at codespeak.net
Wed Sep 10 12:51:43 CEST 2008


Author: hpk
Date: Wed Sep 10 12:51:41 2008
New Revision: 58036

Added:
   pypy/build/doc/tool/
   pypy/build/doc/tool/sysinfo.py   (contents, props changed)
Log:
adding a small script (requires py/trunk) to get 
various machine info from the hosts listed in doc/ssh_config


Added: pypy/build/doc/tool/sysinfo.py
==============================================================================
--- (empty file)
+++ pypy/build/doc/tool/sysinfo.py	Wed Sep 10 12:51:41 2008
@@ -0,0 +1,116 @@
+
+import py
+import sys
+mydir = py.magic.autopath().dirpath()
+ssh_config = mydir.dirpath("ssh_config")
+
+class SshConfig(object):
+    def __init__(self, path):
+        self.path = path
+    
+    def parsehosts(self, ignores):
+        if isinstance(ignores, str):
+            ignores = ignores.split(",")
+        l = []
+        rex = py.std.re.compile(r'Host\s*(\S+)')
+        for line in self.path.readlines():
+            m = rex.match(line)
+            if m is not None:
+                sshname, = m.groups()
+                if sshname not in ignores:
+                    l.append(sshname)
+        return l
+
+class RemoteInfo:
+    def __init__(self, gateway):
+        self.gw = gateway
+        self._cache = {}
+
+    def exreceive(self, execstring):
+        if execstring not in self._cache:
+            channel = self.gw.remote_exec(execstring)
+            self._cache[execstring] = channel.receive()
+        return self._cache[execstring]
+
+    def getmodattr(self, modpath):
+        module = modpath.split(".")[0]
+        return self.exreceive("""
+            import %s
+            channel.send(%s)
+        """ %(module, modpath))
+
+    def islinux(self):
+        return self.getmodattr('sys.platform').find("linux") != -1
+
+    def getmemswap(self):
+        if self.islinux():
+            return self.exreceive(""" 
+            import commands, re
+            out = commands.getoutput("free")
+            mem = re.search(r"Mem:\s+(\S*)", out).group(1)
+            swap = re.search(r"Swap:\s+(\S*)", out).group(1)
+            channel.send((mem, swap))
+            """)
+
+    def getcpuinfo(self):
+        if self.islinux():
+            return self.exreceive(""" 
+                numcpus = 0
+                model = None
+                for line in open("/proc/cpuinfo"):
+                    if not line.strip(): 
+                        continue
+                    key, value = line.split(":")
+                    key = key.strip()
+                    if key == "processor":
+                        numcpus += 1
+                    elif key == "model name":
+                        model = value.strip()
+                channel.send((numcpus, model))
+            """)
+
+def debug(*args):
+    print >>sys.stderr, " ".join(map(str, args))
+def error(*args):
+    debug("ERROR", args[0] + ":", *args[1:])
+
+def getinfo(sshname, loginfo=sys.stdout):
+    debug("connecting to", sshname)
+    try:
+        gw = py.execnet.SshGateway(sshname, ssh_config=ssh_config)
+    except IOError:
+        error("could not get sshagteway", sshname)
+    else:
+        ri = RemoteInfo(gw)
+        #print "%s info:" % sshname
+        prefix = sshname.upper() + " "
+        for attr in (
+            "sys.platform", 
+            "sys.version_info", 
+        ):
+            loginfo.write("%s %s: " %(prefix, attr,))
+            loginfo.flush()
+            value = ri.getmodattr(attr)
+            loginfo.write(str(value))
+            loginfo.write("\n")
+            loginfo.flush()
+        memswap = ri.getmemswap()
+        if memswap:
+            mem,swap = memswap
+            print >>loginfo, prefix, "Memory:", mem, "Swap:", swap 
+        cpuinfo = ri.getcpuinfo()
+        if cpuinfo:
+            numcpu, model = cpuinfo
+            print >>loginfo, prefix, "number of cpus:",  numcpu
+            print >>loginfo, prefix, "cpu model", model 
+        return ri
+            
+if __name__ == '__main__':
+    if len(sys.argv) < 2:
+        sc = SshConfig(ssh_config)
+        hosts = sc.parsehosts(ignores="t20,snake")
+    else:
+        hosts = sys.argv[1:]
+    for host in hosts:
+        getinfo(host)
+        



More information about the Pypy-commit mailing list