o
    6at                     @   s  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Zddlm	Z	 ddlm
Z
 ddlmZ ddlmZ zddlmZ dd	lmZmZ dd
lmZ dZW n eyW   dZY nw eeZercg dng Zd+ddZd+ddZde de!de!fddZ"ddddZ#ddddZ$de!de!fdd Z%d!e!de!fd"d#Z&	d,d$e!d%e!d&e	e' de
e(e!e!f fd'd(Z)d,d$e!d%e!d&e	e' de
e!e!f fd)d*Z*dS )-zk
This compat module handles various platform specific calls that do not fall into one
particular category.
    )absolute_importN)Optional)Tuple)errors)os)shell)GetStdHandleSTD_OUTPUT_HANDLE)errorFT)z	/usr/sbinz/usr/local/binz/usr/local/sbinreturnc                   C   s"   t st dkrtddS dS )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 r   r   5/usr/lib/python3/dist-packages/certbot/compat/misc.py+raise_for_non_administrative_windows_rights!   s   
r   c                  C   sZ   t rdS d} ztt}|r|| | B  W dS W dS  ty,   tjddd Y dS 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hr   r   r   prepare_virtual_console,   s   r   timeoutpromptc                 C   sZ   zt  tjgg g | \}}}|std||d  W S  ty,   tj  Y S 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_r   r   r   readline_with_timeout@   s   r'   z
C:\CertbotzC:\Certbot\libzC:\Certbot\log)configworklogsz/etc/letsencryptz/var/lib/letsencryptz/var/log/letsencryptfolder_typec                 C   s   t jdk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)r+   r   r   r   get_default_folderi   s   

r0   pathc                 C   s.   t jdkr| S t j| \}}||dd 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
    r,   :r&   )r   r-   r1   
splitdrivereplace)r1   drivetailr   r   r   .underscores_for_unsupported_characters_in_pathz   s   
r7   cmd_name	shell_cmdenvc              	   C   sn   t d| | trtj|dtjtjdd|d}ndd|g}tj|tjtjd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 differs from execute_command: it 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checkr:   zpowershell.exez-Command)r;   r<   r=   r>   r:   )	r   infor   
subprocessrunPIPEr;   r<   
returncode)r8   r9   r:   proclineouterrr   r   r   execute_command_status   s   
rH   c                 C   s~   t dt t| ||\}}}tj|ddd }|r%t	d| || |dkr1t
d| || |r;t
d| || ||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 differs from execute_command: it 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` (`str` stderr, `str` stdout)
    zTexecute_command will be deprecated in the future, use execute_command_status insteadN   r   zOutput from %s command %s:
%sz&%s command "%s" returned error code %dz#Error output from %s command %s:
%s)warningswarnPendingDeprecationWarningrH   r   r1   basenamesplitr   r?   r
   )r8   r9   r:   rC   rG   rF   base_cmdr   r   r   execute_command   s   rP   )r   N)N)+__doc__
__future__r   loggingr   r@   r    rJ   typingr   r   certbotr   certbot.compatr   win32com.shellr   r   win32consoler   r	   
pywintypesr
   r   r   ImportError	getLogger__name__r   STANDARD_BINARY_DIRSr   r   floatstrr'   r/   r.   r0   r7   dictintrH   rP   r   r   r   r   <module>   sR    



,"