o
    ÙKÕi  ã                	   @   sœ  d dl Z d dlZd dlmZmZmZ d dlmZmZm	Z	m
Z
mZmZmZmZ dZg d¢ZdZdZdZd	Ze	 ¡ Ze  e e¡¡Zed
defdefdefdefdefgƒZdefdd„Zdefdd„Z			d0dee dee deee   ddfdd„Z!	d1dedeee   ddfdd„Z"dedee fdd„Z#dee fd d!„Z$d"d#„ Z%d$ej&fd%d&„Z'		'd2d(edee d)efd*d+„Z(d(efd,d-„Z)d(edefd.d/„Z*dS )3é    N)ÚListÚ
NamedTupleÚOptional)ÚapiÚaptÚevent_loggerÚ
exceptionsÚhttpÚmessagesÚsystemÚutilz/usr/bin/snap)g      à?g      ð?g      @z
proxy.httpzproxy.httpsz/run/snapd.socketz/v2/snaps/{}ÚSnapPackageÚnameÚversionÚrevisionÚchannelÚ	publisherÚreturnc                   C   s   dt  ¡ v S )z(Returns whether or not snap is installedÚsnapd)r   Úget_installed_packages_names© r   r   ú//usr/lib/python3/dist-packages/uaclient/snap.pyÚis_snapd_installed'   s   r   c                   C   s   t dd„ tƒ D ƒƒS )z3Returns whether or not snapd is installed as a snapc                 s   s   � | ]}|j d kV  qdS )r   N©r   )Ú.0Úsnapr   r   r   Ú	<genexpr>.   s   € z/is_snapd_installed_as_a_snap.<locals>.<genexpr>)ÚanyÚget_installed_snapsr   r   r   r   Úis_snapd_installed_as_a_snap,   s   r   Ú
http_proxyÚhttps_proxyÚretry_sleepsc                 C   sx   t ƒ s
t d¡ dS | s|rt tjjdd�¡ | r(tj	dddd | ¡g|d� |r:tj	dddd	 |¡g|d� dS dS )
a  
    Configure snap to use http and https proxies.

    :param http_proxy: http proxy to be used by snap. If None, it will
                       not be configured
    :param https_proxy: https proxy to be used by snap. If None, it will
                        not be configured
    :param retry_sleeps: Optional list of sleep lengths to apply between
        retries. Specifying a list of [0.5, 1] tells subp to retry twice
        on failure; sleeping half a second before the first retry and 1 second
        before the second retry.
    z6Skipping configure snap proxy. snapd is not installed.Nr   )ÚserviceÚsetr   zproxy.http={}©r"   zproxy.https={})
r   ÚLOGÚdebugÚeventÚinfor
   ÚSETTING_SERVICE_PROXYÚformatr   Úsubp)r    r!   r"   r   r   r   Úconfigure_snap_proxy1   s    
þ
þÿr-   Úprotocol_typec                 C   s4   t ƒ s
t d¡ dS tjdddd | ¡g|d� dS )a€  
    Unset snap configuration settings for http and https proxies.

    :param protocol_type: String either http or https
    :param retry_sleeps: Optional list of sleep lengths to apply between
        retries. Specifying a list of [0.5, 1] tells subp to retry twice
        on failure; sleeping half a second before the first retry and 1 second
        before the second retry.
    z8Skipping unconfigure snap proxy. snapd is not installed.Nr   Úunsetr   zproxy.{}r%   )r   r&   r'   r   r,   r+   )r.   r"   r   r   r   Úunconfigure_snap_proxyV   s   

þr0   Úkeyc                 C   s8   zt  ddd| g¡\}}| ¡ W S  tjy   Y dS w )z¨
    Gets the config value from snap.
    :param protocol: can be any valid snap config option
    :return: the value of the snap config option, or None if not set
    r   Úgetr   N)r   r,   Ústripr   ÚProcessExecutionError)r1   ÚoutÚ_r   r   r   Úget_config_option_valuek   s   
