
    dp                        d 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c m	Z	 ddl
mc mc mZ ddlmZ ej                  ej                   ej"                  ej$                  ej&                  ej(                  ej*                  dgZddZddZddZd Zd	 Zdd
Zd Zd Zd Zd Z d Z!ddZ"d Z#d Z$d Z%d Z&d Z'ddZ(y)z
File operation util functions
    N)ustry   c                     d}t        | |      5 }|j                         }|
	 ddd       y|r|cddd       S |rt        j                  |      }t	        ||      }|cddd       S # 1 sw Y   yxY w)z1
    Read and return contents of 'filepath'.
    rbN)encoding)openreadtextutil
remove_bomr   )filepathasbinr   r   modein_filedatas          G/usr/lib/python3/dist-packages/azurelinuxagent/common/utils/fileutil.py	read_filer   0   s}     D	h	 ||~< 
   &&t,DD8,  s   A(A(%A((A1c                     |rdnd}|}|s|j                  |      }t        | |      5 }|j                  |       ddd       y# 1 sw Y   yxY w)z)
    Write 'contents' to 'filepath'.
    abwbN)encoder   write)r   contentsr   r   appendr   r   out_files           r   
write_filer   D   sP     4tDDx(	h	 t  s   AAc                 $    t        | |||d       y)z*
    Append 'contents' to 'filepath'.
    T)r   r   r   N)r   )r   r   r   r   s       r   append_filer   P   s     x$O    c                 J    t         j                  j                  |       \  }}|S N)ospathsplit)r"   headtails      r   	base_namer&   W   s    t$JD$Kr   c                 l    t        |      j                  d      D ]  }|j                  |       s|c S  y)zE
    Return line from 'filepath' if the line startswith 'prefix'
    
N)r   r#   
startswith)prefixr   lines      r   get_line_startingwithr,   \   s9     (#))$/ ??6"K r   c                     t         j                  j                  |       st        j                  |        |t	        | |       |t        | |       y y r    )r!   r"   isdirmakedirschmodchowner)dirpathr   owners      r   mkdirr4   f   sC    77==!
Ggt r   c                     t         j                  j                  |       s%t        j                  dj                  |              y t        j                  |      }t        j                  | |d   |d          y )NPath does not exist: {0}      )	r!   r"   existsloggererrorformatpwdgetpwnamchown)r"   r3   
owner_infos      r   r1   r1   o   sP    77>>$/66t<=\\%(

z!}jm4r   c                     t         j                  j                  |       s%t        j                  dj                  |              y t        j                  | |       y )Nr6   )r!   r"   r9   r:   r;   r<   r0   )r"   r   s     r   r0   r0   w   s8    77>>$/66t<=
tr   c                      | D ]Q  }t        j                   |      D ]7  }t        j                  j                  |      s#t        j                  |       9 S y r    )globr!   r"   isfileremove)argspathsr"   s      r   rm_filesrH   ~   sD      IIe$ 	 Dww~~d#		$	  r   c                     | D ]  }t         j                  j                  |      s#t        j                  |      D ]  }t         j                  j	                  ||      }t         j                  j                  |      rt        j                  |       Xt         j                  j                  |      rt        j                  |       t         j                  j                  |      st        j                  |         y)z/
    Remove the contents of each directory
    N)r!   r"   r.   listdirjoinrD   rE   islinkunlinkshutilrmtree)rF   pppr"   s       r   rm_dirsrR      s      $ww}}Q**Q- 	$B77<<2&Dww~~d#		$%		$t$d#	$	$r   c                 |    |j                  d      sd|z   }| j                  |      r| j                  |      d   S | S )N.r   )r)   endswithr#   )r"   exts     r   trim_extrW      s:    >>#Ci!%s!34::c?1==r   c                 z   g }t         j                  j                  |       s|rt        dj	                  |             t        |       j                  d      }|D cg c]%  }|!t        |      dkD  r|j                  |      s|' }}|j                  |       t        | dj                  |      dz          y c c}w )NzCan't find config file:{0}r(   r   )r!   r"   rD   IOErrorr<   r   r#   lenr)   r   r   rK   )r"   
line_startvalchk_errconfxs         r   update_conf_filer`      s    D77>>$G299$?@@T?  &D N!}Q!ALL4L  ND NKKtTYYt_t+,Ns   *B8c                     t        j                  |       D ]6  \  }}}|D ]+  }||k(  s	t         j                  j                  ||      c c S  8 y r    )r!   walkr"   rK   )target_dir_nametarget_file_namerootdirsfiles	file_names         r   search_fileri      sS    WW_5 5dE 	5I,,ww||D)44	55 r   c                     t        j                  |       D ]A  \  }}}|D ]6  }t        j                  t         j                  j	                  ||      |       8 C y r    )r!   rb   r0   r"   rK   )r"   r   re   rf   rg   rh   s         r   
chmod_treerk      sL    WWT] :dE 	:IHHRWW\\$	2D9	::r   c                     	 t        | d      5 }|j                         D ]  }||j                         k(  s ddd       y 	 ddd       y# 1 sw Y   yxY w# t        $ r Y yw xY w)zh
    Return True if the line is in the file; False otherwise.
    (Trailing whitespace is ignored.)
    rNTF)r   	readlinesrstrip	Exception)	file_pathline_strfhr+   s       r   findstr_in_filert      ss    
)S! 	 R  t{{},	  	  	  	    s>   A 'AAA  AA AA A 	A#"A#c                     	 t        | d      5 }t        j                  |      }|j                         D ]'  }t        j                  ||      }|s|c cddd       S  	 ddd       y# 1 sw Y   yxY w#  Y yxY w)z/
    Return match object if found in file.
    rm   N)r   recompilern   search)rq   line_rers   patternr+   matchs         r   findre_in_filer|      s~    )S! 	!Rjj)G !		'40 L	! 	!!	! 	! s;   A4 AA(A(	A4 A(A4 (A1-A4 1A4 4A8c           
          g }t        j                  |       D ]C  \  }}}|j                  |D cg c]"  }t         j                  j	                  ||      $ c}       E |S c c}w )z2
    Find all files under the given root path
    )r!   rb   extendr"   rK   )	root_pathresultre   rf   rg   files         r   get_all_filesr      s]     FWWY/ DdEEBDrww||D$/BCD M Cs   'A 
c                     |g }t        | t              rk| j                  t        v rX|D ]R  }|	 t        j
                  j                  |      rt        j                  |d       nt	        j                  |       T yyy# t        $ r Y cw xY w)z
    Clean-up possibly bad files and directories after an IO error.
    The code ignores *all* errors since disk state may be unhealthy.
    NT)ignore_errors)
isinstancerY   errnoKNOWN_IOERRORSr!   r"   r.   rN   rO   rE   rp   )erG   r"   s      r   clean_ioerrorr      s    
 }!W!''^"; 	D|77==&MM$d;IIdO	 #<  s   AB	BB)FFutf-8)Fr   F)Fr   )NN)Fr    ))__doc__r   rC   r!   r=   rv   rN   azurelinuxagent.common.loggercommonr:   %azurelinuxagent.common.utils.textutilutilsr
   azurelinuxagent.common.futurer   EIOENOMEMENFILEEMFILEENOSPCENAMETOOLONGELOOPr   r   r   r   r&   r,   r4   r1   r0   rH   rR   rW   r`   ri   rk   rt   r|   r   r    r   r   <module>r      s   (   	 
 	  . . 8 8 . 
II	LL	LL	LL	LL		KK	(	P
 5 $$>-: "r   