gentoo: use /var/run for new /run compatibility
[util-vserver.git] / python / libvserver.py
index 3f204b9..327a462 100644 (file)
@@ -2,6 +2,7 @@
 # 
 # $Id$
 # Copyright (C) 2008 Daniel Hokka Zakrisson
+# vim:set ts=4 sw=4 expandtab:
 # 
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -16,8 +17,6 @@
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
-# 
-# vim:set ts=4 sw=4 expandtab:
 
 import _libvserver
 
@@ -26,14 +25,14 @@ class struct:
     def __init__(self, *args, **kwargs):
         l = len(args)
         if l > len(self._fields_):
-            raise KeyError, "%s has only %d fields" % (self.__class__,
-                                                       len(self._fields_))
+            raise KeyError("%s has only %d fields" % (self.__class__,
+                                                      len(self._fields_)))
         for i in range(0, l):
             self.__dict__[self._fields_[i]] = args[i]
         for i in kwargs.iterkeys():
             if i not in self._fields_:
-                raise KeyError, "%s has no such field '%s'" % (self.__class__,
-                                                               i)
+                raise KeyError("%s has no such field '%s'" % (self.__class__,
+                                                              i))
             self.__dict__[i] = kwargs[i]
     def __totuple(self):
         return tuple(self.__dict__.get(f, self._default_) for f in self._fields_)
@@ -42,14 +41,17 @@ class struct:
     def __repr__(self):
         return repr(self.__dict__)
     def __addsub(self, other, negator):
+        import copy
+        c = copy.deepcopy(self)
         for i in vars(other):
             if i in self._fields_:
-                self.__dict__[i] = (self.__dict__.get(i, self._default_) +
-                                    (negator * getattr(other, i)))
+                c.__dict__[i] = (self.__dict__.get(i, self._default_) +
+                                 (negator * getattr(other, i)))
+        return c
     def __add__(self, other):
-        self.__addsub(other, 1)
+        return self.__addsub(other, 1)
     def __sub__(self, other):
-        self.__addsub(other, -1)
+        return self.__addsub(other, -1)
 
 get_vci = _libvserver.vc_get_vci
 
@@ -60,9 +62,9 @@ def ctx_create(xid, flags=None):
     if flags is None:
         flags = (0, 0)
     elif not isinstance(flags, struct_ctx_flags):
-        raise TypeError, "flags must be of type struct_ctx_flags"
+        raise TypeError("flags must be of type struct_ctx_flags")
     return _libvserver.vc_ctx_create(xid, *flags)
-def ctx_migrate(xid, flags=0L):
+def ctx_migrate(xid, flags=0):
     return _libvserver.vc_ctx_migrate(xid, flags)
 
 class struct_ctx_stat(struct):
@@ -82,7 +84,7 @@ def get_cflags(xid):
     return struct_ctx_flags(*_libvserver.vc_get_cflags(xid))
 def set_cflags(xid, flags):
     if not isinstance(flags, struct_ctx_flags):
-        raise TypeError, "flags must be of type struct_ctx_flags"
+        raise TypeError("flags must be of type struct_ctx_flags")
     _libvserver.vc_set_cflags(xid, *flags)
 
 class struct_ctx_caps(struct):
@@ -91,7 +93,7 @@ def get_ccaps(xid):
     return struct_ctx_caps(*_libvserver.vc_get_ccaps(xid))
 def set_ccaps(xid, caps):
     if not isinstance(caps, struct_ctx_caps):
-        raise TypeError, "caps must be of type struct_ctx_caps"
+        raise TypeError("caps must be of type struct_ctx_caps")
     _libvserver.vc_set_ccaps(xid, *caps)
 
 class struct_vx_info(struct):
@@ -114,7 +116,7 @@ def get_rlimit(xid, resource):
     return struct_rlimit(*_libvserver.vc_get_rlimit(xid, resource))
 def set_rlimit(xid, resource, limit):
     if not isinstance(limit, struct_rlimit):
-        raise TypeError, "limit must be of type struct_rlimit"
+        raise TypeError("limit must be of type struct_rlimit")
     _libvserver.vc_set_rlimit(xid, resource, *limit)
 
 class stuct_rlimit_stat(struct):
@@ -134,12 +136,12 @@ class struct_net_addr(struct):
 
 def net_add(nid, addr):
     if not isinstance(addr, struct_net_addr):
-        raise TypeError, "addr must be of type struct_net_addr"
+        raise TypeError("addr must be of type struct_net_addr")
     _libvserver.vc_net_add(nid, *addr)
 
 def net_remove(nid, addr):
     if not isinstance(addr, struct_net_addr):
-        raise TypeError, "addr must be of type struct_net_addr"
+        raise TypeError("addr must be of type struct_net_addr")
     _libvserver.vc_net_remove(nid, *addr)
 
 class struct_net_flags(struct):
@@ -148,7 +150,7 @@ def get_nflags(nid):
     return struct_net_flags(*_libvserver.vc_get_nflags(nid))
 def set_nflags(nid, flags):
     if not isinstance(flags, struct_net_flags):
