
    M/e$                     x   d Z ddlm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 ddlmZ 	 ddlmZ dd	lmZ dd
lmZ ddlmZ dZ ej2                  e      Zerg dng Zd$dZd$dZdedee    de fdZ!ddddZ"ddddZ#de de fdZ$de de fdZ%	 d%d e d!e d"ee&   de	e'e e f   fd#Z(y# e$ r dZY lw xY w)&zk
This compat module handles various platform specific calls that do not fall into one
particular category.
    )absolute_importN)Optional)Tuple)errors)os)error)shell)GetStdHandle)STD_OUTPUT_HANDLEFT)z	/usr/sbinz/usr/local/binz/usr/local/sbinreturnc                  j    t         s-t        j                         dk(  rt        j                  d      yy)z
    On Windows, raise if current shell does not have the administrative rights.
    Do nothing on Linux.

    :raises .errors.Error: If the current shell does not have administrative rights on Windows.
    r   zAError, certbot must be run on a shell with administrative rights.N)
POSIX_MODE
shellwin32IsUserAnAdminr   Error     5/usr/lib/python3/dist-packages/certbot/compat/misc.py+raise_for_non_administrative_windows_rightsr   !   s.     *2249ll^__ ::r   c                      t         ryd} 	 t        t              }|r#|j                  |j	                         | z         yy# t
        $ r t        j                  dd       Y yw xY w)zR
    On Windows, ensure that Console Virtual Terminal Sequences are enabled.

    N   zFailed to set console modeT)exc_info)r   r
   r   SetConsoleModeGetConsoleMode
pywinerrorloggerdebug)"ENABLE_VIRTUAL_TERMINAL_PROCESSINGhs     r   prepare_virtual_consoler    ,   sj    
  *0&B*+Q--/2TTU  B1DABs   3A    A#"A#timeoutpromptc                 >   	 t        j                   t        j                  gg g |       \  }}}|s0t        j                  dj                  |r
|            d            |d   j                         S # t        $ r! t        j                  j                         cY S w xY w)a(  
    Read user input to return the first line entered, or raise after specified timeout.

    :param float timeout: The timeout in seconds given to the user.
    :param str prompt: The prompt message to display to the user.

    :returns: The first line entered by the user.
    :rtype: str

    z,Timed out waiting for answer to prompt '{0}' r   )selectsysstdinr   r   formatreadlineOSError)r!   r"   rlist_s       r   readline_with_timeoutr-   @   s    $ mmSYYKRAq!,,>EEPVf_a a\^_a aQx  "" $ yy!!##$s   A/A2 2'BBz
C:\CertbotzC:\Certbot\libzC:\Certbot\log)configworklogsz/etc/letsencryptz/var/lib/letsencryptz/var/log/letsencryptfolder_typec                 L    t         j                  dk7  r	t        |    S t        |    S )z
    Return the relevant default folder for the current OS

    :param str folder_type: The type of folder to retrieve (config, work or logs)

    :returns: The relevant default folder.
    :rtype: str

    nt)r   nameLINUX_DEFAULT_FOLDERSWINDOWS_DEFAULT_FOLDERS)r1   s    r   get_default_folderr7   i   s%     
ww$$[11";//r   pathc                     t         j                  dk7  r| S t         j                  j                  |       \  }}||j	                  dd      z   S )z
    Replace unsupported characters in path for current OS by underscores.
    :param str path: the path to normalize
    :return: the normalized path
    :rtype: str
    r3   :r,   )r   r4   r8   
splitdrivereplace)r8   drivetails      r   .underscores_for_unsupported_characters_in_pathr?   z   sD     
ww$ ''$$T*KE44<<S)))r   cmd_name	shell_cmdenvc           	      t   t         j                  d| |       t        r9t        j                  |dt        j
                  t        j
                  dd|      }n<dd|g}t        j                  |t        j
                  t        j
                  dd|      }|j                  |j                  }}|j                  ||fS )a#  
    Run a command:
        - on Linux command will be run by the standard shell selected with
          subprocess.run(shell=True)
        - on Windows command will be run in a Powershell shell

    This function returns the exit code, and does not log the result and output
    of the command.

    :param str cmd_name: the user facing name of the hook being run
    :param str shell_cmd: shell command to execute
    :param dict env: environ to pass into subprocess.run

    :returns: `tuple` (`int` returncode, `str` stderr, `str` stdout)
    zRunning %s command: %sTF)r	   stdoutstderruniversal_newlinescheckrB   zpowershell.exez-Command)rD   rE   rF   rG   rB   )	r   infor   
subprocessrunPIPErD   rE   
returncode)r@   rA   rB   proclineouterrs          r   execute_command_statusrQ      s    " KK((I>~~itJOO%/__$)s4 !*i8~~d:??:??15UM
 {{DKKC??C$$r   )r   N)N))__doc__
__future__r   loggingr%   rI   r&   typingr   r   certbotr   certbot.compatr   
pywintypesr   r   win32com.shellr	   r   win32consoler
   r   r   ImportError	getLogger__name__r   STANDARD_BINARY_DIRSr   r    floatstrr-   r6   r5   r7   r?   dictintrQ   r   r   r   <module>rc      s%   '    
    .2).J
 
		8	$ NXI]_ `B($5 $(3- $C $<   !"" 0C 0C 0"* * *" 26%S %S % (%:?S#:N%g  Js   B/ /B98B9