o
    ϴf                     @   sx  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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mZ d dlZd dlmZ d dlmZmZmZmZmZmZmZ d dlm Z  d dl!m"Z" dZ#d	Z$d
Z%dZ&dZ'dZ(dZ)dZ*dZ+dZ,dZ-dZ.dZ/dZ0dZ1dZ2dZ3dZ4ej56e dej56e dgej56e dej56e dej56e dej56e dgd Z7g d!Z8e9 Z:e;e<e=Z>ej?G d"d# d#ej@ZAed$d%eBfd&eBfd'eBfgZCd(d) ZDeDd*eBd+eBfd,d-ZEd.d/ ZFd0eBd1eeB fd2d3ZG		dd4eeB d5eeB d6eeeBeBf  d1eBfd7d8ZHe
dd9		dd5eeB d6eeeBeBf  d1eBfd:d;ZIG d<d= d=ZJd>d? ZKd@dA ZLdBeBd1eeB fdCdDZM	EddBeBdFeNd1eeB fdGdHZO	dd6eeeBeBf  d1eBfdIdJZPeQejRejSfe8dKeBfdLdMZT		ddNeeB dOeeeB  d6eeeBeBf  d1eBfdPdQZUdReBd1eejV fdSdTZWdReeB d1eejV fdUdVZX	ddWejVdXeeB d1eejY fdYdZZZd[eeB d\eBd]eNd^eBd1eBf
d_d`Z[	Edd[eeB d\eBd]eNd^eBdaeBdbeNd1eBfdcddZ\deeBd^eBdfeBd[eeB daeBd1dfdgdhZ]didj Z^dkdl Z_	ddeeBd^eBdaeeB d1dfdmdnZ`dodp Zadqdr Zbdsdt Zcdudv ZddweBd1eNfdxdyZed1eeC fdzd{Zfd1eeB fd|d}ZgddeAjhfd~eeB deeB deeA d1dfddZid1eej fddZkd1eej fddZldd ZmdddZndeeB deBfddZodeeB deBfddZpdeeB fddZqdd Zrdd ZsdeeB d1eeBeeBeeB f f fddZtd1eBfddZudS )    N)	lru_cachewraps)DictIterableList
NamedTupleOptionalSetUnion)AcquireProgress)event_logger
exceptionsgpgmessagessecret_managersystemutil)ESM_APT_ROOTDIR)status_cache_fileg      N@z  # ubuntu-pro-clientzDir::Etc::netrc/zDir::Etc::netrcparts/zDir::State::lists/a  /*
 * Autogenerated by ubuntu-pro-client
 * Do not edit this file directly
 *
 * To change what ubuntu-pro-client sets, use the `pro config set`
 * or the `pro config unset` commands to set/unset either:
 *      global_apt_http_proxy and global_apt_https_proxy
 * for a global apt proxy
 * or
 *      ua_apt_http_proxy and ua_apt_https_proxy
 * for an apt proxy that only applies to Ubuntu Pro related repos.
 */
z$Acquire::http::Proxy "{proxy_url}";
z%Acquire::https::Proxy "{proxy_url}";
z4Acquire::http::Proxy::esm.ubuntu.com "{proxy_url}";
z5Acquire::https::Proxy::esm.ubuntu.com "{proxy_url}";
z/etc/apt/trusted.gpg.d/z/usr/share/keyringsz/usr/lib/apt/methods/httpsz /usr/sbin/update-ca-certificatesz//etc/apt/apt.conf.d/90ubuntu-advantage-aptproxyz*/var/lib/apt/periodic/update-success-stamp)xenialbionicfocaljammymanticz# Written by ubuntu-pro-client
Types: deb{deb_src}
URIs: {url}
Suites: {suites}
Components: main
Signed-By: {keyrings_dir}/{keyring_file}
zetc/apt/sources.listzvar/lib/dpkg/statuszetc/apt/apt.conf.dzetc/apt/preferences.dzvar/cache/apt/archives/partialzvar/lib/apt/lists/partial)filesfolders)g      ?g      @g      $@c                   @   s   e Zd Ze Ze ZdS )AptProxyScopeN)__name__
__module____qualname__objectGLOBALUACLIENT r#   r#   ./usr/lib/python3/dist-packages/uaclient/apt.pyr   b   s    
r   InstalledAptPackagenameversionarchc                    s   t   fdd}|S )z*Decorator ensuring apt_pkg is initialized.c                     s&   t jddkrt    | i |S )NDir )apt_pkgconfiggetinit)argskwargsfr#   r$   new_fp   s   z"ensure_apt_pkg_init.<locals>.new_f)r   )r2   r3   r#   r1   r$   ensure_apt_pkg_initm   s   r4   abc                 C   s   t | |S N)r+   version_compare)r5   r6   r#   r#   r$   r8   {   s   r8   c              
   C   s  |  d\}}tjdsdS z0t !}tjddd||||tj	|dgt
