
    Hmd                     N   d dl Z d dlmZmZ d dlmZmZ d dlmc m	Z d dl
Z
d dlZd dlmZ dZdZdZdZd	Z eed
          rej        neZi a G d de          Zd Zd Zd d g dfdZ	 	 d&dZd Zd'dZd(dZd(dZd)dZ d*dZ!d Z"ed              Z#d! Z$d" Z%d# Z&d$ Z'd% Z(dS )+    N)PIPEPopen)EEXISTENOENT)datetimez$00000000-0000-0000-0000-000000000001   z%20z%0Az%25
ParseErrorc                       e Zd ZdZdZdZdZdS )
RecordTypeNEWMODIFYRENAMEDELETEN)__name__
__module____qualname__r   r   r   r        8/usr/lib/x86_64-linux-gnu/glusterfs/glusterfind/utils.pyr   r      s"        
CFFFFFr   r   c                       fd}|S )Nc                      t                               j        d            | i |t           j        <   t           j                 S N)
cache_datagetr   )argskwargsfuncs     r   wrapperzcache_output.<locals>.wrapper%   sA    >>$-..6(,d(=f(=(=Jt}%$-((r   r   )r   r   s   ` r   cache_outputr   $   s#    ) ) ) ) ) Nr   c                 B    |d         j         t          k    rd S |d         )N   )errnor   )r   pathexc_infos      r   handle_rm_errorr%   .   s#    {F""
1+r   c                     dS NTr   xs    r   <lambda>r*   5   s    t r   c                     dS r'   r   r(   s    r   r*   r*   5   s    4 r   Tc                    | |v rd S  ||           }|* || |t           j                            |                      t          j        |           D ]}t           j                            | |          }t           j                            |          }|r/|rt          ||||           X ||          }| |||           r ||          }| ||||           d S r   )osr#   isdirlistdirjoinfind)	r#   callback_funcfilter_funcignore_dirssubdirs_crawlfilter_resultp	full_pathis_dirs	            r   r1   r1   5   s   {  K%%M dM27==+>+>???Z @ @GLLq))	y)) 
	@ <Y{KHHHH +I 6 6 ,!M)];;;'K	22M(i???@ @r   .F  c                     |dk    rd S |dk    r t           j                            ||          }|rt          |          }|dk    rdn|}|                     |                                ||d           d S )Nr;   r:   
)r-   r#   r0   quote_plus_space_newlinewritestrip)fr#   prefixencodetagfield_separatorFSs          r   output_writerH   P   s    rzz}}w||FD)) .'-- byyoBGG#))+++rr444011111r   c                 j    t          j        t          |                                         d          S )Nz%Y-%m-%d %H:%M:%S)r   fromtimestampfloatstrftime)tss    r   
human_timerN   a   s(    !%)),,556IJJJr   c                 ,   |r |                      t          j                   n|                      t          j                   t          j        |          }t          j        d          }|                    |           |                     |           d S )NzP[%(asctime)s] %(levelname)s [%(module)s - %(lineno)s:%(funcName)s] - %(message)s)setLevelloggingDEBUGINFOFileHandler	FormattersetFormatter
addHandler)loggerr#   debugfh	formatters        r   setup_loggerr\   e   s     &&&&&%%% 
	T	"	"B! #2 3 3I OOI br   c                     	 t          | d                                           dS # t          t          f$ r&}|rt	          d| d||           n Y d}~dS d}~ww xY w)z
    If file exists overwrite. Print error to stderr and exit
    if exit_on_err is set, else raise the exception. Consumer
    should handle the exception.
    wzFailed to create file : rX   N)opencloseOSErrorIOErrorfailr#   exit_on_errrX   es       r   create_fileri   x   s    T3W    	D444;FKKKKK LKKKKKs   "& AAAc                    	 t          j        |            dS # t          t          f$ r\}|j        t
          k    r t           j                            |           rn|rt          d| d||           n Y d}~dS Y d}~dS d}~ww xY w)z
    Try creating required directory structure
    ignore EEXIST and raise exception for rest of the errors.
    Print error in stderr and exit if exit_on_err is set, else
    raise exception.
    zFail to create dir r_   r`   N)	r-   makedirsrc   rd   r"   r   r#   r.   re   rf   s       r   mkdirprl      s    	
DW   7ft!4!4 DDD!!<VLLLLL DDDDD MLLLLLs    BAB  Br!   c                     |r|                     |            t          j                            d| z             t          j        |           dS )z(
    Write error to stderr and exit
    z%s
N)errorsysstderrr@   exit)msgcoderX   s      r   re   re      sI      SJVc\"""HTNNNNNr   c                     t          | t          t          t          d          }|                                \  }}|j        dk    r|t	          |d||j        |           |j        ||fS )z
    If failure_msg is not None then return returncode, out and error.
    If failure msg is set, write to stderr and exit.
    T)stdinstdoutrp   	close_fdsr   Nr_   r`   )r   r   communicate
returncodere   )cmdexit_msgrX   r7   outerrs         r   executer~      sp    
 	cd44HHHAJS#|qX133'fEEEEL#s##r   c                    |t           k    rdS d}	 t          j                            | d|dd         |dd         |          }t          j        |          }t          j                            |          }t          j                            t          j                            |          |          }|dt           z  k    rn t          j                            |          }|S )z
    Each directories are symlinked to file named GFID
    in .glusterfs directory of brick backend. Using readlink
    we get PARGFID/basename of dir. readlink recursively till
    we get PARGFID as ROOT_GFID.
    r;   Tz
.glusterfsr         z../../00/00/%s)	ROOT_GFIDr-   r#   r0   readlinkdirnamebasename)brickgfidout_pathr#   path_readlinkpgfids         r   symlink_gfid_to_pathr      s     yrH'w||E<acD1ItLLD))..7<< 0 0 ? ?JJ$y000w&&' Or   c                      g d} t          |           \  }}}|dk    rd S t          j        |          }|                    d          }|j        S )N)glusterzsystem::uuidr   --xmlr   zuuidGenerate/uuid)r~   etree
fromstringr1   text)rz   rcr|   r}   treeuuid_els         r   get_my_uuidr      sV    
9
9
9C3<<LBS	QwwtC  Dii+,,G<r   c                 2    t                      }|| k    rdS dS )NTF)r   )	host_uuidmy_uuids     r   is_host_localr      s"     mmG)t5r   c                 8   ddd| ddg}t          |          \  }}}|dk    rt          S 	 t          j        |          }|                    d          j        }|)t          |                    dd	          d                   S d S # t          $ r
 t          cY S w xY w)
Nr   volumer   zchangelog.rollover-timer   r   zvolGetopts/Opt/Valuer<   r!   )	r~   DEFAULT_CHANGELOG_INTERVALr   r   r1   r   intsplitr	   )
volumenamerz   r   r|   r}   r   vals          r   get_changelog_rollover_timer      s    hz$g/C3<<LBS	Qww))*$$ii.//4? syya((+,,, ?  * * *))))*s   AB BBc                     |j         dk    rTt          j                            |j         |           } |                     d          r| dt          |           dz
           } |j        r| S t          |           S )zS
    If Prefix is set, joins to Path, removes ending slash
    and encodes it.
    r:   /r   r!   )output_prefixr-   r#   r0   endswithlen	no_encoder?   )r#   r   s     r   output_path_preparer      ss    
 S  w||D.55== 	'#d))A+&D~ .'---r   c                     |                      t          d                               t          d                               t          d          S )Nr<   r>   %)replaceSPACE_ESCAPE_CHARNEWLINE_ESCAPE_CHARPERCENTAGE_ESCAPE_CHARss    r   unquote_plus_space_newliner     s9    99&,,W($//W+S112r   c                     |                      dt                                         dt                                         dt                    S )Nr   r<   r>   )r   r   r   r   r   s    r   r?   r?     s9    99S011WS+,,WT.//0r   )r:   Fr;   r<   )F)FN)r!   N)NN))ro   
subprocessr   r   r"   r   r   xml.etree.cElementTreer   cElementTreerQ   r-   r   r   r   r   r   r   hasattrr	   SyntaxErrorr   objectr   r   r%   r1   rH   rN   r\   ri   rl   re   r~   r   r   r   r   r   r   r?   r   r   r   <module>r      s@   


 " " " " " " " "                 & & & & & & & & &  				      2	    !(!=!=NU;

            ,^t@ @ @ @6 9;!$2 2 2 2"K K K   &      &   $ $ $ $  , 	 	 	  * * *&. . . 2 2 20 0 0 0 0r   