MOON
Server: Apache
System: Linux vps.thepromohut.com 2.6.18-398.el5 #1 SMP Tue Sep 16 20:51:48 EDT 2014 i686
User: caretrak (507)
PHP: 5.2.10
Disabled: NONE
Upload Files
File: //usr/lib/Acronis/PyTools/commands/retention.py
import acrort
import inspect
import os
import sys


OBJECT_TO_DELETE_PART_SIZE = 1000


def get_retention_specs(is_cloud):
    file_name = inspect.getframeinfo(inspect.currentframe()).filename
    dir_here = os.path.dirname(file_name)
    sys.path.insert(0, dir_here)
    if is_cloud:
        from retention_specs import RETENTION_SPECS_CLOUD
        return RETENTION_SPECS_CLOUD
    else:
        from retention_specs import RETENTION_SPECS_ONPREM
        return RETENTION_SPECS_ONPREM


def get_objects_count(pattern, dml):
    options = [('.Counter.CounterObjectTemplate.ID', 'guid', '936A68BE-4EE6-4AEB-B67F-1C2191CC9EAD'),
               ('.Counter.CounterObjectTemplate.ID^PrimaryKey', 'nil', None)]
    count_result = dml.select1(acrort.dml.ViewSpec(pattern=acrort.plain.Unit(flat=pattern), options=acrort.plain.Unit(flat=options)))
    return count_result.CounterValue.ref


def get_delete_part_size(objects_count, max_objects_count):
    objects_to_delete = objects_count - max_objects_count
    return min(objects_to_delete, max(OBJECT_TO_DELETE_PART_SIZE, int(objects_to_delete * 0.1)))


def run_logic(connection, retention_specs):
    deleted_objects = 0
    for spec in retention_specs:
        objects_limit = spec.get("objects_count_limit")
        objects_count = get_objects_count(spec["pattern"], connection.dml)
        if (objects_count > objects_limit):
            objects_to_delete = get_delete_part_size(objects_count, objects_limit)
            options = [('.LimitOptions', 'dword', objects_to_delete)]
            if spec["historical_order"] is True:
                options.append(('.SortingOptions.DmlTimeStamp', 'sqword', 0))
                options.append(('.SortingOptions.DmlTimeStamp^Ascending', 'nil', None))
            objects_spec = acrort.dml.ViewSpec(pattern=acrort.plain.Unit(flat=spec["pattern"]), options=acrort.plain.Unit(flat=options))
            deleted_objects += objects_to_delete
            connection.dml.delete(query=objects_spec)

    return deleted_objects


def execute_retention(is_cloud, *args, **kw):
    context = acrort.remoting.CommandContext(*args, **kw)    
    deleted_objects = run_logic(context.connection, get_retention_specs(is_cloud))
    result = [('.DeletedObjectsCount', 'dword', deleted_objects)]
    return acrort.plain.Unit(flat=result)


def execute_retention_cloud(*args, **kw):
    return execute_retention(True, *args, **kw)


def execute_retention_onprem(*args, **kw):
    return execute_retention(False, *args, **kw)