ÿr7   c                  C   sR   t  g d¢¡\} }|  ¡ }|dd … }g }|D ]}| ¡ d }| t|ƒ¡ q|S )N)r   Úlistz--colorÚneverz	--unicoder9   é   r   )r   r,   Ú
splitlinesÚsplitÚappendÚget_snap_info)r5   r6   ÚappsÚsnapsÚliner   r   r   r   r   x   s   ÿr   c               
   C   s’   t  tjjdd�¡ z	t t ¡ ¡ W n tj	y. }  zt
 dt| ƒ¡ W Y d } ~ nd } ~ ww ztjg d¢tjd� W d S  tjyH   t ¡ ‚w )Nzstandard Ubuntur   z<Trying to install snapd. Ignoring apt-get update failure: %s)zapt-getÚinstallz--assume-yesr   r%   )r(   r)   r
   ÚAPT_UPDATING_LISTr+   r   Úupdate_sources_listÚget_system_sources_filer   ÚUbuntuProErrorr&   r'   Ústrr   r,   ÚAPT_RETRIESr4   ÚCannotInstallSnapdError)Úer   r   r   Úinstall_snapd†   s$   þ€ÿþÿrK   Úprogressc              
   C   sv   zt jtdddgdd� W d S  tjy: } zt dt|ƒ ¡ ¡r.t	 
d¡ |  dtj¡ n‚ W Y d }~d S d }~ww )	NÚwaitr   zseed.loadedT©Úcapturezunknown command .*waitz9Detected version of snapd that does not have wait commandr)   )r   r,   ÚSNAP_CMDr   r4   ÚreÚsearchrG   Úlowerr&   ÚwarningÚemitr
   ÚSNAPD_DOES_NOT_HAVE_WAIT_CMD)rL   rJ   r   r   r   Úrun_snapd_wait_cmd˜   s   ÿþ€ûrW   Fr   Úclassic_confinement_supportc                 C   s@   t d| g}|r|dg7 }|r|d |¡g7 }tj|dtd� d S )NrB   z	--classicz--channel={}T)rO   r"   )rP   r+   r   r,   ÚSNAP_INSTALL_RETRIES)r   r   rX   Úcmdr   r   r   Úinstall_snap¥   s   


ýr[   c                 C   s   t jtd| gdd� d S )NÚrefreshTrN   )r   r,   rP   ©r   r   r   r   Úrefresh_snap¹   s   r^   c              	   C   sÜ   t  | ¡}z5t td|¡}|jdkr9|jdkr)|j di ¡ d¡dkr)tj	| d�‚|j di ¡ d¡}tj
|d	�‚W n tyE   t ¡ ‚w |j di ¡}t| d
d¡| dd¡| dd¡| dd¡| di ¡ dd¡d�S )NÚGETéÈ   i”  ÚresultÚkindzsnap-not-foundr]   Úmessage)Úerrorr   Ú r   r   r   r   Úusername)r   r   r   r   r   )ÚSNAPD_SNAPS_APIr+   r	   Úunix_socket_requestÚSNAPD_SOCKET_PATHÚcodeÚ	json_dictr2   r   ÚSnapNotInstalledErrorÚUnexpectedSnapdAPIErrorÚConnectionRefusedErrorÚSnapdAPIConnectionRefusedr   )r   ÚurlÚrespÚ	error_msgÚ	snap_infor   r   r   r>   ½   s.   


ÿ÷ÿ



ûr>   )NNN)N)NF)+ÚloggingrQ   Útypingr   r   r   Úuaclientr   r   r   r   r	   r
   r   r   rP   rY   ÚHTTP_PROXY_OPTIONÚHTTPS_PROXY_OPTIONri   rg   Úget_event_loggerr(   Ú	getLoggerÚreplace_top_level_logger_nameÚ__name__r&   rG   r   Úboolr   r   Úfloatr-   r0   r7   r   rK   ÚProgressWrapperrW   r[   r^   r>   r   r   r   r   Ú<module>   st    (ûþýÿþ
ý
ü&ÿÿ
ÿ
þýÿþ
ý