%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python3/dist-packages/certbot/__pycache__/
Upload File :
Create Path :
Current File : //lib/python3/dist-packages/certbot/__pycache__/reverter.cpython-312.pyc

�

M/�e5V����dZddlZddlZddlZddlZddlZddlZddlmZddlm	Z	ddlm
Z
ddlmZddlmZddl
mZdd	l
mZdd
l
mZddlmZddlmZdd
lmZej,e�ZGd�d�Zy)zGReverter class saves configuration checkpoints and allows for recovery.�N)�Iterable)�List)�Set)�TextIO)�Tuple)�
configuration)�errors)�util)�	constants)�
filesystem)�osc�p�eZdZdZdej
ddfd�Zd!d�Zd"deddfd�Z	d	e
ed
eddfd�Zd	e
ed
eddfd�Z
d
ed	e
ed
eddfd�Zdedeeeeffd�Zd
eddfd�Zdeddfd�Zd	e
eddfd�Zdededdfd�Zdedeeddfd�Zdedefd�Zd!d�Zdedefd�Zdeddfd�Zdefd�Zd!d �Zy)#�ReverteraqReverter Class - save and revert configuration checkpoints.

    This class can be used by the plugins, especially Installers, to
    undo changes made to the user's system. Modifications to files and
    commands to do undo actions taken by the plugin should be registered
    with this class before the action is taken.

    Once a change has been registered with this class, there are three
    states the change can be in. First, the change can be a temporary
    change. This should be used for changes that will soon be reverted,
    such as config changes for the purpose of solving a challenge.
    Changes are added to this state through calls to
    :func:`~add_to_temp_checkpoint` and reverted when
    :func:`~revert_temporary_config` or :func:`~recovery_routine` is
    called.

    The second state a change can be in is in progress. These changes
    are not temporary, however, they also have not been finalized in a
    checkpoint. A change must become in progress before it can be
    finalized. Changes are added to this state through calls to
    :func:`~add_to_checkpoint` and reverted when
    :func:`~recovery_routine` is called.

    The last state a change can be in is finalized in a checkpoint. A
    change is put into this state by first becoming an in progress
    change and then calling :func:`~finalize_checkpoint`. Changes
    in this state can be reverted through calls to
    :func:`~rollback_checkpoints`.

    As a final note, creating new files and registering undo commands
    are handled specially and use the methods
    :func:`~register_file_creation` and :func:`~register_undo_command`
    respectively. Both of these methods can be used to create either
    temporary or in progress changes.

    .. note:: Consider moving everything over to CSV format.

    :param config: Configuration.
    :type config: :class:`certbot.configuration.NamespaceConfig`

    �config�returnNc��||_tj|jtj
|jj�y)N)rr
�make_or_verify_dir�
backup_dirr�CONFIG_DIRS_MODE�strict_permissions)�selfrs  �2/usr/lib/python3/dist-packages/certbot/reverter.py�__init__zReverter.__init__Bs5�����������y�9�9�4�;�;�;Y�;Y�	[�c�d�tjj|jj�r'	|j|jj�yy#tj$r@tjd|jj�t
jd��wxYw)aReload users original configuration files after a temporary save.

        This function should reinstall the users original configuration files
        for all saves with temporary=True

        :raises .ReverterError: when unable to revert config

        z$Incomplete or failed recovery for %sz!Unable to revert temporary configN)
r
�path�isdirr�temp_checkpoint_dir�_recover_checkpointr	�
ReverterError�logger�critical�rs r�revert_temporary_configz Reverter.revert_temporary_configHs����7�7�=�=����8�8�9�
P��(�(����)H�)H�I�:���'�'�
P����:��K�K�3�3���*�*�+N�O�O�

P�s�%A�AB/�rollbackc�>�	t|�}|dkr*tjd�t	j
d��t
j|jj�}|j�|stjd�n.t|�|kr tjd|t|��|dkDrf|rctjj|jj|j��}	|j!|�|dz}|dkDr|r�ay	y	y	y	#t$r+tjd�t	j
d��wxYw#tj
$r+tj#d�t	j
d��wxYw)
ayRevert 'rollback' number of configuration checkpoints.

        :param int rollback: Number of checkpoints to reverse. A str num will be
           cast to an integer. So "2" is also acceptable.

        :raises .ReverterError:
            if there is a problem with the input or if the function is
            unable to correctly revert the configuration checkpoints

        z,Rollback argument must be a positive integerz
