a
    `                     @   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mZ ddl	m
Z
 ddlmZmZ zddlmZ dZW n ey   d	ZY n0 eeZerg d
ng Zdd Zdd ZddddZddddZdd Zdd ZdddZdS )zk
This compat module handles various platform specific calls that do not fall into one
particular category.
    )absolute_importN)errors)os)TupleOptional)shellFT)z	/usr/sbinz/usr/local/binz/usr/local/sbinc                   C   s   t st dkrtd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
shellwin32ZIsUserAnAdminr   Error r   r   5/usr/lib/python3/dist-packages/certbot/compat/misc.py+raise_for_non_administrative_windows_rights   s    r   c                 C   s^   z<t  tjgg g | \}}}|s0td||d  W S  tyX   tj  Y S 0 dS )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   N)selectsysstdinr   r
   formatreadlineOSError)ZtimeoutpromptZrlist_r   r   r   readline_with_timeout*   s    r   z
C:\CertbotzC:\Certbot\libzC:\Certbot\log)ZconfigZworkZlogsz/etc/letsencryptz/var/lib/letsencryptz/var/log/letsencryptc                 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)Zfolder_typer   r   r   get_default_folderT   s    
r   c                 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   path
splitdrivereplace)r   Zdrivetailr   r   r   .underscores_for_unsupported_characters_in_pathf   s    
r!   c                 C   s   t d| | tr.tj|dtjtjd|d}n"dd|g}tj|tjtjd|d}| \}}tj	|
ddd	 }|rt d
| || |jd	krt d| ||j |rt d| || ||fS )a  
    Run a command:
        - on Linux command will be run by the standard shell selected with Popen(shell=True)
        - on Windows command will be run in a Powershell shell

    :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 Popen

    :returns: `tuple` (`str` stderr, `str` stdout)
    zRunning %s command: %sT)r   stdoutstderruniversal_newlinesenvzpowershell.exez-Command)r"   r#   r$   r%   N   r   zOutput from %s command %s:
%sz&%s command "%s" returned error code %dz#Error output from %s command %s:
%s)loggerinfor   
subprocessPopenPIPEZcommunicater   r   basenamesplit
returncodeerror)Zcmd_nameZ	shell_cmdr%   cmdlineouterrZbase_cmdr   r   r   execute_commandw   s*    

r4   )N)__doc__Z
__future__r   Zloggingr   r)   r   Zcertbotr   Zcertbot.compatr   Zacme.magic_typingr   r   Zwin32com.shellr   r	   r   ImportErrorZ	getLogger__name__r'   ZSTANDARD_BINARY_DIRSr   r   r   r   r   r!   r4   r   r   r   r   <module>   s8   