td W d   W dS 1 s8w   Y  W dS  tjyx } z,|jdkrlt|j }td	|r`tj| d
td|rltj| d
tjt|dd}~w tjy   tjt
|dw )a  Validate apt credentials for a PPA.

    @param repo_url: private-ppa url path
    @param username: PPA login username.
    @param password: PPA login password or resource token.

    @raises: UbuntuProError for invalid credentials, timeout or unexpected
        errors.
    ://z/usr/lib/apt/apt-helperNzdownload-filez{}://{}:{}@{}/pool/zapt-helper-output)timeoutretry_sleepsd   z401\s+unauthorized|httperror401)repozconnection timed outdetail)secondsr=   )splitospathexiststempfileTemporaryDirectoryr   subpformatjoinAPT_HELPER_TIMEOUTAPT_RETRIESr   ProcessExecutionError	exit_codestrstderrlowerresearchAPTInvalidCredentials
APTTimeoutAPTUnexpectedError
subprocessTimeoutExpiredAPTCommandTimeout)repo_urlusernamepasswordprotocol	repo_pathtmpderO   r#   r#   r$   assert_valid_apt_credentials   s>   

&
r`   	apt_errorreturnc                 C   sV   t  }|  dD ]}|r(td|}|r(d| d dd  }|| q
|S )aA  Parse apt update errors for invalid apt config in user machine.

    This functions parses apt update errors regarding the presence of
    invalid apt config in the system, for example, a ppa that cannot be
    reached, for example.

    In that scenario, apt will output a message in the following formats:

    The repository 'ppa 404 Release' ...
    Failed to fetch ppa 404 ...

    On some releases, both of these errors will be present in the apt error
    message.

    :param apt_error: The apt error string
    :return: a NamedMessage containing the error message
    
z2(Failed to fetch |The repository .)(?P<url>[^\s]+)z- urlz/distsr   )setstriprA   rQ   rR   	groupdictadd)ra   failed_reposlinepattern_matchrepo_url_matchr#   r#   r$   (_parse_apt_update_for_invalid_apt_config   s   
rm   cmd	error_msgoverride_env_varsc              
   C   s   zt j| dt|d\}}W |S  tjyG } z,dt|jv r"t 	 t|j}|r5tj	d
t|d|r9|nt|}tj|dd}~ww )a  Run an apt command, retrying upon failure APT_RETRIES times.

    :param cmd: List containing the apt command to run, passed to subp.
    :param error_msg: The string to raise as UbuntuProError when all retries
       are exhausted in failure.
    :param override_env_vars: Passed directly as subp's override_env_vars arg

    :return: stdout from successful run of the apt command.
    :raise UbuntuProError: on issues running apt-cache policy.
    T)capturer;   rp   z%Could not get lock /var/lib/dpkg/lockrc   )ri   r>   N)r   rG   rK   r   rL   rN   rO   APTProcessConflictErrorrm   APTInvalidRepoErrorrI   sortedrU   )rn   ro   rp   out_errr_   ri   msgr#   r#   r$   run_apt_command   s*   
