3
 a                 @   s  d dl Z d dlmZ d dl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mZ d dlmZmZmZmZmZmZ d dlmZmZmZmZmZ y(d dlmZmZmZmZmZm Z m!Z! W n e"k
r   dd	 ZY nX d
dej# dddddg ej$j%j&ej'g dddg ej(j)j&ddddg ddZ*ej+e,Z-dZ.dddZ/dZ0d3Z1d4Z2ed"d5Z3G d$d% d%Z4e5d&d'd(Z6d6d)d*Z7e5d+d,d-Z8d.d/ Z9e4d0d1d2Z:dS )7    N)datetime)wraps)
namedtupleOrderedDict)apt
exceptionssnapstatusutilversion)CONFIG_DEFAULTSCONFIG_FIELD_ENVVAR_ALLOWLISTDEFAULT_CONFIG_FILEBASE_CONTRACT_URLBASE_SECURITY_URL)AnycastDictListOptionalTupleUnionc             C   s   |S )N )_xr   r   1/usr/lib/python3/dist-packages/uaclient/config.pyr   !   s    r   zUContent provided in json response is currently considered Experimental and may changez0.1F )idname
created_atproductstech_support_level)r   r   r   external_account_ids)Z_docZ_schema_versionr   
machine_idattached	effectiveexpiresoriginservicesexecution_statusexecution_detailsnoticescontractaccountprivater   type)availableResourcesresourceEntitlementsZ_unset
http_proxyhttps_proxyapt_http_proxyapt_https_proxycontract_urldata_dirfeatureslog_file	log_levelsecurity_urlsettings_overrides	ua_configDataPathfilenamec               @   s  e Zd Zeddeddeddeddeddedded	ded
deddd	ZdZdZdZdjdeddddZ	e
dd Ze
edddZe
edddZe
ddddZejedddZe
ddddZejedd dZe
ddd!d"Zejedd#d"Ze
ddd$d%Zejedd&d%Zd'dd(d)Ze
d*d+ Ze
d,d- Zeed.d/d0Zeed1d2d3Ze
d4d5 Ze
d6d7 Ze
d8dd9d:Ze
d;d< Ze
edd=d>Ze
d?d@ Ze
dAdB ZdCdD Z dkdedEdFdGZ!eddHdIdJZ"eddEdKdLZ#dMdN Z$dlee%dOdPdQdRZ&edSddTdUdVZ'dddWdXZ(dddYdZZ)ddd[d\Z*d]dd^d_Z+ddd`daZ,dmdddbdcZ-ddde Z.dfdg Z/dndhdiZ0dS )oUAConfigzinstance-idTzmachine-access-cis.jsonz
machine-idzmachine-token.jsonlockzstatus.jsonFznotices.jsonzmarker-reboot-cmds-requiredzservices-once-enabled)	zinstance-idzmachine-access-cisz
machine-idzmachine-tokenrA   zstatus-cacher+   zmarker-reboot-cmdszservices-once-enabledNzDict[str, Any])cfgseriesreturnc             C   s0   |rd| _ || _nt | _ t| j | _|| _dS )r   N)cfg_pathrB   get_config_pathparse_configrC   )selfrB   rC   r   r   r   __init__w   s    zUAConfig.__init__c             C   s   | j r| jd d }|gS g S )z?Return the list of accounts that apply to this authorized user.machineTokenInfoaccountInfo)is_attachedmachine_token)rH   rK   r   r   r   accounts   s    zUAConfig.accounts)rD   c             C   s   | j jdtS )Nr6   )rB   getr   )rH   r   r   r   r6      s    zUAConfig.contract_urlc             C   s   | j jdtS )Nr;   )rB   rO   r   )rH   r   r   r   r;      s    zUAConfig.security_urlzOptional[str]c             C   s   | j jdi jdS )Nr=   r2   )rB   rO   )rH   r   r   r   r2      s    zUAConfig.http_proxy)valuec             C   s.   d| j kri | j d< || j d d< | j  d S )Nr=   r2   )rB   	write_cfg)rH   rP   r   r   r   r2      s    

