o
    !dI                     @   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d dlZd dl	m
Z
 d dlmZ d dlmZ ejZejZejjZejjZejZejZejjjjZejZz	d dlZejZW n eyh   ejZY nw zd dlZW n eyz   dZY nw e jdkZerdZ ndZ G dd	 d	e!Z"G d
d de#Z$dd Z%ejrd dl&m'Z( d dl)Z)d dl*m+Z, d dl-m.Z. e/Z0dd Z1dd Z2dd Z3d@ddZ4dAddZ5n2d dl6Z6d dl7Z(d dl)Z)d dl8Z8d dl,Z,d dl9m.Z. e0Z0dd Z1dd Z2dd Z3d@ddZ4dAddZ5dd  Z:d!d" Z;d#d$ Z<dAd%d&Z=d'd( Z>dAd)d*Z?ej@d+d, ZAz	d d-lmBZB W dS  eyx   d.ZCd/d0 ZDeEd1ejFZGeEd2ejFZHeEd3ejFZId4ZJd5d6 ZKeEd7ejLZMeEd8ejLZNeEd9ejLZOd:d:d:eJd;fd<d=ZBd>d? ZPY dS w )B    N)RawConfigParser)six)OrderedDictwin32morezless -Rc                       s   e Zd Z fddZ  ZS )StdinMissingErrorc                    s   d}t t| | d S )Nz;stdin is required for this operation, but is not available.)superr   __init__)selfmessage	__class__ 1usr/lib/python3.10/site-packages/awscli/compat.pyr	   D   s   zStdinMissingError.__init__)__name__
__module____qualname__r	   __classcell__r   r   r   r   r   C   s    r   c                   @   s    e Zd ZdZdd Zdd ZdS )NonTranslatedStdouta   This context manager sets the line-end translation mode for stdout.

    It is deliberately set to binary mode so that `` does not get added to
    the line ending. This can be useful when printing commands where a
    windows style line ending would cause errors.
    c                 C   s.   t jdkrdd l}|t j tj| _t jS Nr   r   )	sysplatformmsvcrtsetmodestdoutfilenoosO_BINARYprevious_mode)r
   r   r   r   r   	__enter__S   s   
zNonTranslatedStdout.__enter__c                 C   s.   t jdkrdd l}|t j | j d S d S r   )r   r   r   r   r   r   r   )r
   typevalue	tracebackr   r   r   r   __exit__Z   s   
