o
    K&b                     @   sj  d dl mZ d dlmZ d dlmZmZmZmZm	Z	 d dl
mZ d dl
mZ d dlmZ d dlmZ e d Zd	Zd
defddefddefdZdd e D ZG dd deZdejdee fddZdeej de	eef fddZdedeeef defddZd eej deej fd!d"Zd#edeeef fd$d%Z d#edeeef fd&d'Z!d(S ))    )defaultdict)Enum)AnyDefaultDictDictListTuple)Cache)package)UAConfig)get_platform_infoseries)	esm-infraesm-appsZUbuntuz{}-securityZUbuntuESMAppsz{}-apps-securityZ	UbuntuESMz{}-infra-security)standard-securityr   r   c                 C   s   i | ]\}}||qS  r   ).0kvr   r   :/usr/lib/python3/dist-packages/uaclient/security_status.py
<dictcomp>   s    
r   c                   @   s    e Zd ZdZdZdZdZdZdS )UpdateStatusz2Represents the availability of a security package.Zupgrade_availableZpending_attachZpending_enableZupgrade_unavailableN)__name__
__module____qualname____doc__	AVAILABLE
UNATTACHEDNOT_ENABLEDUNAVAILABLEr   r   r   r   r      s    r   r
   returnc                 C   sR   g }| j jD ] }|j|jftd kr|d |j|jftd kr&|d q|S )Nr   r   )	installedoriginsoriginarchiveSERVICE_TO_ORIGIN_INFORMATIONappend)r
   esm_servicesr#   r   r   r   list_esm_for_package"   s   

r(   r"   c                 C   s2   | D ]}t |j|jf}|r||jf  S qdS )zGTranslates the archive name in the version origin to a UA service name.) r)   )ORIGIN_INFORMATION_TO_SERVICEgetr#   r$   Zsite)r"   r#   servicer   r   r   get_service_name0   s   
r-   service_nameua_infoc                 C   sP   | dks|d r| |d v rt jjS |d st jjS | |d v r$t jjS t jjS )zDefines the update status for a package based on the service name.

    For ESM-[Infra|Apps] packages, first checks if UA is attached. If this is
    the case, also check for availability of the service.
    r   attachedenabled_servicesentitled_services)r   r   valuer   r   r   )r.   r/   r   r   r   get_update_status;   s   
r4   packagesc                 C   s   dd | D S )zFilters a list of packages looking for available security updates.

    Checks if the package has a greater version available, and if the origin of
    this version matches any of the series' security repositories.
    c                 S   s:   g | ]}|j D ]}||jkrtd d |jD r|qqS )c                 s   s     | ]}|j |jftv V  qd S )N)r#   r$   r*   )r   r#   r   r   r   	<genexpr>Y   s
    
z5filter_security_updates.<locals>.<listcomp>.<genexpr>)Zversionsr!   anyr"   )r   r
   versionr   r   r   
<listcomp>T   s    z+filter_security_updates.<locals>.<listcomp>r   )r5   r   r   r   filter_security_updatesL   s   r:   cfgc                 C   s   dg g d}| j dd}|d r?d|d< |d D ]&}|d tv r>|d d	kr/|d
 |d  |d dkr>|d |d  q|S )z6Returns the UA information based on the config object.F)r0   r1   r2   T)Z	show_betar0   servicesnameZentitledZyesr2   statusenabledr1   )r>   ESM_SERVICESr&   )r;   r/   r>   r,   r   r   r   get_ua_info`   s   rA   c              	   C   s  t | }d|i}g }t }dd |D }t||d< tt}tt}|D ]}t|}	|	D ]
}
||
  d7  < q,q$t|}|D ]$}t|j\}}t	||}||  d7  < |
|jj|j|||d q>|d |d< |d	 |d
< |d |d< |d	 |d< |d |d< d||dS )aZ  Returns the status of security updates on a system.

    The returned dict has a 'packages' key with a list of all installed
    packages which can receive security updates, with or without ESM,
    reflecting the availability of the update based on the UA status.

    There is also a summary with the UA information and the package counts.
    Zuac                 S   s   g | ]}|j r|qS r   )Zis_installed)r   r
   r   r   r   r9      s    z#security_status.<locals>.<listcomp>Znum_installed_packages   )r
   r8   r.   r>   r#   r   Znum_esm_infra_packagesr   Znum_esm_apps_packagesZnum_esm_infra_updatesZnum_esm_apps_updatesr   Znum_standard_security_updatesz0.1)Z_schema_versionsummaryr5   )rA   r	   lenr   intr(   r:   r-   r"   r4   r&   r
   r=   r8   )r;   r/   rC   r5   cacheZinstalled_packagesZpackage_countZupdate_countr
   r'   r,   Zsecurity_upgradable_versions	candidater.   Zorigin_siter>   r   r   r   security_statusu   sD   	

rH   N)"collectionsr   enumr   typingr   r   r   r   r   Zaptr	   r
   Zapt_packageZuaclient.configr   Zuaclient.utilr   r   r@   formatr%   itemsr*   r   ZPackagestrr(   ZOriginr-   r4   r:   rA   rH   r   r   r   r   <module>   s6    
 