Invalid InputrzHCertbot hasn't modified your configuration, so rollback isn't available.z0Unable to rollback %d checkpoints, only %d existz)Failed to load checkpoint during rollbackz)Unable to load checkpoint during rollback�N)�int�
ValueErrorr!�errorr	r r
�listdirrr�sort�warning�lenr�join�poprr")rr%�backups�cp_dirs    r�rollback_checkpointszReverter.rollback_checkpoints\s]��	8��8�}�H�
�a�<��L�L�G�H��&�&��7�7��*�*�T�[�[�3�3�4��������N�N�#�
$���\�H�
$��N�N�M�#�S��\�
3���l�w��W�W�\�\�$�+�+�"8�"8�'�+�+�-�H�F�
A��(�(��0�

��M�H���l�w�l�w�l��'�	8��L�L�G�H��&�&��7�7�	8��.�'�'�
A���� K�L��*�*�?�A�A�
A�s�D'�E�'4E�>F�
save_files�
save_notesc�R�|j|jj||�y)z�Add files to temporary checkpoint.

        :param set save_files: set of filepaths to save
        :param str save_notes: notes about changes during the save

        N)�_add_to_checkpoint_dirrr�rr4r5s   r�add_to_temp_checkpointzReverter.add_to_temp_checkpoint�s$��	
�#�#��K�K�+�+�Z��	Erc�t�|j|�|j|jj||�y)z�Add files to a permanent checkpoint.

        :param set save_files: set of filepaths to save
        :param str save_notes: notes about changes during the save

        N)�_check_tempfile_savesr7r�in_progress_dirr8s   r�add_to_checkpointzReverter.add_to_checkpoint�s2��	
�"�"�:�.��#�#��K�K�'�'��Z�	Arr2c��tj|tj|jj
�|j
tjj|d��\}}t|�}|D]�}||vs�tjd|�	tj|tjj|tjj|�dzt!|�z��|j#dj%|��|dz
}��|j)�t1tjj|d�d	�5}|j#|�d
d
d
�y
#t&$rM|j)�tj+d||�t-j.dj%||���wxYw#1swYy
xYw)agAdd save files to checkpoint directory.

        :param str cp_dir: Checkpoint directory filepath
        :param set save_files: set of files to save
        :param str save_notes: notes about changes made during the save

        :raises IOError: if unable to open cp_dir + FILEPATHS file
        :raises .ReverterError: if unable to add checkpoint

        �	FILEPATHSzCreating backup of %s�_�{0}
z&Unable to add file %s to checkpoint %sz(Unable to add file {0} to checkpoint {1}r'�
CHANGES_SINCE�aN)r
rrrrr�_read_and_appendr
rr/r.r!�debug�shutil�copy2�basename�str�write�format�IOError�closer*r	r �open)	rr2r4r5�op_fd�existing_filepaths�idx�filename�notes_fds	         rr7zReverter._add_to_checkpoint_dir�s���	
����I�.�.����0N�0N�	P�%)�$9�$9��G�G�L�L���-�%/�!��!��$�%��"�	�H��1�1����4�h�?�8��L�L��2�7�7�<�<����� 0� 0�� :�S� @�3�s�8� K�,M�N��K�K����x� 8�9��q���)	�*	���
�
�"�'�'�,�,�v��7��
=�	'���N�N�:�&�	'�	'���8��K�K�M��L�L�@� �&�*�!�.�.��$�f�X�v�6�8�8�8��	'�	'�s�BE4�G
�4AG
�
G�filepathc��tjj|�r.t|d�}|j	�j�}||fSg}t|d�}||fS)z�Reads the file lines and returns a file obj.

        Read the file returning the lines, and a pointer to the end of the file.

        zr+�w)r
r�isfilerN�read�
splitlines)rrTrO�liness    rrDzReverter._read_and_append�s]���7�7�>�>�(�#���4�(�E��J�J�L�+�+�-�E�
�e�|���E���3�'�E��e�|�rc�z�tjjtjj|d��r/|j	tjj|d��tjjtjj|d��r�	ttjj|d��5}|j
�j�}t|�D]e\}}tjtjj|tjj|�dzt|�z�|��g	ddd�|j'tjj|d��	tj(|�y#1swY�OxYw#ttf$r/tj!d|�t#j$d|����wxYw#t$r/tj!d|�t#j$d|z��wxYw)	aRecover a specific checkpoint.

        Recover a specific checkpoint provided by cp_dir
        Note: this function does not reload augeas.

        :param str cp_dir: checkpoint directory file path

        :raises errors.ReverterError: If unable to recover checkpoint

        �COMMANDSr?r@NzUnable to recover files from %szUnable to recover files from �	NEW_FILESzUnable to remove directory: %s)r