c             C   s   | j jdi jdS )Nr=   r3   )rB   rO   )rH   r   r   r   r3      s    zUAConfig.https_proxyc             C   s.   d| j kri | j d< || j d d< | j  d S )Nr=   r3   )rB   rQ   )rH   rP   r   r   r   r3      s    

c             C   s   | j jdi jdS )Nr=   r4   )rB   rO   )rH   r   r   r   r4      s    zUAConfig.apt_http_proxyc             C   s.   d| j kri | j d< || j d d< | j  d S )Nr=   r4   )rB   rQ   )rH   rP   r   r   r   r4      s    

c             C   s   | j jdi jdS )Nr=   r5   )rB   rO   )rH   r   r   r   r5      s    zUAConfig.apt_https_proxyc             C   s.   d| j kri | j d< || j d d< | j  d S )Nr=   r5   )rB   rQ   )rH   rP   r   r   r   r5      s    

zTuple[int, str]c             C   s   | j d}d}tjj|s|S tj|}|jd\}}ytjd|g t||fS  tj	k
r   tj
 dkrtjd|| t||fS tjd|| tj| |S X d	S )a]  Return lock info if config lock file is present the lock is active.

        If process claiming the lock is no longer present, remove the lock file
        and log a warning.

        :param lock_path: Full path to the lock file.

        :return: A tuple (pid, string describing lock holder)
            If no active lock, pid will be -1.
        rA      r   :Zpsr   z.Found stale lock file previously held by %s:%sz1Removing stale lock file previously held by %s:%sN)rT   r   )	data_pathospathexistsr
   	load_filesplitsubpintProcessExecutionErrorgetuidloggingdebugwarningunlink)rH   Z	lock_pathZno_lockZlock_contentlock_pidlock_holderr   r   r   check_lock_info   s,    


zUAConfig.check_lock_infoc             C   s
   | j d S )Nr7   )rB   )rH   r   r   r   r7      s    zUAConfig.data_dirc             C   s>   | j jd}ytt|j S  tk
r8   tttd S X d S )Nr:   )rB   rO   getattrr_   upperAttributeErrorr   )rH   r:   r   r   r   r:      s
    zUAConfig.log_level)labeldescriptionc             C   s8   | j dpg }||g}||kr4|j| | jd| dS )zAdd a notice message to notices cache.

        Such notices are seen in the Notices section from ua status output.
        They are also present in the JSON status output.
        r+   N)
read_cacheappendwrite_cache)rH   ri   rj   r+   Znoticer   r   r   
add_notice   s
    
zUAConfig.add_notice)label_regexdescr_regexc             C   s   g }| j d}|rLx8|D ]0\}}tj||r:tj||r:q|j||f qW |r^| jd| n"tjj| jdrt	j
| jd dS )zRemove matching notices if present.

        :param label_regex: Regex used to remove notices with matching labels.
        :param descr_regex: Regex used to remove notices with matching
            descriptions.
        r+   N)rk   rematchrl   rm   rV   rW   rX   rU   r
   remove_file)rH   ro   rp   r+   Zcached_noticesZnotice_labelZnotice_descrr   r   r   remove_notice   s    
