
    f              	          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mZm	Z	 d dl
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j2                   ej4                  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 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)aptevent_logger
exceptions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_proxyr1   +   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
   r0   r/   )r2   r%   s     r   unconfigure_snap_proxyr5   P   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
   r0   stripr   ProcessExecutionError)r6   out_s      r   get_config_option_valuer=   e   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	--unicoder@      r   )r
   r0   
splitlinessplitappendget_snap_info)r;   r<   appssnapsliner   s         r   r!   r!   r   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_LISTr/   r   update_sources_listget_system_sources_filer   UbuntuProErrorr*   r+   strr
   r0   APT_RETRIESr:   CannotInstallSnapdErrores    r   install_snapdrT      s    	JJx))006G0HI
 ; ; =>3;	
 $$ 
		JF	
 	

 ++ 300223s#   'B 'C CB<<C(C,c                  R   	 t        j                  t        dddgd       y # t        j                  $ rq} t        j                  dt        |       j                               r9t        j                  d       t        j                  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 command)r
   r0   SNAP_CMDr   r:   researchrO   lowerr*   warningr,   r-   r	   SNAPD_DOES_NOT_HAVE_WAIT_CMDrR   s    r   run_snapd_wait_cmdr_      sx    	Xvx?N++ 99.A?KKK JJx<<= >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 )NrJ   z	--classicz--channel={}T)rX   r%   )rY   r/   r
   r0   SNAP_INSTALL_RETRIES)r   r   r`   cmds       r   install_snaprd      sO    
 Y
%C"}%%g.//
KK)r   c                 @    t        j                  t        d| gd       y )NrefreshTrW   )r
   r0   rY   r   s    r   refresh_snaprh      s    
KK9d+T:r   c           
         t        j                   t         j                  t         j                        }|j                  t               t
        j                  j                  d      }||_        t        j                  |       }	 |j                  dt        j                  |              |j                         }|j                         j                  d      }	 t        j                   |      }|j,                  dk7  r|j,                  d	k(  r:|j/                  d
i       j/                  d      dk(  rt)        j0                  |       |j/                  d
i       j/                  d      }t)        j2                  |      	 |j9                          |j9                          |j/                  d
i       }	t;        |	j/                  dd      |	j/                  dd      |	j/                  dd      |	j/                  dd      |	j/                  di       j/                  dd            S # t        j"                  $ rD}t$        j'                  d|||       t)        j*                  dj                  |      |      d }~ww xY w# t4        $ r t)        j6                         w xY w# |j9                          |j9                          w xY w)N	localhostGETzutf-8z]JSONDecodeError while parsing result of snap api call to %s, returning None. output was: "%s")exc_infozSNAPD API {})sourcer;      i  resultkindzsnap-not-foundrg   message)errorr    r   r   r   r   username)r   r   r   r   r   )socketAF_UNIXSOCK_STREAMconnectSNAPD_SOCKET_PATHhttpclientHTTPConnectionsockSNAPD_SNAPS_APIr/   requestgetresponsereaddecodejsonloadsJSONDecodeErrorr*   r]   r   InvalidJsonstatusr8   SnapNotInstalledErrorUnexpectedSnapdAPIErrorConnectionRefusedErrorSnapdAPIConnectionRefusedcloser   )
r   	snap_sockconnurlresponser;   datarS   	error_msg	snap_infos
             r   rE   rE      s;   fnnf.@.@AI'(;;%%k2DDI

 
 
&C"UO22489##%mmo$$W-	::c?D ??c!3&HHXr*..v6:JJ 66DAA HHXr266yA	 88yII " 	

2&I]]62&i,z2.i,--R044ZD = ## 
	KK3   ((%,,S1s 
	. " 522445 	

s>   AI: H  .BI:  I73?I22I77I: :JJ "J=)NNN)N)NF),http.clientrz   r   loggingrZ   ru   typingr   r   r   uaclientr   r   r   r	   r
   r   rY   rb   HTTP_PROXY_OPTIONHTTPS_PROXY_OPTIONry   r~   get_event_loggerr,   	getLoggerreplace_top_level_logger_name__name__r*   rO   r   boolr   r"   floatr1   r5   r=   r!   rT   r_   rd   rh   rE   r   r   r   <module>r      s      	  - - J J&   " '  %%%'g:::8DE 		C	S	C	c	9D 9
Id I !%!%*."
"
#"
 4;'"
 
	"
L ?C&.tE{&;	*
 
# 
T+. 3$
 "(-
c] "&(;s ;3 3 3r   