"""Simple utility to help determine what's eating up your diskspace. It might just be old transactions, in which case a pack will do or it might be some big objects somewhere. To install, move this file to the Extensions folder and to the ZMI and an External Method called spaceman which is located in the module spaceman and the function spaceman. To run, just click on the test tab of the added External Method. This obviously doesn't work with objects that store some of their data outside of the main database, but who knows, maybe that'll be implemented later. :) Questions and comments can be sent to morten@nidelven-it.no """ class dummy_file: def __init__(self): self._len = 0 def write(self, data): self._len += len(data) def writelines(self, lines): for line in lines: self.write(line) def __len__(self): return self._len def get_id(id): if callable(id): return id() else: return id def pretty_size(size): if size < 1024: return "%s bytes" % size elif size < 1024*1024: return "%.2f KBs" % (size / 1024.0) elif size < 1024*1024*1024: return "%.2f MBs" % (size / (1024.0 * 1024.0)) else: return "%.2f GBs" % (size / (1024.0 * 1024.0 * 1024.0)) def get_space_usage(self): """Lists the storage space used.""" usage = [] for object in self.objectValues(): dummy = dummy_file() object._p_jar.exportFile(object._p_oid, dummy) # File starts with ZEXP usage.append([get_id(object.id), len(dummy) - 4, object.objectIds()]) usage[-1].append(pretty_size(usage[-1][1])) return usage usage_template = """ Space used Sort by space usageSort by ID %s
Object Size View
""" usage_entry_template = "%s%s%s" def spaceman(self, sort="usage"): usage = get_space_usage(self) if sort == "usage": sort_func = lambda x,y: cmp(x[1], y[1]) else: sort_func = lambda x,y: cmp(x[0], y[0]) usage.sort(sort_func) html = "" for entry in usage: if entry[2]: link = '%s' % (entry[0], sort, entry[0]) else: link = entry[0] html += usage_entry_template % (link, entry[3], 'View' % entry[0]) return usage_template % html