HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //usr/libexec/kcare/python/kcarectl/__pycache__/utils.cpython-38.pyc
U

n�h �@sRddlmZddlZddlZddlZddlZddlZddlZddlZddl	m	Z	ddl
mZe�d�ZdZed�Zed	�Zed�Zd8dd
�Zd9dd�Zd:dd�Zd;dd�Zdd�Zd<dd�Zdedfdd�Z dd�Z!dd�Z"d d!�Z#d"d#�Z$d$d%�Z%d&d'�Z&ed(d)��Z'd*d+�Z(d=d,d-�Z)d>d.d/�Z*d0d1�Z+d2d3�Z,d4d5�Z-de.fdfd6d7�Z/dS)?�)�print_functionN)�datetime�)�	constantsz^(\d+[.]\d+[-]\d+)���F�wc	Cs�|d}tj�|�}|r.tj�|�s.t�|�t||��&}|�|�|��t�|�	��W5QRXt�|tj
�}zt�|�W5t�|�Xt�||�dS)Nz.tmp)
�os�path�dirname�exists�makedirs�open�write�flush�fsync�fileno�O_RDONLY�close�rename)�fname�content�
ensure_dir�modeZ	tmp_fnameZdname�fZ	folder_fd�r�+/usr/libexec/kcare/python/kcarectl/utils.py�atomic_write!s

r�utf-8cCs4t|�tkr|St|�tkr&|�|�S|�|�SdS�N)�type�ntype�btype�decode�encode��data�encodingrrr�nstr8s

r)�latin1cCst|�tkr|�|�}|Sr )r!�utyper%r&rrr�bstrBs
r,cCst|�tkr|�|�}|Sr )r!r#r$r&rrr�ustrIs
r-cs4i�t�����fdd��}�|_�j|_�|_|S)NcsJ|tt|����f}z
�|WStk
r2YnX�||�}�|<|Sr )�tuple�sorted�items�KeyError)�args�kwargsZ	cache_key�result��cache�fnrr�innerTs
zcached.<locals>.inner)�	functools�wrapsr6�clear�orig�r7r8rr5r�cachedPs	r>csF�dkrtj��dkrtj��dkr*tj�i������fdd�}|S)Ncs������fdd�}|S)Ncs��}t��D]f}z�||�WStk
rN}z�|��s>�W5d}~XYnXt�|�t|t�d��tj�}qz�||�WStk
r�}zt	|d���W5d}~XYnXdS)Nr�attempts)
�range�	Exception�time�sleep�min�random�uniformr�RETRY_MAX_DELAY�setattr)r2r3Zldelay�_�exZfinal_ex)�backoff�check_retry�count�delayr7�staterrr8ps

z'retry.<locals>.decorator.<locals>.innerrr=�rKrLrMrNrO�r7r�	decoratoroszretry.<locals>.decorator)r�RETRY_DELAY�RETRY_COUNT�
RETRY_BACKOFF)rLrMrNrKrRrrPr�retrydsrVc
Cs�tj�|�sdSg}t�|�}|dk	r2t�||�}|D]<}tj�||�}||krf|�t�|�j	|f�|j
dd�q6||d�D]6\}}	tj�|	�s�tj�|	�r�t�
|	�q�t�|	�q�dS)NT)�reverse)r
rr
�listdir�fnmatch�filter�join�append�stat�st_mtime�sort�isfile�islink�remove�shutil�rmtree)
Z	directory�exclude_path�keep_n�patternr'r0�item�	full_pathrI�entryrrr�clean_directory�s
rkcCsNttj�tjd�dd�ttj�tjd�dd�tj�tj�rJt�tj�dS)N�modulesr)rf�patches)	rkr
rr[r�PATCH_CACHEr
�CACHE_KEY_DUMP_PATH�unlinkrrrr�clear_all_cache�srqc	Cs^tj�|�}tj�|�s"t�|�t|d��(}t�||�|��t�	|�
��W5QRXdS)N�wb)r
rrr
rrrc�copyfileobjrrr)�response�dstZ
parent_dirrrrr�save_to_file�s
rvcCst�|�}|r|�d�p|S)Nr)�
VERSION_RE�match�group)�versionrxrrr�strip_version_timestamp�s
r{cCs0|�d�\}}}|s$|�d�\}}}t�|d�S)N�T� z%Y-%m-%d)�	partitionr�strptime)Zstr_rawZstr_date�seprIrrr�parse_response_date�sr�cGs0ddlm}d�dd�td|��f|�D��S)Nr)�ipv6_support�/css|]}|�d�VqdS)r�N)�strip)�.0�itrrr�	<genexpr>�sz'get_patch_server_url.<locals>.<genexpr>)rr�r[rZ�get_patch_server)�partsr�rrr�get_patch_server_url�sr�c
Cs<tj�|�sdSt|��}|����W5QR�SQRXdSr )r
rr
r�readr�)�filenamerrrr�try_to_read�s
r�cCs
ttj�Sr )r�rrorrrr�
get_cache_key�sr�c
Cs:tj�|�s|St||��}|��W5QR�SQRXdSr )r
rr
rr�)rr�defaultrrrr�
_read_file�sr�cCst|d|�}|S)N�r�r��rr�r4rrr�	read_file�sr�cCst|d|�}|S)N�rbr�r�rrr�
read_file_bin�sr�cCs>i}|��}|D](}|r|�d�\}}}|r|��||<q|S)N�:)�
splitlinesr~r�)r'r4Z
data_lines�line�key�	delimiter�valuerrr�data_as_dict�sr�cCsttt����Sr )�str�intrBrrrr�
timestamp_str�sr�cGst|�dS)za workaround to fix T201N)�print)�valuesrrr�
print_wrapper�sr�cs���fdd�}|S)Ncs t�������fdd��}|S)Nc
szz�||�WS�k
rt}zJ�r\dd�|D�dd�|��D�}�d��jd�|�|���WY�Sd}~XYnXdS)NcSsg|]}t|��qSr)r�)r��arrr�
<listcomp>szBcatch_errors.<locals>.decorator.<locals>.inner.<locals>.<listcomp>cSsg|]\}}d�||��qS)z{0}={1})�format)r��k�vrrrr�sz{0}({1}) failed: {2}z, )r0r��__name__r[)r2r3�e�arg_list)�default_return�errorsr7�loggerrrr8s z.catch_errors.<locals>.decorator.<locals>.inner)r9r:r=�r�r�r�rQrrRs
zcatch_errors.<locals>.decoratorr)r�r�r�rRrr�r�catch_errorssr�)Fr	)r)r*)r*)NNN)N)N)0�
__future__rrYr9r
rE�rercrBrrr�Any�Callable�Optional�TupleZTypeVar�Unionr|�compilerwZ
CACHE_ENTRIESr!r"r#r+rr)r,r-r>rVrkrqrvr{r�r�r�r�r�r�r�r�r�r�rAr�rrrr�<module>sH






$