jump to content

I had a requirement to find out the main offenders in terms of file uploads on a Zope site! Here's an External Method function that sums up the object sizes. To install, create an External Method(?) and add this function to the method script.

def folder_size(folder=None, 
    types_to_inspect=['File','Image'],
    detail=0, recursive=0,
    folderish_objects=['Folder']):
    """Given a folder object, sums up the size of every object specified in types_to_inspect

       and returns a list tuple (number_of_objects, total_size, details)
       details = {} if detail is 0. Else it is a dictionary of object_name:(size,meta_type)
       if recursive is 1, then the folderish_objects will be included in the sizing up
    """
    if folder is None: return None
    total_size = 0
    number_of_objects = 0
    details = {}
    if recursive:
        types_to_inspect = types_to_inspect + folderish_objects
    for (id,o) in folder.objectItems(types_to_inspect):
        if (recursive) and (o.meta_type in folderish_objects):
            sub_obj_detail = folder_size(o, types_to_inspect, detail, recursive, folderish_objects)
            number_of_objects = number_of_objects + sub_obj_detail[0]
            total_size = total_size + sub_obj_detail[1]
            if detail:
                details[id] = (sub_obj_detail[1], o.meta_type)
                for sub_key in sub_obj_detail[2].keys():
                    details[id+'/'+sub_key] = sub_obj_detail[2][sub_key]
            continue

        try: #not all objects have getSize. So put it in exception
            this_size = int(o.getSize())
            total_size = total_size + this_size
            number_of_objects = number_of_objects + 1
            if detail:
                details[id] = (this_size, o.meta_type)
        except:
            pass
    return (number_of_objects,total_size,details)