Your IP : 216.73.216.14


Current Path : /usr/lib/python3/dist-packages/uaclient/__pycache__/
Upload File :
Current File : //usr/lib/python3/dist-packages/uaclient/__pycache__/apt.cpython-38.pyc

U

8�-d�f�@s�ddlZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZddlm
Z
mZmZmZmZmZddlZddlZddlmZmZmZmZmZddlmZdZdZdZd	Z d
Z!dZ"dZ#d
Z$dZ%dZ&dZ'dZ(dZ)dZ*dZ+dZ,dddgZ-e�.�Z/ej0Gdd�dej1��Z2edde3fde3fde3fg�Z4dd �Z5e3eej6d!�d"d#�Z7difee3ee3ee
e3e3fe3d$�d%d&�Z8edd'�difee3ee
e3e3fe3d(�d)d*��Z9Gd+d,�d,�Z:d-d.�Z;d/d0�Z<e3ee3d1�d2d3�Z=dife3ee3ee
e3e3fe3d4�d5d6�Z>ifee
e3e3fe3d7�d8d9�Z?ddifee3eee3ee3ee
e3e3fe3d:�d;d<�Z@e3e3e3ee3e3dd=�d>d?�ZAd@dA�ZBdBdC�ZCdqe3e3ee3ddD�dEdF�ZDdGdH�ZEdIdJ�ZFdKdL�ZGdMdN�ZHddO�dPdQ�ZIe3eJd1�dRdS�ZKee4dT�dUdV�ZLdreJee3dX�dYdZ�ZMdde2jNfee3ee3ee2dd[�d\d]�ZOe3e3e3eJd^�d_d`�ZPeeQdT�dadb�ZReejdT�dcdd�ZSddT�dedf�ZTee3e3dg�dhdi�ZUdjdk�ZVdldm�ZWee3e
e3ee3ee3ffdn�dodp�ZXdS)s�N)�	lru_cache)�Dict�Iterable�List�
NamedTuple�Optional�Union)�event_logger�
exceptions�gpg�messages�system)�ESM_APT_ROOTDIRgN@z  # ubuntu-advantage-toolszDir::Etc::netrc/zDir::Etc::netrcparts/zDir::State::lists/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-stampaL# Written by ubuntu-advantage-tools

deb https://esm.ubuntu.com/{name}/ubuntu {series}-{name}-security main
# deb-src https://esm.ubuntu.com/{name}/ubuntu {series}-{name}-security main

deb https://esm.ubuntu.com/{name}/ubuntu {series}-{name}-updates main
# deb-src https://esm.ubuntu.com/{name}/ubuntu {series}-{name}-updates main
g�?g@g$@c@seZdZe�Ze�ZdS)�
AptProxyScopeN)�__name__�
__module__�__qualname__�object�GLOBAL�UACLIENT�rr�./usr/lib/python3/dist-packages/uaclient/apt.pyr=sr�InstalledAptPackages�name�version�archc
Cs|�d�\}}tj�d�sdSzFt���4}tjddd�||||�tj�	|d�gt
td�W5QRXWn�tj
k
r�}z`|jdkr�t|j���}t�d	|�r�t�d
�|���nt�d|�r�t�d�|���t�d
��W5d}~XYn*tjk
�rt�d�t
|���YnXdS)aValidate 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: UserFacingError for invalid credentials, timeout or unexpected
        errors.
    �://z/usr/lib/apt/apt-helperNz
download-filez{}://{}:{}@{}/ubuntu/pool/zapt-helper-output)Ztimeout�retry_sleeps�dz401\s+unauthorized|httperror401z'Invalid APT credentials provided for {}zconnection timed outz-Timeout trying to access APT repository at {}z7Unexpected APT error. See /var/log/ubuntu-advantage.logzVCannot validate credentials for APT repo. Timeout after {} seconds trying to reach {}.)�split�os�path�exists�tempfileZTemporaryDirectoryr
�subp�format�join�APT_HELPER_TIMEOUT�APT_RETRIESr
�ProcessExecutionErrorZ	exit_code�str�stderr�lower�re�search�UserFacingError�
subprocessZTimeoutExpired)�repo_url�username�passwordZprotocol�	repo_pathZtmpd�er+rrr�assert_valid_apt_credentialsHsT

