
    Uٵg                         d Z ddlZddl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mZ ddlmZ dd	lmZmZ d
ZdZ ej4                   e
j6                  e            ZdefdZdedefdZ d Z!y)a%  
Update messaging text for use in MOTD and APT custom Ubuntu Pro messages.

Messaging files will be emitted to /var/lib/ubuntu-advantage/message-* which
will be sourced by apt-hook/hook.cc and various /etc/update-motd.d/ hooks to
present updated text about Ubuntu Pro service and token state.
    N)exists)contractdefaultsmessagessystemutil)_updates)_enabled_services)ContractExpiryStatus_is_attached)UAConfig)machine_tokennoticeszmotd-contract-statusz6/usr/lib/update-notifier/update-motd-updates-availablecfgc                    t        j                  |       }|j                   }|j                  di       j                  di       j                  dd       }t        j                  |       }|j                  |j                  dd      |      }|j                  di       j                  di       j                  dd       }|-||j                  k7  r||d   d   d<   |j                  |       y y y )NmachineTokenInfocontractInfoidmachineToken effectiveTo)r   get_machine_token_filegetr   UAContractClientget_contract_machinecontract_expiry_datetimewrite)r   machine_token_file
orig_tokencontract_idcontract_clientrespresp_expirys          A/usr/lib/python3/dist-packages/uaclient/timer/update_messaging.pyupdate_contract_expiryr%       s    &==cB#11J)2.	^R	 	T4 
 //4O//~r*KD 	#R(	^R	 	]D	!  	-FFF  	%&~6	
 	  , G 	     returnc                    t        |       }|j                  syt        j                  d       t        j
                  j                  | j                  dt              }|j                  }|j                  }t        j                  |       }|t        j                  j                  t        j                   j                  t        j"                  j                  fv r.t%        |        t        |       }|j                  }|j                  }|t        j&                  j                  t        j(                  j                  fv rCt+        j,                  t*        j.                  j0                         t3        j4                  |       y|t        j                  j                  k(  rtt+        j,                  t*        j.                  j0                         t3        j6                  |t8        j:                  j=                  |      j?                  |      dz          y|t        j                   j                  k(  rt+        j,                  t*        j.                  j0                         t@        jB                  |z   }|jD                  }|d}n|jG                  d      }t3        j6                  |t8        jH                  j=                  |      j?                  ||      dz          y|t        j"                  j                  k(  rDt+        jJ                  t*        j.                  j0                         d	}	d
}
tM        |       jN                  D ch c]  }|jP                   }}t3        jR                         r&d|v r[d}	tU        |       jV                  jX                  }
n9t3        jZ                         r%d|v r!d}	tU        |       jV                  j\                  }
|
d
k(  r(t3        j6                  |t8        j0                  dz          yt3        j6                  |t8        j^                  j=                  |
      j?                  |
|	      dz          yc c}w )zEmit human-readable status message used by motd.

    Used by /etc/update.motd.d/91-contract-ua-esm-status

    :param cfg: UAConfig instance for this environment.
    Fz&Updating Ubuntu Pro messages for MOTD.r   )remaining_daysz

Unknownz%d %b %Y)expired_dater)   zn/ar   z	esm-infrazesm-apps)pkg_numserviceT)0r   is_attachedLOGinfoospathjoindata_dirMOTD_CONTRACT_STATUS_FILE_NAMEcontract_statuscontract_remaining_daysr   r   r   ACTIVE_EXPIRED_SOONvalueEXPIRED_GRACE_PERIODEXPIREDr%   ACTIVENONEr   removeNoticeCONTRACT_EXPIREDr   ensure_file_absent
write_filer   CONTRACT_EXPIRES_SOON	pluralizeformatr   !CONTRACT_EXPIRY_GRACE_PERIOD_DAYSr   strftimeCONTRACT_EXPIRED_GRACE_PERIODaddr
   enabled_servicesnameis_current_series_active_esmapi_u_pro_packages_updates_v1summarynum_esm_infra_updatesis_current_series_ltsnum_esm_apps_updatesCONTRACT_EXPIRED_WITH_PKGS)r   is_attached_infomotd_contract_status_msg_pathexpiry_statusr)   r   grace_period_remainingexp_dt
exp_dt_strr-   r,   senabled_services_namess                r$   update_motd_messagesr[   ;   s    $C(''HH56$&GGLLj"@%! %44M%==N&==cB00661177$$** 
 	s#',(88)AA##))!!''  	w~~667!!"?@F E 
.BBHH	Hw~~667)**44^DKK- L  	
@ s 
.CCII	Iw~~66766G 	 $<<>"J4J)22<<f'5   		
^ K 
.66<<	<GNN334 .c2CC"
AFF"
 "
 ..044%7'//  ))+33$7'..  a<-))F2  -33==gFMM## N  	 ?"
s   >P;c                      t        t              r	 t        j                  t        dg       y y # t        $ r} t
        j                  |        Y d } ~ y d } ~ ww xY w)Nz--force)r   UPDATE_NOTIFIER_MOTD_SCRIPTr   subp	Exceptionr/   	exception)excs    r$   refresh_motdrb      sJ     )*	KK4i@A +  	MM#	s   . 	AAA)"__doc__loggingr1   os.pathr   uaclientr   r   r   r   r   &uaclient.api.u.pro.packages.updates.v1r	   rM   -uaclient.api.u.pro.status.enabled_services.v1r
   (uaclient.api.u.pro.status.is_attached.v1r   r   uaclient.configr   uaclient.filesr   r   r5   r]   	getLoggerreplace_top_level_logger_name__name__r/   r%   boolr[   rb    r&   r$   <module>rq      s     	  ? ? L % 1!7 <  g:::8DE- -6fh f4 fR
r&   