rrWr/�_run_undo_commandsrNrXrY�	enumeraterFrGrHrIrL�OSErrorr!r*r	r �_remove_contained_files�rmtree)rr2�paths_fd�	filepathsrQrs      rrzReverter._recover_checkpoint�s����7�7�>�>�"�'�'�,�,�v�z�:�;��#�#�B�G�G�L�L���$D�E�
�7�7�>�>�"�'�'�,�,�v�{�;�<�

U��"�'�'�,�,�v�{�;�<�L�� (�
�
�� :� :� <�I�%.�y�%9�L�	��T����R�W�W�\�\�"��G�G�,�,�T�2�S�8�3�s�8�C�&E�FJ�L�L�L�	
�$�$�R�W�W�\�\�&�+�%F�G�	;��M�M�&�!�L�L���W�%�
U����>��G��*�*�-J�6�(�+S�T�T�
U���	;��L�L�9�6�B��&�&�0�6�9�;�
;�	;�s1�+)G�BF5�'G�H�5F>�:G�>G?�8H:c	�V�ddi}t|dfi|��5}tj|�}tt	|��D]}	tj|��	ddd�y#tj$r(tjddj|��Y�_wxYw#1swYyxYw)zRun all commands in a file.�newline��rzUnable to run undo command: %s� N)rN�csv�reader�reversed�listr
�
run_scriptr	�SubprocessErrorr!r*r/)rrT�kwargs�csvfile�	csvreader�commands      rr^zReverter._run_undo_commandss���
�R���
�(�C�
*�6�
*�	M�g��
�
�7�+�I�#�D��O�4�
M��M��O�O�G�,�
M�	M�	M��
�-�-�M��L�L�8�#�(�(�7�:K�M�M��	M�	M�s/�-B�A!�B�!8B�B�B�B�B(c���g}tjj|jjd�}tjj|�rBt
|d�5}|j|j�j��ddd�tjj|jjd�}tjj|�rBt
|d�5}|j|j�j��ddd�|D]}||vs�tjd|����y#1swY��xYw#1swY�9xYw)z�Verify save isn't overwriting any temporary files.

        :param set save_files: Set of files about to be saved.

        :raises certbot.errors.ReverterError:
            when save is attempting to overwrite a temporary file.

        r?rhNr]z)Attempting to overwrite challenge file - )r
rr/rrrWrN�extendrXrYr	r )rr4�protected_files�	temp_path�protected_fd�new_pathrRs       rr;zReverter._check_tempfile_savess)�����G�G�L�L����!@�!@�+�N�	�
�7�7�>�>�)�$��i��%�
I���&�&�|�'8�'8�':�'E�'E�'G�H�
I��7�7�<�<���� ?� ?��M��
�7�7�>�>�(�#��h��$�
I���&�&�|�'8�'8�':�'E�'E�'G�H�
I�(�	c�H��:�%��*�*�-V�W_�V`�+a�b�b�	c�
I�
I��
I�
I�s�".E�7.E�E�E&�	temporary�filesc��|stjd��|j|�}d}	|jtj
j
|d��\}}|D]'}||vs�|jdj|���)	|�|j�yy#ttf$r;tjd|�tjdj|���wxYw#|�|j�wwxYw)aARegister the creation of all files during certbot execution.

        Call this method before writing to the file to make sure that the
        file will be cleaned up if the program exits unexpectedly.
        (Before a save occurs)

        :param bool temporary: If the file creation registry is for
            a temp or permanent save.
        :param \*files: file paths (str) to be registered

        :raises certbot.errors.ReverterError: If
            call does not contain necessary parameters or if the file creation
            is unable to be registered.

        z,Forgot to provide files to registration callNr]rAz(Unable to register file creation(s) - %sz)Unable to register file creation(s) - {0})r	r �_get_cp_dirrDr
rr/rJrKrLr`r!r*rM)rrzr{r2�new_fd�ex_filesrs       r�register_file_creationzReverter.register_file_creation+s���$��&�&�'U�V�V��!�!�)�,����	�#�4�4�R�W�W�\�\�&�+�5V�W��F�H��
7���x�'��L�L�����!5�6�
7��!�����"����!�	K��L�L�C�U�K��&�&�;�B�B�5�I�K�
K�	K��
�!�����"�s�;B�("B�A
C)�)C,�,Drsc��tjj|j|�d�}ddi}	tjj	|�rdnd}t||fi|��5}t
j|�}|j|�ddd�y#1swYyxYw#ttf$r+tjd�tjd��wxYw)	a�Register a command to be run to undo actions taken.

        .. warning:: This function does not enforce order of operations in terms
            of file modification vs. command registration.  All undo commands
            are run first before all normal files are reverted to their previous
            state.  If you need to maintain strict order, you may create
            checkpoints before and after the the command registration. This
            function may be improved in the future based on demand.

        :param bool temporary: Whether the command should be saved in the
            IN_PROGRESS or TEMPORARY checkpoints.
        :param command: Command to be run.
        :type command: list of str

        r\rfrgrCrVNzUnable to register undo commandz Unable to register undo command.)r