���
������r6)�	apt_error�returncCs�d}t�}|���d�D]<}|rt�d|�}|rd|��d�d�d}|�|�q|r�tjj	t
|�dkrpd	nd
d�t|��d�}|S)aAParse 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
    N�
z2(Failed to fetch |The repository .)(?P<url>[^\s]+)z- �urlz/distsr��s�)Zplural�failed_repos)
�set�striprr-r.�	groupdict�addrZAPT_UPDATE_INVALID_URL_CONFIGr%�lenr&�sorted)r7�	error_msgr>�lineZ
pattern_matchZrepo_url_matchrrr�(_parse_apt_update_for_invalid_apt_config|s$���rG)�cmdrE�envr8c
Cs�ztj|dt|d�\}}Wnrtjk
r�}zRdt|j�krFt���nt|j�}|rbtj	|j
d��|rj|nt|�}t�|��W5d}~XYnX|S)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 UserFacingError when all retries
       are exhausted in failure.
    :param env: Optional dictionary of environment variables to pass to subp.

    :return: stdout from successful run of the apt command.
    :raise UserFacingError: on issues running apt-cache policy.
    T)ZcapturerrIz%Could not get lock /var/lib/dpkg/lock)rEN)r
r$r(r
r)r*r+�APTProcessConflictErrorrG�APTInvalidRepoError�msgr/)rHrErI�out�_errr5Zrepo_error_msgrLrrr�run_apt_command�s$�

�rO)�maxsize)rErIr8cCstddg||d�S�N�	apt-cache�policy�rHrErI�rO)rErIrrr�get_apt_cache_policy�s
�rVc@s$eZdZdd�Zdd�Zdd�ZdS)�PreserveAptCfgcCs||_i|_dS�N)�apt_func�current_apt_cfg)�selfrYrrr�__init__�szPreserveAptCfg.__init__cs&tj��fdd����D�|_|��S)Ncsi|]}|t���|���qSr)�copy�deepcopy�get)�.0�key��cfgrr�
<dictcomp>�sz,PreserveAptCfg.__enter__.<locals>.<dictcomp>)�apt_pkg�config�keysrZrY)r[rrbr�	__enter__�s

�zPreserveAptCfg.__enter__cCs4tj}|j��D]}|�||j|�qt��dSrX)rerfrZrgr?Zinit_system)r[�type�value�	tracebackrcrarrr�__exit__�szPreserveAptCfg.__exit__N)rrrr\rhrlrrrrrW�srWcCs0tj��D]}tj�|�q
t��t��}|SrX)rerfrg�clear�init�apt�Cache�ra�cacherrr�
get_apt_cache�s
rscCslzNtj��D]}t�d|�stj�|�qtj�dt�t��t	j
td�}Wntk
rfi}YnX|S)Nz^AcquireZDir)Zrootdir)rerfrgr-r.rmr?rrnrorp�	Exceptionrqrrr�
get_esm_cache�s
ru)�pkgr8c
Cstt��T}z||}Wn tk
r6YW5QR�dSX|jsLW5QR�dSt|jd�}W5QRX|sjdStt���}|�rz||}Wn$tk
r�|YW5QR�SX|js�|W5QR�St|jd�}|s�|W5QR�St||d��r|W5QR�SW5QRX|S)Nr�ge)rWrsrt�	candidate�getattrru�compare_versions)rvrr�packageZ
pkg_candidateZ	esm_cacheZesm_packageZesm_pkg_candidaterrr�get_pkg_candidate_versions0

