o
    à›h-  ã                   @   sœ   d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZm	Z	 d dl
mZmZmZ e ¡ Zeƒ Ze ¡ ZG dd„ de	jƒZdd„ Zd	d
„ Zdd„ ZdS )é    N)Úcommon)ÚlogÚutil)Ú
componentsÚ	launchersÚserversc                       sn   e Zd ZdZe d¡Z‡ fdd„Zdd„ Zdd„ Z	d	d
„ Z
dd„ Zdd„ Zddd„Zddd„Zdd„ Z‡  ZS )ÚSessionzáA debug session involving a client, an adapter, a launcher, and a debug server.

    The client and the adapter are always present, and at least one of launcher and debug
    server is present, depending on the scenario.
    é   c                    s¬   ddl m} tƒ  ¡  t ¡ ˆ _tˆ jƒˆ _	t 
ˆ j¡ˆ _t ˆ |j¡ˆ _	 t ˆ tj¡ˆ _	 t ˆ tj¡ˆ _	 d ˆ _	 d ˆ _	 i ˆ _	 dˆ _	 ˆ  j‡ fdd„g7  _d S )Nr   )ÚclientsFc                     s   ˆ   ¡ S ©N)Únotify_changed)Ú_©Úself© úNc:\Users\shubh\Videos\agent\nova\Lib\site-packages\debugpy/adapter/sessions.pyÚ<lambda>@   s    z"Session.__init__.<locals>.<lambda>)Údebugpy.adapterr
   ÚsuperÚ__init__Ú	threadingÚRLockÚlockÚnextÚ_counterÚidÚ	ConditionÚ_changed_conditionr   ÚmissingZClientÚclientr   ZLauncherÚlauncherr   ÚServerÚserverZno_debugÚpidZdebug_optionsÚis_finalizingZ	observers)r   r
   ©Ú	__class__r   r   r      s(   

zSession.__init__c                 C   s   d| j › d�S )NzSession[ú]©r   r   r   r   r   Ú__str__B   s   zSession.__str__c                 C   s   | j  ¡  | S )z&Lock the session for exclusive access.)r   Úacquirer   r   r   r   Ú	__enter__E   s   
zSession.__enter__c                 C   s   | j  ¡  dS )zUnlock the session.N)r   Úrelease)r   Úexc_typeÚ	exc_valueÚexc_tbr   r   r   Ú__exit__J   s   zSession.__exit__c                 C   s<   t � t | ¡ t ¡  W d   ƒ d S 1 sw   Y  d S r   )Ú_lockÚ	_sessionsÚaddÚ_sessions_changedÚsetr   r   r   r   ÚregisterN   s   

"þzSession.registerc                 C   s´   | � | j  ¡  W d   ƒ n1 sw   Y  | j| j| jf}tdd„ |D ƒƒrXt�$ | tv rFt 	d| ¡ t 
| ¡ t ¡  W d   ƒ d S W d   ƒ d S 1 sQw   Y  d S d S )Nc                 s   s   � | ]
}| p
|j  V  qd S r   )Úis_connected)Ú.0Úcomr   r   r   Ú	<genexpr>Z   ó   € z)Session.notify_changed.<locals>.<genexpr>z{0} has ended.)r   Ú
notify_allr   r    r"   Úallr1   r2   r   ÚinfoÚremover4   r5   )r   r   r   r   r   r   S   s   ÿ

ü"ÿÿzSession.notify_changedNc                    s”   ‡ ‡‡fdd„‰dˆ_ ˆdurtjˆdd�}d|_| ¡  ˆ �! |ƒ s7ˆj r/	 W d  ƒ dS ˆ j ¡  |ƒ r#	 W d  ƒ dS 1 sCw   Y  dS )a`  Waits until predicate() becomes true.

        The predicate is invoked with the session locked. If satisfied, the method
        returns immediately. Otherwise, the lock is released (even if it was held
        at entry), and the method blocks waiting for some attribute of either self,
        self.client, self.server, or self.launcher to change. On every change, session
        is re-locked and predicate is re-evaluated, until it is satisfied.

        While the session is unlocked, message handlers for components other than
        the one that is waiting can run, but message handlers for that one are still
        blocked.

        If timeout is not None, the method will unblock and return after that many
        seconds regardless of whether the predicate was satisfied. The method returns
        False if it timed out, and True otherwise.
        c                      s   t  ˆ¡ dˆ_ˆ  ¡  d S )NT)ÚtimeÚsleepÚ	timed_outr   r   ©r   ÚtimeoutÚwait_for_timeoutr   r   rE   s   s   
