
    d0                     V   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Zd dlZd dlZd dl	m
c mZ d dlZd dlmZ d Zd'dZd'dZd Zd'dZd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Z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 Z)d Z*d)dZ+d  Z,d! Z-d" Z.d# Z/d$ Z0d*d%Z1d& Z2y)+    N)ustrc                 N    | j                  d      } t        j                  |       S )z(
    Parse xml document from string
    utf-8)encodeminidomparseString)xml_texts    G/usr/lib/python3/dist-packages/azurelinuxagent/common/utils/textutil.py	parse_docr   "   s#     w'Hx((    c                 T    | g S || j                  |      S | j                  ||      S )z=
    Get all nodes by tag and namespace under Node root.
    )getElementsByTagNamegetElementsByTagNameNS)roottag	namespaces      r
   findallr   ,   s8     |	((--**9c::r   c                 J    t        | ||      }|t        |      dk\  r|d   S y)z>
    Get first node by tag and namespace under Node root.
    r   N   r   )r   len)r   r   r   nodess       r
   findr   9   s.     D#3ESZ1_Qxr   c                 z    | y| j                   D ])  }|j                  |j                  k(  s|j                  c S  y)z
    Get node text
    N)
childNodesnodeType	TEXT_NODEdata)nodechilds     r
   gettextr!   D   s>     | >>U__,:: r   c                 4    t        | ||      }t        |      S )z@
    Get text of node by tag and namespace under Node root.
    r   )r   r!   )r   r   r   r   s       r
   findtextr#   Q   s     cY/D4=r   c                 *    | | j                  |      S y)z#
    Get attribute of xml node
    N)getAttribute)r   	attr_names     r
   	getattribr'   Y   s       ++r   c                 F    d}|D ]  }|dz  t        | ||z            z  } |S )z*
    Unpack bytes into python values.
    r      
str_to_ord)bufoffsetvalue_rangeresultis        r
   unpackr1   c   s8     F =A+C
O!<<=Mr   c                 J    t        | |t        t        |dz
  dd                  S )z8
    Unpack little endian bytes into python values.
    r   r1   listranger,   r-   lengths      r
   unpack_little_endianr9   m   s%     #vtE&1*b"$=>??r   c           
      B    t        | |t        t        d|                  S )z5
    Unpack big endian bytes into python values.
    r   r4   r7   s      r
   unpack_big_endianr;   t   s     #vtE!V$4566r   c           	      n    dj                  | |||z    D cg c]  }dt        |      z   c}      S c c}w )z-
    Dump range of buf in formatted hex.
     z%02X)joinr+   )r,   r-   r8   chars       r
   	hex_dump3r@   {   s6     77#fVf_:UV$FZ--VWWVs   2c                 .    t        | dt        |             S )z$
    Dump buf in formatted hex.
    r   )r@   r   )r,   s    r
   	hex_dump2rB      s     S!SX&&r   c                 "    || cxk  xr |k  S c S )z4
    Return True if 'a' in 'low' <= a <= 'high'
     )alowhighs      r
   is_in_rangerH      s     !tr   c                     t        | t        d      t        d            xs@ t        | t        d      t        d            xs t        | t        d      t        d            S )z2
    Return True if character is displayable.
    AZrE   z09)rH   r+   )chs    r
   is_printablerP      sR     JsOZ_= A2z#
3@A2z#
3@Br   c                 |   |dk  rt        |       }d}t        d|      D ]  }|dz  dk(  r|d|z  z  }| |   }t        |      t        k(  rt	        |j                  d            }|d|z  z  }|dz  dk(  r|d	z  }|d
z   dz  dk(  s	|d
z   |k(  sn|}|d
z   dz  dk7  r#|dz  }|dz  dk(  r|d	z  }|d
z  }|d
z   dz  dk7  r#|d	z  }t        ||dz  z
  |d
