o
    Ni=                     @   s   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e	dZ
	 dd ZdddZG dd deZejZG d	d
 d
eZdZdZeeedZdd ZG dd dZG dd deZejZejZdS )    Nz^#!.*python[0-9.]*([ 	].*)?$c                 C   s&   z|  d W dS  ty   Y dS w )NasciiFT)encodeUnicodeError)s r   L/home/ubuntu/.local/lib/python3.10/site-packages/pbr/_compat/easy_install.pyisascii6   s   r   c              	   C   s   t j| \}}tjdkr|dkr| d } t j| r| S |du rBt jdd}|du rBzt d}W n t	t
fyA   t j}Y nw |sFdS |t j}|D ]}t j|| }t j|ra|  S qNdS )zTries to find 'executable' in the directories listed in 'path'.

    A string listing directories separated by 'os.pathsep'; defaults to
    os.environ['PATH'].  Returns the complete filename or None if not found.
    win32.exeNPATHCS_PATH)ospathsplitextsysplatformisfileenvirongetconfstrAttributeError
ValueErrordefpathsplitpathsepjoin)
executabler   _extpathspfr   r   r   find_executable>   s,   
r"   c                   @   s   e Zd ZdZg Ze Zedd Zedd Z	edd Z
edd	 Zed
d Z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.
    c                 C      | S )zV
        Choose the best CommandSpec class based on environmental conditions.
        r   clsr   r   r   bestl   s   zCommandSpec.bestc                 C   s   t jtj}t jd|S )N__PYVENV_LAUNCHER__)r   r   normpathr   r   r   r   )r&   _defaultr   r   r   _sys_executables   s   zCommandSpec._sys_executablec                 C   s:   t || r|S t |tr| |S |du r|  S | |S )zg
        Construct a CommandSpec from a parameter to build_scripts, which may
        be None.
        N)
isinstancelistfrom_environmentfrom_string)r&   paramr   r   r   
from_paramx   s   


zCommandSpec.from_paramc                 C   s   | |   gS N)r+   r%   r   r   r   r.      s   zCommandSpec.from_environmentc                 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.
        )shlexr   
split_args)r&   stringitemsr   r   r   r/      s   zCommandSpec.from_stringc                 C   s<   t | || _t| }t|sdg| jd d< d S d S )Nz-xr   )r3   r   _extract_optionsoptions
subprocesslist2cmdliner   )selfscript_textcmdliner   r   r   install_options   s
   
zCommandSpec.install_optionsc                 C   s8   | d   d }t|}|r|dpdnd}| S )zH
        Extract any options from the first line of the script.
        
r       )
splitlinesshebang_patternmatchgroupstrip)orig_scriptfirstrD   r8   r   r   r   r7      s   
 zCommandSpec._extract_optionsc                 C   s   |  | t| j S r2   )_renderr-   r8   )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)item_QUOTESqr   r   r   _strip_quotes   s   zCommandSpec._strip_quotesc                 C   s    t dd | D }d| d S )Nc                 s   s    | ]
}t | V  qd S r2   )r#   rQ   rF   ).0rN   r   r   r   	<genexpr>   s    
z&CommandSpec._render.<locals>.<genexpr>z#!r?   )r9   r:   )r6   r=   r   r   r   rI      s   
zCommandSpec._renderN)__name__
__module____qualname____doc__r8   dictr4   classmethodr'   r+   r1   r.   r/   r>   staticmethodr7   rJ   rQ   rI   r   r   r   r   r#   c   s,    





	
r#   c                   @   s   e Zd ZeddZdS )WindowsCommandSpecF)posixN)rT   rU   rV   rX   r4   r   r   r   r   r[      s    r[   a  #PBR Generated from %(group)r

import threading

from %(module_name)s import %(import_target)s

if __name__ == "__main__":
    import argparse
    import socket
    import sys
    import wsgiref.simple_server as wss

    parser = argparse.ArgumentParser(
        description=%(import_target)s.__doc__,
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        usage='%%(prog)s [-h] [--port PORT] [--host IP] -- [passed options]')
    parser.add_argument('--port', '-p', type=int, default=8000,
                        help='TCP port to listen on')
    parser.add_argument('--host', '-b', default='',
                        help='IP to bind the server to')
    parser.add_argument('args',
                        nargs=argparse.REMAINDER,
                        metavar='-- [passed options]',
                        help="'--' is the separator of the arguments used "
                        "to start the WSGI server and the arguments passed "
                        "to the WSGI application.")
    args = parser.parse_args()
    if args.args:
        if args.args[0] == '--':
            args.args.pop(0)
        else:
            parser.error("unrecognized arguments: %%s" %% ' '.join(args.args))
    sys.argv[1:] = args.args
    server = wss.make_server(args.host, args.port, %(invoke_target)s())

    print("*" * 80)
    print("STARTING test server %(module_name)s.%(invoke_target)s")
    url = "http://%%s:%%d/" %% (server.server_name, server.server_port)
    print("Available at %%s" %% url)
    print("DANGER! For testing only, do not use in production")
    print("*" * 80)
    sys.stdout.flush()

    server.serve_forever()