rx   )maxsizec                 C   s   t ddg| |dS )N	apt-cachepolicy)rn   ro   rp   )rx   )ro   rp   r#   r#   r$   get_apt_cache_policy   s
   r|   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )PreserveAptCfgc                 C   s   || _ i | _d S r7   )apt_funccurrent_apt_cfg)selfr~   r#   r#   r$   __init__  s   
zPreserveAptCfg.__init__c                    s&   t j  fdd  D | _|  S )Nc                    s   i | ]}|t  |qS r#   )copydeepcopyr-   ).0keycfgr#   r$   
<dictcomp>  s    z,PreserveAptCfg.__enter__.<locals>.<dictcomp>)r+   r,   keysr   r~   )r   r#   r   r$   	__enter__
  s
   
zPreserveAptCfg.__enter__c                 C   s4   t j}| j D ]}||| j|  qt   d S r7   )r+   r,   r   r   re   init_system)r   typevalue	tracebackr   r   r#   r#   r$   __exit__  s   zPreserveAptCfg.__exit__N)r   r   r   r   r   r   r#   r#   r#   r$   r}     s    r}   c                  C   s.   t j D ]} t j|  qt   t d S r7   )r+   r,   r   clearr.   Cacher   r#   r#   r$   get_apt_pkg_cache  s   
r   c                  C   sb   z%t j D ]} td| st j|  qt jdt t   t 	d W S  t
y0   i  Y S w )Nz^Acquirer)   )r+   r,   r   rQ   rR   r   re   r   r.   r   	Exceptionr   r#   r#   r$   get_esm_apt_pkg_cache#  s   r   pkg_namec              	   C   sh   t t}z||  }W n ty   Y W d    d S w W d    n1 s&w   Y  |jr2|jjS d S r7   )r}   r   KeyErrorcurrent_verver_str)r   cachepackager#   r#   r$   get_pkg_version6  s   
r   Fcheck_esm_cachec              	   C   sT  t t7}z||  }W n ty   Y W d    d S w t|}||}|s1	 W d    d S |j}W d    n1 s>w   Y  |sG|S t tU}|rz||  }W n tyf   | Y W  d    S w t|}	|	|}
|
s||W  d    S |
j}t||dkr|W  d    S W d    |S W d    |S 1 sw   Y  |S )Nr   )	r}   r   r   r+   DepCacheget_candidate_verr   r   r8   )r   r   r   r   	dep_cache	candidatecandidate_version	esm_cacheesm_packageesm_dep_cacheesm_candidateesm_candidate_versionr#   r#   r$   get_pkg_candidate_versionC  sZ   







r   c              
   C   s   zDz
t ddg| d}W n2 tjy   t  tjy* } ztj|jdd }~w tjy= } ztj|jdd }~ww W t	
  |S t	
  w )Napt-getupdatern   rp   repo_msgr>   )rx   r   rr   APTUpdateProcessConflictErrorrs   APTUpdateInvalidRepoErrorrw   UbuntuProErrorAPTUpdateFailedr|   cache_clear)rp   ru   r_   r#   r#   r$   run_apt_update_commando  s"   

r   sources_list_pathc                 C   s*  t t}tjdtj|  tjdd tjdd t }|	  t
 }tjtjdd}t|}zDz| |||d W d    n1 sOw   Y  W n" ttd	d
yd   t  tyw } ztjt|dd }~ww W t  nt  w W d    d S 1 sw   Y  d S )NzDir::Etc::sourcelistzDir::Etc::sourcepartszN.O.F.O.L.D.E.RzAPT::List-Cleanup0zDir::State::Listslockr   Errorr#   r>   )r}   r   r+   r,   re   rB   rC   abspath
SourceListread_main_listr   rI   find_dirFileLockr   getattrr   rr   SystemErrorr   rN   r|   r   )r   r   sources_listfetch_progress	lock_filer   r_   r#   r#   r$   update_sources_list  s6   

