
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
;j[#                 @   sd  d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
m
Z
 ddlmZ ejdZdZedZed	ZedZd<ddZd=ddZd>ddZd?ddZdd Zd@ddZdedfddZdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Z ed)d* Z!d+d, Z"dAd-d.Z#dBd/d0Z$d1d2 Z%dCd4d5Z&d6d7 Z'd8d9 Z(de)fdfd:d;Z*dS )D    )print_functionN)datetime   )	constantsz^(\d+[.]\d+[-]\d+)        Fw  c       
      C   s   t jj| }|r(t jj| r(t j| yt j| j}W n tk
rP   |}Y nX tj	||t jj
| d dd<}t j|j | |j| |j  t j|j  |j}W d Q R X t j|t j}	zt j|	 W d t j|	 X t j||  d S )N.F)modedirprefixdelete)ospathdirnameexistsmakedirsstatst_mode	ExceptiontempfileNamedTemporaryFilebasenamefchmodfilenowriteflushfsyncnameopenO_RDONLYcloserename)
fnamecontent
ensure_dirr   Zcreate_modeZdnamer   fZ	tmp_fnameZ	folder_fd r)   +/usr/libexec/kcare/python/kcarectl/utils.pyatomic_write"   s$    

 
r+   utf-8c             C   s4   t | tkr| S t | tkr&| j|S | j|S d S )N)typentypebtypedecodeencode)dataencodingr)   r)   r*   nstr>   s
    
r4   latin1c             C   s   t | tkr| j|} | S )N)r-   utyper1   )r2   r3   r)   r)   r*   bstrH   s    
r7   c             C   s   t | tkr| j|} | S )N)r-   r/   r0   )r2   r3   r)   r)   r*   ustrO   s    
r8   c                s4   i  t j fdd} |_ j|_|_|S )Nc                 sH   | t t|j f}y | S  tk
r0   Y nX | | } |< |S )N)tuplesorteditemsKeyError)argskwargsZ	cache_keyresult)cachefnr)   r*   innerZ   s    zcached.<locals>.inner)	functoolswrapsr@   clearorig)rA   rB   r)   )r@   rA   r*   cachedV   s    
rG   c                sF   d krt jd krt j d kr*t j i  fdd}|S )Nc                s    fdd}|S )Nc                 s   }xlt D ]`}y
| |S  tk
rJ } z|s: W Y d d }~X nX tj| t|tjd  tj}qW y
| |S  tk
r } zt	|d  W Y d d }~X nX d S )Nr   attempts)
ranger   timesleepminrandomuniformr   RETRY_MAX_DELAYsetattr)r=   r>   Zldelay_exZfinal_ex)backoffcheck_retrycountdelayrA   stater)   r*   rB   v   s    



z'retry.<locals>.decorator.<locals>.innerr)   )rA   rB   )rS   rT   rU   rV   rW   )rA   r*   	decoratoru   s    zretry.<locals>.decorator)r   RETRY_DELAYRETRY_COUNTRETRY_BACKOFF)rT   rU   rV   rS   rX   r)   )rS   rT   rU   rV   rW   r*   retryj   s    r\   c       
      C   s   t jj| sd S g }t j| }|d k	r2tj||}xD|D ]<}t jj| |}||krh|jt j|j	|f |j
dd q8W xF||d  D ]6\}}	t jj|	st jj|	rt j|	 qtj|	 qW d S )NT)reverse)r   r   r   listdirfnmatchfilterjoinappendr   st_mtimesortisfileislinkremoveshutilrmtree)
Z	directoryexclude_pathkeep_npatternr2   r;   item	full_pathrQ   entryr)   r)   r*   clean_directory   s    

rp   c               C   sN   t tjjtjddd t tjjtjddd tjjtjrJtjtj d S )Nmodulesr   )rk   patches)	rp   r   r   ra   r   PATCH_CACHEr   CACHE_KEY_DUMP_PATHunlinkr)   r)   r)   r*   clear_all_cache   s    rv   c          
   C   s^   t jj|}t jj|s"t j| t|d(}tj| | |j  t j	|j
  W d Q R X d S )Nwb)r   r   r   r   r   r!   rh   copyfileobjr   r   r   )responsedstZ
parent_dirr(   r)   r)   r*   save_to_file   s    
r{   c             C   s   t j| }|r|jdp| S )Nr   )
VERSION_REmatchgroup)versionr}   r)   r)   r*   strip_version_timestamp   s    
r   c             C   s0   | j d\}}}|s$| j d\}}}tj|dS )NT z%Y-%m-%d)	partitionr   strptime)Zstr_rawZstr_dateseprQ   r)   r)   r*   parse_response_date   s    r   c              G   s0   ddl m} djdd td |j f|  D S )Nr   )ipv6_support/c             s   s   | ]}|j d V  qdS )r   N)strip).0itr)   r)   r*   	<genexpr>   s    z'get_patch_server_url.<locals>.<genexpr>)r   r   ra   r`   get_patch_server)partsr   r)   r)   r*   get_patch_server_url   s    r   c          	   C   s0   t jj| sd S t| }|j j S Q R X d S )N)r   r   r   r!   readr   )filenamer(   r)   r)   r*   try_to_read   s    
r   c               C   s
   t tjS )N)r   r   rt   r)   r)   r)   r*   get_cache_key   s    r   c          	   C   s.   t jj| s|S t| |
}|j S Q R X d S )N)r   r   r   r!   r   )r%   r   defaultr(   r)   r)   r*   
_read_file   s    r   c             C   s   t | d|}|S )Nr)r   )r%   r   r?   r)   r)   r*   	read_file   s    r   c             C   s   t | d|}|S )Nrb)r   )r%   r   r?   r)   r)   r*   read_file_bin   s    r   c             C   sB   i }| j  }x0|D ](}|r|jd\}}}|r|j ||< qW |S )N:)
splitlinesr   r   )r2   r?   Z
data_lineslinekey	delimitervaluer)   r)   r*   data_as_dict   s    
r   cvec             C   s0   t  }x$| D ]}|j|}|r|j| qW |S )a  
    Extract unique CVEs from a list of patches.

    Args:
        patches: List of patch dictionaries
        cve_field: Field name to extract CVE from (default 'cve' for userspace, 'kpatch-cve' for kernel)

    Returns:
        Set of unique CVE strings
    )setgetadd)rr   	cve_fieldZunique_cvespatchr   r)   r)   r*   extract_unique_cves   s    

