3 گa@sdZddlmZddlZddlZddlZddlZddlmZddlm Z ddl Z ddl m Z ddl mZy8ddlmZdd lmZdd lmZdd lmZd ZWnek rd ZYnXejeZerdddgngZddddZddddZe ee!e!dddZ"ddddZ#ddddZ$e!e!d d!d"Z%e!e!d#d$d%Z&d+e!e!ee'e e(e!e!fd&d'd(Z)d,e!e!ee'e e!e!fd&d)d*Z*dS)-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_HANDLEFTz /usr/sbinz/usr/local/binz/usr/local/sbin)returncCs t rtjdkrtjddS)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. rzAError, certbot must be run on a shell with administrative rights.N) POSIX_MODE shellwin32Z IsUserAnAdminrErrorrr/usr/lib/python3.6/misc.py+raise_for_non_administrative_windows_rights"src CsVtrdSd}y"tt}|r,|j|j|BWn"tk rPtjdddYnXdS)zR On Windows, ensure that Console Virtual Terminal Sequences are enabled. NzFailed to set console modeT)exc_info)r r r ZSetConsoleModeZGetConsoleMode pywinerrorloggerdebug)Z"ENABLE_VIRTUAL_TERMINAL_PROCESSINGhrrrprepare_virtual_console-sr)timeoutpromptr c CsbyBtjtjggg|\}}}|s8tjdj|r0|nd|djStk r\tjjSXdS)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}'rN)selectsysstdinrrformatreadlineOSError)rrZrlist_rrrreadline_with_timeoutAs  r#z C:\CertbotzC:\Certbot\libzC:\Certbot\log)configZworkZlogsz/etc/letsencryptz/var/lib/letsencryptz/var/log/letsencrypt) folder_typer cCstjdkrt|St|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)rnameLINUX_DEFAULT_FOLDERSWINDOWS_DEFAULT_FOLDERS)r%rrrget_default_folderjs r*)pathr cCs.tjdkr|Stjj|\}}||jddS)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")rr'r+ splitdrivereplace)r+Zdrivetailrrr.underscores_for_unsupported_characters_in_path{s r0)cmd_name shell_cmdenvr c Csntjd||tr0tj|dtjtjdd|d}n$dd|g}tj|tjtjdd|d}|j|j}}|j||fS)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) zRunning %s command: %sTF)rstdoutstderruniversal_newlinescheckr3zpowershell.exez-Command)r4r5r6r7r3) rinfor subprocessZrunPIPEr4r5 returncode)r1r2r3proclineouterrrrrexecute_command_statuss    r@cCs~tjdtt|||\}}}tjj|jddd}|rJtj d||||dkrbtj d||||rvtj d|||||fS)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 insteadNrzOutput from %s command %s: %sz&%s command "%s" returned error code %dz#Error output from %s command %s: %s) warningswarnPendingDeprecationWarningr@rr+basenamesplitrr8r)r1r2r3r;r?r>Zbase_cmdrrrexecute_commands rG)N)N)+__doc__Z __future__rZloggingrr9rZtypingrrrBZcertbotrZcertbot.compatrZ pywintypesrrZwin32com.shellrr Z win32consoler r r ImportErrorZ getLogger__name__rZSTANDARD_BINARY_DIRSrrfloatstrr#r)r(r*r0dictintr@rGrrrrsB            $!