ok

Mini Shell

Direktori : /lib/python3.6/site-packages/certbot/__pycache__/
Upload File :
Current File : //lib/python3.6/site-packages/certbot/__pycache__/reverter.cpython-36.pyc

3

گazV�@s�dZddlZddlZddlZddlZddlZddlZddlmZddlm	Z	ddlm
Z
ddlmZddlmZddl
mZdd	l
mZdd
l
mZddlmZddlmZdd
lmZeje�ZGdd�d�ZdS)zGReverter class saves configuration checkpoints and allows for recovery.�N)�Iterable)�List)�Set)�TextIO)�Tuple)�
configuration)�errors)�util)�	constants)�
filesystem)�osc@sXeZdZdZejdd�dd�Zdd�dd�Zd5edd
�dd�Z	e
eedd
�dd�Ze
eedd
�dd�Z
ee
eedd�dd�Zeeeeefd�dd�Zedd�dd�Zedd�dd�Ze
edd�dd�Zeedd �d!d"�Zeeedd#�d$d%�Zeed&�d'd(�Zdd�d)d*�Zeed+�d,d-�Zedd.�d/d0�Zed�d1d2�Zdd�d3d4�ZdS)6�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`

    N)�config�returncCs ||_tj|jtj|jj�dS)N)rr	�make_or_verify_dir�
backup_dirr
�CONFIG_DIRS_MODE�strict_permissions)�selfr�r�/usr/lib/python3.6/reverter.py�__init__BszReverter.__init__)rcCsXtjj|jj�rTy|j|jj�Wn0tjk
rRtj	d|jj�tjd��YnXdS)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)rrrr�revert_temporary_configHs	
z Reverter.revert_temporary_config�)�rollbackrcCsyt|�}Wn(tk
r4tjd�tjd��YnX|dkrRtjd�tjd��tj|jj	�}|j
�|sxtjd�nt|�|kr�tjd|t|��xj|dko�|�rtj
j|jj	|j��}y|j|�Wn*tjk
r�tjd�tjd��YnX|d8}q�Wd	S)
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 rollbackr N)�int�
ValueErrorr�errorrrr�listdirrr�sort�warning�lenr�join�poprr)rr!Zbackups�cp_dirrrr�rollback_checkpoints\s2



zReverter.rollback_checkpoints)�
save_files�
save_notesrcCs|j|jj||�dS)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)rr-r.rrr�add_to_temp_checkpoint�szReverter.add_to_temp_checkpointcCs |j|�|j|jj||�dS)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_savesr/r�in_progress_dir)rr-r.rrr�add_to_checkpoint�s
zReverter.add_to_checkpoint)r+r-r.rc	Cstj|tj|jj�|jtjj	|d��\}}t
|�}x�|D]�}||kr:tjd|�y>t
j|tjj	|tjj|�dt|���|jdj|��Wn<tk
r�|j�tjd||�tjdj||���YnX|d7}q:W|j�ttjj	|d�d	��}|j|�Wd
QRXd
S)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�_z{0}
z&Unable to add file %s to checkpoint %sz(Unable to add file {0} to checkpoint {1}r �
CHANGES_SINCE�aN)r	rr
rrr�_read_and_appendrrr)r(r�debug�shutil�copy2�basename�str�write�format�IOError�closer$rr�open)	rr+r-r.�op_fdZexisting_filepaths�idx�filenameZnotes_fdrrrr/�s0
zReverter._add_to_checkpoint_dir)�filepathrcCs:tjj|�r$t|d�}|j�j�}ng}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�isfilerB�read�
splitlines)rrFrC�linesrrrr8�s

zReverter._read_and_append)r+rcCs:tjjtjj|d��r*|jtjj|d��tjjtjj|d��r�ynttjj|d���R}|j�j�}x>t|�D]2\}}t	j
tjj|tjj|�dt|��|�qlWWdQRXWn2t
tfk
r�tjd|�tjd|��YnX|jtjj|d��yt	j|�Wn0tk
�r4tjd|�tjd|��YnXdS)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

        �COMMANDSr4r5NzUnable to recover files from %s�	NEW_FILESzUnable to remove directory: %s)rrrHr)�_run_undo_commandsrBrIrJ�	enumerater:r;r<r=r@�OSErrorrr$rr�_remove_contained_filesZrmtree)rr+Zpaths_fd�	filepathsrDrrrrr�s*
0zReverter._recover_checkpointcCs~ddi}t|df|��^}tj|�}xLtt|��D]<}ytj|�Wq0tjk
rjt	j
ddj|��Yq0Xq0WWdQRXdS)zRun all commands in a file.�newline��rzUnable to run undo command: %s� N)rB�csv�reader�reversed�listr	Z
run_scriptrZSubprocessErrorrr$r))rrF�kwargsZcsvfileZ	csvreader�commandrrrrNs
zReverter._run_undo_commands)r-rcCs�g}tjj|jjd�}tjj|�rJt|d��}|j|j�j	��WdQRXtjj|jjd�}tjj|�r�t|d��}|j|j�j	��WdQRXx"|D]}||kr�t
jd|��q�WdS)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.

        r4rUNrMz+Attempting to overwrite challenge file - %s)rrr)rrrHrB�extendrIrJrr)rr-Zprotected_filesZ	temp_pathZprotected_fd�new_pathrErrrr1s	
