U
    ÓZj[#  ã                   @   sü   d Z ddlmZ ddlZddlmZmZ ddlmZ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 e e¡Zd
d„ Zdd„ Zdd„ Zdd„ Zeƒ Zdd„ Zdd„ Zg Zdd„ Zdd„ Zdd„ Z dd„ Z!dd„ Z"d d!„ Z#d"d#„ Z$d$d%„ Z%dS )&z;Facilities for implementing hooks that call shell commands.é    )Úprint_functionN)ÚPopenÚPIPE)ÚSetÚList)Úerrors)Úutil)Ú
filesystem)Úosc                 C   s4   t | jdƒ t | jdƒ t | jdƒ t | jdƒ dS )z#Check hook commands are executable.ÚpreÚpostZdeployÚrenewN)Úvalidate_hookÚpre_hookÚ	post_hookÚdeploy_hookÚ
renew_hook)Úconfig© r   úP/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/certbot/hooks.pyÚvalidate_hooks   s    r   c                 C   s.   t  | ¡s"t | ¡ t  | ¡s"dS tj | ¡S )zÁExtract the program run by a shell command.

    :param str shell_cmd: command to be executed

    :returns: basename of command or None if the command isn't found
    :rtype: str or None

    N)r   Z
exe_existsÚ	plug_utilZpath_surgeryr
   ÚpathÚbasename)Ú	shell_cmdr   r   r   Ú_prog   s
    	


r   c                 C   s\   | rX|   dd¡d }t|ƒsXtjd }tj |¡r@d ||¡}nd |||¡}t |¡‚dS )z‹Check that a command provided as a hook is plausibly executable.

    :raises .errors.HookCommandNotFound: if the command is not found
    Né   r   ÚPATHz3{1}-hook command {0} exists, but is not executable.z>Unable to find {2}-hook command {0} in the PATH.
(PATH is {1}))	Úsplitr   r
   Úenvironr   ÚexistsÚformatr   ZHookCommandNotFound)r   Ú	hook_nameÚcmdr   Úmsgr   r   r   r   *   s    
  ÿr   c                 C   s>   | j dkr(| jr(t| jƒD ]}t|ƒ q| j}|r:t|ƒ dS )aå  Run pre-hooks if they exist and haven't already been run.

    When Certbot is running with the renew subcommand, this function
    runs any hooks found in the config.renewal_pre_hooks_dir (if they
    have not already been run) followed by any pre-hook in the config.
    If hooks in config.renewal_pre_hooks_dir are run and the pre-hook in
    the config is a path to one of these scripts, it is not run twice.

    :param configuration.NamespaceConfig config: Certbot settings

    r   N)ÚverbÚdirectory_hooksÚ
list_hooksZrenewal_pre_hooks_dirÚ_run_pre_hook_if_necessaryr   )r   Úhookr#   r   r   r   r   <   s    
r   c                 C   s.   | t krt d| ¡ ntd| ƒ t  | ¡ dS )zÑRun the specified pre-hook if we haven't already.

    If we've already run this exact command before, a message is logged
    saying the pre-hook was skipped.

    :param str command: pre-hook to be run

    z*Pre-hook command already run, skipping: %szpre-hookN)Úexecuted_pre_hooksÚloggerÚinfoÚ	_run_hookÚadd©Úcommandr   r   r   r(   T   s    	
r(   c                 C   sN   | j }| jdkr<| jr.t| jƒD ]}t|ƒ q |rJt|ƒ n|rJtd|ƒ dS )a…  Run post-hooks if defined.

    This function also registers any executables found in
    config.renewal_post_hooks_dir to be run when Certbot is used with
    the renew subcommand.

    If the verb is renew, we delay executing any post-hooks until
    :func:`run_saved_post_hooks` is called. In this case, this function
    registers all hooks found in config.renewal_post_hooks_dir to be
    called followed by any post-hook in the config. If the post-hook in
    the config is a path to an executable in the post-hook directory, it
    is not scheduled to be run twice.

    :param configuration.NamespaceConfig config: Certbot settings

    r   ú	post-hookN)r   r%   r&   r'   Zrenewal_post_hooks_dirÚ_run_eventuallyr-   )r   r#   r)   r   r   r   r   d   s    


r   c                 C   s   | t krt  | ¡ dS )zúRegisters a post-hook to be run eventually.

    All commands given to this function will be run exactly once in the
    order they were given when :func:`run_saved_post_hooks` is called.

    :param str command: post-hook to register to be run

    N)Ú