-        raise TypeError, "flags must be of type struct_net_flags"
+        raise TypeError("flags must be of type struct_net_flags")
     _libvserver.vc_set_nflags(nid, *flags)
 
 class struct_net_caps(struct):
@@ -157,7 +159,7 @@ def get_ncaps(nid):
     return struct_net_caps(*_libvserver.vc_get_ncaps(nid))
 def set_ncaps(nid, caps):
     if not isinstance(caps, struct_net_caps):
-        raise TypeError, "caps must be of type struct_net_caps"
+        raise TypeError("caps must be of type struct_net_caps")
     _libvserver.vc_set_ncaps(nid, *caps)
 
 def _vc_set_iattr(f, obj, tag, flags, mask):
@@ -177,7 +179,7 @@ def vhi_type(type):
     if isinstance(type, int):
         return type
     else:
-        return _libvserver.__dict__["vcVHI_" + repr(type)]
+        return _libvserver.__dict__["vcVHI_%s" % type]
 def set_vhi_name(xid, type, val):
     _libvserver.vc_set_vhi_name(xid, vhi_type(type), val)
 def get_vhi_name(xid, type):
@@ -198,7 +200,7 @@ class struct_ctx_dlimit(struct):
     _default_ = _libvserver.VC_CDLIM_KEEP
 def set_dlimit(path, tag, limit, flags=0):
     if not isinstance(limit, struct_ctx_dlimit):
-        raise TypeError, "limit must be of type struct_ctx_dlimit"
+        raise TypeError("limit must be of type struct_ctx_dlimit")
     _libvserver.vc_set_dlimit(path, tag, flags, *limit)
 def get_dlimit(path, tag, flags=0):
     return struct_ctx_dlimit(*_libvserver.vc_get_dlimit(path, tag, flags))
@@ -219,7 +221,7 @@ class struct_set_sched(struct):
             self.set_mask |= _libvserver.__dict__.get("VC_VXSM_" + f, 0)
 def set_sched(xid, sched):
     if not isinstance(sched, struct_set_sched):
-        raise TypeError, "sched must be of type struct_set_sched"
+        raise TypeError("sched must be of type struct_set_sched")
     sched.fill_set_mask()
     _libvserver.vc_set_sched(xid, *sched)
 def get_sched(xid, cpu_id=0, bucket_id=0):
@@ -230,7 +232,21 @@ class struct_sched_info(struct):
                 "token_usec", "vavavoom"]
 def sched_info(xid, cpu_id=-1, bucket_id=0):
     if cpu_id == -1:
-        return None
+        import os
+        ret = struct_sched_info()
+        ncpus = os.sysconf("SC_NPROCESSORS_ONLN")
+        seen = 0
+        # * 2 is to make sure we get all the processors. CPU hot-plug...
+        for cpu in range(0, ncpus * 2):
+            try:
+                ret += struct_sched_info(*_libvserver.vc_sched_info(xid,
+                                                                    cpu, 0))
+                seen += 1
+            except:
+                pass
+            if seen == ncpus:
+                break
+        return ret
     else:
         return struct_sched_info(*_libvserver.vc_sched_info(xid, cpu_id,
                                                                bucket_id))
@@ -260,7 +276,7 @@ def tagopt2tag(opt, honor_static=True):
 def text2bcap(text):
     ret = _libvserver.vc_text2bcap(text)
     if ret == 0:
-        raise ValueError, "%s is not a valid bcap" % text
+        raise ValueError("%s is not a valid bcap" % text)
     return ret
 lobcap2text = _libvserver.vc_lobcap2text
 def bcap2list(bcaps):
@@ -278,7 +294,7 @@ def list2bcap(list):
 def text2ccap(text):
     ret = _libvserver.vc_text2ccap(text)
     if ret == 0:
-        raise ValueError, "%s is not a valid ccap" % text
+        raise ValueError("%s is not a valid ccap" % text)
     return ret
 loccap2text = _libvserver.vc_loccap2text
 def ccap2list(ccaps):
@@ -296,7 +312,7 @@ def list2ccap(list):
 def text2cflag(text):
     ret = _libvserver.vc_text2cflag(text)
     if ret == 0:
-        raise ValueError, "%s is not a valid cflag" % text
+        raise ValueError("%s is not a valid cflag" % text)
     return ret
 locflag2text = _libvserver.vc_locflag2text
 def cflag2list(cflags):
@@ -313,7 +329,7 @@ def list2cflag(list):
 def text2nflag(text):
     ret = _libvserver.vc_text2nflag(text)
     if ret == 0:
-        raise ValueError, "%s is not a valid nflag" % text
+        raise ValueError("%s is not a valid nflag" % text)
     return ret
 lonflag2text = _libvserver.vc_lonflag2text
 def nflag2list(nflags):
@@ -330,7 +346,7 @@ def list2nflag(list):
 def text2ncap(text):
     ret = _libvserver.vc_text2ncap(text)
     if ret == 0:
-        raise ValueError, "%s is not a valid ncap" % text
+        raise ValueError("%s is not a valid ncap" % text)
     return ret
 loncap2text = _libvserver.vc_loncap2text
 def ncap2list(ncaps):