<s> import sys <EOL> import time <EOL> import threading <EOL> import datetime <EOL> import traceback <EOL> import requests <EOL> from labmanager . db import db <EOL> from labmanager . models import RLMS as dbRLMS <EOL> from labmanager . application import app <EOL> from . base import register_blueprint , BaseRLMS , BaseFormCreator , Capabilities , Versions <EOL> from . caches import GlobalCache , VersionCache , InstanceCache , EmptyCache , get_cached_session , CacheDisabler , clean_cache <EOL> assert BaseFormCreator or register_blueprint or Versions or Capabilities or BaseRLMS or True <EOL> _RLMSs = { <EOL> } <EOL> _GLOBAL_PERIODIC_TASKS = [ <EOL> ] <EOL> _LOCAL_PERIODIC_TASKS = [ <EOL> ] <EOL> class Laboratory ( object ) : <EOL> def __init__ ( self , name , laboratory_id , description = None , autoload = False ) : <EOL> self . name = name <EOL> self . laboratory_id = laboratory_id <EOL> self . description = description <EOL> self . autoload = autoload <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . name , self . laboratory_id , self . description , self . autoload ) <EOL> def __hash__ ( self ) : <EOL> return hash ( self . laboratory_id ) <EOL> _current_task_id = <NUM_LIT:0> <EOL> def _next_task_id ( ) : <EOL> global _current_task_id <EOL> _current_task_id += <NUM_LIT:1> <EOL> return _current_task_id <EOL> class _RegistrationRecord ( object ) : <EOL> def __init__ ( self , name , versions ) : <EOL> self . name = name <EOL> self . versions = versions <EOL> global_key = '<STR_LIT>' % ( name , '<STR_LIT:U+002CU+0020>' . join ( versions ) ) <EOL> self . global_cache = self . cache = GlobalCache ( global_key ) <EOL> self . per_version_cache = { } <EOL> for version in versions : <EOL> version_key = '<STR_LIT>' % ( name , version ) <EOL> self . per_version_cache [ version ] = VersionCache ( version_key ) <EOL> self . per_thread = threading . local ( ) <EOL> def get_cache ( self , version = None ) : <EOL> if version is None : <EOL> return self . cache <EOL> else : <EOL> return self . version_cache [ version ] <EOL> @ property <EOL> def rlms_cache ( self ) : <EOL> current_rlms_id = getattr ( self . per_thread , '<STR_LIT>' , None ) <EOL> if current_rlms_id is not None : <EOL> return InstanceCache ( current_rlms_id ) <EOL> else : <EOL> return EmptyCache ( ) <EOL> @ property <EOL> def cached_session ( self ) : <EOL> cached_session = getattr ( self . per_thread , '<STR_LIT>' , None ) <EOL> if cached_session : <EOL> return cached_session <EOL> cached_session = get_cached_session ( ) <EOL> self . per_thread . cached_session = cached_session <EOL> def timeout_get ( url , timeout = ( <NUM_LIT:30> , <NUM_LIT> ) , max_retries = <NUM_LIT:3> , ** kwargs ) : <EOL> count = <NUM_LIT:0> <EOL> while True : <EOL> try : <EOL> return cached_session . get ( url , timeout = timeout , ** kwargs ) <EOL> except requests . Timeout : <EOL> count += <NUM_LIT:1> <EOL> if count >= max_retries : <EOL> raise <EOL> cached_session . timeout_get = timeout_get <EOL> return cached_session <EOL> def _add_periodic_task ( self , where , task_name , function , hours , minutes , disable_cache ) : <EOL> if hours == <NUM_LIT:0> and minutes == <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if hours < <NUM_LIT:0> or minutes < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> where . append ( { <EOL> '<STR_LIT:id>' : _next_task_id ( ) , <EOL> '<STR_LIT:name>' : task_name , <EOL> '<STR_LIT>' : self . name , <EOL> '<STR_LIT>' : self . versions , <EOL> '<STR_LIT>' : function , <EOL> '<STR_LIT>' : datetime . timedelta ( hours = hours , minutes = minutes ) , <EOL> '<STR_LIT>' : disable_cache , <EOL> } ) <EOL> def add_global_periodic_task ( self , task_name , function , hours = <NUM_LIT:0> , minutes = <NUM_LIT:0> , disable_cache = True ) : <EOL> return self . _add_periodic_task ( _GLOBAL_PERIODIC_TASKS , task_name , function , hours , minutes , disable_cache ) <EOL> def add_local_periodic_task ( self , task_name , function , hours = <NUM_LIT:0> , minutes = <NUM_LIT:0> , disable_cache = True ) : <EOL> return self . _add_periodic_task ( _LOCAL_PERIODIC_TASKS , task_name , function , hours , minutes , disable_cache ) <EOL> def is_debug ( self ) : <EOL> return '<STR_LIT>' in sys . argv or '<STR_LIT>' in sys . argv or '<STR_LIT>' in sys . argv <EOL> def _debug ( msg ) : <EOL> sys . stderr . flush ( ) <EOL> sys . stdout . flush ( ) <EOL> print u"<STR_LIT>" % ( time . ctime ( ) , msg ) <EOL> sys . stdout . flush ( ) <EOL> sys . stderr . flush ( ) <EOL> class TaskRunner ( object ) : <EOL> def __init__ ( self ) : <EOL> self . latest_executions = { <EOL> } <EOL> self . _stopping = False <EOL> def _now ( self ) : <EOL> return datetime . datetime . now ( ) . replace ( second = <NUM_LIT:0> , microsecond = <NUM_LIT:0> ) <EOL> def _must_be_run ( self , now , task ) : <EOL> if task [ '<STR_LIT:id>' ] not in self . latest_executions : <EOL> return True <EOL> time_elapsed = now - self . latest_executions [ task [ '<STR_LIT:id>' ] ] <EOL> if time_elapsed >= task [ '<STR_LIT>' ] : <EOL> return True <EOL> return False <EOL> def _run_all ( self ) : <EOL> cache_disabler = CacheDisabler ( ) <EOL> for task in _GLOBAL_PERIODIC_TASKS : <EOL> now = self . _now ( ) <EOL> if self . _must_be_run ( now , task ) : <EOL> for version in task [ '<STR_LIT>' ] : <EOL> _debug ( "<STR_LIT>" % ( task [ '<STR_LIT:name>' ] , task [ '<STR_LIT>' ] , version ) ) <EOL> if task [ '<STR_LIT>' ] : <EOL> cache_disabler . disable ( ) <EOL> try : <EOL> task [ '<STR_LIT>' ] ( ) <EOL> except Exception : <EOL> traceback . print_exc ( ) <EOL> finally : <EOL> cache_disabler . reenable ( ) <EOL> self . latest_executions [ task [ '<STR_LIT:id>' ] ] = now <EOL> for task in _LOCAL_PERIODIC_TASKS : <EOL> now = self . _now ( ) <EOL> if self . _must_be_run ( now , task ) : <EOL> with app . app_context ( ) : <EOL> for version in task [ '<STR_LIT>' ] : <EOL> rlmss = db . session . query ( dbRLMS ) . filter_by ( kind = task [ '<STR_LIT>' ] , version = version ) . all ( ) <EOL> for db_rlms in rlmss : <EOL> ManagerClass = get_manager_class ( db_rlms . kind , db_rlms . version , db_rlms . id ) <EOL> rlms = ManagerClass ( db_rlms . configuration ) <EOL> _debug ( u"<STR_LIT>" % ( repr ( task [ '<STR_LIT:name>' ] ) , repr ( task [ '<STR_LIT>' ] ) , repr ( version ) , repr ( db_rlms . location ) ) ) <EOL> if task [ '<STR_LIT>' ] : <EOL> cache_disabler . disable ( ) <EOL> try : <EOL> task [ '<STR_LIT>' ] ( rlms ) <EOL> except Exception : <EOL> traceback . print_exc ( ) <EOL> finally : <EOL> cache_disabler . reenable ( ) <EOL> self . latest_executions [ task [ '<STR_LIT:id>' ] ] = now <EOL> def _step ( self , initial ) : <EOL> before = self . _now ( ) <EOL> if before . hour == initial . hour and before . minute == initial . minute : <EOL> clean_cache ( ) <EOL> future = before + datetime . timedelta ( minutes = <NUM_LIT:1> ) <EOL> future = future . replace ( second = <NUM_LIT:0> , microsecond = <NUM_LIT:0> ) <EOL> self . _run_all ( ) <EOL> after = datetime . datetime . now ( ) <EOL> remaining_seconds = ( future - after ) . total_seconds ( ) <EOL> if remaining_seconds > <NUM_LIT:0> : <EOL> PIECES = <NUM_LIT> <EOL> per_second = remaining_seconds / PIECES <EOL> for _ in xrange ( PIECES ) : <EOL> if self . _stopping : <EOL> break <EOL> time . sleep ( per_second ) <EOL> else : <EOL> print "<STR_LIT>" % ( after - before ) . total_seconds ( ) <EOL> def run_forever ( self ) : <EOL> initial = self . _now ( ) <EOL> while not self . _stopping : <EOL> self . _step ( initial ) <EOL> def stop ( self ) : <EOL> self . _stopping = True <EOL> def register ( name , versions , module_name ) : <EOL> record = _RegistrationRecord ( name , versions ) <EOL> _RLMSs [ name ] = ( module_name , versions , record ) <EOL> return record <EOL> def get_supported_types ( ) : <EOL> return _RLMSs . keys ( ) <EOL> def get_supported_versions ( rlms_type ) : <EOL> if rlms_type in _RLMSs : <EOL> _ , versions , _ = _RLMSs [ rlms_type ] <EOL> return versions <EOL> return [ ] <EOL> def is_supported ( rlms_type , rlms_version ) : <EOL> _ , versions , _ = _RLMSs . get ( rlms_type , ( None , [ ] ) ) <EOL> return rlms_version in versions <EOL> def _get_module ( rlms_type , rlms_version ) : <EOL> module_name , versions , _ = _RLMSs . get ( rlms_type , ( None , [ ] ) ) <EOL> if rlms_version in versions : <EOL> if hasattr ( sys . modules [ module_name ] , '<STR_LIT>' ) : <EOL> return sys . modules [ module_name ] . get_module ( rlms_version ) <EOL> else : <EOL> return sys . modules [ module_name ] <EOL> else : <EOL> raise Exception ( u"<STR_LIT>" % dict ( rlmstype = rlms_type , rmlsversion = rlms_version ) ) <EOL> def _get_form_creator ( rlms_type , rlms_version ) : <EOL> return _get_module ( rlms_type , rlms_version ) . FORM_CREATOR <EOL> def get_form_class ( rlms_type , rlms_version ) : <EOL> form_creator = _get_form_creator ( rlms_type , rlms_version ) <EOL> return form_creator . get_add_form ( ) <EOL> def get_permissions_form_class ( rlms_type , rlms_version ) : <EOL> form_creator = _get_form_creator ( rlms_type , rlms_version ) <EOL> return form_creator . get_permission_form ( ) <EOL> def get_lms_permissions_form_class ( rlms_type , rlms_version ) : <EOL> form_creator = _get_form_creator ( rlms_type , rlms_version ) <EOL> return form_creator . get_lms_permission_form ( ) <EOL> def get_manager_class ( rlms_type , rlms_version , current_rlms_id = None ) : <EOL> module = _get_module ( rlms_type , rlms_version ) <EOL> _ , _ , record = _RLMSs [ rlms_type ] <EOL> if current_rlms_id : <EOL> record . per_thread . current_rlms_id = current_rlms_id <EOL> return module . RLMS </s>