zReverter._check_tempfile_saves)�	temporary�filesrcGs�|stjd��|j|�}d}z|yB|jtjj|d��\}}x$|D]}||kr>|jdj|��q>WWn4t	t
fk
r�tjd|�tjdj|���YnXWd|dk	r�|j
�XdS)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 callNrMz{0}
z(Unable to register file creation(s) - %sz)Unable to register file creation(s) - {0})rr�_get_cp_dirr8rrr)r>r?r@rPrr$rA)rr_r`r+Znew_fdZex_filesrrrr�register_file_creation.s


zReverter.register_file_creation)r_r\rcCs�tjj|j|�d�}ddi}yFtjj|�r.dnd}t||f|��}tj|�}|j|�WdQRXWn,t	t
fk
r�tjd�t
jd��YnXdS)	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

        rLrSrTr7rGNzUnable to register undo commandz Unable to register undo command.)rrr)rarHrBrW�writerZwriterowr@rPrr$rr)rr_r\Zcommands_fpr[�mode�fZ	csvwriterrrr�register_undo_commandUs

zReverter.register_undo_command)r_rcCs.|r|jj}n|jj}tj|tj|jj�|S)z%Return the proper reverter directory.)rrr2r	rr
rr)rr_r+rrrrass
zReverter._get_cp_dircCsh|j�tjj|jj�rdy|j|jj�Wn8tjk
rbt	j
d|jj�tjd|jj��YnXdS)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)rrrrrr2rrrrr)rrrr�recovery_routines
zReverter.recovery_routine)�	file_listrcCs�tjj|�sdSyXt|d��D}|j�j�}x0|D](}tjj|�rLtj|�q0tj	d|�q0WWdQRXWn4t
tfk
r�tjd|�t
jdj|���YnXdS)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

        FrUzVFile: %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rrHrBrIrJ�lexists�removerr'r@rPrrrr?)rrhZlist_fdrRrrrrrQ�s$
z Reverter._remove_contained_files)�titlercCstjj|jj�sdStjj|jjd�}tjj|jjd�}tjj|�sntjd�t	|d��}|j
d�WdQRXyXt	|d��8}|j
d|�t	|d��}|j
|j��WdQRXWdQRXtj
||�Wn>ttfk
�rtjd	�tjd
tj��tjd��YnX|j�dS)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.

        Nr6zCHANGES_SINCE.tmpz/Rollback checkpoint is empty (no changes made?)rGzNo changes
z	-- %s --
rUz,Unable to finalize checkpoint - adding titlezException was:
%szUnable to add title)rrrrr2r)�existsr�inforBr>rIr:Zmover@rPr$r9�	traceback�
format_excrr�_timestamp_progress_dir)rrkZchanges_since_pathZchanges_since_tmp_pathreZchanges_tmpZchanges_origrrr�finalize_checkpoint�s&

"
zReverter.finalize_checkpointcCs�ttj��}tjtjj|jjd��}dd�|D�}|j|�|j	�|d	|krxtt
|d
�d�}tjd||�|}n<t
|�dkr�|d|kr�tjd|�tt
|d�d�}|}|S)
zBDetermine the timestamp of the checkpoint, enforcing monotonicity.z[0-9]*cSsg|]}tjj|��qSr)rrr<)�.0�drrr�
<listcomp>�sz2Reverter._checkpoint_timestamp.<locals>.<listcomp>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�?���rv���rv)r=�time�globrrr)rr�appendr&�floatrr'r(r9)r�	timestampZothersZ
timetravelrrr�_checkpoint_timestamp�s
zReverter._checkpoint_timestampcCs�x`td�D]T}|j�}tjj|jj|�}ytj|jj	|�dSt
k
r\tjd|�Yq
Xq
Wtj
d|jj	|�tjd��dS)zTimestamp the checkpoint.ruNz(Unexpected race condition, retrying (%s)z'Unable to finalize checkpoint, %s -> %sz&Unable to finalize checkpoint renaming)�ranger}rrr)rrr�replacer2rPrr'r$rr)rr5r|Z	final_dirrrrrp�sz Reverter._timestamp_progress_dir)r ) �__name__�
__module__�__qualname__�__doc__rZNamespaceConfigrrr"r,rr=r0r3r/rrrr8rrNr1�boolrbrrfrargrQrqr}rprrrrr
s&)*
-''&)r
)r�rWryZloggingr:rxrnZtypingrrrrrZcertbotrrr	Zcertbot._internalr
Zcertbot.compatrrZ	getLoggerr�rr
rrrr�<module>s&


Zerion Mini Shell 1.0