post_hooksÚappendr/   r   r   r   r2   †   s    	r2   c                  C   s   t D ]} td| ƒ qdS )zGRun any post hooks that were saved up in the course of the 'renew' verbr1   N)r3   r-   )r#   r   r   r   Úrun_saved_post_hooks“   s    r5   c                 C   s   | j rt| j ||| jƒ dS )a  Run post-issuance hook if defined.

    :param configuration.NamespaceConfig config: Certbot settings
    :param domains: domains in the obtained certificate
    :type domains: `list` of `str`
    :param str lineage_path: live directory path for the new cert

    N)r   Ú_run_deploy_hookÚdry_run)r   ÚdomainsÚlineage_pathr   r   r   r   ™   s
    	 ÿr   c                 C   sl   t ƒ }| jr6t| jƒD ]}t|||| jƒ | |¡ q| jrh| j|krVt 	d| j¡ nt| j||| jƒ dS )a]  Run post-renewal hooks.

    This function runs any hooks found in
    config.renewal_deploy_hooks_dir followed by any renew-hook in the
    config. If the renew-hook in the config is a path to a script in
    config.renewal_deploy_hooks_dir, it is not run twice.

    If Certbot is doing a dry run, no hooks are run and messages are
    logged saying that they were skipped.

    :param configuration.NamespaceConfig config: Certbot settings
    :param domains: domains in the obtained certificate
    :type domains: `list` of `str`
    :param str lineage_path: live directory path for the new cert

    z0Skipping deploy-hook '%s' as it was already run.N)
Úsetr&   r'   Zrenewal_deploy_hooks_dirr6   r7   r.   r   r+   r,   )r   r8   r9   Zexecuted_dir_hooksr)   r   r   r   r   §   s    
ÿ ÿr   c                 C   s<   |rt  d| ¡ dS d |¡tjd< |tjd< td| ƒ dS )a  Run the specified deploy-hook (if not doing a dry run).

    If dry_run is True, command is not run and a message is logged
    saying that it was skipped. If dry_run is False, the hook is run
    after setting the appropriate environment variables.

    :param str command: command to run as a deploy-hook
    :param domains: domains in the obtained certificate
    :type domains: `list` of `str`
    :param str lineage_path: live directory path for the new cert
    :param bool dry_run: True iff Certbot is doing a dry run

    z)Dry run: skipping deploy hook command: %sNú ZRENEWED_DOMAINSZRENEWED_LINEAGEzdeploy-hook)r+   ÚwarningÚjoinr
   r   r-   )r0   r8   r9   r7   r   r   r   r6   Ç   s    ÿ
r6   c                 C   s   t | |ƒ\}}|S )zÜRun a hook command.

    :param str cmd_name: the user facing name of the hook being run
    :param shell_cmd: shell command to execute
    :type shell_cmd: `list` of `str` or `str`

    :returns: stderr if there was any)Úexecute)Úcmd_namer   ÚerrÚ_r   r   r   r-   ß   s    r-   c                 C   s�   t  d| |¡ t|dttdd�}| ¡ \}}tj | dd¡d ¡}|rXt  d| ||¡ |j	dkrtt  
d| ||j	¡ |rˆt  
d	| ||¡ ||fS )
zäRun a command.

    :param str cmd_name: the user facing name of the hook being run
    :param shell_cmd: shell command to execute
    :type shell_cmd: `list` of `str` or `str`

    :returns: `tuple` (`str` stderr, `str` stdout)zRunning %s command: %sT)ÚshellÚstdoutÚstderrÚuniversal_newlinesNr   r   zOutput from %s command %s:
%sz&%s command "%s" returned error code %dz#Error output from %s command %s:
%s)r+   r,   r   r   Úcommunicater
   r   r   r   Ú
returncodeÚerror)r?   r   r#   Úoutr@   Zbase_cmdr   r   r   r>   ë   s$     ÿ
  ÿr>   c                    s.   ‡ fdd„t  ˆ ¡D ƒ}dd„ |D ƒ}t|ƒS )zÒList paths to all hooks found in dir_path in sorted order.

    :param str dir_path: directory to search

    :returns: `list` of `str`
    :rtype: sorted list of absolute paths to executables in dir_path

    c                 3   s   | ]}t j ˆ |¡V  qd S )N)r
   r   r=   )Ú.0Úf©Údir_pathr   r   Ú	<genexpr>  s     zlist_hooks.<locals>.<genexpr>c                 S   s$   g | ]}t  |¡r| d ¡s|‘qS )ú~)r	   Zis_executableÚendswith)rJ   r   r   r   r   Ú
<listcomp>  s     
 
 zlist_hooks.<locals>.<listcomp>)r
   ÚlistdirÚsorted)rM   ZallpathsÚhooksr   rL   r   r'     s    	r'   )&Ú__doc__Ú
__future__r   ÚloggingÚ
subprocessr   r   Zacme.magic_typingr   r   Zcertbotr   r   Zcertbot.compatr	   r
   Zcertbot.pluginsr   Ú	getLoggerÚ__name__r+   r   r   r   r   r:   r*   r(   r   r3   r2   r5   r   r   r6   r-   r>   r'   r   r   r   r   Ú<module>   s4   
 