HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/uaclient/files/__pycache__/files.cpython-310.pyc
o

nHJe�%�@s�ddlZddlZddlZddlmZddlmZmZmZddlm	Z	m
Z
mZmZm
Z
ddlmZe
��Ze�e
�e��ZGdd�d�ZGdd	�d	e�ZGd
d�d�ZdS)�N)�datetime)�Any�Dict�Optional)�defaults�event_logger�
exceptions�system�util)�PublicMachineTokenDatac@s�eZdZejdfdededefdd�Zedefdd	��Z	edefd
d��Z
edd
��Zdefdd�Zde
efdd�Zdd�ZdS)�UAFileT�name�	directory�privatecCs*||_||_||_tj�|j|j�|_dS�N)�
_directory�
_file_name�_is_private�os�path�join�_path)�selfr
rr�r�6/usr/lib/python3/dist-packages/uaclient/files/files.py�__init__szUAFile.__init__�returncC�|jSr)r�rrrrr�zUAFile.pathcCrr)rrrrr�
is_privaterzUAFile.is_privatecCstj�|j�Sr)rr�existsrrrr�
is_present"szUAFile.is_present�contentcCsp|jrtjntj}ztj�|j�tjkrtj	|jdd�nt�	|j�Wn	t
y-Ynwt�|j||�dS)Ni�)�mode)
r r�ROOT_READABLE_MODE�WORLD_READABLE_MODErr�basenamer�PRIVATE_SUBDIR�makedirs�OSErrorr	�
write_file)rr#�	file_moderrr�write&s����zUAFile.writecCs:d}z	t�|j�}W|Styt�d|j�Y|Sw)Nz(Tried to load %s but file does not exist)r	�	load_filer�FileNotFoundError�LOG�debug)rr#rrr�read7s��zUAFile.readcCst�|j�dSr)r	�ensure_file_absentrrrrr�delete?sz
UAFile.deleteN)�__name__�
__module__�__qualname__r�DEFAULT_DATA_DIR�str�boolr�propertyrr r"r-rr2r4rrrrrs&���
�
rcs"eZdZdef�fdd�Z�ZS)�
UserCacheFiler
cst�j|t��dd�dS)NF)rr)�superrr	�get_user_cache_dir)rr
��	__class__rrrDs

�zUserCacheFile.__init__)r5r6r7r9r�
__classcell__rrr?rr<Csr<c@s eZdZejdfdedeefdd�Zdefdd�Z	d	d
�Z
deefdd
�Zedd��Z
edd��Zdd�Zedeefdd��Zedd��Zedefdd��Zedeefdd��Zedd��Zedeefdd ��Zed-d"d#��Zed-d$d%��Zed.d'd(��Zed)d*��Zed+d,��ZdS)/�MachineTokenFileNr�machine_token_overlay_pathcCsFtj}t||dtj�|_t||d�|_||_d|_d|_d|_	dS)N�/F)
r�MACHINE_TOKEN_FILErr(�private_file�public_filerC�_machine_token�
_entitlements�_contract_expiry_datetime)rrrC�	file_namerrrrKs�
zMachineTokenFile.__init__�private_contentcCspt��r4tj|tjd�}|j�|�t�|�j	dd�}tj|tjd�}|j
�|�d|_d|_d|_
dSt���)z8Update the machine_token file for both pub/private files��clsF)�	keep_noneN)r
�we_are_currently_root�json�dumps�DatetimeAwareJSONEncoderrFr-r�	from_dict�to_dictrGrHrIrJr�NonRootUserError)rrL�private_content_str�public_content�public_content_strrrrr-Zs$����
zMachineTokenFile.writecCs:t��r|j��|j��d|_d|_d|_dSt�	��)z!Delete both pub and private filesN)
r
rPrGr4rFrHrIrJrrVrrrrr4rs