"r   packagesapt_optionsc              
   C   sj   |d u rg }zt g d| |  |d}W |S  tjy!   t  tjy4 } ztj|jdd }~ww )N)r   install--assume-yesr   r   )rx   r   rr   APTInstallProcessConflictErrorrs   APTInstallInvalidRepoErrorrw   )r   r   rp   ru   r_   r#   r#   r$   run_apt_install_command  s$   r   originc                 C   sv   t  }tt(}|jD ]}|j}|r$|jD ]\}}|j| kr#|| qqW d    t|S 1 s2w   Y  t|S r7   )	re   r}   r   r   r   	file_listr   rh   list)r   resultr   r   installed_versionfile_r#   r#   r$    get_installed_packages_by_origin  s   




r   c           	      C   s   | d u rg S t  }tt8}t|}|jD ]$}|j}|r:||}|r:||kr:|jD ]\}}|j	| kr9|
| q+qW d    t|S 1 sHw   Y  t|S r7   )re   r}   r   r+   r   r   r   r   r   r   rh   r   )	r   r   r   r   r   r   r   r   r   r#   r#   r$   ;get_installed_packages_with_uninstalled_candidate_in_origin  s&   






r   r   exclude_originc                    s6   g }| j D ]} fdd|jD }|r|| q|S )Nc                    s(   g | ]\}}|j d kr|j kr|qS )now)	componentr   )r   r   r   r   r#   r$   
<listcomp>  s
    z3get_remote_versions_for_package.<locals>.<listcomp>)version_listr   append)r   r   valid_versionsr'   valid_originsr#   r   r$   get_remote_versions_for_package  s   


r   suitesseriesupdates_enabledrY   c                 C   sR   d}| D ]"}||vrqd}d|v r|st d|| d}|dj|||d7 }q|S )Nr*   -updatesz?Not enabling apt suite "%s" because "%s-updates" is not enabledz# zC{maybe_comment}deb {url} {suite} main
# deb-src {url} {suite} main
)maybe_commentrd   suite)LOGwarningrH   )r   r   r   rY   contentr   r   r#   r#   r$   _get_list_file_content  s&   r   keyring_fileinclude_deb_srcc           	         sZ    fdd| D }|st d  dd |D }|rdnd}tj|d|t||d}|S )	Nc                    s   g | ]} |v r|qS r#   r#   r   r   r   r#   r$   r          z-_get_sources_file_content.<locals>.<listcomp>zPNot enabling service-related -updates suites because "%s-updates" is not enabledc                 S   s   g | ]}d |vr|qS )r   r#   r   r#   r#   r$   r   '  s    z deb-srcr*    )rd   r   keyrings_dirr   deb_src)r   r   DEB822_REPO_FILE_CONTENTrH   rI   KEYRINGS_DIR)	r   r   r   rY   r   r   appliable_suitesr   r   r#   r   r$   _get_sources_file_content  s$   r   repo_filenamecredentialsc                 C   s  z	| d\}}W n ty   d}|}Y nw tj| t j}|dr-|dd }t	||| d}t
ddgtj}	|	 D ]}
d	||
vrKqAd
|
vrPqAd} t||| |tv rytjt|}tjt|}t|| t||||}nt|||||}t| | dS )zAdd an authenticated apt repo and credentials to the system.

    @raises: InvalidAPTCredentialsError when the token provided can't access
        the repo PPA.
    :bearer/NFrz   r{   za={}-updatesz	o=Ubuntu,T)rA   