r|)r{rErIr8cCstdd|g||d�SrQrU)r{rErIrrr� get_apt_cache_policy_for_package/s
�r})rIr8c
Cs�z�ztddg|d�}Wn�tjk
r4t���Ynttjk
rd}ztj|jd��W5d}~XYnDtj	k
r�}z$tj	t
jjd|jt
jjd��W5d}~XYnXW5t��X|S)N�apt-get�update)rHrI)�repo_msgr9)rLZmsg_code)
rV�cache_clearrOr
rJZAPTUpdateProcessConflictErrorrKZAPTUpdateInvalidRepoErrorrLr/rZAPT_UPDATE_FAILEDr)rIrMr5rrr�run_apt_update_command9s�
r�)�packages�apt_optionsrErIr8c
Cs�|dkrg}z tdddg||||d�}WnTtjk
rNtj|d��Yn4tjk
r�}ztj|j|d��W5d}~XYnX|S)Nr~�install�--assume-yesrT)�
header_msg)r�r�)rOr
rJZAPTInstallProcessConflictErrorrKZAPTInstallInvalidRepoErrorrL)r�r�rErIrMr5rrr�run_apt_install_commandNs(���
�r�)�
repo_filenamer1�credentials�suites�keyring_filer8cCs6z|�d�\}}Wntk
r.d}|}YnXt��d}|�d�rR|dd�}t|||�d}tdd	gtjj	�}	|	�
�D]&}
d
�|�|
kr�q|d|
kr�q|d}q�q|d
}|D]D}||kr�q�d
}
d|kr�|s�t�
d||�d}
|dj|
||d�7}q�t�||�t|||�tj�t|�}tj�t|�}t�||�dS)z�Add an authenticated apt repo and credentials to the system.

    @raises: InvalidAPTCredentialsError when the token provided can't access
        the repo PPA.
    �:Zbearer�series�/N���FrRrSza={}-updatesz	o=Ubuntu,Tr=z-updatesz?Not enabling apt suite "%s" because "%s-updates" is not enabledz# zQ{maybe_comment}deb {url}/ubuntu {suite} main
# deb-src {url}/ubuntu {suite} main
)�
maybe_commentr:�suite)r�
ValueErrorr
Zget_platform_info�endswithr6rOrZAPT_POLICY_FAILEDrL�
splitlinesr%�logging�debug�
write_file�add_apt_auth_conf_entryr r!r&�KEYRINGS_DIR�APT_KEYS_DIRrZexport_gpg_key)r�r1r�r�r�r2r3r�Zupdates_enabledrSrF�contentr�r�Zsource_keyring_fileZdestination_keyring_filerrr�add_auth_apt_repoisZ

����r�c
Cs�t�}|�d�\}}|�d�s&|d7}tj�|�r>t�|�}nd}dj|||t	d�}d}g}	|�
�D]\}
|s�|
��}t|�dkr�|d}||kr�|	�|�d	}qd||kr�|	�|�d	}|	�|
�qd|s�|	�|�|	�d�tj
|d
�|	�dd�d
S)zBAdd or replace an apt auth line in apt's auth.conf file or conf.d.rr�r=z:machine {repo_path} login {login} password {password}{cmt})r4�loginr3ZcmtF�r;Tr9���modeN)�!get_apt_auth_file_from_apt_configrr�r r!r"r
�	load_filer%�APT_AUTH_COMMENTr�rC�appendr�r&)
r1r�r3�
apt_auth_file�	_protocolr4Zorig_contentZrepo_auth_lineZadded_new_authZ	new_linesrFZ
split_lineZcurr_line_reporrrr��sB
��	