zNonTranslatedStdout.__exit__N)r   r   r   __doc__r   r#   r   r   r   r   r   K   s    r   c                 C   s6   t | tjr| S t | tjr| dS tdt|  )Nutf-8z,Expected str, unicode or bytes, received %s.)
isinstancer   Z	text_typebinary_typedecode
ValueErrorr    )sr   r   r   ensure_text_type`   s
   
r+   )URLErrorc                   C   s   t jd u rt t jjS N)r   stdinr   bufferr   r   r   r   get_binary_stdinq   s   
r0   c                   C   s   t jjS r-   )r   r   r/   r   r   r   r   get_binary_stdoutv   s   r1   c                 C   s   | S r-   r   )streamerrorsr   r   r   _get_text_writery   s   r4   rc                 C   s   d|vrt  }t| ||dS )a>  Back-port open() that accepts an encoding argument.

        In python3 this uses the built in open() and in python2 this
        uses the io.open() function.

        If the file is not being opened in binary mode, then we'll
        use locale.getpreferredencoding() to find the preferred
        encoding.

        bencoding)localegetpreferredencodingopenfilenamemoder8   r   r   r   compat_open|   s   r?   c                 C   s>   |du rt j}t|ddr|j|  dS || d dS )zE
        This function is used to write raw bytes to stdout.
        Nr/   r%   )r   r   getattrr/   writer(   	statementr   r   r   r   bytes_print   s
   rD   c                   C   s   t jd u rt t jS r-   )r   r.   r   r   r   r   r   r0      s   
c                   C   s   t jS r-   )r   r   r   r   r   r   r1      s   c                 C   s$   t  }|d u r
d}t|| |S )Nascii)r9   r:   codecs	getwriter)r2   r3   r8   r   r   r   r4      s   c                 C   s    d|vrt  }tj| ||dS )Nr6   r7   )r9   r:   ior;   r<   r   r   r   r?      s   c                 C   s   |d u rt j}||  d S r-   )r   r   rA   rB   r   r   r   rD      s   c                   C      t tjddS )Nstrictr3   )r4   r   r   r   r   r   r   get_stdout_text_writer      rL   c                   C   rI   )NreplacerK   )r4   r   stderrr   r   r   r   get_stderr_text_writer   rM   rP   c                 C   s   t j|  t j  t S )au  
    Cygwin's pty's are based on pipes. Therefore, when it interacts with a Win32
    program (such as Win32 python), what that program sees is a pipe instead of
    a console. This is important because python buffers pipes, and so on a
    pty-based terminal, text will not necessarily appear immediately. In most
    cases, this isn't a big deal. But when we're doing an interactive prompt,
    the result is that the prompts won't display until we fill the buffer. Since
    raw_input does not flush the prompt, we need to manually write and flush it.

    See https://github.com/mintty/mintty/issues/56 for more details.
    )r   r   rA   flush	raw_input)promptr   r   r   compat_input   s   
rT   c                 C   s&   |du rt j}|dkrt| S t| S )zReturn a shell-escaped version of the string *s*

    Unfortunately `shlex.quote` doesn't support Windows, so this method
    provides that functionality.
    Nr   )r   r   _windows_shell_quoteshlex_quote)r*   r   r   r   r   compat_shell_quote   s
   rW   c                 C   s   | sdS g }d}| D ]6}|dkr|d7 }q
|dkr.|dkr(| d|d   d}| d q
|dkr;| d|  d}| | q
|dkrL| d|  d|}d	|v sYd
|v r]d| S |S )a  Return a Windows shell-escaped version of the string *s*

    Windows has potentially bizarre rules depending on where you look. When
    spawning a process via the Windows C runtime the rules are as follows:

    https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments

    To summarize the relevant bits:

    * Only space and tab are valid delimiters
    * Double quotes are the only valid quotes
    * Backslash is interpreted literally unless it is part of a chain that
      leads up to a double quote. Then the backslashes escape the backslashes,
      and if there is an odd number the final backslash escapes the quote.

    :param s: A string to escape
    :return: An escaped string
    z""r   \   "   z\"  	z"%s")appendjoin)r*   ZbuffZnum_backspaces	characterZnew_sr   r   r   rU      s,   

rU   c                 C   s4   i }| du rt } trddi}nt| } | |d< |S )zReturns the default pager to use dependent on platform

    :rtype: str
    :returns: A string represent the paging command to run based on the
        platform being used.
    NshellTargs)default_pager
is_windowsshlexsplit)Z	pager_cmdZpopen_kwargsr   r   r   get_popen_kwargs_for_pager_cmd9  s   

rh   c                  c   s    t rtjg} ntjtjtjg} g }| D ]}|t|tj qzdV  W t| D ]\}}t|||  q*dS t| D ]\}}t|||  q=w )zG
    Ignores user entered signals to avoid process getting killed.
    N)re   signalSIGINTSIGQUITSIGTSTPr_   SIG_IGN	enumerate)Zsignal_listZactual_signalsZuser_signalsigr   r   r   ignore_user_entered_signalsM  s   
rp   )linux_distributionz/etcc                 C   s  t jdrLd} td3}|D ](}| }t|dkr |\}}nq|dkr*| }q|dkr7|d}|d }qW d   n1 sBw   Y  | ||fS t jdrtd.}|D ]#}|d}	t|	dkr||	d	 d
kr|d
|	d |f  W  d   S qYW d   n1 sw   Y  t jdrt d}
t	t|
d ddD ]}|
| dd dkr|
|= q|
r|

  d} |
d dd }| ||fS | ||fS )z Tries some special tricks to get the distribution
            information in case the default method fails.
            Currently supports older SuSE Linux, Caldera OpenLinux and
            Slackware Linux distributions.
        z/var/adm/inst-log/infoSuSEr[   ZMIN_DIST_VERSIONZ
DIST_IDENT-Nz/etc/.installedr   Z	OpenLinuxrY   z/usr/lib/setup   zslack-version-	slackware)r   pathexistsr;   rg   lenstripisdirlistdirrangesort)distnameversionidflineZtvtagr!   valuespkgZverfilesnr   r   r   _dist_try_harderg  sN   









r   z(\w+)[-_](release|version)z'(.+) release ([\d.]+)[^(]*(?:\((.+)\))?z1([^0-9]+)(?: release )?([\d.]+)[^(]*(?:\((.+)\))?)rr   ZdebianZfedoraZredhatZcentosZmandrakeZmandrivaZrocksrv   Z	yellowdogZgentooZUnitedLinuxZ
turbolinuxarchZmageiac                 C   sz   d}d}t | }|d urt| S t| }|d ur"t| S |   }|r8|d }t|dkr8|d }d||fS )Nr\   r   rY   )_lsb_release_versionmatchtuplegroups_release_versionrz   rg   ry   )	firstliner   r   mlr   r   r   _parse_release_file  s   


r   z(?:DISTRIB_ID\s*=)\s*(.*)z(?:DISTRIB_RELEASE\s*=)\s*(.*)z(?:DISTRIB_CODENAME\s*=)\s*(.*)r\   rY   c                 C   s   t | ||||S r-   )_linux_distribution)r   r   r   supported_distsfull_distribution_namer   r   r   rq     s   
rq   c              	   C   s  zWt ddH}|D ],}t|}|r|d }t|}|r'|d }	t|}|r5|d }
q	|rG|	rG||	|
fW  d   W S W d   n1 sQw   Y  W n ttfyb   Y nw zt	
t}W n tyx   | ||f Y S w |  |D ]}t|}|dur| \}}||v r|}  nqt| ||S t t	jt|dddd}| }W d   n1 sw   Y  t|\}}}|r|r|} |r|}|r|}| ||fS )a   Tries to determine the name of the Linux OS distribution name.
            The function first looks for a distribution release file in
            /etc and then reverts to _dist_try_harder() in case no
            suitable files are found.
            supported_dists may be given to define the set of Linux
            distributions to look for. It defaults to a list of currently
            supported Linux distributions identified by their release file
            name.
            If full_distribution_name is true (default), the full
            distribution read from the OS is returned. Otherwise the short
            name taken from supported_dists is used.
            Returns a tuple (distname, version, id) which default to the
            args given as parameters.
        z/etc/lsb-releaser5   rY   Nr%   surrogateescape)r8   r3   )r;   _distributor_id_file_researchgrouprz   _release_file_re_codename_file_reEnvironmentErrorUnboundLocalErrorr   r|   _UNIXCONFDIROSErrorr~   _release_filenamer   r   r   rw   r`   readliner   )r   r   r   r   r   Z	etclsbrelr   r   Z_u_distnameZ
_u_versionZ_u_idetcfileZ	_distnamedummyr   r   _versionZ_idr   r   r   r     sf   


 


r   )r5   Nr-   )Qr   rerf   r   os.pathr   zipfileri   
contextlibconfigparserr   Zbotocore.compatr   r   Zadvance_iteratorPY3ZmovesqueuerV   StringIOBytesIOurllibZrequestZurlopenr'   zlibZIP_DEFLATEDZZIP_COMPRESSION_MODEImportError
ZIP_STOREDZsqlite3re   rd   	Exceptionr   objectr   r+   collections.abcabcZcollections_abcr9   urllib.parseparseurlparseZurllib.errorr,   inputrR   r0   r1   r4   r?   rD   rF   collectionsrH   Zurllib2rL   rP   rT   rW   rU   rh   contextmanagerrp   rq   r   r   compileASCIIr   r   r   Z_supported_distsr   Ir   r   r   r   r   r   r   r   <module>   s   







?
0