rr/r}rWrNrj�writer�writerowrLr`r!r*r	r )rrzrs�commands_fprp�mode�f�	csvwriters        r�register_undo_commandzReverter.register_undo_commandRs��� �g�g�l�l�4�#3�#3�I�#>�
�K���R���	4��'�'�.�.��5�3�3�D��k�4�2�6�2�
,�a��J�J�q�M�	��"�"�7�+�
,�
,�
,����!�	4��L�L�:�;��&�&�2�4�
4�	4�s)�0B!�%'B�B!�B�B!�B!�!:Cc���|r|jj}n|jj}tj|t
j|jj�|S)z%Return the proper reverter directory.)rrr<r
rrrr)rrzr2s   rr}zReverter._get_cp_dirpsP����[�[�4�4�F��[�[�0�0�F�����I�.�.����0N�0N�	P��
rc��|j�tjj|jj
�r'	|j
|jj
�yy#tj$rWtjd|jj
�tjd|jj
z��wxYw)aCRevert configuration to most recent finalized checkpoint.

        Remove all changes (temporary and permanent) that have not been
        finalized. This is useful to protect against crashes and other
        execution interruptions.

        :raises .errors.ReverterError: If unable to recover the configuration

        z=Incomplete or failed recovery for IN_PROGRESS checkpoint - %sN)r$r
rrrr<rr	r r!r"r#s r�recovery_routinezReverter.recovery_routine|s���	
�$�$�&�
�7�7�=�=����4�4�5�	
:��(�(����)D�)D�E�6���'�'�
:����!/�!�[�[�8�8�:��*�*��!�[�[�8�8�9�:�:�
:�s
�%A,�,A*C�	file_listc��tjj|�sy	t|d�5}|j	�j�}|D]M}tjj
|�rtj|��8tjd|��O	ddd�y#1swYyxYw#ttf$r;tjd|�tjdj|���wxYw)a Erase all files contained within file_list.

        :param str file_list: file containing list of file paths to be deleted

        :returns: Success
        :rtype: bool

        :raises certbot.errors.ReverterError: If
            all files within file_list cannot be removed

        FrhzVFile: %s - Could not be found to be deleted
 - Certbot probably shut down unexpectedlyNz.Unable to remove filepaths contained within %sz/Unable to remove filepaths contained within {0}T)r
rrWrNrXrY�lexists�remover!r-rLr`r"r	r rK)rr��list_fdrdrs     rraz Reverter._remove_contained_files�s����w�w�~�~�i�(��	)��i��%�
"��#�L�L�N�5�5�7�	�%�	"�D��w�w���t�,��	�	�$�����I� �"�
	"�
"�&�'
"�&����!�	)��O�O�@�)�
M��&�&���f�Y�'�)�
)�	)�s*�B5�A1B)� B5�)B2�.B5�2B5�5A
C?�titlec�
�tjj|jj�sytjj|jjd�}tjj|jjd�}tjj
|�s;tjd�t|d�5}|jd�ddd�	t|d�5}|jd|z�t|d�5}|j|j��ddd�ddd�tj||�|j-�y#1swY��xYw#1swY�DxYw#1swY�HxYw#ttf$rStj!d	�tj#d
t%j&��t)j*d��wxYw)aZFinalize the checkpoint.

        Timestamps and permanently saves all changes made through the use
        of :func:`~add_to_checkpoint` and :func:`~register_file_creation`

        :param str title: Title describing checkpoint

        :raises certbot.errors.ReverterError: when the
            checkpoint is not able to be finalized.

        NrBzCHANGES_SINCE.tmpz/Rollback checkpoint is empty (no changes made?)rVzNo changes