z         D ]P  }| |   }t        |      t        k(  rt        |j                  d            }d}t        |      rt        |      }||z  }R |d
z   |k7  s|dz  } |S )z;
    Return Hex formated dump of a 'buffer' of 'size'.
    r   r=      z%06X: latin1z%02X        r   z   .
)	r   r6   typestrorddecoder+   rP   chr)buffersizer/   r0   bytejks          r
   hex_dumprc      s    ax6{F1d^ Fq=hl"Fay:t{{8,-D'D. Fq=cMFUbLQ1q5T/AER<A%%Ea<cMFQ	 ER<A%
 cMF1B<Q/ ay:$%dkk(&;<D%D	A! A$$56 Mr   c                 x    t        |       t        d      k(  st        |       t        d      k(  rt        |       } | S )zl
    Allows indexing into a string or an array of integers transparently.
    Generic utility function.
    r   r=   )rY   r[   rE   s    r
   r+   r+      s1    
 Aw$s)tAw$s)3FHr   c                 j    t        |||z         D ]!  }t        | |         t        ||         k7  s! y y)NFT)r6   r+   )rE   bstartr8   r-   s        r
   compare_bytesri      s>    uv~. ai Jqy$99 r   c                 >    d| dz	  dz  | dz	  dz  | dz	  dz  | dz  fz  S )z$
    Build DHCP request string.
    z%u.%u.%u.%u      rR   r)   rD   re   s    r
   int_to_ip4_addrrm      s?     Q"W,"W,!VtO$J( ( (r   c                     d}t        dt        |       dz        D ]1  }|t        j                  dt	        | |dz  |dz  dz    d            z  }3 |S )z8
    Return hex string packed into a binary struct.
    r   r      BrR   )r6   r   structpackint)rE   rg   cs      r
   hexstr_to_bytearrayru      s[     	A1c!fk" ;	V[[c!AE!a%!)"4b9::;Hr   c                    d}d}|}t        dt        |             D ]  }| |   j                  |      r||k(  rdj                  ||      | |<   d}4| |   j	                         j                  d      sW| |   j	                         j                  d      r|}|||k(  s|} |s)||k7  r|}| j                  dj                  ||             | S )NFr3   r   z{0} {1}Tmatchz	match all)r6   r   
startswithformatlowerinsert)confignamevalfoundno_matchmatch_startr0   s          r
   set_ssh_configr      s    EHK1c&k" 
 !9%+*A!((s3F1IEAY__))'2ay ++K8&(
  ("Aa))$45Mr   c                     d}|dz   }t        |       }dj                  ||      }t        t        d|            D ]  }| |   j	                  |      s|| |<   d} n |r| j                  |dz
  |       y y )NT=z	{0}="{1}"r   Fr   )r   ry   reversedr6   rx   r{   )r|   r}   r~   notfound	nameEqualr8   textr0   s           r
   set_ini_configr      s    Hs
I[Fc*DeAv&' !9	*F1IH	 fqj$' r   c                 N    d}|  | D ]  }t        |      dkD  r||z  }||z  } |S )Nr=      r*   )incomingreplace_charoutgoingrt   s       r
   replace_non_asciir   
  sB    H 	A!}s"L(A		
 Or   c                     t        |       sFt        |       dkD  r8t        | d         dkD  r't        | d         dkD  rt        | d         dkD  r| dd } | S )z[
    bom is comprised of a sequence of three chars,0xef, 0xbb, 0xbf, in case of utf-8.
    ro   r   r   r      N)is_str_none_or_whitespacer   r+   )rt   s    r
   
remove_bomr     sZ     %Q'
1vz!A$#!A$#!A$#abEHr   c                 *   t         j                  t         j                  z   dj                  fdt	        |      D              }dj                  ||      }t        j                  d   dk(  r| j                  d      } t        j                  | |      S )Nr=   c              3   H   K   | ]  }t        j                          y wN)randomchoice).0_
collections     r
   	<genexpr>z$gen_password_hash.<locals>.<genexpr>$  s     F6==,Fs   "z${0}${1}r   ro   r   )
stringascii_lettersdigitsr>   r6   ry   sysversion_infor   crypt)passwordcrypt_idsalt_lensaltr   s       @r
   gen_password_hashr   "  ss    %%5J77FeHoFFDXt,D
a??7+;;x&&r   c                 J    d}| j                  d      D ]  }d|vs||z  } |S )Nr=   rX   z----)split)pem_strbase64_byteslines      r
   get_bytes_from_pemr   ,  s9    Ld# !D L! r   c                     ddl m} |dkD  rAt        j                  t	        j
                  t        | d                  j                  d      S t        j                  t	        j
                  |             S )aT  
    Compress a string, and return the base64 encoded result of the compression.

    This method returns a string instead of a byte array.  It is expected
    that this method is called to compress smallish strings, not to compress
    the contents of a file. The output of this method is suitable for
    embedding in log statements.
    r   PY_VERSION_MAJORro   r   )azurelinuxagent.common.versionr   base64	b64encodezlibcompressbytesr\   sr   s     r
   r   r   4  sV     @!eAw.? @AHHQQDMM!,--r   c                     ddl m} |dkD  r.t        j                  t	        | d            j                  d      S t        j                  |       S Nr   r   ro   r   )r   r   r   r   r   r\   r   s     r
   r   r   C  sA    ?!a 1299'BBAr   c                     ddl m} |dkD  r$t        j                  |       j	                  d      S t        j                  |       S r   )r   r   r   	b64decoder\   r   s     r
   r   r   J  s:    ?!"))'22Ar   c                     dd l }ddlm} |d d dk(  r |j                  | j	                  d            S |j                  |       S )Nr   )
