
Ke%                 @   s   d  d l  Z  d  d l Z d  d l Z d  d l m Z d  d l m Z m Z m Z d  d l m	 Z	 m
 Z
 m Z m Z m Z d  d l m Z e
 j   Z e j e j e   Z Gd d   d  Z Gd d	   d	 e  Z Gd
 d   d  Z d S)    N)datetime)AnyDictOptional)defaultsevent_logger
exceptionssystemutil)PublicMachineTokenDatac               @   s   e  Z d  Z e j d e e e d d d   Z e e d d d     Z	 e e d d d	     Z
 e d
 d    Z e d d d    Z e e d d d    Z d d   Z d S)UAFileT)name	directoryprivatec             C   s=   | |  _  | |  _ | |  _ t j j |  j  |  j  |  _ d  S)N)
_directoryZ
_file_name_is_privateospathjoin_path)selfr   r   r    r   6/usr/lib/python3/dist-packages/uaclient/files/files.py__init__   s    			zUAFile.__init__)returnc             C   s   |  j  S)N)r   )r   r   r   r   r      s    zUAFile.pathc             C   s   |  j  S)N)r   )r   r   r   r   
is_private   s    zUAFile.is_privatec             C   s   t  j j |  j  S)N)r   r   exists)r   r   r   r   
is_present"   s    zUAFile.is_present)contentc             C   s   |  j  r t j n t j } yK t j j |  j  t j k rU t j	 |  j d d n t j	 |  j  Wn t
 k
 rz Yn Xt j |  j | |  d  S)Nmodei  )r   r   ROOT_READABLE_MODEWORLD_READABLE_MODEr   r   basenamer   PRIVATE_SUBDIRmakedirsOSErrorr	   
write_file)r   r   Z	file_moder   r   r   write&   s    	zUAFile.writec             C   sH   d  } y t  j |  j  } Wn% t k
 rC t j d |  j  Yn X| S)Nz(Tried to load %s but file does not exist)r	   	load_filer   FileNotFoundErrorLOGdebug)r   r   r   r   r   read7   s    zUAFile.readc             C   s   t  j |  j  d  S)N)r	   ensure_file_absentr   )r   r   r   r   delete?   s    zUAFile.deleteN)__name__
__module____qualname__r   DEFAULT_DATA_DIRstrboolr   propertyr   r   r   r'   r   r,   r.   r   r   r   r   r      s   r   c                   s+   e  Z d  Z e d   f d d    Z   S)UserCacheFile)r   c                s&   t    j | d t j   d d d  S)Nr   r   F)superr   r	   get_user_cache_dir)r   r   )	__class__r   r   r   D   s    	zUserCacheFile.__init__)r/   r0   r1   r3   r   r   r   )r9   r   r6   C   s   r6   c               @   s  e  Z d  Z e j d e e e d d d   Z e d d d    Z	 d d	   Z
 e e d
 d d    Z e d d    Z e d d    Z d d   Z e e e d
 d d     Z e d d    Z e e d d d     Z e e e d
 d d     Z e d d    Z e e e d
 d d     Z e d  d
 d! d"     Z e d  d
 d# d$     Z e d% d
 d& d'     Z e d( d)    Z e d* d+    Z d S),MachineTokenFileN)r   machine_token_overlay_pathc             C   sc   t  j } t | | d t  j  |  _ t | | d  |  _ | |  _ d  |  _ d  |  _ d  |  _	 d  S)N/F)
r   MACHINE_TOKEN_FILEr   r#   private_filepublic_filer;   _machine_token_entitlements_contract_expiry_datetime)r   r   r;   	file_namer   r   r   r   K   s    				zMachineTokenFile.__init__)private_contentc             C   s   t  j   r t j | d t  j } |  j j |  t j |  j	 d d  } t j | d t  j } |  j
 j |  d |  _ d |  _ d |  _ n t j    d S)z8Update the machine_token file for both pub/private filescls	keep_noneFN)r
   we_are_currently_rootjsondumpsDatetimeAwareJSONEncoderr>   r'   r   	from_dictto_dictr?   r@   rA   rB   r   NonRootUserError)r   rD   Zprivate_content_strZpublic_contentZpublic_content_strr   r   r   r'   Z   s    			zMachineTokenFile.writec             C   sT   t  j   rD |  j j   |  j j   d |  _ d |  _ d |  _ n t j	    d S)z!Delete both pub and private filesN)
