Current File : //usr/lib64/python3.6/site-packages/borg/testsuite/__pycache__/upgrader.cpython-36.pyc
3

y��b��@sddlZddlZddlZddlTddlmZddlmZmZddl	m
Z
ddlmZdd	l
mZejjejje�d
�Zdd�Zd
d�Zdd�Zdd�Zej�dd��Zejddgd�dd��Zdd�Zej�dd��Zdd�Zejje�d d!�d"d#��Z ejje�d d!�d$d%��Z!dS)&�N�)�*)�
KeyfileKey)�AtticRepositoryUpgrader�AtticKeyfileKey)�get_keys_dir)�
Repository�)�are_hardlinks_supportedzattic.tar.gzcsD�fdd�}tj|d��}|j|||�d�WdQRXtjj|��S)zo
    extract <tarfname> tar archive to <path>, all stuff starting with <what>.

    return path to <what>.
    c3s$x|D]}|jj��r|VqWdS)N)�name�
startswith)�membersZtarinfo)�what�� /usr/lib64/python3.6/upgrader.py�filess
zuntar.<locals>.files�r)r
N)�tarfile�openZ
extractall�os�path�join)ZtarfnamerrrZtfr)rr�untarsrc	Cs&tt|�ddd��
}|j�SQRXdS)z�
    utility function to check if borg can open a repository

    :param path: the path to the repository
    :returns: if borg can check the repository
    TF)Z	exclusive�createN)r�strZcheck)r�
repositoryrrr�
repo_valid$src	Cs>tjjt�tjj|��}t|d��}|j�jtj	�SQRXdS)z�
    check that the new keyfile is alright

    :param path: the path to the key file
    :returns: if the file starts with the borg magic string
    rN)
rrrr�basenamer�readrrZFILE_ID)r�keyfile�frrr�	key_valid0s
r!cCsttt|�d�S)z�
    create an attic repo with some stuff in it

    :param dir: path to the repository to be created
    :returns: path to attic repository
    �repo)r�	ATTIC_TARr)�dirrrr�make_attic_repo=sr%cCst|�S)N)r%)�tmpdirrrr�
attic_repoHsr'TF)ZparamscCs|jS)N)Zparam)Zrequestrrr�inplaceMsr(cCs�|}tjtj��t|�WdQRXt|dd�}|�dd�|jj�D�}WdQRX|j|d|d�|j	dd�t|�s|t
�dS)atest segment conversion

    this will load the given attic repository, list all the segments
    then convert them one at a time. we need to close the repo before
    conversion otherwise we have errors from borg

    :param attic_repo: a populated attic repository (fixture)
    NF)rcSsg|]\}}|�qSrr)�.0�i�filenamerrr�
<listcomp>`sz)test_convert_segments.<locals>.<listcomp>)�dryrunr()r-)�pytest�raisesr�AtticRepositoryrr�ioZsegment_iteratorZconvert_segmentsZ
convert_cache�AssertionError)r'r(�	repo_pathrZsegmentsrrr�test_convert_segmentsRs	r4cCsBttt|�d�}|jd|�|jd|�|jdd�tjj|d�S)z�
    create an attic key file from the given repo, in the keys
    subdirectory of the given tmpdir

    :param tmpdir: a temporary directory (a builtin fixture)
    :returns: path to key file
    �keysZATTIC_KEYS_DIRZ
BORG_KEYS_DIRZATTIC_PASSPHRASEZtestr")rr#rZsetenvrrr)r&ZmonkeypatchZkeys_dirrrr�attic_key_filefs
	r6cCsR|}t|�st�t|dd��}tj|�}tj|dd�WdQRXt|�sNt�dS)autest key conversion

    test that we can convert the given key to a properly formatted
    borg key. assumes that the ATTIC_KEYS_DIR and BORG_KEYS_DIR have
    been properly populated by the attic_key_file fixture.

    :param attic_repo: path to an attic repository (fixture defined above)
    :param attic_key_file: path to an attic key file (fixture defined above)
    F)r)r-N)r!r2rrZ
find_key_fileZconvert_keyfiles)r'r6Zkeyfile_pathrrrrr�	test_keys~s

r7zhardlinks not supported)�reasoncs�|}tjtj��t|�WdQRXdd���fdd�}||�}t|dd���}tjt�|j	d|d�}|r�|dksvt
�||j�|ks�t
�n>|s�t
�||j�||�ks�t
�d	tjkrȈ|�j
t@d
ks�t
�WdQRXt|�s�t
�t|�s�t
�dS)a[test all conversion steps

    this runs everything. mostly redundant test, since everything is
    done above. yet we expect a NotImplementedError because we do not
    convert caches yet.

    :param attic_repo: path to an attic repository (fixture defined above)
    :param attic_key_file: path to an attic key file (fixture defined above)
    NcSstjtjj|ddd��S)N�data�0)r�statrr)rrrr�stat_segment�sz&test_convert_all.<locals>.stat_segmentcs
�|�jS)N)�st_ino)r)r<rr�first_inode�sz%test_convert_all.<locals>.first_inodeF)r)r-r(ZBORG_TESTS_IGNORE_MODESr)r.r/rr0rrr�umaskZ
UMASK_DEFAULTZupgrader2r�environ�st_moder!)r'r6r(r3r>Z
orig_inoderZbackupr)r<r�test_convert_all�s$

 rBcCs�t|jd��}t|d��}|jd�WdQRXt|jd��}tj||�tj|dd|d�|s|tj|�j	tj|�j	ks�t
�ntj|�j	tj|�j	ks�t
�t|d	��}|j�d
ks�t
�WdQRXdS)abtest that we handle hard links properly

    that is, if we are in "inplace" mode, hardlinks should *not*
    change (ie. we write to the file directly, so we do not rewrite the
    whole file, and we do not re-create the file).

    if we are *not* in inplace mode, then the inode should change, as
    we are supposed to leave the original inode alone.�a�wbsaXXXN�b�a�b)r(�rbsbXXX)rrr�writer�linkrZheader_replacer;r=r2r)r&r(rCZtmprErrr�
test_hardlink�s
rK)"rrr.Z	constantsZ
crypto.keyrZupgraderrrZhelpersrrr�r
rr�dirname�__file__r#rrr!r%Zfixturer'r(r4r6r7ZmarkZskipifrBrKrrrr�<module>s(
*