zUAConfig.remove_noticec             C   s   | j jdtd S )Nr9   )rB   rO   r   )rH   r   r   r   r9     s    zUAConfig.log_filec             C   s   | j r| j S | j}|si S i | _ |d d }tdd |jdg D }tdd |d D }xH|j D ]<\}}d|i}||kr|| |d	< tj|| j || j |< qfW | j S )
zReturn a dictionary of entitlements keyed by entitlement name.

        Return an empty dict if no entitlements are present.
        rJ   contractInfoc             s   s   | ]}|d  |d fV  qdS )r/   tokenNr   ).0er   r   r   	<genexpr>#  s   z(UAConfig.entitlements.<locals>.<genexpr>ZresourceTokensc             s   s   | ]}|d  |fV  qdS )r/   Nr   )rw   rx   r   r   r   ry   '  s    r1   entitlementresourceToken)_entitlementsrM   dictrO   itemsr
   apply_series_overridesrC   )rH   rM   ru   Ztokens_by_nameZent_by_nameentitlement_nameZ	ent_valueentitlement_cfgr   r   r   entitlements  s(    
zUAConfig.entitlementsr   c             C   s    | j s| jd d d | _ | j S )z6Return a datetime of the attached contract expiration.rJ   ru   effectiveTo)_contract_expiry_datetimerM   )rH   r   r   r   contract_expiry_datetime3  s
    z!UAConfig.contract_expiry_datetimec             C   s
   t | jS )z<Report whether this machine configuration is attached to UA.)boolrM   )rH   r   r   r   rL   =  s    zUAConfig.is_attachedc             C   s   | j j tj j  }|jS )a  Report num days until contract expiration based on effectiveTo

        :return: A positive int representing the number of days the attached
            contract remains in effect. Return a negative int for the number
            of days beyond contract's effectiveTo date.
        )r   dater   utcnowdays)rH   deltar   r   r   contract_remaining_daysB  s    z UAConfig.contract_remaining_daysc             C   s.   | j jd}|r*t|tr|S tjd| i S )z>Return a dictionary of any features provided in uaclient.conf.r8   zDUnexpected uaclient.conf features value. Expected dict, but found %s)rB   rO   
isinstancer}   r_   ra   )rH   r8   r   r   r   r8   M  s    
zUAConfig.featuresc             C   sJ   | j sD| jd}| jjd}|r>|r>| j|}|r>t||d || _ | j S )zAReturn the machine-token if cached in the machine token response.zmachine-tokenmachine_token_overlay)	base_dictoverlay_dict)_machine_tokenrk   r8   rO   parse_machine_token_overlaydepth_first_merge_overlay_dict)rH   Zraw_machine_tokenmachine_token_overlay_pathr   r   r   r   rM   \  s    
zUAConfig.machine_tokenc             C   sv   t jj|s tjtjj|dytj	|}t
j|S  tk
rp } z tjtjjt||dW Y d d }~X nX d S )N)	file_path)errorr   )rV   rW   rX   r   UserFacingErrorr	   &INVALID_PATH_FOR_MACHINE_TOKEN_OVERLAYformatr
   rY   jsonloads
ValueErrorERROR_JSON_DECODING_IN_FILEstr)rH   r   Zmachine_token_overlay_contentrx   r   r   r   r   u  s    

z$UAConfig.parse_machine_token_overlay)keyrD   c             C   sh   | j d }|stjj|tS || jkrX| j| }|jrHtjj|t|jS tjj||jS tjj|t|S )zAReturn the file path in the data directory represented by the keyr7   )rB   rV   rW   joinPRIVATE_SUBDIR
data_pathsr.   r?   )rH   r   r7   rU   r   r   r   rU     s    


zUAConfig.data_path)
cache_pathrD   c             C   s   t jj|rt j| dS )zDelete the given cache_path if it exists.

        (This is a separate method to allow easier disabling of deletion during
        tests.)
        N)rV   rW   rX   rb   )rH   r   r   r   r   _perform_delete  s    zUAConfig._perform_deletec             C   sX   |st d|jds|dkr,d| _d| _n|dkr@| jdd | j|}| j| dS )zRemove specific cache file.z1Invalid or empty key provided to delete_cache_keyzmachine-accesszmachine-tokenNrA   r   zOperation in progress.*)RuntimeError