r�cs�|�d�\}}|�d�r$|dd�}t�}tj�|�r�t�|�}dj|d��d�	�fdd	�|�
�D��}|sxt�|�ntj||d
d�dS)z+Remove a repo from the shared apt auth filerr�Nr�zmachine {repo_path}/ login)r4r9csg|]}�|kr|�qSrr)r`rF�Zauth_prefixrr�
<listcomp>�sz2remove_repo_from_apt_auth_file.<locals>.<listcomp>r�r�)
rr�r�r r!r"r
r�r%r&r��ensure_file_absentr�)r1r�r4r�Zapt_authr�rr�r�remove_repo_from_apt_auth_file�s

�r�)r�r1r�r8cCs2t�|�|r&tj�t|�}t�|�t|�dS)z>Remove an authenticated apt repo and credentials to the systemN)r
r�r r!r&r�r�)r�r1r�rrr�remove_auth_apt_repo�s


r�cCsB|�d�\}}|�d�r$|dd�}dj||d�}t�||�dS)z.Add an apt preferences file and pin for a PPA.rr�Nr�z<Package: *
Pin: release o={origin}
Pin-Priority: {priority}
)�origin�priority)rr�r%r
r�)Zapt_preference_filer1r�r�r�r4r�rrr�add_ppa_pinning�s
��r�cCsZt�dddtg�\}}|r,|�d�ddSt�dddtg�\}}|�d�d�d�SdS)	z7Return to patch to the system configured APT auth file.�
apt-config�shellra�'r;z90ubuntu-advantager�N)r
r$�APT_CONFIG_AUTH_PARTS_DIRr�APT_CONFIG_AUTH_FILE�rstrip)rMrNrrrr��s
�
�r�c
Cs~|�d�\}}|�d�r$|dd�}d}t�dddtg�\}}|rP|�d	�d
}|�dd�}tt�tj	�
||d�|����S)
zEList any apt files in APT_CONFIG_LISTS_DIR given repo_url and series.rr�Nr�z/var/lib/apt/listsr�r�rar�r;�_z
_dists_{}*)rr�r
r$�APT_CONFIG_LISTS_DIR�replacerD�globr r!r&r%)r1r�r�r4Z	lists_dirrMrNZaptlist_filenamerrr�find_apt_list_filess$

����r�cCst||�D]}t�|�q
dS)z?Remove any apt list files present for this repo_url and series.N)r�r
r�)r1r�r!rrr�remove_apt_list_files!sr�)�
_entitlementscCs�ddlm}|dkr$ddlm}|}|jD]�}t||�s:q*|jj|jd�}|j	j|jd�}t
j�|�r�t
jd�|�tjd�t�|�t
j�|�r*t
jd�|�tjd�t�|�q*dS)	a
    Clean apt files written by uaclient

    :param _entitlements:
        The uaclient.entitlements module to use, defaults to
        uaclient.entitlements. (This is only present for testing, because the
        import happens within the function to avoid circular imports.)
    r)�RepoEntitlementN)�entitlements�rzRemoving apt source file: {})Z	file_typez!Removing apt preferences file: {})Zuaclient.entitlements.repor��uaclientr�ZENTITLEMENT_CLASSES�
issubclassZrepo_list_file_tmplr%rZrepo_pref_file_tmplr r!r"�event�info�sysr+r
r�)r�r�Z__entitlementsZent_clsZ	repo_fileZ	pref_filerrr�clean_apt_files's*	

�
�r�cCs
|t�kSrX)�get_installed_packages_names)rvrrr�is_installedJsr�)r8cCs2t�dddg�\}}|��dd�}dd�|D�S)Nro�listz--installedr;cSs8g|]0}t|�d�d|�d�d|�d�dd��qS)r�r� r;r�)rrr)rr)r`�entryrrrr�Qs��z*get_installed_packages.<locals>.<listcomp>)r
r$r�)rMr��package_listrrr�get_installed_packagesNs
�r�F)�include_versionsr8cCst�}dd�|D�}|S)NcSsg|]
}|j�qSrr�)r`rvrrrr�]sz0get_installed_packages_names.<locals>.<listcomp>)r�)r�r�Z	pkg_namesrrrr�[sr�)�
http_proxy�https_proxy�proxy_scoper8cCs�|s|rB|rBd}|tjkr d}n|tjkr.d}t�tjj|d��d}|r�|tjkrf|tj|d�7}n|tjkr�|t	j|d�7}|r�|tjkr�|t
j|d�7}n|tjkr�|tj|d�7}|dkr�tj|}|dkr�t
�t�nt
�t|�dS)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)Zscope)Z	proxy_urlN)rrrr�r�rZSETTING_SERVICE_PROXY_SCOPEr%�APT_CONFIG_UA_PROXY_HTTP�APT_CONFIG_GLOBAL_PROXY_HTTP�APT_CONFIG_UA_PROXY_HTTPS�APT_CONFIG_GLOBAL_PROXY_HTTPSZAPT_PROXY_CONFIG_HEADERr
r��APT_PROXY_CONF_FILEr�)r�r�r��messageZapt_proxy_configrrr�setup_apt_proxyasD

�
�

�
�

�
r�)�version1�version2�relationr8cCs8zt�dd|||g�WdStjk
r2YdSXdS)zCReturn True comparing version1 to version2 with the given relation.Zdpkgz--compare-versionsTFN)r
r$r
r))r�r�r�rrrrz�s�rzcCs d}tj�t�rt�t�j}|SrX)r r!r"�APT_UPDATE_SUCCESS_STAMP_PATH�stat�st_mtime�Z
cache_timerrr�get_apt_cache_time�sr�cCs$t�}|dkrdStj�|tjj�SrX)r��datetimeZ
fromtimestamp�timezoneZutcr�rrr�get_apt_cache_datetime�sr�cCsVt��sdSddlm}ddlm}ddlm}m}d}d}|�	d�}|dkrZ||�d}|�
dg�D]D}|�
dd	�d
kr�|�
dd�d
k}|�
dd	�dkrf|�
dd�d
k}qf||�}	|r�|	��d|jkr�|	�
�n|	��t���r||�}
|�r|
��d|jk�r|
�
�n|
��t�}z|��Wn$tjjk
�rPt�d�YnXdS)Nr)�status)�ApplicationStatus)�ESMAppsEntitlement�ESMInfraEntitlementFzstatus-cache�servicesrr=zesm-appsZ	availableZnoZyesz	esm-infraz!Failed to fetch the ESM Apt Cache)r
Zis_current_series_ltsZuaclient.actionsr�Z(uaclient.entitlements.entitlement_statusr�Zuaclient.entitlements.esmr�r�Z
read_cacher_Zapplication_statusZDISABLEDZsetup_local_esm_repoZdisable_local_esm_repoZis_current_series_active_esmrurrorrZFetchFailedExceptionr�Zwarning)rcr�r�r�r�Zapps_availableZinfra_availableZcurrent_status�serviceZappsZinfrarrrrr�update_esm_caches�sF
��

��
r��Z
package_namesZ
error_messagecCs(tdddddgt|�|ddid�dS)	Nr~�remover�z$-o Dpkg::Options::="--force-confdef"z$-o Dpkg::Options::="--force-confold"ZDEBIAN_FRONTENDZnoninteractive)rI)rOr�r�rrr�remove_packages�s���r�cCs*tj��D]}tj�|�q
t��tjSrX)rerfrgrmZinit_config)rarrr�_get_apt_config�sr�c	Cs"tt��}|�|�}W5QRX|SrX)rWr�r�)Zbase_key�apt_cfgZapt_cfg_keysrrr�get_apt_config_keyss
r�)�	cfg_namesr8c	CsNi}tt��8}|D],}|�|�}t|�s6|�|�p4d}|||<qW5QRX|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)rWr�r_r*Z
value_list)r�Zapt_cfg_dictr�Zcfg_nameZ	cfg_valuerrr�get_apt_config_valuess

r�)N)F)Yr]r��enumr�r�r r-r0r�r#�	functoolsr�typingrrrrrrrorer�r	r
rrr
Zuaclient.defaultsrr'r�r�r�r�r�r�r�r�r�r�ZAPT_METHOD_HTTPS_FILEZCA_CERTIFICATES_FILEr�r�ZESM_REPO_FILE_CONTENTr(Zget_event_loggerr��unique�Enumrr*rr6ZNamedMessagerGrOrVrWrsrur|r}r�r�r�r�r�r�r�r�r�r�r��boolr�r�r�rr�rz�floatr�r�r�r�r�r�r�rrrr�<module>s� ��
�5�/��'��	&��
 �
��?+��
#
��89