z	-- %s --
rhz,Unable to finalize checkpoint - adding titlezException was:
%szUnable to add title)r
rrrr<r/�existsr!�inforNrJrXrF�moverLr`r*rE�	traceback�
format_excr	r �_timestamp_progress_dir)rr��changes_since_path�changes_since_tmp_pathr��changes_tmp�changes_origs       r�finalize_checkpointzReverter.finalize_checkpoint�s����w�w�}�}�T�[�[�8�8�9���W�W�\�\�$�+�+�*E�*E��W��!#�����d�k�k�.I�.I�K^�!_���w�w�~�~�0�1��K�K�I�J��(�#�.�
(�!�����'�
(�	>��,�c�2�
;�k��!�!�,��"6�7��,�c�2�;�l��%�%�l�&7�&7�&9�:�;�
;�
�K�K�.�0B�C�	
�$�$�&�%
(�
(��;�;��
;�
;����!�	>��L�L�G�H��L�L�-�y�/C�/C�/E�F��&�&�'<�=�=�	>�sI�E<�8F �!F�% F�F�
F �<F�F	�
F�F�F � A"Hc�|�ttj��}tjtjj|jjd��}|D�cgc]!}tjj|���#}}|j|�|j�|d|k7r5tt|d�dz�}tjd||�|}|St|�dkDr:|d|k(r2tjd|�tt|d�dz�}|}|Scc}w)zBDetermine the timestamp of the checkpoint, enforcing monotonicity.z[0-9]*���r'zyCurrent timestamp %s does not correspond to newest reverter checkpoint; your clock probably jumped. Time travelling to %s���z6Race condition with timestamp %s, incrementing by 0.01g{�G�z�?)rI�time�globr
rr/rrrH�appendr,�floatr!r-r.rE)r�	timestamp�others�d�
timetravels     r�_checkpoint_timestampzReverter._checkpoint_timestamp�s����	�	��$�	����2�7�7�<�<����(>�(>��I�J��/5�6�!�"�'�'�"�"�1�%�6��6��
�
�i� ����
��"�:��"��U�6�"�:�.��2�3�J��N�N�P��:�
'�#�I���
��[�1�_����y�!8�
�L�L�Q�S\�]��U�6�"�:�.��5�6�J�"�I����7s�)&D9c���td�D]r}|j�}tjj	|j
j|�}	tj|j
j|�ytjd|j
j�tjd��#t$rtjd|�Y��wxYw)zTimestamp the checkpoint.�Nz(Unexpected race condition, retrying (%s)z'Unable to finalize checkpoint, %s -> %sz&Unable to finalize checkpoint renaming)�ranger�r
rr/rrr�replacer<r`r!r-r*r	r )rr@r��	final_dirs    rr�z Reverter._timestamp_progress_dir�s���
�q��	V�A��2�2�4�I������T�[�[�%;�%;�Y�G�I�
V��"�"�4�;�;�#>�#>�	�J��	V�	���5��K�K�'�'��	4��"�"�4�6�	6���
V����I�9�U�
V�s�*C�C#�"C#)rN)r') �__name__�
__module__�__qualname__�__doc__r�NamespaceConfigrr$r(r3rrIr9r=r7rrrrDrr^r;�boolr�rr�r}r�rar�r�r��rrrrs���(�R[�}�<�<�[��[�P�((�S�(��(�TE��S��E�s�E�t�E�
A�C��H�
A�#�
A�$�
A�+'�S�+'�c�#�h�+'�TW�+'�\`�+'�Z����v�t�C�y�7H�1I��"$;�#�$;�$�$;�L
M�3�
M�4�
M�c��C��c�T�c�8%��%�c�%�d�%�N4�t�4�h�s�m�4�PT�4�<
�T�
�c�
�:�8$��$��$�L''��''��''�R�s��*6rr)r�rjr��loggingrFr�r��typingrrrrr�certbotrr	r
�certbot._internalr�certbot.compatrr
�	getLoggerr�r!rr�rr�<module>r�sY��M�
���
��������!���'�%��	��	�	�8�	$��w6�w6r

Zerion Mini Shell 1.0