o
    ÙKÕiÔ!  ã                   @   sÜ   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZmZmZ e  e e¡¡Ze
 ¡ Zedg d¢ƒZG dd	„ d	eeƒZG d
d„ dƒZdadd„ Zdeddfdd„Zdeddfdd„Zdee fdd„ZdS )é    N)Ú
namedtuple)ÚEnum)ÚList)ÚdefaultsÚevent_loggerÚmessagesÚsystemÚutilÚNoticeFileDetails)Úorder_idÚlabelÚis_permanentÚmessagec                   @   s*  e Zd Zedddejd�Zeddddd�Zed	d
dejd�Zedddej	d�Z	edddej
d�Zedddejd�Zedddejd�Zedddejd�Zedddejjd�Zedddejd�Zedddejd�Zedddejd�Zeddddd�Zedd dejd�Zed!d"dejd�Zed#d$dejd�Zd%S )&ÚNoticeÚcontract_expiredÚ5T)r   r   r   r   Úreboot_requiredÚ10FzSystem reboot requiredÚenable_reboot_requiredÚ11Úreboot_script_failedÚ12Úfips_reboot_requiredÚ20Úfips_system_reboot_requiredÚ21Úfips_install_out_of_dateÚ22Úfips_disable_reboot_requiredÚ23Úfips_proc_file_errorÚ24Úfips_manual_disable_urlÚ25Úwrong_fips_metapackage_on_cloudÚlp_lts_reboot_requiredÚ30Úoperation_in_progressÚ60z"Operation in progress: {operation}Úauto_attach_retry_full_noticeÚ70Úauto_attach_total_failureÚ71Úlimited_to_releaseÚ80N)Ú__name__Ú
__module__Ú__qualname__r
   r   ÚCONTRACT_EXPIREDÚREBOOT_REQUIREDÚENABLE_REBOOT_REQUIRED_TMPLÚENABLE_REBOOT_REQUIREDÚREBOOT_SCRIPT_FAILEDÚFIPS_REBOOT_REQUIRED_MSGÚFIPS_REBOOT_REQUIREDÚFIPS_SYSTEM_REBOOT_REQUIREDÚFIPS_INSTALL_OUT_OF_DATEÚFIPS_DISABLE_REBOOT_REQUIREDÚFIPS_PROC_FILE_ERRORÚtmpl_msgÚNOTICE_FIPS_MANUAL_DISABLE_URLÚFIPS_MANUAL_DISABLE_URLÚ&NOTICE_WRONG_FIPS_METAPACKAGE_ON_CLOUDÚWRONG_FIPS_METAPACKAGE_ON_CLOUDÚLIVEPATCH_LTS_REBOOT_REQUIREDÚOPERATION_IN_PROGRESSÚAUTO_ATTACH_RETRY_NOTICEÚAUTO_ATTACH_RETRY_FULL_NOTICEÚ&AUTO_ATTACH_RETRY_TOTAL_FAILURE_NOTICEÚAUTO_ATTACH_RETRY_TOTAL_FAILUREÚLIMITED_TO_RELEASE© rI   rI   ú8/usr/lib/python3/dist-packages/uaclient/files/notices.pyr      sÂ    üüüüüüüüüüüüüüü
ür   c                   @   s|   e Zd Zdedefdd„Zdefdd„Zdedee fd	d
„Zdedede	fdd„Z
dedefdd„Zdee fdd„ZdS )ÚNoticesManagerÚnotice_detailsÚdescriptionc                 C   s^   t  ¡ st d|jj¡ dS |jjrtjntj	}d 
|jj|jj¡}t tj ||¡|¡ dS )zâAdds a notice file. If the notice is found,
        it overwrites it.

        :param notice_details: Holds details concerning the notice file.
        :param description: The content to be written to the notice file.
        z.NoticesManager.add(%s) called as non-root userNú{}-{})r	   Úwe_are_currently_rootÚLOGÚwarningÚvaluer   r   r   ÚNOTICES_PERMANENT_DIRECTORYÚNOTICES_TEMPORARY_DIRECTORYÚformatr   r   Ú
