
    Uٵg!                     8   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 j                   ej                  e            Z e
j$                         Z edg d      Z G d d	ee      Z G d
 d      Zdad ZdeddfdZdeddfdZdee   fdZy)    N)
namedtuple)Enum)List)defaultsevent_loggermessagessystemutilNoticeFileDetails)order_idlabelis_permanentmessagec                      e Zd Z edddej
                        Z edddd      Z ed	d
dej                        Z edddej                        Z	 edddej                        Z edddej                        Z edddej                        Z edddej                        Z edddej                  j                         Z edddej"                        Z edddej&                        Z edddej*                        Z edddd      Z edd dej.                        Z ed!d"dej2                        Z ed#d$dej6                        Zy%)&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     8/usr/lib/python3/dist-packages/uaclient/files/notices.pyr   r      s   ( ))	 ((	O /&44	 -$--	 -$11	 #4+44	#  1(11	  $5,55	$  -$--66	 0'77	 '8/??	'# %6&66	%! .%4	 %6-11	%! '8)??	'# +"++	rL   r   c                   t    e Zd ZdedefdZdefdZdedee   fdZdedede	fd	Z
dedefd
Zdee   fdZy)NoticesManagernotice_detailsdescriptionc                    t        j                         s+t        j                  d|j                  j
                         y|j                  j                  rt        j                  nt        j                  }dj                  |j                  j                  |j                  j
                        }t        j                  t        j                  j!                  ||      |       y)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	   
write_fileospathjoin)selfrP   rQ   	directoryfilenames        rM   addzNoticesManager.addt   s     ))+KK@$$**  ##00 0055 	
 >>  ))>+?+?+E+E
 	GGLLH-	
rL   c                    t        j                         s+t        j                  d|j                  j
                         y|j                  j                  rt        j                  nt        j                  }dj                  |j                  j                  |j                  j
                        }t        j                  t        j                  j!                  ||             y)ziDeletes a notice file.

        :param notice_details: Holds details concerning the notice file.
        z1NoticesManager.remove(%s) called as non-root userNrS   )r
   rT   rU   rV   rW   r   r   r   rX   rY   rZ   r   r	   ensure_file_absentr\   r]   r^   )r_   rP   r`   ra   s       rM   removezNoticesManager.remove   s    
 ))+KKC$$**  ##00 0055 	
 >>  ))>+?+?+E+E
 	!!"'',,y("CDrL   r`   returnc           	          t        j                  |      D cg c]S  }t         j                  j                  t         j                  j	                  ||            r| j                  ||      r|U c}S c c}w )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.
        )r\   listdirr]   isfiler^   _is_valid_notice_file)r_   r`   	file_names      rM   _get_notice_file_namesz%NoticesManager._get_notice_file_names   s\      ZZ	2
ww~~bggll9i@A**9i@ 
 	
 
s   AA3rk   c                     |t         j                  k(  }t        D ch c]7  }|j                  |k(  r&dj	                  |j
                  |j                        9 }}||v S c c}w )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.
        rS   )r   rX   r   r   rZ   r   r   )r_   r`   rk   is_permanent_dirnvalid_file_namess         rM   rj   z$NoticesManager._is_valid_notice_file   sh     %(L(LL 
~~!11 NN1::qww/
 

 ,,,
s   <Ac                     |j                  d      \  }}t        D ]9  }|j                  |k(  s|j                  |k(  s#|j                  j
                  c S  y)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   rW   r   )r_   rk   r   r   notices        rM   _get_default_messagez#NoticesManager._get_default_message   sQ     $//#.% 	,F(*v||u/D||+++	, rL   c                 
   t         j                  t         j                  f}g }|D ]  }t        j                  j                  |      s#| j                  |      }|D ]l  }	 t        j                  t        j                  j                  ||            }|r|j                  |       K| j                  |      }|j                  |       n  |j                          |S # t        $ r t        j                  d|z          Y w xY w)zdGets all the notice files currently saved.

        :returns: List of notice file contents.
        zPermission error while reading )r   rX   rY   r\   r]   existsrl   r	   	load_filer^   PermissionErrorrU   rV   appendrv   sort)r_   notice_directoriesnoticesnotice_directorynotice_file_namesnotice_file_namenotice_file_contentsdefault_messages           rM   listzNoticesManager.list   s    0000
  2 	477>>"23 $ ; ;<L M$5 4 +1+;+;%57GH,( (NN#78&*&?&?('O NN?3!4		4* 	 ' KK9<LL 	s   3C!DDN)r1   r2   r3   r   strrb   re   r   rl   boolrj   rv   r   rK   rL   rM   rO   rO   s   s{    

 
>EV E,
 
S	 
-s -s -t -
c 
c 
 d3i  rL   rO   c                  .    t         
t               a t         S N)_notice_clsrO   rK   rL   rM   
get_noticer      s    $&rL   rP   rf   c                 t    t               } | j                  j                  di |}|j                  | |       y )NrK   )r   r   rZ   rb   )rP   kwargsru   rQ   s       rM   rb   rb      s2    \F/.((//9&9K
JJ~{+rL   c                 :    t               }|j                  |        y r   )r   re   )rP   ru   s     rM   re   re     s    \F
MM.!rL   c                  6    t               } | j                         S r   )r   r   )ru   s    rM   r   r   	  s    \F;;=rL   )loggingr\   collectionsr   enumr   typingr   uaclientr   r   r   r	   r
   	getLoggerreplace_top_level_logger_namer1   rU   get_event_loggereventr   r   rO   r   r   rb   re   r   r   rK   rL   rM   <module>r      s     	 "   C Cg:::8DE%%%'I 
` `F} }@ , ,T ,"6 "d "
d3i rL   