
    g              	           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	j*                         Z e j.                   ej0                  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ZdefdZ	 	 	 d$dee   dee   deee       ddfdZ!	 d%dedeee       ddfdZ"dedee   fdZ#dee   fdZ$d Z%dejL                  fdZ'	 	 d&dedee   d efd!Z(defd"Z)dedefd#Z*y)'    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                  .    dt        j                         v S )z(Returns whether or not snap is installedsnapd)r   get_installed_packages_names     //usr/lib/python3/dist-packages/uaclient/snap.pyis_snapd_installedr   '   s    c66888r   c                  6    t        d t               D              S )z3Returns whether or not snapd is installed as a snapc              3   :   K   | ]  }|j                   d k(    yw)r   Nr   ).0snaps     r   	<genexpr>z/is_snapd_installed_as_a_snap.<locals>.<genexpr>.   s     G		W$Gs   )anyget_installed_snapsr   r   r   is_snapd_installed_as_a_snapr$   ,   s    G1D1FGHHr   
http_proxyhttps_proxyretry_sleepsc                 d   t               st        j                  d       y| s|r3t        j	                  t
        j                  j                  d             | r*t        j                  ddddj                  |       g|       |r+t        j                  dddd	j                  |      g|       yy)
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'   s      r   configure_snap_proxyr3   1   s    " 		JK[

811888HIUHo&<&<Z&HI%	

 UH&6&=&=k&JK%	
 r   protocol_typec                     t               st        j                  d       yt        j                  ddddj                  |       g|       y)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   r2   r1   )r4   r'   s     r   unconfigure_snap_proxyr7   V   s@     		LM
KK	(J$5$5m$DE!r   keyc                     	 t        j                  ddd| g      \  }}|j                         S # t        j                  $ r Y yw xY 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   r2   stripr	   ProcessExecutionError)r8   out_s      r   get_config_option_valuer?   k   sF    feXs;<Qyy{++ s   +. AAc                      t        j                  g d      \  } }| j                         }|dd  }g }|D ]/  }|j                         d   }|j	                  t        |             1 |S )N)r    listz--colorneverz	--unicoderB      r   )r   r2   
splitlinessplitappendget_snap_info)r=   r>   appssnapsliner    s         r   r#   r#   x   sk    [[BFC >>D8DE *zz|A]4()* Lr   c                     t         j                  t        j                  j	                  d             	 t        j                  t        j                                	 t        j                  g dt
        j                         y # t        j                  $ r)} t        j                  dt        |              Y d } ~ `d } ~ ww xY w# t        j                   $ r t        j"                         w xY 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_LISTr1   r   update_sources_listget_system_sources_filer	   UbuntuProErrorr,   r-   strr   r2   APT_RETRIESr<   CannotInstallSnapdError)es    r   install_snapdrU      s    	JJx))006G0HI
 ; ; =>3;	
 $$ 
		JF	
 	

 ++ 300223s#   'B 'C CB<<C(C,progressc                 L   	 t        j                  t        dddgd       y # t        j                  $ rn}t        j                  dt        |      j                               r6t        j                  d       | j                  dt        j                         n Y d }~y d }~ww xY w)	Nwaitr   zseed.loadedTcapturezunknown command .*waitz9Detected version of snapd that does not have wait commandr/   )r   r2   SNAP_CMDr	   r<   researchrQ   lowerr,   warningemitr   SNAPD_DOES_NOT_HAVE_WAIT_CMD)rV   rT   s     r   run_snapd_wait_cmdrb      s{    	Xvx?N++ 99.A?KKK MM&("G"GH Is   " B#A$BB#r    classic_confinement_supportc                     t         d| g}|r|dgz  }|r|dj                  |      gz  }t        j                  |dt               y )NrL   z	--classicz--channel={}T)rZ   r'   )r[   r1   r   r2   SNAP_INSTALL_RETRIES)r    r   rc   cmds       r   install_snaprg      sO    
 Y
%C"}%%g.//
KK)r   c                 @    t        j                  t        d| gd       y )NrefreshTrY   )r   r2   r[   r    s    r   refresh_snaprk      s    
KK9d+T:r   c           
         t         j                  |       }	 t        j                  t        d|      }|j
                  dk7  r|j
                  dk(  rD|j                  j                  di       j                  d      dk(  rt        j                  |       |j                  j                  di       j                  d      }t        j                  |	      	 |j                  j                  di       }t        |j                  d
d      |j                  dd      |j                  dd      |j                  dd      |j                  di       j                  dd            S # t        $ r t        j                         w xY w)NGET   i  resultkindzsnap-not-foundrj   message)errorr    r   r   r   r   username)r   r   r   r   r   )SNAPD_SNAPS_APIr1   r
   unix_socket_requestSNAPD_SOCKET_PATHcode	json_dictr:   r	   SnapNotInstalledErrorUnexpectedSnapdAPIErrorConnectionRefusedErrorSnapdAPIConnectionRefusedr   )r    urlresp	error_msg	snap_infos        r   rG   rG      sM   

 
 
&C5''(95#F99		S NN&&x488@#$ !66DAA NN..x<@@K	 88yII  ""8R0I]]62&i,z2.i,--R044ZD 	 " 522445s   B>E" "F )NNN)N)NF)+loggingr\   typingr   r   r   uaclientr   r   r   r	   r
   r   r   r   r[   re   HTTP_PROXY_OPTIONHTTPS_PROXY_OPTIONrw   ru   get_event_loggerr.   	getLoggerreplace_top_level_logger_name__name__r,   rQ   r   boolr   r$   floatr3   r7   r?   r#   rU   ProgressWrapperrb   rg   rk   rG   r   r   r   <module>r      s    	 - -	 	 	 &   " '  %%%'g:::8DE 		C	S	C	c	9D 9
Id I !%!%*."
"
#"
 4;'"
 
	"
L ?C&.tE{&;	*
 
# 
T+. 3$
!4!4 
 "(-
c] "&(;s ;  r   