
    x[h,                     6   S SK r S SKrS SKrS SKrS SKrS SKJrJr  S SKJ	r	J
r
  Sr\R                  " \5      r\	R                  " S5      S 5       r\	R                  " S5      S 5       r\S	S
4S jr\S	S
4S jrS r\	R                  " S5      S 5       r\4S jrg)    N)	b64decode	b64encode)performanceutili  zBase64 decodingc                 `    [        U 5      n UR                  S5      $ ! [         a    Us $ f = f)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-8)r   decodeUnicodeDecodeError)sourcedecodeds     9/usr/lib/python3/dist-packages/cloudinit/atomic_helper.pyb64dr      s6     G~~g&& s    --zBase64 encodingc                     [        U [        5      (       d  U R                  S5      n [        U 5      R	                  S5      $ )zXbase64 encode data

:param source: a bytes or str to decode
:return: base64 encoded str
r   )
isinstancebytesencoder   r	   )r   s    r   b64er      s5     fe$$w'V##G,,    wbFc                     [         R                  " SU  35         [        XX#U5      sSSS5        $ ! , (       d  f       g= f)zCopen filename in mode omode, write content, set permissions to modezWriting N)r   Timed_write_file)filenamecontentmodeomodepreserve_modes        r   
write_filer   *   s1    
 
		XhZ0	18d=I 
2	1	1s   1
?c           
         U(       a7   [         R                  " U 5      n[        R                  " UR                  5      nS n [         R
                  R                  U 5      n[        R                  " U5        [        R                  " USUS9n[        R                  SU UR                  UU[        U5      5        UR                  U5        UR!                  5         [         R"                  " UR                  U5        [         R$                  " UR                  U 5        g ! [         a     Nf = f! [&         a*  nUb   [         R(                  " UR                  5        UeS nAff = f)NF)dirdeleter   zOAtomically writing to file %s (via temporary file %s) - %s: [%o] %d bytes/chars)osstatS_IMODEst_modeOSErrorpathdirnamer   
ensure_dirtempfileNamedTemporaryFileLOGdebugnamelenwriteclosechmodrename	Exceptionunlink)	r   r   r   r   r   	file_stattfr(   es	            r   r   r   3   s    	)I<<	 1 12D 
B''//(+ ((WUO		GGL	
 	


$
		"''8$)  		*  >IIbggs*   6D CD. 
D+*D+.
E"8%EE"c                      SR                  [        U 5      5      $ ! [         a    SR                  [        U 5      5      s $ f = f)z1Handler for types which aren't json serializable.z
ci-b64:{0}z)Warning: redacted unserializable type {0})formatr   AttributeErrortype)_objs    r   json_serialize_defaultr>   U   sC    N""4:.. N:AA$t*MMNs    $AAzDumping jsonc                 :    [         R                  " U SSS[        S9$ )z%Return data in nicely formatted json.   T),z: )indent	sort_keys
separatorsdefault)jsondumpsr>   )datas    r   
json_dumpsrI   ]   s$     ::& r   c                 2    [        U [        U5      S-   SUS9$ )N
w)r   r   )r   rI   )r   rH   r   s      r   
write_jsonrM   i   s%    44	 r   )rF   loggingr"   r#   r*   base64r   r   	cloudinitr   r   
_DEF_PERMS	getLogger__name__r,   timedr   r   r   r   r>   rI   rM    r   r   <module>rV      s      	   ' '
! $%
 &
 $%- &- 'd%J 'd%DN >" # %/ r   