write_fileÚosÚpathÚjoin)ÚselfrL   rM   Ú	directoryÚfilenamerI   rI   rJ   Úaddt   s"   þÿýÿþzNoticesManager.addc                 C   s\   t  ¡ st d|jj¡ dS |jjrtjntj	}d 
|jj|jj¡}t tj ||¡¡ dS )ziDeletes a notice file.

        :param notice_details: Holds details concerning the notice file.
        z1NoticesManager.remove(%s) called as non-root userNrN   )r	   rO   rP   rQ   rR   r   r   r   rS   rT   rU   r   r   Úensure_file_absentrW   rX   rY   )rZ   rL   r[   r\   rI   rI   rJ   Úremove“   s   þÿýÿzNoticesManager.remover[   Úreturnc                    s   ‡ ‡fdd„t  ˆ ¡D ƒS )zµGets the list of notice file names in the given directory.

        :param directory: The directory to search for notice files.
        :returns: List of notice file names.
        c                    s2   g | ]}t j t j ˆ |¡¡rˆ ˆ |¡r|‘qS rI   )rW   rX   ÚisfilerY   Ú_is_valid_notice_file)Ú.0Ú	file_name©r[   rZ   rI   rJ   Ú
<listcomp>¯   s    ý
üÿz9NoticesManager._get_notice_file_names.<locals>.<listcomp>)rW   Úlistdir)rZ   r[   rI   re   rJ   Ú_get_notice_file_names©   s   þz%NoticesManager._get_notice_file_namesrd   c                    s$   |t jk‰ ‡ fdd„tD ƒ}||v S )z¢Checks if the notice file is valid.

        :param file_name: The name of the notice file.
        :returns: True if the file is valid, False otherwise.
        c                    s&   h | ]}|j ˆ krd  |j|j¡’qS )rN   )r   rU   r   r   )rc   Ún©Úis_permanent_dirrI   rJ   Ú	<setcomp>½   s
    
þz7NoticesManager._is_valid_notice_file.<locals>.<setcomp>)r   rS   r   )rZ   r[   rd   Úvalid_file_namesrI   rj   rJ   rb   ¶   s
   

þz$NoticesManager._is_valid_notice_filec                 C   s<   |  d¡\}}tD ]}|j|kr|j|kr|jj  S q	dS )z§Gets the default message for a notice file.

        :param file_name: The name of the notice file.
        :returns: The default message defined in the enum.
        ú-Ú )Úsplitr   r   r   rR   r   )rZ   rd   r   r   ÚnoticerI   rI   rJ   Ú_get_default_messageÄ   s   €z#NoticesManager._get_default_messagec              
   C   s¦   t jt jf}g }|D ]B}tj |¡sq
|  |¡}|D ]1}zt tj 	||¡¡}W n t
y8   t d| ¡ Y qw |rA| |¡ q|  |¡}| |¡ qq
| ¡  |S )zdGets all the notice files currently saved.

        :returns: List of notice file contents.
        zPermission error while reading )r   rS   rT   rW   rX   Úexistsrh   r   Ú	load_filerY   ÚPermissionErrorrP   rQ   Úappendrr   Úsort)rZ   Únotice_directoriesÚnoticesÚnotice_directoryÚnotice_file_namesÚnotice_file_nameÚnotice_file_contentsÚdefault_messagerI   rI   rJ   ÚlistÐ   s8   þ
ÿÿüÿðzNoticesManager.listN)r/   r0   r1   r   Ústrr]   r_   r   rh   Úboolrb   rr   r   rI   rI   rI   rJ   rK   s   s    þ
ýrK   c                   C   s   t d u rtƒ a t S ©N)Ú_notice_clsrK   rI   rI   rI   rJ   Ú
get_noticeö   s   r„   rL   r`   c                 K   s(   t ƒ }| jjdi |¤Ž}| | |¡ d S )NrI   )r„   r   rU   r]   )rL   Úkwargsrq   rM   rI   rI   rJ   r]   þ   s   r]   c                 C   s   t ƒ }| | ¡ d S r‚   )r„   r_   )rL   rq   rI   rI   rJ   r_     s   r_   c                  C   s   t ƒ } |  ¡ S r‚   )r„   r   )rq   rI   rI   rJ   r   	  s   r   )ÚloggingrW   Úcollectionsr   Úenumr   Útypingr   Úuaclientr   r   r   r   r	   Ú	getLoggerÚreplace_top_level_logger_namer/   rP   Úget_event_loggerÚeventr
   r   rK   rƒ   r„   r]   r_   r€   r   rI   rI   rI   rJ   Ú<module>   s&    ÿc 