3 گa! @ s U d Z ddlZddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z dd l mZ dd l mZ ddl m Z ddlmZ ddlm Z ejeZejdd ddZeee dddZeeddddZejdd ddZe Zee eddddZejdd ddZg Z ee eddddZ!ddd d!Z"ejee edd"d#d$Z#ejee edd"d%d&Z$eee ee%dd'd(d)Z&eeed*d+d,Z'eee d-d.d/Z(dS )0z;Facilities for implementing hooks that call shell commands. N)List)Optional)Set) configuration)errors)util) filesystem)misc)os)ops)configreturnc C s4 t | jd t | jd t | jd t | jd dS )z#Check hook commands are executable.ZpreZpostZdeployrenewN) validate_hookpre_hook post_hookdeploy_hook renew_hook)r r /usr/lib/python3.6/hooks.pyvalidate_hooks s r ) shell_cmdr c C s. t j| s"tj| t j| s"dS tjj| S )zExtract 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_utilZpath_surgeryr pathbasename)r r r r _prog s r )r hook_namer c C s\ | rX| j ddd }t|sXtjd }tjj|r@dj||}ndj|||}tj|dS )zCheck 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 existsformatr ZHookCommandNotFound)r r cmdr msgr r r r - s r c C sB | j dkr,| jr,xt| jD ]}t| qW | j}|r>t| dS )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)verbdirectory_hooks list_hooksZrenewal_pre_hooks_dir_run_pre_hook_if_necessaryr )r hookr# r r r r ? s r )commandr c C s. | t krtjd| ntd| t j| dS )zRun 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: %szpre-hookN)executed_pre_hooksloggerinfo _run_hookadd)r* r r r r( W s r( c C sR | j }| jdkr@| jr2xt| jD ]}t| q"W |rNt| n|rNtd| dS )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 z post-hookN)r r% r&