else:
    application = None
    app_lock = threading.Lock()

    with app_lock:
        if application is None:
            application = %(invoke_target)s()

z# PBR Generated from %(group)r

import sys

from %(module_name)s import %(import_target)s


if __name__ == "__main__":
    sys.exit(%(invoke_target)s())
)console_scriptsgui_scriptswsgi_scriptsc                 C   sF   |j r
t|j dkrtd|| |j|j d d|j d }|| S )a@  Generate the script based on the template.

    :param str group: The entry-point group name, e.g., "console_scripts".
    :param str header: The first line of the script, e.g.,
        "!#/usr/bin/env python".
    :param str template: The script template.
    :returns: The templated script content
    :rtype: str
       zBScript targets must be of the form 'func' or 'Class.class_method'.r   .)rE   module_nameimport_targetinvoke_target)attrslenr   rb   r   )rE   entry_pointheadertemplater<   r   r   r   generate_script  s   

rj   c                   @   sp   e Zd ZdZeZedddZedddZeddd	Z	e
d
d Zedd Zedd ZedddZdS )ScriptWriterz`
    Encapsulates behavior around writing entry point scripts for console and
    gui apps.
    NFc                 C   s*   |rt nt }| d||}|||S )NrA   )WindowsScriptWriterrk   r'   get_script_headerget_args)r&   distr   wininstwriterrh   r   r   r   get_script_args)  s   zScriptWriter.get_script_argsc                 C   s   |rd}|  ||S )N
python.exe)
get_header)r&   r<   r   rp   r   r   r   rm   1  s   zScriptWriter.get_script_headerc                 c   s`    |du r	|   }t D ] \}}tjj||D ]\}}| | |t||||fV  qqdS )
        Yield write_script() argument tuples for a distribution's
        console_scripts and gui_scripts entry points.
        N)	rt   ENTRY_POINTS_MAPr6   pbr_compatmetadataget_entry_points_ensure_safe_namerj   )r&   ro   rh   rE   ri   nameepr   r   r   rn   9  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searchr   )r|   has_path_sepr   r   r   r{   I  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.
        r	   javant)r   r   r   r|   _namerl   r'   r%   r   r   r   r'   R  s   zScriptWriter.bestc                 c   s    ||| fV  d S r2   r   )r&   type_r|   rh   r<   r   r   r   _get_script_args\  s   zScriptWriter._get_script_argsrA   c                 C   s"   | j  |}|| | S )z;Create a #! line, getting options (if any) from script_text)command_spec_classr'   r1   r>   rJ   )r&   r<   r   cmdr   r   r   rt   a  s   
zScriptWriter.get_header)NFr2   )rA   N)rT   rU   rV   rW   r#   r   rY   rr   rm   rn   rZ   r{   r'   r   rt   r   r   r   r   rk   !  s"    

	
rk   c                   @   s\   e 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 ZdS )rl   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    |du r	|   }t| }dD ]1}|d }tjj||D ]"\}}| | | j|||d }| 	||||}	|	D ]}
|
V  q;qqdS )ru   Nconsolegui_scripts)specrE   r|   )
rt   stras_requirementrw   rx   ry   rz   r{   ri   r   )r&   ro   rh   r   r   rE   r|   r}   r<   argsresr   r   r   rn     s&   
zWindowsScriptWriter.get_argsc                 C   r$   )zC
        Select the best ScriptWriter suitable for Windows
        r   r%   r   r   r   r'     s   zWindowsScriptWriter.bestc           	      #   s    t ddd| }|tjd  dvr!dj|d}t|t g d}|	| | 
||} fd	d
|D } | || d|fV  dS )z For Windows, add a .py extension.pya.pywr   PATHEXT;zK{ext} not listed in PATHEXT; scripts will not be recognized as executables.)r   )r   z.pyz
-script.pyz.pycz.pyor   r
   c                    s   g | ]} | qS r   r   )rR   xr|   r   r   
<listcomp>  s    z8WindowsScriptWriter._get_script_args.<locals>.<listcomp>tN)rX   r   r   lowerr   formatwarningswarnUserWarningremove_adjust_header)	r&   r   r|   rh   r<   r   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.exers   r   )r5   repl)r~   compileescape
IGNORECASEsub_use_header)r&   r   orig_headerpatternr   
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.
        r`   rK   "r	   )rF   r   r   r"   )r   clean_headerr   r   r   r     s   	zWindowsScriptWriter._use_headerr2   )rT   rU   rV   textwrapdedentlstripri   r[   r   rY   rn   r'   r   r   rZ   r   r   r   r   r   rl   i  s"    #%
	

rl   r2   )r   r~   r3   r9   r   r   r   pbr._compat.metadatarw   r   rC   r   r"   r-   r#   r+   sys_executabler[   
_wsgi_text_script_textrv   rj   rk   rl   rr   rm   r   r   r   r   <module>   s4   $

%T6Ht