z*Session.wait_for.<locals>.wait_for_timeoutFNzSession.wait_for() timeout)ÚtargetÚnameT)rB   r   ÚThreadÚdaemonÚstartr   Úwait)r   Ú	predicaterD   Úthreadr   rC   r   Úwait_fora   s"   ÿý
ý$ûzSession.wait_forc                 C   sz   | j rdS d| _ t d|| ¡ |du rt| jƒ}z|  ||¡ W n ty4   t d| ¡ t 	d¡ Y nw t d| ¡ dS )aÒ  Finalizes the debug session.

        If the server is present, sends "disconnect" request with "terminateDebuggee"
        set as specified request to it; waits for it to disconnect, allowing any
        remaining messages from it to be handled; and closes the server channel.

        If the launcher is present, sends "terminate" request to it, regardless of the
        value of terminate; waits for it to disconnect, allowing any remaining messages
        from it to be handled; and closes the launcher channel.

        If the client is present, sends "terminated" event to it.

        If terminate_debuggee=None, it is treated as True if the session has a Launcher
        component, and False otherwise.
        NTz{0}; finalizing {1}.z Fatal error while finalizing {0}r	   z{0} finalized.)
r$   r   r>   Úboolr    Ú	_finalizeÚ	ExceptionÚswallow_exceptionÚosÚ_exit)r   ÚwhyÚterminate_debuggeer   r   r   Úfinalize‡   s   
üzSession.finalizec              	      sb  t  ¡  ˆjr5ˆjjr0|rˆjrˆjjrˆj ¡  nzˆjj dd|i¡ W n	 ty/   Y nw ˆj 	¡  ˆjr�ˆjjr�ˆjr\ˆjj
js\t dˆ¡ ˆj‡fdd„tjd�s\t dˆ¡ ˆjrdˆjj
jsiˆj ¡  t dˆˆj¡ ˆ ‡fd	d„¡ zˆjj ¡  W n ty�   t ¡  Y nw ˆj�r)ˆjjr¶i }ˆjjr¢d
|d< z
ˆjj d|¡ W n	 tyµ   Y nw ˆjjd u�r+ˆjjjdk�r-ˆjrÎˆjj
j�s/t  ¡  t d¡ tƒ ‰ ˆjrêˆjjd urêˆ  ˆjj¡ 	 ‡ fdd„t  ¡ D ƒ}t|ƒsüd S |D ])}t d|¡ z
t  |jt!j"¡ W n t�y    t d|¡ Y nw ˆ  |j¡ qþqëd S d S d S d S )NZ
disconnectZterminateDebuggeez!{0} waiting for "exited" event...c                      s   ˆ j jd uS r   )r    Ú	exit_coder   r   r   r   r   È   s    z#Session._finalize.<locals>.<lambda>)rD   z){0} timed out waiting for "exited" event.z${0} waiting for {1} to disconnect...c                      s
   ˆ j j S r   )r    r7   r   r   r   r   r   Ú   s   
 TZrestartZ
terminatedZlaunchz>"launch" session ended - killing remaining debuggee processes.c                    s   g | ]	}|j ˆ vr|‘qS r   ©r#   )r8   Úconn)Úpids_killedr   r   Ú
<listcomp>ý   s
    
þz%Session._finalize.<locals>.<listcomp>zKilling {0}zFailed to kill {0})#r   Zdont_wait_for_first_connectionr"   r7   r    rV   ZchannelÚrequestrQ   Zdetach_from_sessionÚ
connectionZprocess_replacedr   r>   rN   r   ZPROCESS_EXIT_TIMEOUTÚwarningÚcloserR   r   Zrestart_requestedZ
send_eventZstart_requestÚcommandZstop_servingr5   r#   r3   ÚconnectionsÚlenrS   ÚkillÚsignalÚSIGTERM)r   rU   rV   ÚbodyZconnsrZ   r   )r[   r   r   rP   ª   sŽ   ÿÿ

þ
ÿÿÿÿÿ
þÿòåzSession._finalizer   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	itertoolsÚcountr   r   r)   r+   r0   r6   r   rN   rW   rP   Ú__classcell__r   r   r%   r   r      s    
$

&#r   c                    s@   t � t‡ fdd„tD ƒd ƒW  d   ƒ S 1 sw   Y  d S )Nc                 3   s   � | ]
}|j ˆ kr|V  qd S r   rY   )r8   ÚsessionrY   r   r   r:     r;   zget.<locals>.<genexpr>)r1   r   r2   rY   r   rY   r   Úget  s   $ÿrp   c                   C   sR   	 t � ttƒs	 W d  ƒ dS t ¡  W d  ƒ n1 sw   Y  t ¡  q)zvBlocks until all sessions have ended.

    A session ends when all components that it manages disconnect from it.
    TN)r1   rc   r2   r4   ÚclearrK   r   r   r   r   Úwait_until_ended  s   þ
ýûrr   c                  C   s:   t sd S tt dd„ d�d } | j}|d ur| ¡  d S d S )Nc                 S   s   | j S r   r(   )ro   r   r   r   r   "  s    z report_sockets.<locals>.<lambda>)Úkeyr   )r2   Úsortedr   Úreport_sockets)ro   r   r   r   r   ru     s   ÿru   )rl   rS   re   r   r@   Zdebugpyr   Zdebugpy.commonr   r   r   r   r   r   r   r1   r5   r2   ÚEventr4   Z
Observabler   rp   rr   ru   r   r   r   r   Ú<module>   s     y