U
    Jh!                     @   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ddd	d
gZG dd deeZG dd dZdadd ZeddddZeddddZee d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 )&NoticeZcontract_expired5T)r   r   r   r   Zreboot_requiredZ10FzSystem reboot requiredZenable_reboot_requiredZ11Zreboot_script_failedZ12Zfips_reboot_requiredZ20Zfips_system_reboot_requiredZ21Zfips_install_out_of_dateZ22Zfips_disable_reboot_requiredZ23Zfips_proc_file_errorZ24Zfips_manual_disable_urlZ25Zwrong_fips_metapackage_on_cloudZlp_lts_reboot_requiredZ30Zoperation_in_progressZ60z"Operation in progress: {operation}Zauto_attach_retry_full_noticeZ70Zauto_attach_total_failureZ71Zlimited_to_releaseZ80N)__name__
__module____qualname__r
   r   ZCONTRACT_EXPIREDZREBOOT_REQUIREDZENABLE_REBOOT_REQUIRED_TMPLZENABLE_REBOOT_REQUIREDZREBOOT_SCRIPT_FAILEDZFIPS_REBOOT_REQUIRED_MSGZFIPS_REBOOT_REQUIREDZFIPS_SYSTEM_REBOOT_REQUIREDZFIPS_INSTALL_OUT_OF_DATEZFIPS_DISABLE_REBOOT_REQUIREDZFIPS_PROC_FILE_ERRORZtmpl_msgZNOTICE_FIPS_MANUAL_DISABLE_URLZFIPS_MANUAL_DISABLE_URLZ&NOTICE_WRONG_FIPS_METAPACKAGE_ON_CLOUDZWRONG_FIPS_METAPACKAGE_ON_CLOUDZLIVEPATCH_LTS_REBOOT_REQUIREDZOPERATION_IN_PROGRESSZAUTO_ATTACH_RETRY_NOTICEZAUTO_ATTACH_RETRY_FULL_NOTICEZ&AUTO_ATTACH_RETRY_TOTAL_FAILURE_NOTICEZAUTO_ATTACH_RETRY_TOTAL_FAILUREZLIMITED_TO_RELEASE r   r   8/usr/lib/python3/dist-packages/uaclient/files/notices.pyr      s   r   c                   @   sr   e Zd ZeedddZedddZeee ddd	Zeee	d
ddZ
eedddZee d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 )zAdds 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   Z
write_fileospathjoin)selfr   r   	directoryfilenamer   r   r   addt   s$     zNoticesManager.add)r   c                 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 userNr   )r	   r   r   r   r   r   r   r   r   r   r    r   r   Zensure_file_absentr!   r"   r#   )r$   r   r%   r&   r   r   r   remove   s     zNoticesManager.remove)r%   returnc                    s    fddt  D S )zGets 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 r   )r!   r"   isfiler#   _is_valid_notice_file).0	file_namer%   r$   r   r   
<listcomp>   s   z9NoticesManager._get_notice_file_names.<locals>.<listcomp>)r!   listdir)r$   r%   r   r.   r   _get_notice_file_names   s    z%NoticesManager._get_notice_file_names)r%   r-   r)   c                    s$   |t jk  fddtD }||kS )zChecks 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 )r   )r   r    r   r   )r,   nZis_permanent_dirr   r   	<setcomp>   s   
z7NoticesManager._is_valid_notice_file.<locals>.<setcomp>)r   r   r   )r$   r%   r-   Zvalid_file_namesr   r3   r   r+      s
    

z$NoticesManager._is_valid_notice_file)r-   r)   c                 C   s<   | d\}}tD ]$}|j|kr|j|kr|jj  S qdS )zGets 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   r   r   )r$   r-   r   r   noticer   r   r   _get_default_message   s
    z#NoticesManager._get_default_messager)   c              
   C   s   t jt jf}g }|D ]}tj|s&q| |}|D ]h}zttj	||}W n& t
k
rv   td|  Y q4Y nX |r|| q4| |}|| q4q|  |S )zdGets all the notice files currently saved.

        :returns: List of notice file contents.
        zPermission error while reading )r   r   r   r!   r"   existsr1   r   Z	load_filer#   PermissionErrorr   r   appendr9   sort)r$   Znotice_directoriesZnoticesZnotice_directoryZnotice_file_namesZnotice_file_nameZnotice_file_contentsZdefault_messager   r   r   list   s4    

zNoticesManager.listN)r   r   r   r   strr'   r(   r   r1   boolr+   r9   r?   r   r   r   r   r   s   s   r   c                   C   s   t d krt a t S N)_notice_clsr   r   r   r   r   
get_notice   s    rD   )r   r)   c                 K   s$   t  }| jjf |}|| | d S rB   )rD   r   r    r'   )r   kwargsr8   r   r   r   r   r'      s    r'   c                 C   s   t  }||  d S rB   )rD   r(   )r   r8   r   r   r   r(     s    r(   r:   c                  C   s   t  } |  S rB   )rD   r?   )r8   r   r   r   r?   	  s    r?   )Zloggingr!   collectionsr   enumr   typingr   Zuaclientr   r   r   r   r	   Z	getLoggerZreplace_top_level_logger_namer   r   Zget_event_loggerZeventr
   r   r   rC   rD   r'   r(   r@   r?   r   r   r   r   <module>   s&    
c 