a i!@sZddlmZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z edZdZedZed ZedZd9d dZd:ddZd;ddZdd-d.Z*d?d/d0Z+d1d2Z,d3d4Z-d5d6Z.de/fdfd7d8Z0dS)@)print_functionN)datetime) constantsz^(\d+[.]\d+[-]\d+)Fwc Cstj|}|r&tj|s&t|zt|j}WntyL|}Yn0tj ||tj |dddF}t | || ||t| |j}Wdn1s0Yt|tj} zt| Wt| n t| 0t||dS)N.F)modedirprefixdelete)ospathdirnameexistsmakedirsstatst_mode ExceptiontempfileNamedTemporaryFilebasenamefchmodfilenowriteflushfsyncnameopenO_RDONLYcloserename) fnamecontent ensure_dirr Z create_modeZdnamerfZ tmp_fnameZ folder_fdr)+/usr/libexec/kcare/python/kcarectl/utils.py atomic_write"s$      $ r+utf-8cCs4t|tur|St|tur&||S||SdSN)typentypebtypedecodeencodedataencodingr)r)r*nstr>s    r6latin1cCst|tur||}|Sr-)r.utyper2r3r)r)r*bstrHs  r9cCst|tur||}|Sr-)r.r0r1r3r)r)r*ustrOs  r:cs4itfdd}|_j|_|_|S)NcsL|tt|f}z |WSty0Yn0|i|}|<|Sr-)tuplesorteditemsKeyError)argskwargsZ cache_keyresultcachefnr)r*innerZs  zcached..inner) functoolswrapsrCclearorigrDrEr)rBr*cachedVs rKcsFdurtjdurtjdur*tjifdd}|S)Ncsfdd}|S)Nc s}tD]l}z|i|WStyT}z|s@WYd}~n d}~00t|t|tdtj}q z|i|WSty}zt |dWYd}~n d}~00dS)Nrattempts) rangertimesleepminrandomuniformrRETRY_MAX_DELAYsetattr)r?r@Zldelay_exZfinal_ex)backoff check_retrycountdelayrDstater)r*rEvs    z'retry..decorator..innerr)rJrWrXrYrZr[rDr* decoratoruszretry..decorator)r RETRY_DELAY RETRY_COUNT RETRY_BACKOFF)rXrYrZrWr^r)r\r*retryjsrbc Cstj|sdSg}t|}|dur2t||}|D]<}tj||}||krf|t|j |f|j ddq6||dD]6\}} tj | stj | rt | qt| qdS)NT)reverse)rrrlistdirfnmatchfilterjoinappendrst_mtimesortisfileislinkremoveshutilrmtree) Z directory exclude_pathkeep_npatternr4r=item full_pathrUentryr)r)r*clean_directorys    rvcCsNttjtjdddttjtjdddtjtjrJttjdS)Nmodulesr)rqpatches) rvrrrgr PATCH_CACHErCACHE_KEY_DUMP_PATHunlinkr)r)r)r*clear_all_cachesr|cCsrtj|}tj|s"t|t|d2}t|||t | Wdn1sd0YdS)Nwb) rrrrrr!rn copyfileobjrrr)responsedstZ parent_dirr(r)r)r* save_to_files     rcCst|}|r|dp|S)Nr) VERSION_REmatchgroup)versionrr)r)r*strip_version_timestamps rcCs0|d\}}}|s$|d\}}}t|dS)NT z%Y-%m-%d) partitionrstrptime)Zstr_rawZstr_dateseprUr)r)r*parse_response_datesrcGs0ddlm}dddtd|f|DS)Nr) ipv6_support/css|]}|dVqdS)rN)strip).0itr)r)r* rz'get_patch_server_url..)rrrgrfget_patch_server)partsrr)r)r*get_patch_server_urls rcCsHtj|sdSt|}|WdS1s:0YdSr-)rrrr!readr)filenamer(r)r)r* try_to_reads  rcCs ttjSr-)rrrzr)r)r)r* get_cache_keysrcCsFtj|s|St||}|WdS1s80YdSr-)rrrr!r)r%r defaultr(r)r)r* _read_files  rcCst|d|}|S)Nrrr%rrAr)r)r* read_files rcCst|d|}|S)Nrbrrr)r)r* read_file_bins rcCs>i}|}|D](}|r|d\}}}|r|||<q|S)N:) splitlinesrr)r4rAZ data_lineslinekey delimitervaluer)r)r* data_as_dictsrcCstttSr-)strintrNr)r)r)r* timestamp_strsrcGs t|dS)za workaround to fix T201N)print)valuesr)r)r* print_wrappersrcsfdd}|S)Ncs tfdd}|S)Nc s|z|i|WSyv}zNr^dd|Ddd|D}djd||WYd}~Sd}~00dS)NcSsg|] }t|qSr))r)rar)r)r* rzBcatch_errors..decorator..inner..cSsg|]\}}d||qS)z{0}={1})format)rkvr)r)r*rrz{0}({1}) failed: {2}z, )r=r__name__rg)r?r@earg_list)default_returnerrorsrDloggerr)r*rE s z.catch_errors..decorator..inner)rFrGrJrrrr]r*r^ s zcatch_errors..decoratorr))rrrr^r)rr* catch_errorssr)Fr r )r,)r7)r7)NNN)N)N)1 __future__rrerFrrQrernrrNrrrAnyCallableOptionalTupleTypeVarUnionrcompilerZ CACHE_ENTRIESr.r/r0r8r+r6r9r:rKrbrvr|rrrrrrrrrrrrrrr)r)r)r*sJ        $