o
    @&ßa½I  ã                   @   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 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e
jjjZz	d dlZejZW n eyg   ejZY nw zd dlZW n eyy   dZY nw e jdkZer„dZ ndZ G dd„ de!ƒZ"G d	d
„ d
e#ƒZ$dd„ Z%e
jrÌd 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„Z4d@d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„Z4d@dd„Z5dd„ Z:d d!„ Z;d"d#„ Z<d@d$d%„Z=d&d'„ Z>d@d(d)„Z?ej@d*d+„ ƒZAz	d d,lmBZB W dS  e�yw   d-ZCd.d/„ ZDe Ed0ejF¡ZGe Ed1ejF¡ZHe Ed2ejF¡ZId3ZJd4d5„ ZKe Ed6ejL¡ZMe Ed7ejL¡ZNe Ed8ejL¡ZOd9d9d9eJd:fd;d<„ZBd=d>„ ZPY dS w )Aé    N)Ú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__© ú//usr/lib/python3/dist-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   Ú	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 )zŸReturn 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*   ÚbuffÚnum_backspacesÚ	characterÚnew_sr   r   r   rU   ú   s,   

rU   c                 C   s4   i }| du rt } trddi}nt | ¡} | |d< |S )z±Returns 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)Ú	pager_cmdÚpopen_kwargsr   r   r   Úget_popen_kwargs_for_pager_cmd9  s   

rm   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)rh   ÚsignalÚSIGINTÚSIGQUITÚSIGTSTPr_   ÚSIG_IGNÚ	enumerate)Úsignal_listÚactual_signalsÚuser_signalÚsigr   r   r   Úignore_user_entered_signalsM  s   €
ÿÿrx   )Ú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¡rŒtdƒ�.}|D ]#}| d¡}	t|	ƒdkr||	d	 d
kr|d
|	d |f  W  d  ƒ S qYW d  ƒ n1 s‡w   Y  t j d¡rÆt  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[   ÚMIN_DIST_VERSIONÚ
DIST_IDENTú-Nz/etc/.installedr   Ú	OpenLinuxrY   z/usr/lib/setupéÿÿÿÿé   zslack-version-Ú	slackware)r   ÚpathÚexistsr;   rj   ÚlenÚstripÚisdirÚlistdirÚrangeÚsort)ÚdistnameÚversionÚidÚfÚlineÚtvÚtagr    ÚvaluesÚpkgÚverfilesÚnr   r   r   Ú_dist_try_harderg  sN   



€öÿ


ú€ÿ
€

r•   z(\w+)[-_](release|version)z'(.+) release ([\d.]+)[^(]*(?:\((.+)\))?z1([^0-9]+)(?: release )?([\d.]+)[^(]*(?:\((.+)\))?)rz   ÚdebianÚfedoraÚredhatÚcentosÚmandrakeÚmandrivaÚrocksr�   Ú	yellowdogÚgentooÚUnitedLinuxÚ
turbolinuxÚarchÚ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_versionr…   rj   r„   )Ú	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   ry   Ê  s   
ÿry   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 s»w   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Úgroupr…   Ú_release_file_reÚ_codename_file_reÚEnvironmentErrorÚUnboundLocalErrorr   r‡   Ú_UNIXCONFDIRÚOSErrorr‰   Ú_release_filenamer¤   r¦   r•   r‚   r`   Úreadliner«   )rŠ   r‹   rŒ   r­   r®   Ú	etclsbrelrŽ   r©   Ú_u_distnameÚ
_u_versionÚ_u_idÚetcÚfileÚ	_distnameÚdummyr�   r¨   Ú_versionÚ_idr   r   r   r¬   Ð  sf   


€ô€ €ÿþ
€ÿ
þ
r¬   )r5   Nr-   )Qr   Úreri   r   Úos.pathr   Úzipfilern   Ú
contextlibÚbotocore.compatr   r   Úadvance_iteratorÚPY3ÚmovesÚqueuerV   ÚStringIOÚBytesIOÚurllibÚrequestÚurlopenr'   ÚconfigparserÚRawConfigParserÚzlibÚZIP_DEFLATEDÚZIP_COMPRESSION_MODEÚImportErrorÚ
ZIP_STOREDÚsqlite3rh   rg   Ú	Exceptionr   Úobjectr   r+   Úcollections.abcÚabcÚcollections_abcr9   Úurllib.parseÚparseÚurlparseÚurllib.errorr,   ÚinputrR   r0   r1   r4   r?   rD   rF   ÚcollectionsrH   Úurllib2rL   rP   rT   rW   rU   rm   Úcontextmanagerrx   ry   r·   r•   ÚcompileÚASCIIr¹   r£   r§   Ú_supported_distsr«   ÚIr°   r³   r´   r¬   r   r   r   r   Ú<module>   s¸   


ÿÿ





?
0ýý

þ•