
    Ѐi                         d Z ddlZddlZddlmZmZmZ ddlmZ ddl	Z	 G d de
      Z G d de
      Z G d	 d
      Zy)zu
Safe Executor for Strategy Code
Provides a sandboxed environment for executing user-provided betting strategy code.
    N)DictListAny)contextmanagerc                       e Zd Zy)TimeoutErrorN__name__
__module____qualname__     */var/www/html/eventheodds/safe_executor.pyr   r          r   r   c                       e Zd Zy)SafeExecutionErrorNr	   r   r   r   r   r      r   r   r   c                       e Zd ZdZh dZej                  ej                  ej                  ej                  hZ
d	dZd Zd Zed        Zd Zy)
SafeExecutorzQ
    Safely execute user-provided strategy code in a restricted environment.
    >   absallanyintlenmapmaxminsetstrsumzipNoneTruebooldictlistFalsefloatrangeroundtuplefiltersorted	enumeratec                      || _         || _        y )N)timeoutmax_iterations)selfr/   r0   s      r   __init__zSafeExecutor.__init__$   s    ,r   c                    	 t        j                  |      }t        j                  |      D ]  }t        |      | j                  v r!t        dt        |      j                         t        |t         j                        r3|j                  j                  d      rt        d|j                         t        |t         j                        st        |j                  t         j                        s|j                  j                  dv st        d|j                  j                          y # t        $ r}t        d|       d }~ww xY w)NzSyntax error in strategy code: z&Forbidden operation in strategy code: _z+Access to private attributes is forbidden: )evalexeccompileopen
__import__inputgetattrsetattrdelattrglobalslocalszForbidden function call: )astparseSyntaxErrorr   walktypeFORBIDDEN_NODESr
   
isinstance	Attributeattr
startswithCallfuncNameid)r1   codetreeenodes        r   _validate_codezSafeExecutor._validate_code(   s    	L99T?D HHTN 	DDzT111(<T$Z=P=P<QR  $.99'',,Edii[Q  $)dii2yy|| (S S 17		~F #	  	L$'Fqc%JKK	Ls   D2 2	E;E		Ec                     t        t        t              rt        nt        j                  }i }| j                  D ]  }||v s||   ||<    d|d<   d|d<   d |d<   |dd dS )NTr"   Fr&   r!   __strategy__)__builtins__r
   __doc__)rF   rU   r$   __dict__ALLOWED_BUILTINS)r1   builtins_dictsafe_builtinsnames       r   _create_safe_globalsz!SafeExecutor._create_safe_globalsC   s~    (2<(FLLaLa)) 	:D}$&3D&9d#	: !%f!&g $f *&
 	
r   c              #     K   fd}t        j                   t         j                  |      }t        j                         	 d  t        j                  d       t        j                   t         j                  |       y # t        j                  d       t        j                   t         j                  |       w xY ww)Nc                 "    t        d d      )N#Strategy execution timed out after  seconds)r   )signumframesecondss     r   timeout_handlerz6SafeExecutor._timeout_context.<locals>.timeout_handlerV   s    !DWIXVWWr   r   )signalSIGALRMalarm)r1   rc   rd   old_handlers    `  r   _timeout_contextzSafeExecutor._timeout_contextT   st     	X mmFNNODW	7LLOMM&..+6 LLOMM&..+6s   ?B?B :B?;B<<B?c                    | j                  |       | j                         }i }	 | j                  | j                        5  t	        |||       d d d        |j                  d      }t        |      st        d      g }	t        |      D ]  \  }
}|
| j                  k\  r nu	  ||      }|rh|j                  d      rW|	j                  |j                  d|
      ||j                  dd      |j                  dd      |j                  d	d
      d        |	|dS # 1 sw Y   xY w# t        $ r
}Y d }~d }~ww xY w# t        $ r t        d| j                   d      t        $ r}t        dt        |             d }~ww xY w)Nexecutez5Strategy code must define an 'execute(game)' functionbetrM   pickhomebet_typespread
confidenceg      ?)game_idgame
predictionro   rq   )betsgamesr_   r`   zStrategy execution failed: )rR   r\   ri   r/   r6   getcallabler   r-   r0   append	Exceptionr   r   )r1   rN   rv   playersteamsparamssafe_globalssafe_locals
execute_fnru   irs   resultrP   s                 r   execute_strategyzSafeExecutor.execute_strategyb   s   D!002#	M&&t||4 6T<56 %3JJ'(K  D$U+ 4+++'-F&**U"3'+xxa'8$(*0**VV*D(.

:x(H*0**\3*G% " !51176 60 ! 
  	$5dll^8L   	M$'B3q6(%KLL	MsU   D=  DAD= "A2D'D= D$ D= '	D:0D= 5D::D= =*F'E>>FN)   i )r
   r   r   rV   rX   r@   Import
ImportFromGlobalNonlocalrE   r2   rR   r\   r   ri   r   r   r   r   r   r      s[     	

CNN

CLLO
-6
" 7 7)Mr   r   )rV   r@   systypingr   r   r   
contextlibr   re   rz   r   r   r   r   r   r   <module>r      sD   
  
 " " % 	9 		 	xM xMr   