o
    wiG                     @   s  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m	Z	m
Z
mZ dejv s+J d dlZd dlmZ d dlZd dlZd dlmZmZ d dlmZ ed Zd	Zd
ejeZG dd deZe Zddde_dd Z e e!d dZ"dd Z#dd Z$dd fddZ%dd Z&dd Z'dd Z(d d d!fd"efd#d$Z)d%de#fd&de$fd'd(e%d)fd*de&d+dfd,d-e'd.fd/d-e'd0fd1de&d2dfd3d4e(fd5d6e%d7d8d fd9d:e%d;fd<de&d=dfd>d?e)d@ddAfdBdCe)dDfdEdFe)dGfdHd6e)dIe"fgZ*dJdK Z+dLe,fdMdNZ-dOdP Z.dQe/fdRdSZ0dTe/fdUdVZ1e/ d!fdLe,dTe/dWe/fdXdYZ2dZd[ Z3d\d] Z4d^d_ Z5d`da Z6dbdc Z7ddde Z8dS )f    N)	find_spec)AnyUnionTupleDictLiteralpydevd)pydevd_runpy)logsockets)apifilemodulecodepidz2<filename> | -m <module> | -c <code> | --pid <pid>a  debugpy {0}
See https://aka.ms/debugpy for documentation.

Usage: debugpy --listen | --connect
               [<host>:]<port>
               [--wait-for-client]
               [--configure-<name> <value>]...
               [--log-to <path>] [--log-to-stderr]
               [--parent-session-pid <pid>]]
               [--adapter-access-token <token>]
               [--disable-sys-remote-exec]]
               {1}
               [<arg>]...
c                   @   s   e Zd ZU dZeed df ed< dZeee	e
f df ed< dZdZdZee	df ed< dZeedf ed< dZdZi Zee	ef ed< dZee
df ed	< dZdS )
OptionsN)connectlistenmodeaddressFtargettarget_kindconfigparent_session_pid)__name__
__module____qualname__r   r   r   __annotations__r   r   strintlog_tolog_to_stderrr   r   
TargetKindwait_for_clientadapter_access_tokenr   r   r   r   disable_sys_remote_exec r'   r'   O/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/debugpy/server/cli.pyr   1   s   
 r   noneT)qt
subProcessc                        fdd}|S )Nc                    sH    | }d ur|k rt dd ur"|kr"t d|S )Nzmust be >= {0}zmust be < {0})
ValueErrorformat)snparserstartstopr'   r(   parseD   s   zin_range.<locals>.parser'   )r2   r3   r4   r5   r'   r1   r(   in_rangeC   s   r6   c                 C   s   t ttjd td d S )Nr   r   )printHELPsysstderrexitswitchitr'   r'   r(   print_help_and_exitR   s   r@   c                 C   s   t tj td d S Nr   )r8   debugpy__version__r:   r<   r=   r'   r'   r(   print_version_and_exitW   s   
rD   c                 C      | S Nr'   xr'   r'   r(   <lambda>\       rI   c                        fdd}|S )Nc                    s    t |}tt| d S rF   )nextsetattroptions)argr?   valuer2   varnamer'   r(   do]   s   zset_arg.<locals>.dor'   )rR   r2   rS   r'   rQ   r(   set_arg\   s   rT   c                    rK   )Nc                    s   t t  d S rF   )rM   rN   )rO   r?   rP   rR   r'   r(   rS   e      zset_const.<locals>.dor'   )rR   rP   rS   r'   rU   r(   	set_constd   s   rW   c                    s    fdd}|S )Nc                    s   t jd ur	tdt|}|d\}}}|d}|s"t }|}zt|}W n t	y3   d}Y nw d|  krAdk sFtd td t _
||ft _d S )Nz---listen and --connect are mutually exclusive:z[]r   i   zinvalid port number)rN   r   r-   rL   
rpartitionstripr   get_default_localhostr    	Exceptionr   )rO   r?   rP   hostsepportr   r'   r(   rS   l   s&   

