+MAX_MEMORY_TOTAL = 512
+MAX_MEMORY_SINGLE = 256
+MIN_MEMORY_SINGLE = 16
+MAX_DISK_TOTAL = 50
+MAX_DISK_SINGLE = 50
+MIN_DISK_SINGLE = 0.1
+MAX_VMS_TOTAL = 10
+MAX_VMS_ACTIVE = 4
+
+def getMachinesByOwner(owner):
+ """Return the machines owned by a given owner."""
+ return Machine.select_by(owner=owner)
+
+def maxMemory(user, machine=None, on=None):
+ """Return the maximum memory for a machine or a user.
+
+ If machine is None, return the memory available for a new
+ machine. Else, return the maximum that machine can have.
+
+ on is a dictionary from machines to booleans, whether a machine is
+ on. If None, it is recomputed. XXX make this global?
+ """
+
+ machines = getMachinesByOwner(user.username)
+ if on is None:
+ on = getUptimes(machines)
+ active_machines = [x for x in machines if on[x]]
+ mem_usage = sum([x.memory for x in active_machines if x != machine])
+ return min(MAX_MEMORY_SINGLE, MAX_MEMORY_TOTAL-mem_usage)