zMachineTokenFile.deletercCsRt��r|j}n|j}|��}|sdSztj|tjd�}W|Sty(Y|Sw�NrM)	r
rPrFrGr2rQ�loads�DatetimeAwareJSONDecoder�	Exception)r�file_handlerr#rrrr2~s��zMachineTokenFile.readcCs t��r|jjo|jjS|jjSr)r
rPrGr"rFrrrrr"�szMachineTokenFile.is_presentcCsB|js|��}|r|jr|�|j�}|rtj||d�||_|jS)zAReturn the machine-token if cached in the machine token response.)�	base_dict�overlay_dict)rHr2rC�parse_machine_token_overlayr
�depth_first_merge_overlay_dict)rr#�machine_token_overlayrrr�
machine_token�s
��zMachineTokenFile.machine_tokencCst�|�}tj|tjd�SrZ)r	r.rQr[r
r\)rrC�machine_token_overlay_contentrrrra�s��z,MachineTokenFile.parse_machine_token_overlaycCst|j�r|jddSdS)N�machineTokenInfo�accountInfo�r:rdrrrr�account�s
zMachineTokenFile.accountcCs*|jr|jS|jsiS|�|j�|_|jS)z9Return configured entitlements keyed by entitlement named)rIrd�get_entitlements_from_tokenrrrr�entitlements�s�zMachineTokenFile.entitlementsrdc	Cs�ddlm}|s
iSi}|�di��d�}|siStdd�|�dg�D��}tdd�|�d	g�D��}|��D]\}}d
|i}||vrI|||d<||�|||<q7|S)z�Return a dictionary of entitlements keyed by entitlement name.

        Return an empty dict if no entitlements are present.
        r)�apply_contract_overridesrf�contractInfocss$�|]
}|�d�|�d�fVqdS)�type�tokenN��get��.0�errr�	<genexpr>�s
��
�z?MachineTokenFile.get_entitlements_from_token.<locals>.<genexpr>�resourceTokenscss�|]
}|�d�|fVqdS)rnNrprrrrrru�s
��
��resourceEntitlements�entitlement�
resourceToken)�uaclient.contractrlrq�dict�items)	rdrlrkrm�tokens_by_name�ent_by_name�entitlement_name�	ent_value�entitlement_cfgrrrrj�s0�
�
��
z,MachineTokenFile.get_entitlements_from_tokencCs,|js|j�di��di��dd�|_|jS)z6Return a datetime of the attached contract expiration.rfrm�effectiveToN)rJrdrqrrrr�contract_expiry_datetime�s�z)MachineTokenFile.contract_expiry_datetimecCs
t|j�S)z<Report whether this machine configuration is attached to UA.rhrrrr�is_attached�s
zMachineTokenFile.is_attachedcCs*|jdurdS|j��t����}|jS)aReport 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)r��dater�utcnow�days)r�deltarrr�contract_remaining_days�s
z(MachineTokenFile.contract_remaining_days�
Optional[str]cC�|jr
|j�di��d�SdS)N�activityInfo�
activityToken�rdrqrrrr�activity_token�
�zMachineTokenFile.activity_tokencCr�)Nr��
activityIDr�rrrr�activity_id
szMachineTokenFile.activity_id�
Optional[int]cCr�)Nr��activityPingIntervalr�rrrr�activity_ping_intervalr�z'MachineTokenFile.activity_ping_intervalcCs&|jr|j�di��di��d�SdS)Nrfrm�idr�rrrr�contract_ids�zMachineTokenFile.contract_idcCs|jr
|j�dg�SdS)Nrvr�rrrr�resource_tokens"sz MachineTokenFile.resource_tokens)rr�)rr�)r5r6r7rr8r9rrr{r-r4r2r;r"rdrarirk�staticmethodrrjrr�r��intr�r�r�r�r�r�rrrrrBJsL��
�

	
#

	rB)rQ�loggingrr�typingrrr�uaclientrrrr	r
�uaclient.contract_data_typesr�get_event_logger�event�	getLogger�replace_top_level_logger_namer5r0rr<rBrrrr�<module>s5