+RLIMITS = { "NSOCK": VLIMIT_NSOCK,
+ "OPENFD": VLIMIT_OPENFD,
+ "ANON": VLIMIT_ANON,
+ "SHMEM": VLIMIT_SHMEM}
+
+# add in the platform supported rlimits
+for entry in resource.__dict__.keys():
+ if entry.find("RLIMIT_")==0:
+ k = entry[len("RLIMIT_"):]
+ if not RLIMITS.has_key(k):
+ RLIMITS[k]=resource.__dict__[entry]
+ else:
+ print "WARNING: duplicate RLIMITS key %s" % k
+
+class NoSuchVServer(Exception): pass
+
+
+class VServerConfig:
+ def __init__(self, name, directory):
+ self.name = name
+ self.dir = directory
+ if not (os.path.isdir(self.dir) and
+ os.access(self.dir, os.R_OK | os.W_OK | os.X_OK):
+ raise NoSuchVServer, "%s does not exist" % self.dir
+
+ def get(self, option, default = None):
+ try:
+ f = open(os.path.join(self.dir, option), "r")
+ buf = f.readline().rstrip()
+ f.close()
+ return buf
+ except IOError, e:
+ if default is not None:
+ return default
+ else:
+ raise KeyError, "Key %s is not set for %s" % (option, self.name)
+
+ def update(self, option, value):
+ try:
+ old_umask = os.umask(0022)
+ filename = os.path.join(self.dir, option)
+ try:
+ os.makedirs(os.path.dirname(filename), 0755)
+ except:
+ pass
+ f = open(filename, 'w')
+ if isinstance(value, list):
+ f.write("%s\n" % "\n".join(value))
+ else:
+ f.write("%s\n" % value)
+ f.close()
+ os.umask(old_umask)
+ except:
+ raise
+
+ def unset(self, option):
+ try:
+ filename = os.path.join(self.dir, option)
+ os.unlink(filename)
+ os.removedirs(os.path.dirname(filename))
+ return True
+ except:
+ return False
+