<s> from django . db . models . signals import post_save <EOL> from django . dispatch import receiver <EOL> import pdb <EOL> from generate . dependency_walker import * <EOL> from synchronizers . openstack import model_policies <EOL> from xos . logger import logger <EOL> from datetime import datetime <EOL> import time <EOL> from core . models import * <EOL> from django . db import reset_queries <EOL> from django . db . transaction import atomic <EOL> from django . db . models import F , Q <EOL> modelPolicyEnabled = True <EOL> bad_instances = [ ] <EOL> def EnableModelPolicy ( x ) : <EOL> global modelPolicyEnabled <EOL> modelPolicyEnabled = x <EOL> def update_wp ( d , o ) : <EOL> try : <EOL> save_fields = [ ] <EOL> if ( d . write_protect != o . write_protect ) : <EOL> d . write_protect = o . write_protect <EOL> save_fields . append ( '<STR_LIT>' ) <EOL> if ( save_fields ) : <EOL> d . save ( update_fields = save_fields ) <EOL> except AttributeError , e : <EOL> raise e <EOL> def update_dep ( d , o ) : <EOL> try : <EOL> print '<STR_LIT>' % d <EOL> save_fields = [ ] <EOL> if ( d . updated < o . updated ) : <EOL> save_fields = [ '<STR_LIT>' ] <EOL> if ( save_fields ) : <EOL> d . save ( update_fields = save_fields ) <EOL> except AttributeError , e : <EOL> raise e <EOL> except Exception , e : <EOL> logger . info ( '<STR_LIT>' % ( d , e ) , extra = d . tologdict ( ) ) <EOL> def delete_if_inactive ( d , o ) : <EOL> try : <EOL> d . delete ( ) <EOL> print "<STR_LIT>" % ( d , d . __class__ . __name__ ) <EOL> except : <EOL> pass <EOL> return <EOL> def execute_model_policy ( instance , deleted ) : <EOL> if ( instance in bad_instances ) : <EOL> return <EOL> delete_policy_models = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> sender_name = instance . __class__ . __name__ <EOL> policy_name = '<STR_LIT>' % sender_name <EOL> noargs = False <EOL> if ( not deleted ) : <EOL> walk_inv_deps ( update_dep , instance ) <EOL> walk_deps ( update_wp , instance ) <EOL> elif ( sender_name in delete_policy_models ) : <EOL> walk_inv_deps ( delete_if_inactive , instance ) <EOL> try : <EOL> policy_handler = getattr ( model_policies , policy_name , None ) <EOL> logger . error ( "<STR_LIT>" % ( policy_name , policy_handler ) ) <EOL> if policy_handler is not None : <EOL> if ( deleted ) : <EOL> try : <EOL> policy_handler . handle_delete ( instance ) <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> policy_handler . handle ( instance ) <EOL> except : <EOL> logger . log_exc ( "<STR_LIT>" ) <EOL> try : <EOL> instance . policed = datetime . now ( ) <EOL> instance . save ( update_fields = [ '<STR_LIT>' ] ) <EOL> except : <EOL> logging . error ( '<STR_LIT>' % instance ) <EOL> bad_instances . append ( instance ) <EOL> def noop ( o , p ) : <EOL> pass <EOL> def run_policy ( ) : <EOL> while ( True ) : <EOL> start = time . time ( ) <EOL> run_policy_once ( ) <EOL> if ( time . time ( ) - start < <NUM_LIT:1> ) : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> def run_policy_once ( ) : <EOL> from core . models import Instance , Slice , Controller , Network , User , SlicePrivilege , Site , SitePrivilege , Image , ControllerSlice , ControllerUser , ControllerSite <EOL> models = [ Controller , Site , SitePrivilege , Image , ControllerSlice , ControllerSite , ControllerUser , User , Slice , Network , Instance , SlicePrivilege ] <EOL> objects = [ ] <EOL> deleted_objects = [ ] <EOL> for m in models : <EOL> res = m . objects . filter ( ( Q ( policed__lt = F ( '<STR_LIT>' ) ) | Q ( policed = None ) ) & Q ( no_policy = False ) ) <EOL> objects . extend ( res ) <EOL> res = m . deleted_objects . filter ( Q ( policed__lt = F ( '<STR_LIT>' ) ) | Q ( policed = None ) ) <EOL> deleted_objects . extend ( res ) <EOL> for o in objects : <EOL> execute_model_policy ( o , o . deleted ) <EOL> for o in deleted_objects : <EOL> execute_model_policy ( o , True ) <EOL> reaped = [ Slice ] <EOL> for m in reaped : <EOL> dobjs = m . deleted_objects . all ( ) <EOL> for d in dobjs : <EOL> deps = walk_inv_deps ( noop , d ) <EOL> if ( not deps ) : <EOL> print '<STR_LIT>' % d <EOL> d . delete ( purge = True ) <EOL> try : <EOL> reset_queries ( ) <EOL> except : <EOL> logger . log_exc ( "<STR_LIT>" ) </s>
