o
    Z;àf»&  ã                   @   sÌ   d dl mZmZ d dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
 d dlmZ G dd„ deƒZG dd„ deƒZG d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )é    )Úprint_functionÚunicode_literalsN)Úfind_executable)Úgettextc                   @   ó   e Zd ZdS )ÚAptBtrfsSnapshotErrorN©Ú__name__Ú
__module__Ú__qualname__© r   r   ú@/usr/lib/python3/dist-packages/DistUpgrade/apt_btrfs_snapshot.pyr   !   ó    r   c                   @   r   )ÚAptBtrfsNotSupportedErrorNr   r   r   r   r   r   %   r   r   c                   @   r   )ÚAptBtrfsRootWithNoatimeErrorNr   r   r   r   r   r   )   r   r   c                   @   s.   e Zd ZdZedd„ ƒZd
dd„Zdd„ Zd	S )Ú
FstabEntryz a single fstab entry line c                 C   s"   |  d¡d  ¡ }t|dd… Ž S )Nú#r   é   )Ú	partitionÚsplitr   )ÚclsÚlineÚargsr   r   r   Ú	from_line/   s   zFstabEntry.from_liner   c                 C   s(   || _ || _|| _|| _|| _|| _d S ©N©Úfs_specÚ
mountpointÚfstypeÚoptionsÚdumpÚpassno)Úselfr   r   r   r   r    r!   r   r   r   Ú__init__7   s   
zFstabEntry.__init__c                 C   s    d| j | j| j| j| j| jf S )Nz*<FstabEntry '%s' '%s' '%s' '%s' '%s' '%s'>r   ©r"   r   r   r   Ú__repr__@   s   þzFstabEntry.__repr__N)r   r   )r	   r
   r   Ú__doc__Úclassmethodr   r#   r%   r   r   r   r   r   -   s    

	r   c                       s"   e Zd ZdZd‡ fdd„	Z‡  ZS )ÚFstabz a list of FstabEntry items ú
/etc/fstabc              
      s�   t t| ƒ ¡  t|ƒ�3}dd„ |D ƒD ]"}|dks| d¡rqzt |¡}W n	 ty/   Y qw |  |¡ qW d   ƒ d S 1 sAw   Y  d S )Nc                 s   s   � | ]}|  ¡ V  qd S r   )Ústrip)Ú.0r   r   r   r   Ú	<genexpr>L   s   € z!Fstab.__init__.<locals>.<genexpr>Ú r   )	Úsuperr(   r#   ÚopenÚ
startswithr   r   Ú
ValueErrorÚappend)r"   ÚfstabÚ
fstab_fileÚstripped_lineÚentry©Ú	__class__r   r   r#   H   s   
ÿù"ÿzFstab.__init__©r)   )r	   r
   r   r&   r#   Ú__classcell__r   r   r7   r   r(   F   s    r(   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚLowLevelCommandszi lowlevel commands invoked to perform various tasks like
        interact with mount and btrfs tools
    c                 C   s   t  d||g¡}|dkS )NÚmountr   ©Ú
subprocessÚcall)r"   r   r   Úretr   r   r   r<   Z   s   zLowLevelCommands.mountc                 C   s   t  d|g¡}|dkS )NÚumountr   r=   )r"   r   r@   r   r   r   rA   ^   s   zLowLevelCommands.umountc                 C   s   t  ddd||g¡}|dkS )NÚbtrfsÚ	subvolumeÚsnapshotr   r=   )r"   ÚsourceÚdestr@   r   r   r   Úbtrfs_subvolume_snapshotb   s   
ÿz)LowLevelCommands.btrfs_subvolume_snapshotc                 C   s   t  ddd|g¡}|dkS )NrB   rC   Údeleter   r=   )r"   rD   r@   r   r   r   Úbtrfs_delete_snapshotg   s   z&LowLevelCommands.btrfs_delete_snapshotN)r	   r
   r   r&   r<   rA   rG   rI   r   r   r   r   r;   V   s    r;   c                   @   s¦   e Zd ZdZdZed Zd)dd„Zdd„ Zd	d
„ Zd)dd„Z	dd„ Z
dd„ Zdd„ Zd*dd„Zd+dd„Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd,d$d%„Zd&d'„ Zd(S )-ÚAptBtrfsSnapshotz? the high level object that interacts with the snapshot system z@apt-snapshot-z	old-root-r)   c                 C   s   t |ƒ| _tƒ | _d | _d S r   )r(   r3   r;   ÚcommandsÚ_btrfs_root_mountpoint)r"   r3   r   r   r   r#   t   s   

zAptBtrfsSnapshot.__init__c                 C   s   t dƒsdS |  ¡ }|duS )zx verify that the system supports apt btrfs snapshots
            by checking if the right fs layout is used etc
        rB   FN)r   Ú%_get_supported_btrfs_root_fstab_entry©r"   r6   r   r   r   Úsnapshots_supportedy   s   z$AptBtrfsSnapshot.snapshots_supportedc                 C   s6   | j D ]}|jdkr|jdkrd|jv r|  S qdS )z4 return the supported btrfs root FstabEntry or None ú/rB   zsubvol=@N)r3   r   r   r   rN   r   r   r   rM   ƒ   s   



€z6AptBtrfsSnapshot._get_supported_btrfs_root_fstab_entryc                 C   s$   | j D ]}|j|kr|j  S qdS )z4 return the device or UUID for the given mountpoint N)r3   r   r   )r"   r   r3   r6   r   r   r   Ú_uuid_for_mountpoint�   s
   