startswithr|   r   rt   rU   r   )rH   r   r   r   r   r   delete_cache_key  s    
zUAConfig.delete_cache_keyc             C   s"   x| j j D ]}| j| qW dS )z<Remove configuration cached response files class attributes.N)r   keysr   )rH   Zpath_keyr   r   r   delete_cache  s    zUAConfig.delete_cachezOptional[Any])r   silentrD   c             C   sx   | j |}ytj|}W n4 tk
rL   tjj| rH| rHtjd| d S X yt	j
|tjdS  tk
rr   |S X d S )NzFile does not exist: %s)cls)rU   r
   rY   	ExceptionrV   rW   rX   r_   r`   r   r   DatetimeAwareJSONDecoderr   )rH   r   r   r   contentr   r   r   rk     s    
zUAConfig.read_cacher   )r   r   rD   c             C   s   | j |}tjj|}tjj|sHtj| tjj|tkrHtj|d |j	dsZ|dkrhd | _
d | _n,|dkrd|kr| jddj|jdd  t|tstj|tjd	}d
}|| jkr| j| jsd}tj|||d d S )Ni  zmachine-accesszmachine-tokenrA   rS   r   zOperation in progress: {}rR   )r   i  i  )mode)rU   rV   rW   dirnamerX   makedirsbasenamer   chmodr   r   r|   rn   r   rZ   r   r   r   dumpsr
   DatetimeAwareJSONEncoderr   r.   
write_file)rH   r   r   filepathr7   r   r   r   r   rm     s*    



zUAConfig.write_cachec       	      C   s   ddl m} tj|}g }xf|jdi D ]V}|d }|j|}|dkrR|j| q(tjjj	}|j
 st|jdd|kr(|j| q(W |r||d< |S )z( Remove beta services from response dictr   )ENTITLEMENT_CLASS_BY_NAMEr(   r   Nr	   r   )uaclient.entitlementsr   copydeepcopyrO   rl   r	   UserFacingStatusACTIVErP   is_beta)	rH   responser   Znew_responseZreleased_resourcesresourceZresource_nameent_clsenabled_statusr   r   r   _remove_beta_resources  s     



zUAConfig._remove_beta_resourcesc       
      C   s   t j}|jj}t j}| j \}}| jdp,g }|dkrP|jj}t jj	||d}nLt
jj| jdr|jj}d}x|D ]\}}	|dkrt|	}P qtW t jj	|d}|||| j| jdS )	as  Return a dict with execution_status, execution_details and notices.

            Values for execution_status will be one of UserFacingConfigStatus
            enum:
                inactive, active, reboot-required
            execution_details will provide more details about that state.
            notices is a list of tuples with label and description items.
        r+   r   )pidrd   zmarker-reboot-cmdszconfiguration changeszReboot required)	operation)r)   r*   r+   config_pathconfig)r	   UserFacingConfigStatusINACTIVErP   MESSAGE_NO_ACTIVE_OPERATIONSre   rk   r   MESSAGE_LOCK_HELDr   rV   rW   rX   rU   REBOOTREQUIRED#MESSAGE_ENABLE_REBOOT_REQUIRED_TMPLrE   rB   )
rH   Z
userStatusZ
status_valZstatus_descrc   rd   r+   r   ri   rj   r   r   r   _get_config_status  s.    	zUAConfig._get_config_statusc             C   s   ddl m} ddlm} tjt}tj| j	d|d< || }xvt
|dd dD ]b}|d	 rftjjj}n
tjjj}|j|d
 }|stjd|d
  qN|d j|d
 |j|d qNW |S )z#Return unattached status as a dict.r   )get_available_resources)r   )r8   r   c             S   s   | d S )Nr   r   )r   r   r   r   <lambda>(  s    z-UAConfig._unattached_status.<locals>.<lambda>)r   	availabler   z@Ignoring availability of unknown service %s from contract serverr(   )r   rj   r   )uaclient.contractr   r   r   r   r   DEFAULT_STATUSr   get_versionr8   sortedr	   UserFacingAvailability	AVAILABLErP   UNAVAILABLErO   LOGr`   rl   rj   )rH   r   r   r   	resourcesr   r   r   r   r   r   _unattached_status  s(    


zUAConfig._unattached_statuszDict[str, Optional[str]]c             C   sz   d}d }|j  }|tjjkr&tjj}n*|j|krDtjj}||j }n|j \}}|j|j	|j
|j
|||j|krrdnddS )Nr   yesno)r   rj   entitledr	   Zstatus_detailsdescription_overrider   )contract_statusr	   ContractStatus
UNENTITLEDr   r   r   INAPPLICABLEuser_facing_statusrj   rP   )rH   entinapplicable_resourcesdetailsr   r   
ent_statusr   r   r   _attached_service_status@  s"    

