o
    5h+                     @  s:  d dl mZ d dlZd dlZd dlZ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 erDd dlmZ ddlmZ d d	lmZ d d
lmZ G dd deddZG dd deZG dd deZG dd dZG dd deZG dd deZ dd Z!dd Z"dd Z#dd  Z$d!d" Z%dS )#    )annotationsN)Iterable)TYPE_CHECKING	TypedDict   )metadata	resources)Self)SetuptoolsWarning)first_line_re)get_platformc                   @  s   e Zd ZU ded< ded< dS )
_SplitArgsboolZcommentsposixN)__name__
__module____qualname____annotations__ r   r   3lib/python3.10/site-packages/setuptools/_scripts.pyr      s   
 r   F)totalc                   @  s   e Zd ZU dZg Zded< e Zedd Z	edd Z
ed ddZedd Zed!ddZd"ddZedd Zdd Zedd Zedd ZdS )#CommandSpeczm
    A command spec for a #! header, specified as a list of arguments akin to
    those passed to Popen.
    z	list[str]optionsc                 C  s   | S )zV
        Choose the best CommandSpec class based on environmental conditions.
        r   clsr   r   r   best'   s   zCommandSpec.bestc                 C  s   t jtj}t jd|S )N__PYVENV_LAUNCHER__)ospathnormpathsys
executableenvironget)r   Z_defaultr   r   r   _sys_executable.   s   zCommandSpec._sys_executableparam!Self | str | Iterable[str] | Nonereturnr	   c                 C  sV   t || r|S t |tr| |S t |tr| |S |du r"|  S tdt| )zg
        Construct a CommandSpec from a parameter to build_scripts, which may
        be None.
        Nz!Argument has an unsupported type )
isinstancestrfrom_stringr   from_environment	TypeErrortype)r   r%   r   r   r   
from_param3   s   



zCommandSpec.from_paramc                 C  s   | |   gS N)r$   r   r   r   r   r+   C   s   zCommandSpec.from_environmentstringr)   c                 C  s   t j|fi | j}| |S )z}
        Construct a command spec from a simple string representing a command
        line parseable by shlex.split.
        )shlexsplit
split_args)r   r0   itemsr   r   r   r*   G   s   zCommandSpec.from_stringscript_textc                 C  s<   t | || _t| }t|sdg| jd d< d S d S )Nz-xr   )r1   r2   _extract_optionsr   
subprocesslist2cmdlineisascii)selfr5   cmdliner   r   r   install_optionsP   s
   
zCommandSpec.install_optionsc                 C  s:   | d   d }t |}|r|dpdnd}| S )zH
        Extract any options from the first line of the script.
        
