o 6at @sdZddlmZddlZddlZddlZddlZddlZddlm Z ddlm Z ddl m Z ddl mZzddlmZdd lmZmZdd lmZd ZWn eyWd ZYnweeZercgd ngZd+ddZd+ddZde de!de!fddZ"ddddZ#ddddZ$de!de!fdd Z%d!e!de!fd"d#Z& d,d$e!d%e!d&e e'de e(e!e!ffd'd(Z)d,d$e!d%e!d&e e'de e!e!ffd)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)shell) GetStdHandleSTD_OUTPUT_HANDLE)errorFT)z /usr/sbinz/usr/local/binz/usr/local/sbinreturncCs"ts tdkrtddSdS)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 shellwin32 IsUserAnAdminrErrorrr5/usr/lib/python3/dist-packages/certbot/compat/misc.py+raise_for_non_administrative_windows_rights!s rcCsZtrdSd}ztt}|r|||BWdSWdSty,tjdddYdSw)zR On Windows, ensure that Console Virtual Terminal Sequences are enabled. NzFailed to set console modeT)exc_info)r rr SetConsoleModeGetConsoleMode pywinerrorloggerdebug)"ENABLE_VIRTUAL_TERMINAL_PROCESSINGhrrrprepare_virtual_console,s rtimeoutpromptcCsZzttjggg|\}}}|std||dWSty,tjYSw)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)selectsysstdinrrformatreadlineOSError)rrrlist_rrrreadline_with_timeout@s  r'z C:\CertbotzC:\Certbot\libzC:\Certbot\log)configworklogsz/etc/letsencryptz/var/lib/letsencryptz/var/log/letsencrypt folder_typecCstjdkr t|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_folderis r0pathcCs.tjdkr|Stj|\}}||ddS)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-r1 splitdrivereplace)r1drivetailrrr.underscores_for_unsupported_characters_in_pathzs r7cmd_name shell_cmdenvc Csntd||trtj|dtjtjdd|d}ndd|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_newlinescheckr:zpowershell.exez-Command)r;r<r=r>r:) rinfor subprocessrunPIPEr;r< returncode)r8r9r:proclineouterrrrrexecute_command_statuss   rHcCs~tdtt|||\}}}tj|ddd}|r%t d||||dkr1t d||||r;t 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) warningswarnPendingDeprecationWarningrHrr1basenamesplitrr?r )r8r9r:rCrGrFbase_cmdrrrexecute_commandsrP)r N)N)+__doc__ __future__rloggingrr@r rJtypingrrcertbotrcertbot.compatrwin32com.shellrr win32consolerr pywintypesr rr ImportError getLogger__name__rSTANDARD_BINARY_DIRSrrfloatstrr'r/r.r0r7dictintrHrPrrrrsR              ,"