z!UAConfig._attached_service_statusc             C   s  ddl m} ddlm} tjt}| jd }|d }tj	j
j}|jtj| jd|d d|jd	| jd
pjg |d |d |jdd|jdg |d| jd d | jd d | jd jdd| jd jdg dd |jdr| j|d< |jdr|d |d< | jjd}|s|| }dd t|dd dD }x,|D ]$}	|	| }
|d j| j|
| q8W | jjdi jd }|r|jd!i jd"}|r||d# d$< |S )%z8Return configuration of attached status as a dictionary.r   )r   )ENTITLEMENT_CLASSESrJ   ru   )r8   Z	machineIdTr'   r+   r   r   Z	createdAtr   r    )r   r   r   r    r!   ZexternalAccountIDs)r   r   r   r"   )r   r#   r$   r'   r+   r,   r-   r   r&   ZeffectiveFromr%   r0   c             S   s$   i | ]}|d  s|j d|d qS )r   rj   r   )rO   )rw   r   r   r   r   
<dictcomp>  s   z-UAConfig._attached_status.<locals>.<dictcomp>c             S   s   | d S )Nr   r   )r   r   r   r   r     s    z+UAConfig._attached_status.<locals>.<lambda>)r   r(   supportrz   affordancessupportLevelr,   r!   )r   r   r   r   r   r   r   rM   r	   r   r   rP   updater   r   r8   rO   rk   rN   r   r   rl   r   r   )rH   r   r   r   rJ   ru   r!   r   r   r   r   r   r   r   r   r   _attached_status[  sR    









zUAConfig._attached_statusc             C   s   t j dkr*td| jd}|sB| j }n| js:| j }n| j }|j| j  t j dkr| j	d| t
j s| jdtjjdd t
j| jdd}||O }|s| j|}|S )	a  Return status as a dict, using a cache for non-root users

        When unattached, get available resources from the contract service
        to report detailed availability of different resources for this
        machine.

        Write the status-cache when called by root.
        r   zDict[str, Any]zstatus-cacher   zfix operation)r   zfeatures.allow_beta)r   path_to_value)rV   r^   r   rk   r   rL   r   r   r   rm   r
   should_rebootrt   r	   r   r   is_config_value_truerB   r   )rH   Z	show_betar   Zconfig_allow_betar   r   r   r	     s*    




zUAConfig.statusc             C   s   ddl m} ddlm} || }d}t }||d< x8|D ]0}|d |kr8||kr8|}|j|}|| }	P q8W |dkrtjdj|| j	r| j
|	i }
|
d }|
d |d< ||d< |d	kr|jrd
|d< n&|d rtjjj}n
tjjj}||d< |	j|d< |S )zReturn help information from an uaclient service as a dict

        :param name: Name of the service for which to return help data.

        :raises: UserFacingError when no help is available.
        r   )r   )r   Nr   zNo help available for '{}'r	   r   enabledTbetar   help)r   r   r   r   r   rO   r   r   r   rL   r   r   r	   r   r   rP   r   	help_info)rH   r   r   r   r   Zhelp_resourceZresponse_dictr   Zhelp_ent_clsZhelp_entservice_statusZ
status_msgr   r   r   r   r     s8    