r   r    )
splitlines_first_line_rematchgroupstrip)Zorig_scriptfirstrA   r   r   r   r   r6   V   s    zCommandSpec._extract_optionsc                 C  s   |  | t| j S r/   )_renderlistr   )r:   r   r   r   	as_header`   s   zCommandSpec.as_headerc                 C  s6   d}|D ]}|  |r| |r| dd   S q| S )Nz"'r   )
startswithendswith)itemZ_QUOTESqr   r   r   _strip_quotesc   s   zCommandSpec._strip_quotesc                 C  s    t dd | D }d| d S )Nc                 s  s    | ]
}t | V  qd S r/   )r   rM   rC   ).0rK   r   r   r   	<genexpr>m   s    
z&CommandSpec._render.<locals>.<genexpr>z#!r=   )r7   r8   )r4   r;   r   r   r   rE   k   s   
zCommandSpec._renderN)r%   r&   r'   r	   )r0   r)   r'   r	   )r5   r)   )r   r   r   __doc__r   r   r   r3   classmethodr   r$   r.   r+   r*   r<   staticmethodr6   rG   rM   rE   r   r   r   r   r      s,   
 




	
r   c                   @  s   e Zd ZeddZdS )WindowsCommandSpecF)r   N)r   r   r   r   r3   r   r   r   r   rS   s   s    rS   c                   @  sh   e Zd ZdZed ZeZ	e
dddZedd Ze
dd	 Ze
d
d Ze
		ddddZdS )ScriptWriterz`
    Encapsulates behavior around writing entry point scripts for console and
    gui apps.
    aJ  
        # EASY-INSTALL-ENTRY-SCRIPT: %(spec)r,%(group)r,%(name)r
        import re
        import sys

        # for compatibility with easy_install; see #2198
        __requires__ = %(spec)r

        try:
            from importlib.metadata import distribution
        except ImportError:
            try:
                from importlib_metadata import distribution
            except ImportError:
                from pkg_resources import load_entry_point


        def importlib_load_entry_point(spec, group, name):
            dist_name, _, _ = spec.partition('==')
            matches = (
                entry_point
                for entry_point in distribution(dist_name).entry_points
                if entry_point.group == group and entry_point.name == name
            )
            return next(matches).load()


        globals().setdefault('load_entry_point', importlib_load_entry_point)


        if __name__ == '__main__':
            sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
            sys.exit(load_entry_point(%(spec)r, %(group)r, %(name)r)())
        Nc           
      c  s    t |dstd tj|j}|du r|  }|j d|j	 }dD ].}| d}|j
j|dD ]}|j}| |j | jt  }| ||j||}	|	E dH  q3q%dS )z
        Yield write_script() argument tuples for a distribution's
        console_scripts and gui_scripts entry points.
        entry_pointsz%Unsupported distribution encountered.Nz==ZconsoleguiZ_scripts)rB   )hasattrr
   emitr   ZDistributionZatZegg_info
get_headernameversionrU   Zselect_ensure_safe_nametemplatelocals_get_script_args)
r   Zdistheaderspectype_rB   Zepr[   r5   argsr   r   r   get_args   s"   
	

zScriptWriter.get_argsc                 C  s   t d| }|rtddS )z?
        Prevent paths in *_scripts entry point names.
        z[\\/]z+Path separators not allowed in script namesN)research
ValueError)r[   Zhas_path_sepr   r   r   r]      s   zScriptWriter._ensure_safe_namec                 C  s*   t jdkstjdkrtjdkrt S | S )zD
        Select the best ScriptWriter for this environment.
        win32javant)r    platformr   r[   _nameWindowsScriptWriterr   r   r   r   r   r      s   zScriptWriter.bestc                 c  s    ||| fV  d S r/   r   )r   rc   r[   ra   r5   r   r   r   r`      s   zScriptWriter._get_script_argsr>   r5   r)   r!   (str | CommandSpec | Iterable[str] | Noner'   c                 C  s"   | j  |}|| | S )z;Create a #! line, getting options (if any) from script_text)command_spec_classr   r.   r<   rG   )r   r5   r!   cmdr   r   r   rZ      s   
zScriptWriter.get_headerr/   )r>   N)r5   r)   r!   ro   r'   r)   )r   r   r   rP   textwrapdedentlstripr^   r   rp   rQ   re   rR   r]   r   r`   rZ   r   r   r   r   rT   w   s(    #%

	
rT   c                   @  s@   e Zd ZeZedd Zedd Zedd Ze	dd Z
d	S )
rn   c                 C  s"   t t| d}tjdd}|| S )zC
        Select the best ScriptWriter suitable for Windows
        )r!   ZnaturalZSETUPTOOLS_LAUNCHERr!   )dictWindowsExecutableLauncherWriterr   r"   r#   )r   Zwriter_lookupZlauncherr   r   r   r      s   zWindowsScriptWriter.bestc           	      #  s    t ddd| }|tjd  dvr#djdi t }t| g d}|	| | 