r   c               C   s   t ttj S )N)strintrJ   r)   r)   r)   r*   timestamp_str  s    r   c              G   s   t |   dS )za workaround to fix T201N)print)valuesr)   r)   r*   print_wrapper  s    r   c                s    fdd}|S )Nc                s    t j  fdd}|S )Nc                 sn   y
| |S  k
rh } zBrZdd | D dd |j  D  }djjdj||  S d }~X nX d S )Nc             S   s   g | ]}t |qS r)   )r   )r   ar)   r)   r*   
<listcomp>&  s    zBcatch_errors.<locals>.decorator.<locals>.inner.<locals>.<listcomp>c             S   s   g | ]\}}d j ||qS )z{0}={1})format)r   kvr)   r)   r*   r   &  s    z{0}({1}) failed: {2}z, )r;   r   __name__ra   )r=   r>   earg_list)default_returnerrorsrA   loggerr)   r*   rB     s    
 z.catch_errors.<locals>.decorator.<locals>.inner)rC   rD   )rA   rB   )r   r   r   )rA   r*   rX     s    zcatch_errors.<locals>.decoratorr)   )r   r   r   rX   r)   )r   r   r   r*   catch_errors  s    r   )Fr	   r
   )r,   )r5   )r5   )NNN)N)N)r   )+
__future__r   r_   rC   r   rM   rerh   r   rJ   r   r   r   compiler|   ZCACHE_ENTRIESr-   r.   r/   r6   r+   r4   r7   r8   rG   r\   rp   rv   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r)   r)   r)   r*   <module>   sJ   






$
	


