ok
Direktori : /lib/python3.6/site-packages/certbot/__pycache__/ |
Current File : //lib/python3.6/site-packages/certbot/__pycache__/reverter.cpython-36.pyc |
3 گazV � @ s� d Z ddlZddlZddlZddlZddlZddlZddlmZ ddlm Z ddlm Z ddlmZ ddlmZ ddl mZ dd l mZ dd l mZ ddlmZ ddlmZ dd lmZ eje�ZG dd� d�ZdS )zGReverter class saves configuration checkpoints and allows for recovery.� N)�Iterable)�List)�Set)�TextIO)�Tuple)� configuration)�errors)�util)� constants)� filesystem)�osc @ sX e Zd ZdZejdd�dd�Zdd�dd�Zd5edd �dd�Z e e edd �dd�Ze e edd �dd�Z ee e edd�dd�Zeeeee f d�dd�Zedd�dd�Zedd�dd�Ze e dd�dd�Zeedd �d!d"�Zeee dd#�d$d%�Zeed&�d'd(�Zdd�d)d*�Zeed+�d,d-�Zedd.�d/d0�Zed�d1d2�Zdd�d3d4�ZdS )6�Reverteraq Reverter 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�returnc C s || _ tj|jtj| j j� d S )N)r r �make_or_verify_dir� backup_dirr �CONFIG_DIRS_MODE�strict_permissions)�selfr � r �/usr/lib/python3.6/reverter.py�__init__B s zReverter.__init__)r c C sX t jj| jj�rTy| j| jj� W n0 tjk rR tj d| jj� tjd��Y nX dS )a Reload 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)r r r r �revert_temporary_configH s z Reverter.revert_temporary_config� )�rollbackr c C s yt |�}W n( tk r4 tjd� tjd��Y nX |dk rRtjd� tjd��tj| jj �}|j � |sxtjd� nt|�|k r�tjd|t|�� xj|dko�|�r tj j| jj |j� �}y| j|� W n* tjk r� tjd� tjd��Y nX |d8 }q�W d S ) ay Revert '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 Inputr zHCertbot 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 �errorr r r �listdirr r �sort�warning�lenr �join�popr r )r r! Zbackups�cp_dirr r r �rollback_checkpoints\ s2 zReverter.rollback_checkpoints)� save_files� save_notesr c C s | 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_dirr r )r r- r. r r r �add_to_temp_checkpoint� s zReverter.add_to_temp_checkpointc C s | 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)r r- r. r r r �add_to_checkpoint� s zReverter.add_to_checkpoint)r+ r- r. r c C s t j|tj| jj� | jtjj |d��\}}t |�}x�|D ]�}||kr:tjd|� y>t j|tjj |tjj|�d t|� �� |jdj|�� W n< tk r� |j� tjd||� tjdj||���Y nX |d7 }q:W |j� ttjj |d�d ��}|j|� W d Q R X d S )ag Add 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 r r r r r �_read_and_appendr r r) r( r �debug�shutil�copy2�basename�str�write�format�IOError�closer$ r r �open) r r+ r- r. �op_fdZexisting_filepaths�idx�filenameZnotes_fdr r r r/ � s0 zReverter._add_to_checkpoint_dir)�filepathr c C s: t jj|�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)r rF rC �linesr r r r8 � s zReverter._read_and_append)r+ r c C s: t jjt jj|d��r*| jt jj|d�� t jjt jj|d��r�yntt jj|d���R}|j� j� }x>t|�D ]2\}}t j t jj|t jj|�d t|� �|� qlW W dQ R X W n2 t tfk r� tjd|� tjd| ��Y nX | jt jj|d�� yt j|� W n0 tk �r4 tjd|� tjd| ��Y nX dS )a Recover 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 �COMMANDSr4 r5 NzUnable to recover files from %s� NEW_FILESzUnable to remove directory: %s)r r rH r) �_run_undo_commandsrB rI rJ � enumerater: r; r<