ValueErrorr   secrets
add_secretr   get_release_infor   endswithr`   rx   r   APT_POLICY_FAILED
splitlinesrH   add_apt_auth_conf_entrySERIES_NOT_USING_DEB822rB   rC   rI   r   APT_KEYS_DIRr   export_gpg_keyr   r   
write_file)r   rY   r   r   r   rZ   r[   r   r   r{   rj   source_keyring_filedestination_keyring_filer   r#   r#   r$   add_auth_apt_repo8  sD   



r  c                 C   s   t  }| d\}}|ds|d7 }tj|rt|}nd}dj|||t	d}d}g }	|
 D ].}
|s[|
 }t|dkr[|d }||krP|	| d	}q2||v r[|	| d	}|	|
 q2|sh|	| |	d tj|d
|	dd dS )zBAdd or replace an apt auth line in apt's auth.conf file or conf.d.r9   r   r*   z:machine {repo_path} login {login} password {password}{cmt})r]   loginr[   cmtF      Trc     modeN)!get_apt_auth_file_from_apt_configrA   r   rB   rC   rD   r   	load_filerH   APT_AUTH_COMMENTr   lenr   r  rI   )rY   r  r[   apt_auth_file	_protocolr]   orig_contentrepo_auth_lineadded_new_auth	new_linesrj   
split_linecurr_line_repor#   r#   r$   r   p  sD   
	



r   c                    s   |  d\}}|dr|dd }t }tj|rGt|}dj|d d	 fdd	|
 D }|s=t| dS tj||d
d dS dS )z+Remove a repo from the shared apt auth filer9   r   Nr   zmachine {repo_path}/ login)r]   rc   c                    s   g | ]} |vr|qS r#   r#   )r   rj   auth_prefixr#   r$   r     r   z2remove_repo_from_apt_auth_file.<locals>.<listcomp>r  r  )rA   r   r  rB   rC   rD   r   r  rH   rI   r   ensure_file_absentr  )rY   r  r]   r  apt_authr   r#   r  r$   remove_repo_from_apt_auth_file  s   

r  c                 C   sN   t |  | drt t| d |r!tjt|}t | t	| dS )z>Remove an authenticated apt repo and credentials to the systemz.sourcesr   N)
r   r  r   r   set_filename_extensionrB   rC   rI   r  r  )r   rY   r   r#   r#   r$   remove_auth_apt_repo  s   



r!  c                 C   sB   | d\}}|dr|dd }dj||d}t| | dS )z.Add an apt preferences file and pin for a PPA.r9   r   Nr   z<Package: *
Pin: release o={origin}
Pin-Priority: {priority}
)r   priority)rA   r   rH   r   r  )apt_preference_filerY   r   r"  r  r]   r   r#   r#   r$   add_ppa_pinning  s   
r$  c                  C   sV   t dddtg\} }| r| dd d S t dddtg\} }| dd dS )z7Return to patch to the system configured APT auth file.
apt-configshellr   'r  z90ubuntu-advantager   )r   rG   APT_CONFIG_AUTH_PARTS_DIRrA   APT_CONFIG_AUTH_FILErstrip)ru   rv   r#   r#   r$   r    s   

r  c              
   C   s~   |  d\}}|dr|dd }d}tdddtg\}}|r(| d	d
 }|dd}tttj	
||d| S )zEList any apt files in APT_CONFIG_LISTS_DIR given repo_url and series.r9   r   Nr   z/var/lib/apt/listsr%  r&  r   r'  r  r   z
_dists_{}*)rA   r   r   rG   APT_CONFIG_LISTS_DIRreplacert   globrB   rC   rI   rH   )rY   r   r  r]   	lists_dirru   rv   aptlist_filenamer#   r#   r$   find_apt_list_files  s"   

r0  c                 C   s   t | |D ]}t| qdS )z?Remove any apt list files present for this repo_url and series.N)r0  r   r  )rY   r   rC   r#   r#   r$   remove_apt_list_files  s   r1  pkgc                 C   s
   | t  v S r7   )get_installed_packages_names)r2  r#   r#   r$   is_installed  s   
r4  c               	   C   sb   g } t t!}|jD ]}|j}|r| t|j|j|jd q
W d    | S 1 s*w   Y  | S )N)r&   r'   r(   )	r}   r   r   r   r   r%   r&   r   r(   )	installedr   r   r   r#   r#   r$   get_installed_packages  s$   


r6  c                  C   s   t  } dd | D }|S )Nc                 S   s   g | ]}|j qS r#   )r&   )r   r2  r#   r#   r$   r     s    z0get_installed_packages_names.<locals>.<listcomp>)r6  )package_list	pkg_namesr#   r#   r$   r3  
  s   r3  
http_proxyhttps_proxyproxy_scopec                 C   s   | s|r!|r!d}|t jkrd}n|t jkrd}ttjj|d d}| r@|t jkr3|tj| d7 }n|t jkr@|t	j| d7 }|r]|t jkrP|t
j|d7 }n|t jkr]|tj|d7 }|dkret| }|dkrptt dS tt| dS )a  
    Writes an apt conf file that configures apt to use the proxies provided as
    args.
    If both args are None, then no apt conf file is written. If this function
    previously wrote a conf file, and was run again with both args as None,
    the existing file is removed.

    :param http_proxy: the url of the http proxy apt should use, or None
    :param https_proxy: the url of the https proxy apt should use, or None
    :return: None
    r*   z	UA-scopedglobal)scope)	proxy_urlN)r   r"   r!   eventinfor   SETTING_SERVICE_PROXY_SCOPErH   APT_CONFIG_UA_PROXY_HTTPAPT_CONFIG_GLOBAL_PROXY_HTTPAPT_CONFIG_UA_PROXY_HTTPSAPT_CONFIG_GLOBAL_PROXY_HTTPSAPT_PROXY_CONFIG_HEADERr   r  APT_PROXY_CONF_FILEr  )r9  r:  r;  messageapt_proxy_configr#   r#   r$   setup_apt_proxy  sD   







rJ  c                  C   s    d } t jtrt tj} | S r7   )rB   rC   rD   APT_UPDATE_SUCCESS_STAMP_PATHstatst_mtime
cache_timer#   r#   r$   get_apt_cache_timeH  s   rP  c                  C   s$   t  } | d u r	d S tj| tjjS r7   )rP  datetimefromtimestamptimezoneutcrN  r#   r#   r$   get_apt_cache_datetimeO  s   rU  c                     s   t j tjtddd td td  } t fdd| D r!d S tt td D ]}t	| q*td D ]
}tj
|ddd	 q6d S )
Nz**/*T)	recursiver   r   c                 3   s    | ]}| v V  qd S r7   r#   )r   r   existing_filesr#   r$   	<genexpr>^  s    z._ensure_esm_cache_structure.<locals>.<genexpr>i  )exist_okr  )r-  rB   rC   rI   r   ESM_BASIC_FILE_STRUCTUREallr   ensure_folder_absentcreate_filemakedirs)desired_filesr   folderr#   rW  r$   _ensure_esm_cache_structureV  s   
rb  c                 C   s  t  sd S t  ddlm} ddlm} ddlm}m	} d}d}t
 }|d u r/|| d }|dg D ]"}|ddd	krG|d
ddk}|dddkrW|d
ddk}q5|| }	|rl|	 d |jkrl|	  n|	  t  r|| }
|r|
 d |jkr|
  n|
  ttJ}t }|  G dd dt}| }z	|||d W n ty } ztdt| W Y d }~nd }~ww W d    d S W d    d S 1 sw   Y  d S )Nr   )status)ApplicationStatus)ESMAppsEntitlementESMInfraEntitlementFservicesr&   r*   zesm-apps	availablenoyesz	esm-infrac                   @   s,   e Zd ZdejfddZdejfddZdS )z-update_esm_caches.<locals>.EsmAcquireProgressitemc                 S      t d|j d S )NzFetched ESM Apt Cache item: {})r   debugrH   urir   rk  r#   r#   r$   done  s   z2update_esm_caches.<locals>.EsmAcquireProgress.donec                 S   rl  )Nz&Failed to fetch ESM Apt Cache item: {})r   r   rH   rn  ro  r#   r#   r$   fail  s   
z2update_esm_caches.<locals>.EsmAcquireProgress.failN)r   r   r   r+   AcquireItemDescrp  rq  r#   r#   r#   r$   EsmAcquireProgress  s    rs  z%Failed to fetch the ESM Apt Cache: {})r   is_current_series_ltsrb  uaclient.actionsrc  (uaclient.entitlements.entitlement_statusrd  uaclient.entitlements.esmre  rf  r   readr-   application_statusDISABLEDsetup_local_esm_repodisable_local_esm_repois_current_series_active_esmr}   r   r+   r   r   r   r   r   r   r   rH   rN   )r   rc  rd  re  rf  apps_availableinfra_availablecurrent_statusserviceappsinfrar   r   rs  r   r_   r#   r#   r$   update_esm_cachesk  sZ   


	 "r  package_nameserror_messagec                 C   "   t g dt|  |ddid dS )a  
    Remove APT packages from the system.

    Setting DEBIAN_FRONTEND to noninteractive makes sure no prompts will
    appear during the operation. In this case, --force-confdef will
    automatically pick the default option when some debconf should appear.
    In the absence of a default option, --force-confold will automatically
    choose to keep the old configuration file.
    )r   remover   $-o Dpkg::Options::="--force-confdef"$-o Dpkg::Options::="--force-confold"DEBIAN_FRONTENDnoninteractiverp   Nrx   r   r  r  r#   r#   r$   remove_packages     

r  c                 C   r  )a  
    Purge APT packages from the system - remove everything.

    Setting DEBIAN_FRONTEND to noninteractive makes sure no prompts will
    appear during the operation. In this case, --force-confdef will
    automatically pick the default option when some debconf should appear.
    In the absence of a default option, --force-confold will automatically
    choose to keep the old configuration file.
    )r   purger   r  r  r  r  r  Nr  r  r#   r#   r$   purge_packages  r  r  c                 C   s   t | g dddid dS )a6  
    Install packages, allowing downgrades.

    The --allow downgrades flag is needed because sometimes we need to
    reinstall the packages to a lower version (passed in the package_name
    string, as package=version).

    Setting DEBIAN_FRONTEND to noninteractive makes sure no prompts will
    appear during the operation. In this case, --force-confdef will
    automatically pick the default option when some debconf should appear.
    In the absence of a default option, --force-confold will automatically
    choose to keep the old configuration file.
    )z--allow-downgradesr  r  r  r  )r   rp   N)r   )r  r#   r#   r$   reinstall_packages  s
   
r  c                  C   s*   t j D ]} t j|  qt   t jS r7   )r+   r,   r   r   init_configr   r#   r#   r$   _get_apt_config  s   r  c                 C   s8   t t}|| }W d    |S 1 sw   Y  |S r7   )r}   r  r   )base_keyapt_cfgapt_cfg_keysr#   r#   r$   get_apt_config_keys  s   

r  	cfg_namesc                 C   sd   i }t t"}| D ]}||}t|s||pd}|||< q	W d   |S 1 s+w   Y  |S )z
    Get all APT configuration values for the given config names. If
    one of the config names is not present on the APT config, that
    config name will have a value of None
    N)r}   r  r-   rN   
value_list)r  apt_cfg_dictr  cfg_name	cfg_valuer#   r#   r$   get_apt_config_values
  s   



		r  c                  C   s   d} d}t j|r|S | S )Nz/etc/apt/sources.listz&/etc/apt/sources.list.d/ubuntu.sources)rB   rC   rD   )old_sources_pathnew_sources_pathr#   r#   r$   get_system_sources_file   s   
r  )NN)Fr7   )rb   N)vr   rQ  enumr-  loggingrB   rQ   rV   rE   	functoolsr   r   typingr   r   r   r   r   r	   r
   r+   apt.progress.baser   uaclientr   r   r   r   r   r   r   uaclient.defaultsr   uaclient.files.state_filesr   rJ   r  r)  r(  r+  rF  rC  rE  rB  rD  r  r   APT_METHOD_HTTPS_FILECA_CERTIFICATES_FILErG  rK  r  r   rC   rI   r[  rK   get_event_loggerr?  	getLoggerreplace_top_level_logger_namer   r   uniqueEnumr   rN   r%   r4   r8   r`   rm   rx   r|   r}   r   r   r   boolr   r   retryrr   r   r   r   Packager   r   Versionr   r   r   r  r   r  r!  r$  r  r0  r1  r4  r6  r3  r!   rJ  floatrP  rU  rb  r  r  r  r  r  r  r  r  r#   r#   r#   r$   <module>   s   $$	)
(
*
-

'




 
 
8,

8
E

