a '¡Dg, ã@sÄddlZddlZddlZddlZddlZddlmZmZddlm Z m Z dZ e  e ¡Ze  d¡dd„ƒZe  d¡d d „ƒZe d d fd d„Ze d d fdd„Zdd„Ze  d¡dd„ƒZe fdd„ZdS)éN)Ú b64decodeÚ b64encode)Ú performanceÚutili¤zBase64 decodingcCs0t|ƒ}z | d¡WSty*|YS0dS)z‹base64 decode data :param source: a bytes or str to decode :return: base64 as a decoded str if utf-8 encoded, otherwise bytes úutf-8N)rÚdecodeÚUnicodeDecodeError)ÚsourceZdecoded©r ú;/usr/lib/python3.9/site-packages/cloudinit/atomic_helper.pyÚb64ds   r zBase64 encodingcCs"t|tƒs| d¡}t|ƒ d¡S)zdbase64 encode data :param source: a bytes or str to decode :return: base64 encoded str r)Ú isinstanceÚbytesÚencoderr)r r r r Úb64es  rÚwbFcCsDt d|›�¡� t|||||ƒWdƒS1s60YdS)zCopen filename in mode omode, write content, set permissions to modezWriting N)rZTimedÚ _write_file)ÚfilenameÚcontentÚmodeÚomodeÚ preserve_moder r r Ú write_file*src Csê|r2zt |¡}t |j¡}Wnty0Yn0d}zrtj |¡}t |¡t j |d|d�}t   d||j ||t|ƒ¡| |¡| ¡t |j |¡t |j |¡Wn<tyä}z$|durÌt |j ¡|‚WYd}~n d}~00dS)NF)ÚdirÚdeleterzOAtomically writing to file %s (via temporary file %s) - %s: [%o] %d bytes/chars)ÚosÚstatÚS_IMODEÚst_modeÚOSErrorÚpathÚdirnamerZ ensure_dirÚtempfileZNamedTemporaryFileÚLOGÚdebugÚnameÚlenÚwriteÚcloseÚchmodÚrenameÚ ExceptionÚunlink) rrrrrZ file_statZtfr!Úer r r r3s6    ù  rcCs6zd t|ƒ¡WSty0d t|ƒ¡YS0dS)z1Handler for types which aren't json serializable.z ci-b64:{0}z)Warning: redacted unserializable type {0}N)ÚformatrÚAttributeErrorÚtype)Z_objr r r Újson_serialize_defaultUs r1z Dumping jsoncCstj|dddtd�S)z%Return data in nicely formatted json.éT)ú,z: )ÚindentZ sort_keysZ separatorsÚdefault)ÚjsonÚdumpsr1)Údatar r r Ú json_dumps]sûr9cCst|t|ƒdd|d�S)NÚ Úw)rr)rr9)rr8rr r r Ú write_jsonis  ür<)r6Zloggingrrr"Úbase64rrZ cloudinitrrZ _DEF_PERMSZ getLoggerÚ__name__r#Ztimedr rrrr1r9r<r r r r Ús(   ÿ ÿ "