
N_c           @   s  d  Z  d d l Z d d l Z d d l Z d d l Z d d l m 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 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 g Z y d d l Z e j d  Wn e k
 rTd Z n Xd d l m Z m Z d d l  Ty d d l! Z! d d l" Z! Wn e k
 rd Z! n Xy+ d d l# Z# e$ d	 d
 d d d g  Z% WnN e k
 r&d d l& Z& d d l' Z' e$ d	 d
 g  Z% d dY d     YZ# n Xe$   Z( x e) e%  D] Z* y e# j+ e*  Wq=e, k
 rZ- e. e- e/  re0 e-  j1 d  re( j2 e*  n e j3 d e4 e*  IJe% j5 e*  q=Xq=Wx0 dZ D] Z* e* e% k re* g Z6 PqqWe d  d d l m7 Z7 m8 Z8 d d l9 m: Z: m; Z; i  a< i  a= d   Z> d   Z? d a@ d   ZA eB d  ZC d aD d   ZE d   ZF d   ZG d aH d   ZI d   ZJ d   ZK d d[ d      YZL d! d\ d"     YZM d] d d%  ZN d&   ZO d' d^ d(     YZP d)   ZQ d* d+  ZR eB d,  ZS d-   ZT d.   ZU d d eV d/  ZW d0   ZX d d1  ZY d2 eV d3 d4  ZZ d5   Z[ d6   Z\ d7   Z] d8   Z^ d9   Z_ d:   Z` d;   Za d<   Zb d=   Zc d>   Zd d? d@  Ze d? dA  Zf dB   Zg dC jh dD   ei dE  D  Zj eB dF  Zk dG   Zl eB dH  Zm dI   Zn d ao dJ   Zp d d lq Zq eV eB dK  Zr dL   Zs dM   Zt dN   Zu dO   Zv d eB eB dP  Zw eB dQ  Zx eV dR  Zy d az dS   Z{ dT   Z| dU   Z} dV   Z~ dW   Z dX   Z d S(_   s%   
Assorted utility functions for yum.
iN(   t   StringIOt   gzt   bz2t   xz(   t   stringToVersiont   flagToString(   t   *t   md5t   sha1t   sha256t   sha384t   sha512t   hashlibc           B   s   e  Z e d     Z RS(   c         C   s9   |  d k r t  j   S|  d k r, t j   St d  d  S(   NR   R   s   Bad checksum type(   R   t   newt   shat
   ValueError(   t   algo(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyR   4   s
    

(   t   __name__t
   __module__t   staticmethodR   (    (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyR   2   s   s   disabled for fipss   Checksum type %s disableds   broken hashlib(   t	   MiscErrort   FIPSNonCompliantError(   t   to_utf8t
   to_unicodec         C   sG   t  } t |  t  r t } n  t |   t j k r7 |  S| j |  |   S(   s    Take a value and use the same value from the store,
        if the value isn't in the store this one becomes the shared version. (   t   _share_data_storet
   isinstancet   unicodet   _share_data_store_ut   typet   typest	   TupleTypet
   setdefault(   t   valuet   store(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt
   share_data[   s    	c           C   s   i  a  i  a d  S(   N(   R   R   (    (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   unshare_datak   s    c         C   s+   t  d k r! t j d  j a  n  t  |   S(   s(    Tests if a string is a shell wildcard. s   [*?]|\[.+\]N(   t   _re_compiled_glob_matcht   Nonet   ret   compilet   search(   t   s(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   re_globr   s    c            s   t     r[ y5 | r t j p! d } t j t j    |  j SWq[ t j k
 rW q[ Xn  | rz   j       f d   S  f d   S(   s5    Compile shell wildcards, return a 'match' function. i    c            s   |  j      k S(   N(   t   lower(   R)   (   t   pat(    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   <lambda>   s    c            s
   |    k S(   N(    (   R)   (   R,   (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyR-      s    (	   R*   R&   t   IR'   t   fnmatcht	   translatet   matcht   errorR+   (   R,   t   ignore_caset   flags(    (   R,   s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   compile_pattern{   s     c         C   s+   t  d k r! t j d  j a  n  t  |   S(   s    Tests if a string could be a filename. We still get negated character
        classes wrong (are they supported), and ranges in character classes. s   [/*?]|\[[^]]*/[^]]*\]N(   t   _re_compiled_filename_matchR%   R&   R'   R1   (   R)   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   re_filename   s    c         C   s$   t  |   r t S|  d k r  t St S(   s"   Tests if a filename string, can be matched against just primary.
        Note that this can produce false negatives (Eg. /b?n/zsh) but not false
        positives (because the former is a perf hit, and the later is a
        failure). Note that this is a superset of re_primary_dirname(). s   /usr/lib/sendmail(   t   re_primary_dirnamet   Truet   False(   t   filename(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   re_primary_filename   s
    c         C   s'   d |  k r t  S|  j d  r# t  St S(   s~    Tests if a dirname string, can be matched against just primary. Note
        that this is a subset of re_primary_filename(). s   bin/s   /etc/(   R9   t
   startswithR:   (   t   dirname(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyR8      s
    c         C   sd   t  d k r? t j d  j } t j d  j } | | f a  n  x t  D] } | |   rF t SqF Wt S(   sC    Tests if a string needs a full nevra match, instead of just name. s   .*([-.*?]|\[.+\]).s   [0-9]+:N(   t   _re_compiled_full_matchR%   R&   R'   R1   R9   R:   (   R)   t   onet   twot   rec(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   re_full_search_needed   s    c         C   sI   |  j    }  |  j d  r t S|  j d  r2 t S|  j d  rE t St S(   s8    Tests if a string is a "remote" URL, http, https, ftp. s   http://s   https://s   ftp://(   R+   R=   R9   R:   (   R)   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   re_remote_url   s    c         C   s,  t  |   } | d k r g  Sy t |   } Wn t k
 r? n Xt |  Sy t |   } | j   Wn t k
 rz ~ n{ X| d k s t  | d } d } } xI | | k  r | | | k r | | | | <} | d 7} n  | d 7} q W| |  Sg  } x* |  D]" } | | k r| j |  qqW| S(   s  Return a list of the elements in s, but without duplicates.

    For example, unique([1,2,3,1,2,3]) is some permutation of [1,2,3],
    unique("abcabc") some permutation of ["a", "b", "c"], and
    unique(([1, 2], [2, 3], [1, 2])) some permutation of
    [[2, 3], [1, 2]].

    For best speed, all sequence elements should be hashable.  Then
    unique() will usually work in linear time.

    If not possible, the sequence elements should enjoy a total
    ordering, and if list(s).sort() doesn't raise TypeError it's
    assumed that they do enjoy a total ordering.  Then unique() will
    usually work in O(N*log2(N)) time.

    If that's not possible either, the sequence elements must support
    equality-testing.  Then unique() will usually work in quadratic
    time.
    i    i   (   t   lent   sett	   TypeErrort   listt   sortt   AssertionErrort   append(   R)   t   nt   ut   tt   lastt   lastit   it   x(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   unique   s6    
	

t	   Checksumsc           B   sz   e  Z d  Z d e e d  Z d   Z e d d    Z d   Z	 d d  Z
 d	   Z d d
  Z d   Z d d  Z RS(   sn    Generate checksum(s), on given pieces of data. Producing the
        Length and the result(s) when complete. c         C   s  | d  k r t } n  g  |  _ g  |  _ d |  _ t   } x | D] } | d k r[ d } n  | | k rm q@ n  | t k r t j |  } n4 | r q@ n( | t	 k r t
 |   n t d |  | j |  |  j j |  |  j j |  q@ W| r
| r
t d  n  d  S(   Ni    R   R   s(   Error Checksumming, bad checksum type %ss*   Error Checksumming, no valid checksum type(   R%   t   _default_checksumst	   _sumalgost	   _sumtypest   _lenRF   t   _available_checksumsR   R   t   _fips_noncompliantR   R   t   addRK   (   t   selft	   checksumst   ignore_missingt   ignore_nonet   donet   sumtypet   sumalgo(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   __init__	  s.    						c         C   s   |  j  S(   N(   RX   (   R\   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   __len__%  s    t   fgetc         C   s   |  j  S(   N(   RX   (   R\   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyR-   )  s    c         C   s:   |  j  t |  7_  x |  j D] } | j |  q Wd  S(   N(   RX   RE   RV   t   update(   R\   t   dataRb   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyRf   +  s    i   i   c         C   s    | j  |  } |  j |  | S(   N(   t   readRf   (   R\   t   fot   sizeRg   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyRh   0  s    c         C   s@   i  } x3 t  |  j |  j  D] \ } } | j   | | <q W| S(   N(   t   zipRW   RV   t	   hexdigest(   R\   t   retRa   t   sumdata(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt
   hexdigests5  s    "c         C   sL   | d  k r) |  j s d  S|  j d } n  | d k r> d } n  |  j   | S(   Ni    R   R   (   R%   RW   Ro   (   R\   t   checksum(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyRl   ;  s    		c         C   s@   i  } x3 t  |  j |  j  D] \ } } | j   | | <q W| S(   N(   Rk   RW   RV   t   digest(   R\   Rm   Ra   Rn   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   digestsD  s    "c         C   sL   | d  k r) |  j s d  S|  j d } n  | d k r> d } n  |  j   | S(   Ni    R   R   (   R%   RW   Rr   (   R\   Rp   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyRq   J  s    		Ni   (   R   R   t   __doc__R%   R:   Rc   Rd   t   propertyt   lengthRf   Rh   Ro   Rl   Rr   Rq   (    (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyRT     s   					t   AutoFileChecksumsc           B   s2   e  Z d  Z e e d  Z d   Z d d  Z RS(   sk    Generate checksum(s), on given file/fileobject. Pretending to be a file
        object (overrrides read). c         C   s"   | |  _  t | | |  |  _ d  S(   N(   t   _foRT   R]   (   R\   Ri   R]   R^   R_   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyRc   X  s    	c         C   s   t  |  j |  S(   N(   t   getattrRw   (   R\   t   attr(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   __getattr__\  s    ic         C   s   |  j  j |  j |  S(   N(   R]   Rh   Rw   (   R\   Rj   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyRh   _  s    (   R   R   Rs   R:   Rc   Rz   Rh   (    (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyRv   T  s   	i   i   c         C   s  y t  |  t j k r! | } n t | d  } t |  g  } x5 | j | |  rv | d k	 rB | j | k rB PqB qB Wt  |  t j k r | j	   n  | d k	 r | | j k r d | | j
 |   f S| j
 |   SWn& t t f k
 r} t d |  n Xd S(   s   takes filename, hand back Checksum of it
       sumtype = md5 or sha/sha1/sha256/sha512 (note sha == sha1)
       filename = /path/to/file
       CHUNK=65536 by defaultt   rs   !%u!%ss#   Error opening file for checksum: %sN(   R   R   t   StringTypest   openRT   Rh   R%   Ru   t
   StringTypet   closeRl   t   IOErrort   OSErrorR   (   Ra   t   filet   CHUNKt   datasizeRi   Rg   t   e(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyRp   c  s    	c         C   s   t  |  } y t j |   } Wn& t k
 rG } t d |  | f  n Xx | D] } t j j |  d |  r t |  d | | |  } qO | s | | j   d | k rO t j j	 |  d |  } | j
 |  qO qO W| S(   sf   Return all files in path matching ext, store them in filelist, 
       recurse dirs return list objects    Error accessing directory %s, %st   /s   %s(   RE   t   ost   listdirR   R   t   patht   isdirt   getFileListR+   t   normpathRK   (   R   t   extt   filelistt   extlent   dir_listR   t   dt   newpath(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyR     s    "t   GenericHolderc           B   s,   e  Z d  Z d d  Z d   Z d   Z RS(   s   Generic Holder class used to hold other objects of known types
       It exists purely to be able to do object.somestuff, object.someotherstuff
       or object[key] and pass object to another function that will 
       understand itc         C   s   | |  _  d  S(   N(   t   _GenericHolder__iter(   R\   t   iter(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyRc     s    c         C   s$   |  j  d  k	 r  t |  |  j   Sd  S(   N(   R   R%   R   (   R\   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   __iter__  s    c         C   s)   t  |  |  r t |  |  St |  d  S(   N(   t   hasattrRx   t   KeyError(   R\   t   item(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   __getitem__  s    N(   R   R   Rs   R%   Rc   R   R   (    (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyR     s   	c         C   s   t  j d d |   }  t   } d } d } x |  j d  D] } | j d  rX d } q: | ry | j   d k ry d } q: | r | j d  r Pq: | r | j d  r Pq: | r: | j | d  q: q: Wt j | j	    S(	   s-   Convert ASCII armoured GPG key to binary
    s   
?s   
i    s$   -----BEGIN PGP PUBLIC KEY BLOCK-----i   t    s"   -----END PGP PUBLIC KEY BLOCK-----t   =(
   R&   t   subR    t   splitR=   t   stript   writet   base64t   decodestringt   getvalue(   t   rawkeyt   blockt   inblockt   pastheaderst   line(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt
   procgpgkey  s     			i   c         C   sn   |  d   } t  j |  } | rj g  t d t |  |  D] } | | | | !^ q; } d j |  } n  | S(   s    Given a key_info data from getgpgkeyinfo(), return an ascii
    fingerprint. Chop every 4 ascii values, as that is what GPG does. t   fingerprinti    t    (   t   binasciit   hexlifyt   rangeRE   t   join(   t   infot   chopt   fpRQ   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   gpgkey_fingerprint_ascii  s    6c   
      C   s  g  } y t  j |   } Wn% t k
 r@ } t t |    n Xt |  d k rb t d   n  x| D]} | j j   } i | j d 6t	 j
 d |  d d 6| j j d 6| j j d 6| j d 6t d	 6t d
 6} x | j d D]{ } t | t  j  sq n  | j   | k r t | d  r`| j t  j  }	 |	 d k r]t |	 d  | d <Pq]q`q q W| j |  qi W| r| S| d Sd S(   s   Return a dict of info for the given ASCII armoured key text

    Returned dict will have the following keys: 'userid', 'keyid', 'timestamp'

    Will raise ValueError if there was a problem decoding the key.
    i    s   No key found in given key datat   userids   >Qt   keyidt	   timestampR   t   raw_keyt   has_sigt	   valid_sigt   hashed_subpaksi   N(   t   pgpmsgt   decode_multiple_keyst	   ExceptionR   t   strRE   t
   public_keyt   key_idt   user_idt   structt   unpackR   R   R   R:   t   user_idsR   t	   signatureR   t   get_hashed_subpakt   SIG_SUB_TYPE_CREATE_TIMER%   t   intRK   (
   R   t   multiplet   key_info_objst   keysR   t   keyt
   keyid_blobR   R   t   tspkt(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   getgpgkeyinfo  s<    


c         C   s   d |  d @S(   s[   Convert an integer representing a GPG key ID to the hex version string
    used by RPM
    s   %08xl    (    (   R   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   keyIdToRPMVer  s    c         C   sw   t  |  } xd |  j d d  D]P } | d | k r t | d d  } | | k rX d S| | k  rh d Sd Sq q Wd	 S(
   sd  
    Return if the GPG key described by the given keyid and timestamp are
    installed in the rpmdb.  

    The keyid and timestamp should both be passed as integers.
    The ts is an rpm transaction set object

    Return values:
        - -1      key is not installed
        - 0       key with matching ID and timestamp is installed
        - 1       key with matching ID is installed but has a older timestamp
        - 2       key with matching ID is installed but has a newer timestamp

    No effort is made to handle duplicates. The first matching keyid is used to 
    calculate the return result.
    t   names
   gpg-pubkeyt   versiont   releasei   i    i   i   i(   R   t   dbMatchR   (   t   tsR   R   t   hdrt   installedts(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   keyInstalled	  s    c         C   s  t  d  k r t S| s# d | } n  t j j |  sE t j |  n  t |   } | t j d <t  j	   } t
 t j j | d  d  } | j d  | j   | j |  | j   | j |  } t  j j | | t  j  | r| d }	 t j j |	  st j |	 d d x[ t j | d	  D]F }
 t j j |
  } |	 d
 | } t j |
 |  t j | d  q'Wt
 |	 d d d  } d } | j |  | j   qn  t S(   Ns	   %s/gpgdirt	   GNUPGHOMEs   gpg.conft   wbR   s   -rot   modei  s   /*R   s	   /gpg.conft   ws   lock-never    
no-auto-check-trustdb    
trust-model direct
no-expensive-trust-checks
no-permission-warning         
preserve-permissions
(   t   gpgmeR%   R:   R   R   t   existst   makedirsR    t   environt   ContextR}   R   R   R   t   import_t   get_keyt   editutilt
   edit_trustt   VALIDITY_ULTIMATEt   globt   basenamet   shutilt   copyt   chmodR9   (   R   R   t   cachedirt   gpgdirt   make_ro_copyt   key_fot   ctxR   t   kt   rodirt   fR   t   ro_ft   opts(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   import_key_to_pubring*  s<    


c         C   s   t  d  k s t j j |   r# g  S|  t j d <t  j   } g  } xD | j   D]6 } x- | j D]" } | j	 r_ | j
 | j  q_ q_ WqO W| S(   NR   (   R   R%   R   R   R   R   R   t   keylistt   subkeyst   can_signRK   R   (   R   R   t   keyidsR   t   subkey(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   return_keyids_from_pubring[  s    	c   
      C   s  t  d k r t S| r< t j j |  s, t S| t j d <n  t |  d  rT |  } n t |  d  } t | d  r{ | } n t | d  } d } t  j	   } y | j
 | | |  } Wn t  j k
 r } t SX| s t S| d }	 |	 s t S|	 j t  j t  j t  j f k rt St S(   sA   takes signature , file that was signed and an optional gpghomedirR   Rh   R{   i    N(   R   R%   R:   R   R   R   R   R   R}   R   t   verifyt
   GpgmeErrort   validityt   VALIDITY_FULLt   VALIDITY_MARGINALR   R9   (
   t   sig_filet   signed_filet   gpghomet   sigt   signed_textt	   plaintextR   t   sigsR   t   thissig(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   valid_detached_sigi  s6    		
s   /var/tmps   yum-c         C   s  t  j   } y, t j |  } | d } t j |  } Wn t k
 rL d SX| r d | | f } d |  | f } t t	 j	 |   } xZ | D]O } t  j
 |  }	 t |	 d  r t |	 d  d k r |	 d | k r | Sq Wn  t j d | d |   }
 |
 S(	   sq   return a path to a valid and safe cachedir - only used when not running
       as root or when --tempcache is seti    s   %s%s-s   %s/%s*i  i   t   prefixt   dirN(   R   t   geteuidt   pwdt   getpwuidt   urllibt   quoteR   R%   t   sortedR   t   lstatt   S_ISDIRt   S_IMODEt   tempfilet   mkdtemp(   t   tmpdirt   reuseR  t   uidt   usertupt   usernamet   dirpatht	   cachedirst   thisdirt   statsR   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   getCacheDir  s"    
6c         C   s4   |  j  | j  k r d S|  j  | j  k r, d Sd Sd S(   s+   sorts a list of yum package objects by namei   i    iN(   R   (   t   pkg1t   pkg2(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt
   sortPkgObj  s
    c         C   sn   |  j    g } | d } xN |  D]F } | j |  rG | g } | } q  | j |  r  | j |  q  q  W| S(   s,    Return the newest in the list of packages. i    (   t   popt   verGTt   verEQRK   (   t   pkgsRm   t   newestt   pkg(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   newestInList  s    
		c         C   sp   |  \ } } } d } | d k r2 | d | 7} n  | d k	 rO | d | 7} n  | d k	 rl | d | 7} n  | S(	   s   
    Convert a tuple representing a package version to a string.

    @param evrTuple: A 3-tuple of epoch, version, and release.

    Return the string representation of evrTuple.
    R   i    t   0s   %s:s   %ss   -%sN(   i    R,  N(   R%   (   t   evrTupleR   t   vR{   R)   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   version_tuple_to_string  s    c         C   sc   |  \ } } } i d d 6d d 6d d 6d d 6d	 d
 6} | d k rH | Sd | | | t |  f S(   s7   returns a text string of the prco from the tuple formatt   >t   GTs   >=t   GER   t   EQt   <t   LTs   <=t   LEs   %s %s %sN(   R%   R/  (   t	   prcoTupleR   t   flagt   evrR4   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   prco_tuple_to_string  s
    )c   	      C   s  t  |   t j k r' |  \ } } } nc |  } d } } | d d k r | j d  r | j   } t |  d k r | \ } } } q n  | d k	 r	| t j k r	| t j	 k r y t
 t |   } Wqt t f k
 r } t j d |  qXq	t j	 | } n  t  |  t j t j t j f k rEt |  \ } } } n0 t  |  t j t j f k ru| \ } } } n  | | | | | f f S(   s:   returns a prco tuple (name, flags, (e, v, r)) for a stringi    R   s   gpg(i   s   Invalid version flag: %sN(   R   R   R   R%   R=   R   RE   t	   constantst   LETTERFLAGSt   SYMBOLFLAGSR   R   R   RG   t   ErrorsR   R~   t   NoneTypet   UnicodeTypeR   t   ListType(	   t
   prcoStringRL   R   R.  t
   prco_splitR   t   prco_et   prco_vt   prco_r(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   string_to_prco_tuple  s(    
 $c         C   s7   t  j d |   r$ t j |   } n t  j |   } | S(   sm   Takes a search string from the cli for Search or Provides
       and cleans it up so it doesn't make us vomits   [*{}?+]|\[.+\](   R&   R(   R/   R0   t   escape(   t   argt   restring(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   refineSearchPattern  s    c         C   si  | t  k r% d | } t j |  n  | d k rF t j |  d  } nB | d k rg t j |  d  } n! | d k r t j |  d  } n  t	 | d  } x t
 rPy | j d  } Wn> t t t f k
 r } d |  t |  f } t j |  n X| s Pn  y | j |  Wq t t f k
 rL} d	 | t |  f } t j |  q Xq W| j   | j   d  S(
   Ns   %s compression not availableR   R{   R   R   R   i  s   Error reading from file %s: %ss   Error writing to file %s: %s(   t   _available_compressionR>  R   R   t   BZ2Filet   lzmat   LZMAFilet   gzipt   GzipFileR}   R9   Rh   R   R   t   EOFErrorR   R   R   (   t   sourcet   destt   ztypet   msgt   s_fnt   destinationRg   R   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   _decompress_chunked  s2    
	 
c         C   s   t  |  | d d d S(   s1    Extract the bzipped contents of source to dest. RU  R   N(   RY  (   RS  RT  (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt
   bunzipFile/  s    c         C   s   t  j   d } t t j d |   } x | D]x } |  j d |  } x] | D]U } | d } | d d
 k rx d } n t |  } | d | d | | d | d	 f SWq0 Wd S(   s}   This takes the output of uname and figures out the pkgtup of the running
       kernel (name, arch, epoch, version, release).i   s   /boot/vmlinuz*%s*t	   basenamest   epochR,  R   t   archR   R   N(   NNNNN(   R   t   unameR  R   R   R%   R   (   R   t   vert   fnst   fnt   mit   hR   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   get_running_kernel_pkgtup3  s    
	(c         C   s2   t  |   } | d d k	 r. | d | d f Sd S(   sg   This takes the output of uname and figures out the (version, release)
    tuple for the running kernel.i    i   i   N(   NN(   Rd  R%   (   R   t   pkgtup(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt"   get_running_kernel_version_releaseG  s    s   /var/lib/yumc   	      C   s   g  } d |  d f } d |  d f } t  j  |  } t  j  |  } xT | D]L } | j d  rf qK n  t j j |  } | j d d  } | j |  qK W| j   | S(   s   returns a list of the timestamps from the filenames of the unfinished 
       transactions remaining in the yumlibpath specified.
    s   %s/%ss   transaction-all*s   transaction-done*t   disableds   transaction-all.R   (   R   t   endswithR   R   R   t   replaceRK   RI   (	   t
   yumlibpatht
   timestampst   tsallgt   tsdonegt   tsallst   tsdonesRa  t   transR   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   find_unfinished_transactionsO  s    
c         C   se  g  } d | d |  f } d | d |  f } g  } t  j j |  sH | St  j j |  r t | d  } | j   } | j   n  t | d  } | j   } | j   x- | D]% }	 |	 | k r q n  | j |	  q Wx | D] }	 |	 j d d  }	 |	 d k rq n  y |	 j   \ }
 } Wn) t	 k
 rI} d | } t
 j |  n X| j |
 | f  q W| S(   s  this function takes the timestamp of the transaction to look at and 
       the path to the yum lib dir (defaults to /var/lib/yum)
       returns a list of tuples(action, pkgspec) for the unfinished transaction
       elements. Returns an empty list if none.

    s   %s/%s.%ss   transaction-alls   transaction-doneR{   s   
R   s(   Transaction journal  file %s is corrupt.(   R   R   R   R}   t	   readlinesR   t   removeRi  R   R   R>  R   RK   (   R   Rj  t   to_complete_itemst	   tsallpatht
   tsdonepatht   tsdone_itemst	   tsdone_fot   tsall_fot   tsall_itemsR   t   actiont   pkgspecR   RV  (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   find_ts_remainingc  s8    

c         C   s~   g  } t  |   } t |   }  d } xB | | k rh | | } | j |  | | ! | | 7} | | 8} q' W| j |  |  | S(   sE    Given a seq, split into a list of lists of length max_entries each. i    (   RE   RH   RK   (   t   seqt   max_entriesRm   t   numt   begt   end(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   seq_max_split  s    

R   c         c   s'   |  ] } | d k r t  |  Vq d S(   i	   i
   i   N(   i	   i
   i   (   t   chr(   t   .0RQ   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pys	   <genexpr>  s    i    c         C   s  t  |   t k rR y t |  d  Wq t k
 rN t |  d  j d  }  q XnG t  |   t k rv |  j d  }  n# |  d k r d St d t |    |  j   }  |  j	 d t
  }  |  j d d  }  |  j d d  }  |  j d	 d
  }  | r|  j d d  }  |  j d d  }  n  |  S(   s_    Returns xml-friendly utf-8 encoded string.
        Accepts utf-8, iso-8859-1, or unicode.
    s   utf-8s
   iso-8859-1R   s   String expected, got %st   &s   &amp;R4  s   &lt;R0  s   &gt;t   "s   &quot;t   's   &apos;N(   R   R   R   t   UnicodeDecodeErrort   encodeR%   R   t   reprt   rstripR0   t   _deletecharsRi  (   R   t   attrib(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   to_xml  s&     c         C   sX   y t  j |   Wn@ t k
 rS } | j t j t j t j t j f k rT   qT n Xd S(   s|    Call os.unlink, but don't die if the file isn't there. This is the main
        difference between "rm -f" and plain "rm". N(   R   t   unlinkR   t   errnot   ENOENTt   EPERMt   EACCESt   EROFS(   R;   R   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   unlink_f  s
    'c         C   si   y t  j |   SWnQ t k
 rd } | j t j t j f k rB d S| r^ | j t j k r^ d S  n Xd S(   sF    Call os.stat(), but don't die if the file isn't there. Returns None. N(   R   t   statR   R  R  t   ENOTDIRR%   R  (   R;   t   ignore_EACCESR   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   stat_f  s    c          C   sX   y t  d  }  Wn t k
 r$ d SX|  j   } y t |  SWn t k
 rS d SXd S(   s    Get the audit-uid/login-uid, if available. None is returned if there
        was a problem. Note that no caching is done here. s   /proc/self/loginuidN(   R}   R   R%   Rh   R   R   (   Ri   Rg   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   _getloginuid  s    c           C   s   t  d k r t   a  n  t  S(   s    Get the audit-uid/login-uid, if available. None is returned if there
        was a problem. The value is cached, so you don't have to save it. N(   t   _cached_getloginuidR%   R  (    (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   getloginuid  s    c         C   s   y3 t  j t  j d  | r2 t  j t  j d  n  WnC t  j k
 rx } t j d IJd t j d <t  j t  j d  n X|  r d d d     Y} | t j	 t  j
    t _	 n  d  S(   NR   t   Cs%   Failed to set locale, defaulting to Ct   LC_ALLt   UnicodeStreamc           B   s#   e  Z d    Z d   Z d   Z RS(   c         S   s   | |  _  | |  _ d  S(   N(   t   streamt   encoding(   R\   R  R  (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyRc     s    	c         S   s;   t  | t  r' | j |  j d  } n  |  j j |  d  S(   NRi  (   R   R   R  R  R  R   (   R\   R)   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyR     s    c         S   s   t  |  j |  S(   N(   Rx   R  (   R\   R   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyRz     s    (   R   R   Rc   R   Rz   (    (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyR     s   		(    (   t   localet	   setlocaleR  t   LC_TIMEt   Errort   syst   stderrR   R   t   stdoutt   getpreferredencoding(   t   override_codecst   override_timeR   R  (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   setup_locale  s    
c          C   sZ   y t  j t  j  }  Wn t k
 r1 } d }  n X|  d k rG d }  n d j |   }  |  S(   NR  t   .(   NN(   NN(   R  t	   getlocalet   LC_MESSAGESR   R%   R   (   t   mylangR   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   get_my_lang_code  s    
	c          C   sg   t  j   }  g  } xN t j d  D]= } |  t  j j |  k rF q" n  | j t  j j |   q" W| S(   s5   return list of running processids, excluding this ones   /proc/[0123456789]*(   R   t   getpidR   R   R   RK   (   t   mypidt   pidsRa  (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   return_running_pids  s    c         C   sr  g  } d |  } y t  | d  } Wn t t f k
 r? } | SXx | D] } | j d  d k rh qG n  | j d d  } | j d  } | | } | j d d  } | j   } | | k rG | j |  qG qG Wd	 |  } y t  | d  }	 Wn t t f k
 r
} | SX|	 j   }
 |
 j d
  d k rn|
 j d
  } x0 | D]% } | j	 d  rB| j |  qBqBWn  | S(   s    returns files open from this pids   /proc/%s/mapsR{   s   fd:is   
R   R   s	   (deleted)s   /proc/%s/cmdlinet    (
   R}   R   R   t   findRi  R   RK   Rh   R   R=   (   t   pidt   filest   maps_ft   mapsR   R   t   slashR;   t   cli_ft   clit   cmdlinet   cmdsRQ   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   get_open_files$  s8    


c         C   s   t  j j |   r% t |  d  j   Sy d d l m } Wn& t k
 ra t d d  j   } n Xt |    } y4 t |  d  } | j	 |  | j
   | j   Wn t t f k
 r } n X| Sd S(   sw   create, store and return a uuid. If a stored one exists, report that
       if it cannot be stored, return a random oneR{   i(   t   uuid4s   /proc/sys/kernel/random/uuidR   N(   R   R   R   R}   Rh   t   uuidR  t   ImportErrorR   R   t   flushR   R   R   (   t   savepathR  t   myidt   sfR   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   get_uuidG  s    
c         C   s  | } | s |  } n  |  j  d  rH d } | s |  j d d  } q n |  j  d  sf |  j  d  r d } | s |  j  d  r |  j d d  } q |  j d d  } q n7 |  j  d  r d } | s |  j d d  } q n |  S| rAt |   } t |  } | rA| rAt | j  t | j  k r1| S| r>d	 SqAn  | sy? t |  | |  | r| rt j | | j | j f  n  Wqt	 |    qXn  | S(
   s|   take a filename and decompress it into the same relative location.
       if the file is not compressed just return the files   .gzR   R   s   .bzs   .bz2R   s   .xzR   N(
   Rh  Ri  R  R   t   st_mtimeR%   RY  R   t   utimeR  (   R;   RT  t   fn_onlyt   check_timestampst   outRU  t   fiRi   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt
   decompress^  sF    	
#

c         C   ss   t  j j |   d | } y t |  d | d t SWn8 t t f k
 rn } | rh | j t j k rh d S  n Xd S(   s    This is a wrapper around decompress, where we work out a cached
        generated name, and use check_timestamps. filename _must_ be from
        a repo. and generated_name is the type of the file. s   /gen/RT  R  N(
   R   R   R>   R  R9   R   R   R  R  R%   (   R;   t   generated_namet   cachedRT  R   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   repo_gen_decompress  s    c         C   s   g  } x t  j  |   D] } x t |  D] } t j d |  rG q) n  | j   } | j   } | sk q) n  | r | j d d  } | j d d  } | j | j    q) n  | j	 |  q) Wq W| S(   s'  takes a glob of a dir (like /etc/foo.d/*.foo)
       returns a list of all the lines in all the files matching
       that glob, ignores comments and blank lines,
       optional paramater 'line_as_list tells whether to
       treat each line as a space or comma-separated list, defaults to Trues   \s*(#|$)s   
R   t   ,(
   R   R}   R&   R1   R  t   lstripRi  t   extendR   RK   (   t   thisglobt   line_as_listt   resultst   fnameR   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   read_in_items_from_dot_dir  s     c          C   sM   t  d k rI y d d l m }  Wn t k
 r? d d l }  n X|  a  n  d S(   s    Importing xElementTree all the time, when we often don't need it, is a
        huge timesink. This makes python -c 'import yum' suck. So we hide it
        behind this function. And have accessors. i(   t   cElementTreeN(   t   __cached_cElementTreeR%   t	   xml.etreeR  R  (   R  (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   _cElementTree_import  s    c         C   s   t    t j |   S(   s)    Lazily load/run: cElementTree.iterparse (   R  R  t	   iterparse(   R;   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   cElementTree_iterparse  s    c         C   s   t    t j |   S(   s%    Lazily load/run: cElementTree.parse (   R  R  t   parse(   R;   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   cElementTree_xmlparse  s    c         C   s   | d k r |  Sg  } xo |  D]g } | j d k rb d | j k rG q n  | j j | k rw q qw n | j | k rw q n  | j |  q W| S(   s    Given a list of packages, filter them for those "in" the repoid.
    uses from_repo for installed packages, used by repo-pkgs commands. t	   installedt	   from_repoN(   R%   t   repoidt
   yumdb_infoR  RK   (   R(  R  Rm   R*  (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   filter_pkgs_repoid  s    	c         C   s=   t  j t  j d } x" |  D] } | | k r | Sq Wd Sd S(   s;   Return the first invalid char found in the repoid, or None.s   -_.:N(   t   stringt   ascii_letterst   digitsR%   (   R  t   allowed_charst   char(    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   validate_repoid  s
    c            s   d     |   } t  j j |   s+ | SxZ t  j |   D]I \   } }   f d   | | D } | t  f d   | D  7} q; W| S(   s>   Return disk usage of the given filename, recursively for dirs.c         S   s   t  j |   j d S(   Ni   (   R   R  t	   st_blocks(   R   (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   usage  s    c         3   s$   |  ] } t  j j   |  Vq d  S(   N(   R   R   R   (   R  t   entry(   t   root(    s,   /usr/lib/python2.7/site-packages/yum/misc.pys	   <genexpr>  s    c         3   s   |  ] }   |  Vq d  S(   N(    (   R  R   (   R  (    s,   /usr/lib/python2.7/site-packages/yum/misc.pys	   <genexpr>  s    (   R   R   R   t   walkt   sum(   R   t   totalt   dirsR  t   paths(    (   R  R  s,   /usr/lib/python2.7/site-packages/yum/misc.pyt
   disk_usage  s    	$(    (   R	   s   sha1(    (    i   (    (   Rs   R   R   R  t   os.patht	   cStringIOR    R   R   R   R&   R  R>  R;  R   R  R   R  R/   R   RP  R   R  R  RL  RN  RK   R  R%   t   rpmUtils.miscutilsR   R   R  R   t   gpgme.editutilR   RF   RY   R   R   RZ   RH   t   ctypeR   R   R   R   R   R   Rh  R[   R  R  Rs  RU   R   R   t   i18nR   R   R   R   R"   R#   R$   R*   R:   R5   R6   R7   R<   R8   R?   RC   RD   RS   RT   Rv   Rp   R   R   R   R   R   R   R   R9   R   R   R
  R!  R$  R+  R/  R:  RG  RK  RY  RZ  Rd  Rf  Rq  R}  R  R   R   R  R  R  R  R  R  R  R  R  R  R  R  R  R  R  R  R  R  R  R  R  R  R  (    (    (    s,   /usr/lib/python2.7/site-packages/yum/misc.pyt   <module>   s   


	$													BO		2		!1	(					
	#		"			.	!				
		
	#	4						