ÿz%AptBtrfsSnapshot._uuid_for_mountpointc                 C   s4   |   d¡}tjdd�}| j ||¡sd S || _| jS )NrP   zapt-btrfs-snapshot-mp-)Úprefix)rQ   ÚtempfileÚmkdtemprK   r<   rL   )r"   Úuuidr   r   r   r   Úmount_btrfs_root_volume”   s   
z(AptBtrfsSnapshot.mount_btrfs_root_volumec                 C   s$   | j  | j¡}t | j¡ d | _|S r   )rK   rA   rL   ÚosÚrmdir)r"   Úresr   r   r   Úumount_btrfs_root_volumeœ   s   z)AptBtrfsSnapshot.umount_btrfs_root_volumec                 C   s   t j  ¡ jdd� tdƒ¡S )Nr   )ÚmicrosecondÚ_)ÚdatetimeÚnowÚreplaceÚ	isoformatÚstrr$   r   r   r   Ú_get_now_str¢   s   ÿzAptBtrfsSnapshot._get_now_strr-   c                 C   sr   |   ¡ }|  ¡ }tj |d¡}tj || j| | ¡}tj |¡r,ttdƒ|f ƒ dS | j	 
||¡}|  ¡  |S )Nú@zDINFO: snapshot directory '%s' already exists, not creating duplicateT)rV   rb   rW   ÚpathÚjoinÚSNAP_PREFIXÚexistsÚprintr\   rK   rG   rZ   )r"   Úadditional_prefixÚmpÚsnap_idrE   ÚtargetrY   r   r   r   Úcreate_btrfs_root_snapshot¦   s   ÿÿz+AptBtrfsSnapshot.create_btrfs_root_snapshotr   c              	   C   sš   g }|dkr|   ¡ }|stƒ ‚d|jv rtƒ ‚|dkrt ¡ }|  ¡ }t |¡D ]}| | j	¡rFtj
 tj
 ||dd¡¡}||k rF| |¡ q(|  ¡  |S )zµ get the list of available snapshot
            If "older_then" is given (in unixtime format) it will only include
            snapshots that are older then the given date)
        r   ÚnoatimeÚetcr3   )rM   r   r   r   ÚtimerV   rW   Úlistdirr0   rf   rd   Úgetatimere   r2   rZ   )r"   Ú
older_thanÚresultr6   rj   ÚeÚatimer   r   r   Úget_btrfs_root_snapshots_list¶   s$   

€z.AptBtrfsSnapshot.get_btrfs_root_snapshots_listc                 C   s   t dƒ t d |  ¡ ¡ƒ dS )NzAvailable snapshots:ú  
T)rh   re   rw   r$   r   r   r   Úprint_btrfs_root_snapshotsÒ   s   z+AptBtrfsSnapshot.print_btrfs_root_snapshotsc                 C   s>   t   ¡ }| d¡stdƒ‚t|d d… ƒ}||d d d  S )NÚdz&Please specify time in days (e.g. 10d)éÿÿÿÿé   é<   )rp   ÚendswithÚ	ExceptionÚint)r"   Útimefmtr^   Údaysr   r   r   Ú_parse_older_than_to_unixtime×   s
   
z.AptBtrfsSnapshot._parse_older_than_to_unixtimec                 C   sT   |   |¡}ztd| ƒ td | j|d�¡ƒ W dS  ty)   tj d¡ Y dS w )Nz$Available snapshots older than '%s':rx   ©rs   ú6Error: fstab option 'noatime' incompatible with optionFT)rƒ   rh   re   rw   r   ÚsysÚstderrÚwrite)r"   r�   Úolder_than_unixtimer   r   r   Ú%print_btrfs_root_snapshots_older_thanÞ   s   

ÿüýz6AptBtrfsSnapshot.print_btrfs_root_snapshots_older_thanc                 C   sV   d}|   |¡}z| j|d�D ]	}||  |¡M }qW |S  ty*   tj d¡ Y dS w )NTr„   r…   F)rƒ   rw   Údelete_snapshotr   r†   r‡   rˆ   )r"   r�   rY   r‰   Úsnapr   r   r   Ú%clean_btrfs_root_snapshots_older_thanê   s   

ÿþüýz6AptBtrfsSnapshot.clean_btrfs_root_snapshots_older_thanc                 C   s   |   |¡}|S r   )Úset_default)r"   Úsnapshot_namerY   r   r   r   Úcommand_set_default÷   s   
z$AptBtrfsSnapshot.command_set_defaultTc                 C   s”   |   ¡ }tj ||¡}tj |¡r@| d¡r@|dkr@tj |d¡}tj || j|  ¡  ¡}t ||¡ t ||¡ t	d| ƒ nt	dƒ |  
¡  dS )z set new default rc   z@Default changed to %s, please reboot for changes to take effect.z_You have selected an invalid snapshot. Please make sure that it exists, and that it is not "@".T)rV   rW   rd   re   Úisdirr0   ÚBACKUP_PREFIXrb   Úrenamerh   rZ   )r"   r�   Úbackuprj   Únew_rootÚdefault_rootr   r   r   rŽ   û   s"   
ÿþÿzAptBtrfsSnapshot.set_defaultc                 C   s*   |   ¡ }| j tj ||¡¡}|  ¡  |S r   )rV   rK   rI   rW   rd   re   rZ   )r"   r�   rj   rY   r   r   r   r‹     s   ÿz AptBtrfsSnapshot.delete_snapshotNr9   )r-   )r   )T)r	   r
   r   r&   rf   r’   r#   rO   rM   rQ   rV   rZ   rb   rm   rw   ry   rƒ   rŠ   r�   r�   rŽ   r‹   r   r   r   r   rJ   l   s(    






rJ   )Ú
__future__r   r   r]   rW   r>   r†   rp   rS   Údistutils.spawnr   r   r\   r   r   r   r   Úobjectr   Úlistr(   r;   rJ   r   r   r   r   Ú<module>   s    