3
.(]#                 @   s   d dl m Z d dlZd dlZd dlmZmZmZ y,d dlZ	d dl
Z
ejdd d dlmZ W n eefk
rt   Y nX d dlmZmZmZmZ d dlmZ d	d
 ZG dd dZG dd dejZdS )    )gettextN)GioGLibGObjectSnapd1)r   )has_gnome_online_accountsis_current_distro_ltsis_current_distro_supportedretry)LivepatchSnapc             C   sH   xB| j  D ]6\}}ytjj|| |< W q
 ttfk
r>   Y q
X q
W | S )N)itemsdateutilparserparse
ValueError	TypeError)Z	json_dictkeyvalue r   E/usr/lib/python3/dist-packages/softwareproperties/LivepatchService.pydatetime_parser/   s    
r   c               @   s   e Zd ZdZdZdZdZdS )LivepatchAvailabilityr            N)__name__
__module____qualname__FALSETRUENO_CONNECTIVITYCHECKINGr   r   r   r   r   7   s   r   c               @   s   e Zd ZdZdZdZdZedZedZ	e
ddejejejejfedddejfedddejfd	Zd
d Zdd Zdd Zdd Zdd Zdd Zeedd Zdd Zeedd Zdd Zdd ZdS ) LivepatchServicezRhttp+unix://%2Fvar%2Fsnap%2Fcanonical-livepatch%2Fcurrent%2Flivepatchd.sock/statuszWhttp+unix://%2Fvar%2Fsnap%2Fcanonical-livepatch%2Fcurrent%2Flivepatchd-priv.sock/enablezXhttp+unix://%2Fvar%2Fsnap%2Fcanonical-livepatch%2Fcurrent%2Flivepatchd-priv.sock/disablez2/var/snap/canonical-livepatch/common/machine-tokenzFailed to enable Livepatch: {}zFailed to disable Livepatch: {}NF)availabilityzavailability-messageenabledc             C   s   t j j|  d| _t | _tj | _tj	| _
d | _tjj| jd}|j | _tjj | _| jjd| j |jtjj| _| jjd| j d S )Nr   )pathznotify::connectivityZchanged)r   __init___timeout_idr   _snaprequests_unixsocketZSession_sessionr   r   _availability_availability_messager   ZFileZnew_for_pathLIVEPATCH_RUNNING_FILEquery_exists_enabledZNetworkMonitorZget_default_nmZconnect_network_changed_cbZmonitor_fileZFileMonitorFlagsZNONEZ_lp_monitor_livepatch_enabled_changed_cb)selfZlp_filer   r   r   r'   W   s    

zLivepatchService.__init__c             C   s8   |j dkr| jS |j dkr | jS |j dkr0| jS td S )Nr$   zavailability-messager%   )namer,   r-   r0   AssertionError)r4   Zpspecr   r   r   do_get_propertym   s    


z LivepatchService.do_get_propertyc                sJ    fdd}t j _d _ jd  jd  jdkrFtjd| _dS )a  Trigger a Livepatch availability check to be executed after a short
        timeout. Multiple triggers will result in a single request.

        A notify::availability will be emitted when the check starts, and
        another one when the check ends.
        c                 s    fddt dftt dftt dftt dfg}  jjjtjj	krVt
j _d  _n6x4| D ]\}}| s\t
j _| _P q\W t
j _d  _ jd  jd d	 _d
S )Nc                  s    j j tjjkS )N)r)   
get_statusr   
SnapStatusZUNKNOWNr   )r4   r   r   <lambda>   s    z[LivepatchService.trigger_availability_check.<locals>._update_availability.<locals>.<lambda>z*Canonical Livepatch snap is not available.z6Gnome Online Accounts is required to enable Livepatch.z,Livepatch is not available for this release.z+The current release is no longer supported.r$   zavailability-messager   F)_r   r	   r
   r1   ZpropsZconnectivityr   ZNetworkConnectivityZFULLr   r!   r,   r-   r   r    notifyr(   )Zrulesfuncmessage)r4   r   r   _update_availability   s,    