zUAConfig.helpc             C   sJ  t jd| jt j t jd| jt j t jd| jt j t jd| jt j	 t
j| j| j g }tj rtj| j| j | j rtjtjs| j rtjtjr|jd ddlm} |j }|j \}}|tjjkr|j| j| j | j r|j|js| j r|j|jr|jd t|dkrFdj|}ttjj|d d S )	Nhttphttpsr   r   )	livepatchr   z, )r(   ) r
   validate_proxyr4   PROXY_VALIDATION_APT_HTTP_URLr5   PROXY_VALIDATION_APT_HTTPS_URLr2   PROXY_VALIDATION_SNAP_HTTP_URLr3   PROXY_VALIDATION_SNAP_HTTPS_URLr   Zsetup_apt_proxyr   is_installedZconfigure_snap_proxyZget_config_option_valueZHTTP_PROXY_OPTIONZHTTPS_PROXY_OPTIONrl   r   r   LivepatchEntitlementapplication_statusr	   ApplicationStatusENABLEDZconfigure_livepatch_proxylenr   print)MESSAGE_PROXY_DETECTED_BUT_NOT_CONFIGUREDr   )rH   Zservices_with_proxiesr   Zlivepatch_entZlivepatch_statusr   r(   r   r   r   process_config  sF    





zUAConfig.process_configc                s   |st }tj}tj j}d|kr.td |d< xdD ]}t |||< q4W  fddtD |d< |t	j
|d	d
7 }tj|| dS )z?Write config values back to config_path or DEFAULT_CONFIG_FILE.r:   r6   r;   r7   r9   c                s   i | ]}t  ||qS r   )rf   )rw   r   )rH   r   r   r   <  s   z&UAConfig.write_cfg.<locals>.<dictcomp>r=   F)default_flow_styleN)r6   r;   r7   r9   )r   r	   MESSAGE_UACLIENT_CONF_HEADERr   r   rB   r   rf   UA_CONFIGURABLE_KEYSyamldumpr
   r   )rH   r   r   Zcfg_dictattrr   )rH   r   rQ   .  s    

zUAConfig.write_cfg)NN)N)F)F)N)1__name__
__module____qualname__r>   r   r|   r   r   r   rI   propertyrN   r6   r;   r2   setterr3   r4   r5   re   r7   r:   rn   rt   r9   r   r   rL   r\   r   r8   rM   r   rU   r   r   r   r   rk   rm   r   r   r   r   r   r	   r   r  rQ   r   r   r   r   r@   e   s   $	
	!%#=(4:r@   )rD   c              C   sB   t jjd} | r| S t jjt j t jjt}t jj|r>|S tS )z4Get config path to be used when loading config dict.UA_CONFIG_FILE)	rV   environrO   rW   r   getcwdr   r   rX   )Zconfig_fileZ	local_cfgr   r   r   rF   D  s    rF   c             C   s  t j t}| st } tjd|  tjj| rB|jt	j
tj|  i }xtjj D ]\}}|j }|jdrR|dd }|jdr|dd }|jdrtjj|rt	j
tj|}ntjdj|d	|kr||i|d	< q||d	 |< qR|tkrR|||< qRW |j| tjj|d
 |d