zset_address.<locals>.dor'   )r   rS   r'   ra   r(   set_addressk   s   rb   c                 C   s   d}|  |s	J | t|d  }t|}|tjvr!td|ttj| }z|tu r7ddd|	  }n||}W n t
yL   td||jw |tj|< d S )Nz--configure-zunknown property {0!r}TF)truefalsez{0!r} must be a {1})
startswithlenrL   rN   r   r-   r.   typeboollowerr]   r   )rO   r?   prefixnamerP   expected_typer'   r'   r(   
set_config   s    
rm   c                 C   rE   rF   r'   rG   r'   r'   r(   rI      rJ   Fkindc                    r,   )Nc                    s    t _r| nt|}t|trBz	|t }W n& tyA   z|d}W n ty>   dd l	}||
d}Y nw Y nw |t _d S )Nutf-8r   F)rN   r   rL   
isinstancebytesdecoder:   getfilesystemencodingUnicodeDecodeErrorlocalegetpreferredencodingr   )rO   r?   r   ru   rn   r2   
positionalr'   r(   rS      s   

zset_target.<locals>.dor'   )rn   r2   rx   rS   r'   rw   r(   
set_target   s   ry   z-(\?|h|-help)z-(V|-version)z--log-toz<path>r!   z--log-to-stderrr"   z--listenz	<address>r   z	--connectr   z--wait-for-clientr$   z--configure-.+z<value>z--parent-session-pidz<pid>r   c                 C   s   | rt | S d S rF   )r    rG   r'   r'   r(   rI      s    z--adapter-access-tokenz<token>r%   z--disable-sys-remote-execr&    z
<filename>r   )rx   z-mz<module>r   -cz<code>r   z--pidr   c                  c   s>    t tjdkrtjd } tjd= | V  t tjdksd S d S )N      )rf   r:   argv)rP   r'   r'   r(   consume_argv   s   
r   argsc                 c   s@    | t ju rt E d H  d S | r| d }| d= |V  | sd S d S rA   )r:   r~   r   )r   rP   r'   r'   r(   consume_args   s   
r   c                  C   s   t  } t|  t|  tjd u stjdkrtdt tjd u r$tdtjd ur2tjdkr2tdtj	d ur@tjdkr@tdtj
dkrLtjrLtdtj
d usSJ tjd usZJ d S )	Nrz   zmissing target: z(either --listen or --connect is requiredr   z)--adapter-access-token requires --connectz'--parent-session-pid requires --connectr   z(--pid does not support --wait-for-client)setparse_args_from_command_lineparse_args_from_environmentrN   r   r-   TARGETr   r%   r   r   r$   r   seenr'   r'   r(   
parse_args   s   
r   r   c                 C   s   t tj|  d S rF   )parse_args_helperr:   r~   r   r'   r'   r(   r      rV   r   seenFromCommandLinec                 C   s4   t jd}|s
d S | }t }t|| |d d S )NDEBUGPY_EXTRA_ARGVT)osenvirongetsplitr   r   )r   r   argsListseenFromEnvironmentr'   r'   r(   r      s   r   r   c              
   C   s@  t | }	 zt|}W n
 ty   Y d S w |}|dsd}tD ]\}}}	td| d |r2 nq!td| |sE||v rEtd| |rQ||v rQtd| |rX||v rXq|r`|| n|| z|	|| W n) ty   |d usyJ td		|| t
y }
 z	td
	|||
d }
~
ww |stjd urd S q)NT-rz   z^(z)$zunrecognized switch z"duplicate switch on command line: z#duplicate switch from environment: z{0}: missing {1}zinvalid {0} {1}: {2})r   rL   StopIterationre   switchesrematchr-   addr.   r]   rN   r   )r   r   r   isFromEnvironmentiteratorrO   r>   patternplaceholderactionexcr'   r'   r(   r     sH   

r   c                 C   s   | t jd< tdt j ttj tj	
dddkrPtjdkr,tjd ur,ttj ntjdkrBtjd urBtjtjtjtjd ntttjtjrPt  dtj	d< d S )	Nr   zsys.argv after patching: {0!r}DEBUGPY_RUNNINGrd   rc   r   r   )access_tokenr   )r:   r~   r
   debugrB   	configurerN   r   r   r   r   r   r   r   r   r%   r   AssertionErrorreprr$   )argv_0r'   r'   r(   start_debugging?  s   
r   c                  C   sr   t j} t|  | d urtj| rtj| }tjd| nt	
d|  t	d t	d|  tj| dd d S )Nr   zNot a file: {0!r}Pre-launch environment:zRunning file {0!r}__main__)run_name)rN   r   r   r   pathisfiledirnamer:   insertr
   r   describe_environmentinforunpyrun_path)r   dirr'   r'   r(   run_fileW  s   
r   c                  C   s   t jdtd t jd } ztjd u rd nttj}|d ur"|j} W n t	y1   t
d Y nw t|  t
d t
dtj ztj}W n ty^   t
d tjtjdd Y d S w |tjdd	 d S )
Nr   rz   z*Error determining module path for sys.argvr   zRunning module {0!r}zArunpy._run_module_as_main is missing, falling back to run_module.T)	alter_sys)
alter_argv)r:   r   r   r   r~   rN   r   r   originr]   r
   swallow_exceptionr   r   r   r   _run_module_as_mainAttributeErrorwarning
run_module)r   specrun_module_as_mainr'   r'   r(   r   l  s(   



r   c                  C   sr   t jd ur2tjdtd tt jtdtd} ttd t	d t
dt j t| i  d S td d S )	Nr   rz   z<string>execr{   r   zRunning code:

{0}zNo target to run.)rN   r   r:   r   r   r   compiler   r
   r   r   evalerror)r   r'   r'   r(   run_code  s   

r   c               
   C   sD  t j} td|  dd }tjtjj	}tj
|sJ ||}t jt jt jt jt jd}|t|}d}|ddddj||d	}t jsttd
rd}zIdd l}|jdd4}|j}tjd|d ||  |dj|d  |  |  t| | W d    W d S 1 sw   Y  W d S  ty } ztj
|rt | tj!d|d W Y d }~nd }~ww td|dd h dt"|@ rJ dtj#tjt$j	d}	tj
|	sJ tj%|	 zdd l&}
td|  |
j'| |dt(t)ddd W n ty   t*d|  Y nw td|  d S ) Nz!Attaching to process with PID={0}c                 S   s   | d urt t| dS d S )Nro   )list	bytearrayencode)r/   r'   r'   r(   rI     s    zattach_to_pid.<locals>.<lambda>)r   r   r$   r!   r%   a9  
import codecs;
import json;
import sys;

decode = lambda s: codecs.utf_8_decode(bytearray(s))[0] if s is not None else None;

script_dir = decode({script_dir});
setup = json.loads(decode({setup}));

sys.path.insert(0, script_dir);
import attach_pid_injected;
del sys.path[0];

attach_pid_injected.attach(setup);
rz   
)
script_dirsetupremote_execr   F)deletezFAttempting to inject code at '{tmp_file_path}' using sys.remote_exec())tmp_file_pathz'import os;os.remove("{tmp_file_path}");z]Injecting code using sys.remote_exec() failed with error:
"{e}"
Will reattempt using pydevd.
)ezCode to be injected: 
{0};z;
>   r   r   "'zOInjected code should not contain any single quotes, double quotes, or newlines.pydevd_attach_to_processz,Injecting code into process with PID={0} ...T DEBUGPY_ATTACH_BY_PID_DEBUG_INFO0)connect_debugger_tracingshow_debug_infoz#Code injection into PID={0} failed:z&Code injection into PID={0} completed.)+rN   r   r
   r   r   r   r   rB   server__file__existsr   r   r$   r!   r%   jsondumpsreplacer.   r&   hasattrr:   tempfileNamedTemporaryFilerk   writer   flushcloser   r]   remover   r   joinr   appendadd_code_to_python_processrun_python_coder    getenvreraise_exception)r   r   r   r   python_coder   r   tmp_filer   pydevd_attach_to_process_pathr   r'   r'   r(   attach_to_pid  s   



r   c               
   C   s  t tj} zt  W n( ty2 } zttttd t| tjd t	d W Y d }~nd }~ww t
jd ur>tt
j t
jrGttj t  ttd| tj zt
jd urkttttdt
j }|  W d S W d S  ty } ztjd|jdd W Y d }~d S d }~ww )	Nz
Error: r7   r|   z=sys.argv before parsing: {0!r}
         after parsing:  {1!r}r   z%Debuggee exited via SystemExit: {0!r}r   )level)r   r:   r~   r   r]   r8   r   r9   r;   r<   rN   r!   rB   r"   r   ensure_loggingr
   r   r   r   r   r   r   
SystemExitr   r   )original_argvr   runr'   r'   r(   main  sF   

"

r   )9r   r   r   r:   importlib.utilr   typingr   r   r   r   r   modulesr   _pydevd_bundler	   r   rB   debugpy.serverdebugpy.commonr
   r   r   r#   r   r.   rC   r9   objectr   rN   r   r6   r    r   r@   rD   rT   rW   rb   rm   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r'   r'   r'   r(   <module>   s   
7!d