zILivepatchService.trigger_availability_check.<locals>._update_availabilityNr$   zavailability-messager   r   )r   r"   r,   r-   r<   r(   r   Ztimeout_add_seconds)r4   r?   r   )r4   r   trigger_availability_checkx   s    !


z+LivepatchService.trigger_availability_checkc             C   s^   | j |krdS |s| j S | jj tjjkr6| j|S | jj \}}|rR| j|S d|fS dS )a  Enable or disable Canonical Livepatch in the current system. This
        function will return once the operation succeeded or failed.

        Args:
            enabled(bool): wheater to enable or disable the service.
            token(str): the authentication token to be used to enable Canonical
                Livepatch service.

        Returns:
            (False, '') if successful, (True, error_message) otherwise.
        F TN)FrA   )	r0   _disable_servicer)   r8   r   r9   ZACTIVE_enable_serviceZenable_or_install)r4   r%   tokenZsuccessmsgr   r   r   set_enabled   s    

zLivepatchService.set_enabledc             C   sb   y(ddd}| j j| j|d}|jtdS  tk
r\ } ztjdjt	| dS d}~X nX dS )zSynchronously retrieve the status of Canonical Livepatch.

        Returns:
            str: The status. A valid string for success, None otherwise.
        r   json)	verbosityformat)params)Zobject_hookz"Failed to get Livepatch status: {}N)
r+   getSTATUS_ENDPOINTrG   r   	ExceptionloggingdebugrI   str)r4   rJ   rer   r   r   r8      s    
zLivepatchService.get_statusc             C   s@   y
| j |S  tk
r: } zd| jjt|fS d}~X nX dS )a`  Enable Canonical Livepatch in the current system. This function will
        return once the operation succeeded or failed.

        Args:
            token(str): the authentication token to be used to enable Canonical
                Livepatch service.

        Returns:
            (False, '') if successful, (True, error_message) otherwise.
        TN)_enable_service_with_retryrM   ENABLE_ERROR_MSGrI   rP   )r4   rD   rR   r   r   r   rC      s    
z LivepatchService._enable_servicec             C   s:   d|i}| j j| j|d}|j |jr*dn| jj|jfS )Nz
auth-token)rJ   rA   )r+   putENABLE_ENDPOINTokrT   rI   text)r4   rD   rJ   rQ   r   r   r   rS      s    z+LivepatchService._enable_service_with_retryc             C   s>   y| j  S  tk
r8 } zd| jjt|fS d}~X nX dS )zDisable Canonical Livepatch in the current system. This function will
        return once the operation succeeded or failed.

        Returns:
            (False, '') if successful, (True, error_message) otherwise.
        TN)_disable_service_with_retryrM   DISABLE_ERROR_MSGrI   rP   )r4   rR   r   r   r   rB      s    z!LivepatchService._disable_servicec             C   s.   | j j| j}|j |jrdn| jj|jfS )NrA   )r+   rU   DISABLE_ENDPOINTrW   rZ   rI   rX   )r4   rQ   r   r   r   rY      s    z,LivepatchService._disable_service_with_retryc             C   s   | j   d S )N)r@   )r4   ZmonitorZnetwork_availabler   r   r   r2      s    z$LivepatchService._network_changed_cbc             C   s&   |j  }| j|kr"|| _| jd d S )Nr%   )r/   r0   r<   )r4   ZfmfileZ
other_fileZ
event_typer%   r   r   r   r3      s    
z.LivepatchService._livepatch_enabled_changed_cb) r   r   r   rL   rV   r[   r.   r;   rT   rZ   intr   r   r"   r   ZPARAM_READABLErP   boolZ__gproperties__r'   r7   r@   rF   r8   rC   r   rM   rS   rB   rY   r2   r3   r   r   r   r   r#   >   s0   0r#   )r   r;   rN   ZgiZgi.repositoryr   r   r   Zdateutil.parserr   r*   Zrequire_versionr   ImportErrorr   Zsoftwareproperties.gtk.utilsr   r	   r
   r   Z softwareproperties.LivepatchSnapr   r   r   r#   r   r   r   r   <module>   s   