< x4dD ],}tj|| s"tjdj||| q"W x4tt|j jtD ]}tjd||j| qjW |S )a  Parse known UA config file

    Attempt to find configuration in cwd and fallback to DEFAULT_CONFIG_FILE.
    Any missing configuration keys will be set to CONFIG_DEFAULTS.

    Values are overridden by any environment variable with prefix 'UA_'.

    @param config_path: Fullpath to ua configfile. If unspecified, use
        DEFAULT_CONFIG_FILE.

    @return: Dict of configuration values.
    z(Using UA client configuration file at %sZua_   NZ	features_	   r  zCould not find yaml file: {}r8   r7   r6   r;   zInvalid url in config. {}: {}z)Ignoring invalid uaclient.conf key: %s=%s)r6   r;   ) r   r   rF   r   r`   rV   rW   rX   r   r  	safe_loadr
   rY   r  r~   lowerr   endswithr   r   r   r   
expanduseris_service_urlr   setr   
differenceVALID_UA_CONFIG_KEYSr_   ra   pop)r   rB   Zenv_keysr   rP   
field_nameZfeature_field_namer   r   r   rG   S  sB    





rG   )override_keyc                s    fdd}|S )a  Decorator used to override function return by config settings.

    To identify if we should override the function return, we check
    if the config object has the expected override key, we use it
    has, we will use the key value as the function return. Otherwise
    we will call the function normally.

    @param override_key: key to be looked for in the settings_override
     entry in the config dict. If that key is present, we will return
     its value as the function return.
    c                s   t   fdd}|S )Nc                 s,   t  } | jdi jt}|tkr&|S   S )Nr<   )rG   rO   UNSET_SETTINGS_OVERRIDE_KEY)rB   Zvalue_override)fr#  r   r   new_f  s    z>apply_config_settings_override.<locals>.wrapper.<locals>.new_f)r   )r%  r&  )r#  )r%  r   wrapper  s    z/apply_config_settings_override.<locals>.wrapperr   )r#  r'  r   )r#  r   apply_config_settings_override  s    r(  c             C   s   dd }x|j  D ]\}}| j|}t|trHt|trHt| | | qt|trt|trt|rt|d tr|| | ||d q|| |< q|| |< qW dS )a  Merge the contents of overlay dict into base_dict not only on top-level
    keys, but on all on the depths of the overlay_dict object. For example,
    using these values as entries for the function:

    base_dict = {"a": 1, "b": {"c": 2, "d": 3}}
    overlay_dict = {"b": {"c": 10}}

    Should update base_dict into:

    {"a": 1, "b": {"c": 10, "d": 3}}

    @param base_dict: The dict to be updated
    @param overlay_dict: The dict with information to be added into base_dict
    c       	      S   st   g }t j|}xV|D ]N}d}x6t| D ]*\}}|j||j|kr&t|| d}q&W |s|j| qW | j| d S )NFT)MERGE_ID_KEY_MAPrO   	enumerater   rl   extend)	Zbase_valuesZoverlay_valuesr   Zvalues_to_appendZid_keyZoverlay_valueZwas_replacedZbase_value_idx
base_valuer   r   r   update_dict_list  s    


z8depth_first_merge_overlay_dict.<locals>.update_dict_listr   )r   N)r~   rO   r   r}   r   listr  )r   r   r-  r   rP   r,  r   r   r   r     s    

r   )rB   c             C   sf   t jjd zHy$td tt jd d}||  W n tk
rP   tjd Y nX W dt jj	  X dS )zHelper to load and run ua_update_messaging.

    This is needed because we don't have /usr/lib/ubuntu-advantage
    python scripts in our path and we don't want to shell out with
    subp to call python3 /path/to/ua_update_messaging.py.
    z/usr/lib/ubuntu-advantageZua_update_messagingZupdate_apt_and_motd_messagesz@Unable to update UA messages. Cannot import ua_update_messaging.N)
sysrW   rl   
__import__rf   modulesImportErrorr_   r`   r!  )rB   Zupdate_msgsr   r   r   update_ua_messages  s    r3  )r2   r3   r4   r5   )r6   r7   r8   r9   r:   r;   r<   r=   )r?   r.   )N);r   r   	functoolsr   r   r_   rV   rq   r/  r  collectionsr   r   uaclientr   r   r   r	   r
   r   uaclient.defaultsr   r   r   r   r   typingr   r   r   r   r   r   r   r2  r   r   r   rP   r   r   r   r   	getLoggerr  r   r   r)  r$  r  r   r>   r@   r   rF   rG   r(  r   r3  r   r   r   r   <module>   s    (	
          
     d
A/