r
   rG   r?   r.   r>   r@   rA   rB   r   rM   )r   r   r   r   r.   r   s    		zMachineTokenFile.delete)r   c             C   sl   t  j   r |  j } n	 |  j } | j   } | s7 d  Sy t j | d t  j } Wn t k
 rg Yn X| S)NrE   )	r
   rG   r>   r?   r,   rH   loadsDatetimeAwareJSONDecoder	Exception)r   Zfile_handlerr   r   r   r   r,   ~   s    	zMachineTokenFile.readc             C   s0   t  j   r" |  j j o! |  j j S|  j j Sd  S)N)r
   rG   r?   r   r>   )r   r   r   r   r      s    zMachineTokenFile.is_presentc             C   sb   |  j  s[ |  j   } | rR |  j rR |  j |  j  } | rR t j d | d |  | |  _  |  j  S)zAReturn the machine-token if cached in the machine token response.	base_dictoverlay_dict)r@   r,   r;   parse_machine_token_overlayr
   depth_first_merge_overlay_dict)r   r   machine_token_overlayr   r   r   machine_token   s    			zMachineTokenFile.machine_tokenc             C   s%   t  j |  } t j | d t j S)NrE   )r	   r(   rH   rN   r
   rO   )r   r;   Zmachine_token_overlay_contentr   r   r   rS      s
    	z,MachineTokenFile.parse_machine_token_overlayc             C   s"   t  |  j  r |  j d d Sd  S)NmachineTokenInfoZaccountInfo)r4   rV   )r   r   r   r   account   s    zMachineTokenFile.accountc             C   s9   |  j  r |  j  S|  j s i  S|  j |  j  |  _  |  j  S)z9Return configured entitlements keyed by entitlement named)rA   rV   get_entitlements_from_token)r   r   r   r   entitlements   s    		zMachineTokenFile.entitlements)rV   c       	      C   s   d d l  m } |  s i  Si  } |  j d i   j d  } | sE i  St d d   |  j d g   D  } t d d   | j d	 g   D  } xT | j   D]F \ } } d
 | i } | | k r | | | d <| |  | | | <q W| S)zReturn a dictionary of entitlements keyed by entitlement name.

        Return an empty dict if no entitlements are present.
        r   )apply_contract_overridesrW   contractInfoc             s   s-   |  ]# } | j  d   | j  d  f Vq d S)typetokenN)get).0er   r   r   	<genexpr>   s   z?MachineTokenFile.get_entitlements_from_token.<locals>.<genexpr>resourceTokensc             s   s$   |  ] } | j  d   | f Vq d S)r]   N)r_   )r`   ra   r   r   r   rb      s   resourceEntitlementsZentitlementZresourceToken)Zuaclient.contractr[   r_   dictitems)	rV   r[   rZ   r\   Ztokens_by_nameZent_by_nameZentitlement_nameZ	ent_valueZentitlement_cfgr   r   r   rY      s,    			
z,MachineTokenFile.get_entitlements_from_tokenc             C   s@   |  j  s9 |  j j d i   j d i   j d d  |  _  |  j  S)z6Return a datetime of the attached contract expiration.rW   r\   ZeffectiveToN)rB   rV   r_   )r   r   r   r   contract_expiry_datetime   s
    	z)MachineTokenFile.contract_expiry_datetimec             C   s   t  |  j  S)z<Report whether this machine configuration is attached to UA.)r4   rV   )r   r   r   r   is_attached   s    zMachineTokenFile.is_attachedc             C   s9   |  j  d k r d S|  j  j   t j   j   } | j S)a  Report num days until contract expiration based on effectiveTo

        :return: A positive int representing the number of days the attached
            contract remains in effect. Return a negative int for the number
            of days beyond contract's effectiveTo date.
        N)rg   dater   utcnowdays)r   deltar   r   r   contract_remaining_days   s    z(MachineTokenFile.contract_remaining_dayszOptional[str]c             C   s)   |  j  r% |  j  j d i   j d  Sd  S)NactivityInfoZactivityToken)rV   r_   )r   r   r   r   activity_token  s    	zMachineTokenFile.activity_tokenc             C   s)   |  j  r% |  j  j d i   j d  Sd  S)Nrn   Z
activityID)rV   r_   )r   r   r   r   activity_id
  s    	zMachineTokenFile.activity_idzOptional[int]c             C   s)   |  j  r% |  j  j d i   j d  Sd  S)Nrn   ZactivityPingInterval)rV   r_   )r   r   r   r   activity_ping_interval  s    	z'MachineTokenFile.activity_ping_intervalc             C   s5   |  j  r1 |  j  j d i   j d i   j d  Sd  S)NrW   r\   id)rV   r_   )r   r   r   r   contract_id  s
    	zMachineTokenFile.contract_idc             C   s    |  j  r |  j  j d g   Sd  S)Nrc   )rV   r_   )r   r   r   r   resource_tokens"  s    	z MachineTokenFile.resource_tokens)r/   r0   r1   r   r2   r3   r   r   re   r'   r.   r,   r5   r   rV   rS   rX   rZ   staticmethodr   rY   r   rg   rh   intrm   ro   rp   rq   rs   rt   r   r   r   r   r:   J   s4   	#
r:   )rH   loggingr   r   typingr   r   r   uaclientr   r   r   r	   r
   Zuaclient.contract_data_typesr   get_event_loggerevent	getLoggerreplace_top_level_logger_namer/   r*   r   r6   r:   r   r   r   r   <module>   s   (5