||} fdd	|D } | || d
|fV  dS )z For Windows, add a .py extension.pya.pywrV   PATHEXT;zK{ext} not listed in PATHEXT; scripts will not be recognized as executables.)rw   .py
-script.py.pyc.pyorx   .exec                      g | ]} | qS r   r   rN   xr[   r   r   
<listcomp>       z8WindowsScriptWriter._get_script_args.<locals>.<listcomp>tNr   )ru   r   r"   lowerr2   formatr_   r
   rY   remove_adjust_header)	r   rc   r[   ra   r5   extmsgoldblockersr   r   r   r`      s   

z$WindowsScriptWriter._get_script_argsc                 C  sN   d}d}|dkr||}}t t |t j}|j||d}| |r%|S |S )z
        Make sure 'pythonw' is used for gui and 'python' is used for
        console (regardless of what sys.executable is).
        zpythonw.exez
python.exerW   )r0   repl)rf   compileescape
IGNORECASEsub_use_header)r   rc   Zorig_headerpatternr   Z
pattern_ob
new_headerr   r   r   r     s   
z"WindowsScriptWriter._adjust_headerc                 C  s&   | dd  d}tjdkpt|S )z
        Should _adjust_header use the replaced header?

        On non-windows systems, always use. On
        Windows systems, only use the replaced header if it resolves
        to an executable on the system.
           rH   "ri   )rC   r    rl   shutilwhich)r   Zclean_headerr   r   r   r     s   	zWindowsScriptWriter._use_headerN)r   r   r   rS   rp   rQ   r   r`   r   rR   r   r   r   r   r   rn      s    


rn   c                   @  s   e Zd Zedd ZdS )rv   c                 #  s    |dkrd}d}dg}nd}d}g d}|  ||} fdd|D }	 | || d	|	fV   d
 t|dfV  t sJ d }
|
t d	fV  dS dS )zG
        For Windows, add a .py extension and an .exe launcher
        rW   z-script.pywrx   Zclir|   )r{   r}   r~   c                   r   r   r   r   r   r   r   r   +  r   zDWindowsExecutableLauncherWriter._get_script_args.<locals>.<listcomp>r   r   bz.exe.manifestN)r   get_win_launcheris_64bitload_launcher_manifest)r   rc   r[   ra   r5   Zlauncher_typer   r   Zhdrr   Zm_namer   r   r   r`     s&   z0WindowsExecutableLauncherWriter._get_script_argsN)r   r   r   rQ   r`   r   r   r   r   rv     s    rv   c                 C  sV   |  d}t  rt dkr|dd}n|dd}n|dd}td| S )z
    Load the Windows launcher (executable) suitable for launching a script.

    `type` should be either 'cli' or 'gui'

    Returns the executable as a byte string.
    r   z	win-arm64.z-arm64.z-64.z-32.Z
setuptools)r   r   replacer   filesjoinpath
read_bytes)r-   Zlauncher_fnr   r   r   r   <  s   

r   c                 C  s"   t td}|jddt  S )Nzlauncher manifest.xmlzutf-8)encoding)r   r   r   r   	read_textvars)r[   resr   r   r   r   O  s   r   c                   C  s    t tjtrtS ttj S )z_
    Return a regular expression based on first_line_re suitable for matching
    strings.
    )r(   r   r   r)   rf   r   decoder   r   r   r   r@   T  s   r@   c                   C  s   t ddkS )NP   )structcalcsizer   r   r   r   r   `  s   r   c                 C  s&   z|  d W dS  ty   Y dS w )NasciiFT)encodeUnicodeError)sr   r   r   r9   d  s   r9   )&Z
__future__r   r   rf   r1   r   r   r7   r    rr   Zcollections.abcr   typingr   r   Z
_importlibr   r   Ztyping_extensionsr	   warningsr
   Zdistutils.command.build_scriptsr   Zdistutils.utilr   r   rF   r   rS   rT   rn   rv   r   r   r@   r   r9   r   r   r   r   <module>   s8    Uj; 