PY_VERSIONro   )ro      r   )shlexr   r   r   r   )r   r   r   s      r
   safe_shlex_splitr   Q  s<    9"1~{{188G,--;;q>r   c           	          t        |       |z  }|dk7  rd|t        |       |z  z
  z  | z   } dj                  t        t        j                  dj                  |      | t        j                                    S )Nr   rM   r=   z[a-f0-9]{{{0}}})r   r>   r   rer   ry   
IGNORECASE)r   widthrs      r
   swap_hexstringr   Y  sn    AAAvESVe^,-2778

 2 9 9% @ ! "/0 1 1r   c                 h    d}t        |       s$ddl}|j                  | j                  d            }|S )z=
    Parse json string and return a resulting dictionary
    Nr    	
 )is_str_emptyjsonloadsrstrip)json_strr/   r   s      r
   
parse_jsonr   e  s0    
 F!HOOK89Mr   c                 N    | d u xs  t        |       dk(  xs | j                         S )Nr   )r   isspacer   s    r
   r   r   r  s$    92A!2qyy{2r   c                 P    t        |       xs t        | j                  d            S )Nr   )r   r   r   s    r
   r   r   v  s!    $Q'[+DQXXkEZ+[[r   c                     t        j                         }| D ]!  }|j                  |j                                # |j	                         S )z
    Compute a cryptographic hash of a list of strings

    :param string_list: The strings to be hashed
    :return: The cryptographic hash (digest) of the strings in the order provided
    )hashlibsha1updater   digest)string_list	sha1_hashitems      r
   hash_stringsr   z  sA     I ('(r   c                     ddddd}| |vr(t        dj                  |j                                     	 t        |      }t        |||    z        S # t        $ r t	        d      w xY w)Nr   i   i   i   @)r   	kilobytes	megabytes	gigabyteszUnit must be one of {0}z$Value must be convertible to a float)
ValueErrorry   keysfloat	TypeErrorrs   )unitvalueunitss      r
   format_memory_valuer     sz    dQ_`E5299%**,GHH@e uuT{"##  @>??@s   A A&c                     ddl m} | t        |       t        u r| S |dkD  r-	 t	        | t
              r| j                  |      S t        |       S t        | |      S # t        $ r t        |       cY S w xY w)a  
    This function takes the string and converts it into the corresponding encoded ustr if its not already a ustr.
    The encoding is utf-8 by default if not specified.
    Note: ustr() is a unicode object for Py2 and a str object for Py3.
    :param s: The string to convert to ustr
    :param encoding: Encoding to use. Utf-8 by default
    :return: Returns the corresponding ustr string. Returns None if input is None.
    r   r   ro   )encoding)r   r   rY   r   
isinstancer   r\   	Exception)r   r   r   s      r
   str_to_encoded_ustrr     sv     @yDGtO!
	!U#xx)) Aw
 H%%	  	7N	s    A 
A A21A2c                 J   d }t         j                  d   dk(  rt        j                         \  }}}n| j                  }t	        |       dz   }|t         j                  d   dk(  r|| k7  r|dz  }|S |dj                  t        j                  t        |       | |            z  }|S )Nr   ro   rX   z[Traceback not available]r=   )r   tb)	r   r   exc_info__traceback__r   r>   	tracebackformat_exceptionrY   )	exceptioner   r   msgs        r
   r   r     s    A
a<<>1b$$
y/D
 C	zc&&q)Q.1	>** J 	rwwy11$y/WYZ[[Jr   r   )r=   )ro   )r   )3r   r   r   r   r   r   rq   r   r   xml.dom.minidomdomr   r   azurelinuxagent.common.futurer   r   r   r   r!   r#   r'   r1   r9   r;   r@   rB   rH   rP   rc   r+   ri   rm   ru   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rD   r   r
   <module>r      s   &     	   
  ! !  .)
;
@7X'B"J(.( 
'.	1
3\

$&@r   