
N_c           @   ss  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 Td d l m Z d d l m Z d d l	 m
 Z
 m Z d d l m Z d d l m Z d d l j Z d d	 l m Z y e j e j d
  Z Wn e e f k
 r d Z n Xd   Z d   Z d   Z d   Z d   Z d   Z d   Z d e f d     YZ  d   Z! e" d k roe!   n  d S(   s/   Various utility functions, and a utility class.iN(   t   *(   t   Errors(   t   _(   t
   utf8_widtht   exception2msg(   t   logginglevels(   t   OptionGroup(   t   format_numbert
   SC_CLK_TCKid   c             s%   t  j     f d   }  |  t  _ d S(   s   Change settings so that nothing will be printed to the
    terminal after an uncaught :class:`exceptions.KeyboardInterrupt`.
    c            s&   |  t  j k r"   |  | |  n  d  S(   N(   t
   exceptionst   KeyboardInterrupt(   t   typet   valuet	   traceback(   t   old_excepthook(    s   /usr/share/yum-cli/utils.pyt   new_hook.   s    N(   t   syst
   excepthook(   R   (    (   R   s   /usr/share/yum-cli/utils.pyt#   suppress_keyboard_interrupt_message(   s    	c         C   s   t  |   t S(   s   Convert a number of jiffies to seconds. How many jiffies are in a second
    is system-dependent, e.g. 100 jiffies = 1 second is common.

    :param jiffies: a number of jiffies
    :return: the equivalent number of seconds
    (   t   intt   _USER_HZ(   t   jiffies(    (    s   /usr/share/yum-cli/utils.pyt   jiffies_to_seconds6   s    c         C   s{   |  d k r8 d |  d	 |  d
 d |  d d |  d f S|  d k re d |  d |  d d |  d f Sd |  d |  d f S(   s  Return a human-readable string representation of the length of
    a time interval given in seconds.

    :param seconds: the length of the time interval in seconds
    :return: a human-readable string representation of the length of
    the time interval
    i<   i   s   %d day(s) %d:%02d:%02ds   %d:%02d:%02ds	   %02d:%02di  iQ i  iQ i  i  i  (    (   t   seconds(    (    s   /usr/share/yum-cli/utils.pyt   seconds_to_ui_time?   s    
c         C   s5  |  s
 d Sy t  |   }  Wn t k
 r0 } d SXi  } x t d |   D] } | d d k rd qH n  | d  j d d  } t |  d k  r qH n  | d j d  r | d d	  | d <n  | d j   | | d
 j   j   <qH Wd | k r d Sd | k rd St   } | d k rd S| j
 t |  |   | S(   s   Return information about a process taken from
    /proc/*pid*/status, /proc/stat/, and /proc/*pid*/stat.

    :param pid: the process id number
    :return: a dictionary containing information about the process
    Ns   /proc/%d/statusis   
s   :	i   i   s    kBii    t   vmrsst   vmsize(   R   t
   ValueErrort   opent   splitt   lent   endswitht   stript   lowert   get_boot_timet   Nonet   updatet   get_process_time(   t   pidt   et   pst   linet   datat	   boot_time(    (    s   /usr/share/yum-cli/utils.pyt   get_process_infoQ   s2    (	c          C   sA   x: t  d  D], }  |  j d  r t |  t d  d ! Sq Wd  S(   Ns
   /proc/stats   btime i(   R   t
   startswithR   R   (   R)   (    (    s   /usr/share/yum-cli/utils.pyR"   u   s    c         C   s  i  } t  d |   j   j   } | | j d  d j   } t | d  | d <t | d  | d <t | d  | d	 <t | d
  | d <| t | d  | d <i t d  d 6t d  d 6t d  d 6t d  d 6t d  d 6j | d t d   | d <| S(   Ns   /proc/%d/statt   )i   i   t   utimei   t   stimei   t   cutimei   t   cstimei   t
   start_timet   Runningt   Rt   Sleepingt   St   Uninterruptiblet   Dt   Zombiet   Zs   Traced/Stoppedt   Ti    t   Unknownt   state(   R   t   readR    t   rfindR   R   R   t   get(   R&   R+   R(   t   ps_stat(    (    s   /usr/share/yum-cli/utils.pyR%   {   s    c         C   sC  y t  |   } Wn/ t k
 rA } | j d t |   d } n X| sL d S| d d k rk t d  } n t d  | d } | j d |  | j t d  t t | d  d  t t | d	  d  f  t t t	 j	    | d
  } | j t d  t	 j
 | d
  | f  | j t d  | d |  f  | S(   sw  Output information about another process that is holding the
    yum lock.

    :param pid: the process id number of the process holding the yum
       lock
    :param logger: the logger to output the information to
    :return: a dictionary containing information about the process.
       This is the same as the dictionary returned by
       :func:`get_process_info`.
    s   %st   names   yumBackend.pys&     The other application is: PackageKits     The other application is: %ss       Memory : %5s RSS (%5sB VSZ)R   i   R   R3   s       Started: %s - %s agos       State  : %s, pid: %dR>   N(   R,   t   IOErrort   criticalR   R#   R   R   R   R   t   timet   ctime(   R&   t   loggerR(   R'   t   nmsgt   ago(    (    s   /usr/share/yum-cli/utils.pyt   show_lock_owner   s&    
 !t   YumUtilBasec           B   s   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z e j d
 e j f d  Z d   Z e d  Z d   Z RS(   s,   A class to extend the yum cli for utilities.c         C   s   t  j |   t d |  d t d |  |  _ | |  _ | |  _ | |  _ t |  j d |  j d  |  _	 |  j j
 |  j	  t   t j d  } t j d  } t |  d  r |  j j d	  n  d  S(
   Nt   baset   utilst   usages
   %s optionst    s   yum.utils   yum.verbose.utilt   run_with_package_namess	   yum-utils(   t
   YumBaseClit   __init__t   YumOptionParsert   Truet   _parsert   _usaget	   _utilNamet   _utilVerR   t   _option_groupt   add_option_groupR   t   loggingt	   getLoggert   hasattrRQ   t   add(   t   selfRC   t   verRO   RH   t   verbose_logger(    (    s   /usr/share/yum-cli/utils.pyRS      s    			c         C   s*   |  j  j t d   |  j   r& d Sd S(   sw   Output a message stating that the operation was cancelled
        by the user.

        :return: the exit code
        s   

Exiting on user canceli   i   (   RH   RE   R   t   unlock(   R`   (    (    s   /usr/share/yum-cli/utils.pyt   exUserCancel   s     c         C   s\   | j  d k r( |  j j t d   n  |  j j t d  t |   |  j   rX d Sd S(   s   Output a message stating that the program is exiting due to
        an IO exception.

        :param e: the IO exception
        :return: the exit code
        i    s   

Exiting on Broken Pipes   

%si   i   (   t   errnoRH   RE   R   R   Rc   (   R`   R'   (    (    s   /usr/share/yum-cli/utils.pyt	   exIOError   s      c         C   s<   t  |  } | r( |  j j d |  n  |  j   r8 d Sd S(   s   Called when a plugin raises
           :class:`yum.plugins.PluginYumExit`.  Log the plugin's exit
           message if one was supplied.

        :param e: the exception
        :return: the exit code
        s   

%si   i   (   R   RH   t   warnRc   (   R`   R'   t   exitmsg(    (    s   /usr/share/yum-cli/utils.pyt   exPluginExit   s     c         C   s-   |  j  j d t |   |  j   r) d Sd S(   s   Output a message stating that a fatal error has occurred.

        :param e: the exception
        :return: the exit code
        s   

%si   i   (   RH   RE   R   Rc   (   R`   R'   (    (    s   /usr/share/yum-cli/utils.pyt   exFatal   s     c         C   s6   y |  j    |  j   Wn t j k
 r1 } d SXd S(   s>   Release the yum lock.

        :return: the exit code
        i   i    (   t
   closeRpmDBt   doUnlockR   t	   LockError(   R`   R'   (    (    s   /usr/share/yum-cli/utils.pyRc      s    
c         C   s   |  j  S(   s   Return the :class:`cli.YumOptionParser` for this object.

        :return: the :class:`cli.YumOptionParser` for this object
        (   RV   (   R`   (    (    s   /usr/share/yum-cli/utils.pyt   getOptionParser  s    c         C   s   |  j  S(   s   Return an option group to add non inherited options.

        :return: a :class:`optparse.OptionGroup` for adding options
           that are not inherited from :class:`YumBaseCli`.
        (   RZ   (   R`   (    (    s   /usr/share/yum-cli/utils.pyt   getOptionGroup
  s    c         C   s   d |  j  |  j t j f GHd  S(   Ns   %s - %s (yum - %s)(   RX   RY   t   yumt   __version__(   R`   (    (    s   /usr/share/yum-cli/utils.pyt   _printUtilVersion  s    i   c         C   sG  |  j  j |  } |  j | j  |  j ra x3 |  j j D]" } t | | t |  j |   q8 Wn  | j r |  j	   t
 j d  n  |  j  j |  } | j r d | _ n  | j r d | _ | _ n  y|  j } | j | _ | | _ | j | _ | | _ |  j  | _ | j | _ | j | _ t | d  rL|  j  j | j  | _ n  t | d  rv|  j  j | j  | _ n  t | d  r| j | _ n  |  j  |  j rx6 |  j j D]% } t |  j  | t |  j |   qWn  Wnt! j" k
 r"} |  j# j$ t% d  t& |   t
 j d  n t' k
 r`} |  j# j$ t% d  t& |   t
 j d  n t( j) k
 r} |  j# j$ t% d	  t& |   t
 j d  nB t! j* k
 r} |  j# j$ t% d
  t& |   t
 j d  n X|  j  j+ |  j,  |  j  j-   \ } |  _. |  j. r*|  j. d |  _/ n	 d |  _/ |  j. d |  _1 | S(   s'  Parse command line options, and perform configuration.

        :param args: list of arguments to use for configuration
        :param pluginsTypes: a sequence specifying the types of
           plugins to load
        :return: a dictionary containing the values of command line options
        i    i   t   disablepluginst   enablepluginst
   releasevers   Config Error: %si   s   Options Error: %ss   PluginExit Error: %ss   Yum Error: %sN(2   RV   t
   firstParset   _parseSetOptst   setoptst   main_setoptst   itemst   setattrt   getattrt   versionRr   R   t   exitt   getRoott   quiett
   debuglevelt   verboset
   errorlevelt   preconft   conffilet   fnt   roott	   nopluginst   init_pluginst   plugin_typest	   optparserR^   t	   _splitArgRs   t   disabled_pluginsRt   t   enabled_pluginsRu   t   confR   t   ConfigErrorRH   RE   R   R   R   t   pluginst   PluginYumExitt   YumBaseErrort	   set_usageRW   t   setupYumConfigt   cmdst   basecmdR#   t   extcmds(   R`   t   argst   pluginsTypest   optst   optR   t   pcR'   (    (    s   /usr/share/yum-cli/utils.pyt   doUtilConfigSetup  sf    		#	
						*		c         C   s|   y< |  j    |  j   |  j   |  j d t  |  j   Wn9 t j k
 rw } |  j j	 t
 |   t j d  n Xd S(   s   Do a default setup for all the normal or necessary yum components;
           this method is mostly just a used for testing.
        t   doSetupi   N(   t   waitForLockt   _getTst	   _getRpmDBt	   _getReposRU   t	   _getSacksR   R   RH   RE   R   R   R~   (   R`   t   msg(    (    s   /usr/share/yum-cli/utils.pyt   doUtilYumSetupd  s    


c         C   s  y |  j  d |  \ } } Wn} t j k
 r> } |  j |  St j k
 rh } d } t |  g } n4 t k
 r |  j   St	 k
 r } |  j
 |  SX| d k r |  j   r d Sd S| d k rkx0 |  j |  D] \ } } |  j j | |  q W|  j j s |  j j t d   n  |  j d |  j j d t  sW|  j j t d   n  |  j   rgd Sd S| d	 k rznN |  j j t d
  |  x | D] } |  j j |  qW|  j   rd Sd S|  j j t j t d   d S(   s   Build the transaction.

        :param unfinished_transactions_check: whether to check if an
           unfinished transaction has been saved
        t   unfinished_transactions_checki   i    i   s=    You could try using --skip-broken to work around the problemt   outRg   s4    You could try running: rpm -Va --nofiles --nodigesti   s    Unknown Error(s): Exit Code: %d:i   s   
Dependencies ResolvedN(   t   buildTransactionR   R   Ri   R   R   R   R
   Rd   RD   Rf   Rc   t   pretty_output_restringRH   RE   R   t   skip_brokenRb   t   infoR   t   _rpmdb_warn_checkst   Falset   logR   t   INFO_2(   R`   R   t   resultt
   resultmsgsR'   t   prefixR   (    (    s   /usr/share/yum-cli/utils.pyt   doUtilBuildTransactions  sD    
   c         C   s   y |  j    } Wnr t j k
 r2 } |  j |  St j k
 rQ } |  j |  St k
 rh |  j   St	 k
 r } |  j
 |  SX|  j j t j t d   |  j   r d S| S(   s   Perform the transaction.s	   Complete!i   (   t   doTransactionR   R   Ri   R   R   Rj   R
   Rd   RD   Rf   Rb   R   R   R   R   Rc   (   R`   t   return_codeR'   (    (    s   /usr/share/yum-cli/utils.pyt   doUtilTransaction  s    
 (   t   __name__t
   __module__t   __doc__RS   Rd   Rf   Ri   Rj   Rc   Rn   Ro   Rr   R   t   argvR   t	   TYPE_CORER   R   RU   R   R   (    (    (    s   /usr/share/yum-cli/utils.pyRL      s   		
			
				O	-c          C   s   d }  d } d } t  |  | |  } | j   } | j d d d d d d	 d
 t d d | j j d  | j   } | j j d  | j   d d j | j	  GHd GH| GH| j j d |   d  S(   Nt   testutils   0.1s   testutil [options] [args]RP   s
   --myoptiont   destt   myoptiont   actiont
   store_truet   defaultt   helps   This is an util options   Setup Yum Configs	   Setup Yums   Command line args: %st    s   Command line options :s   %s Completed(
   RL   Rn   t
   add_optionR   RH   R   R   R   t   joinR   (   RC   Ra   RO   t   utilt   parserR   (    (    s   /usr/share/yum-cli/utils.pyt   main  s     
t   __main__(#   R   t   osR   RF   R	   Rp   t   cliR   R   t   yum.i18nR   R   R   t   optparseR   t   yum.pluginsR   t   urlgrabber.progressR   t   sysconft   sysconf_namesR   t   AttributeErrort   KeyErrorR   R   R   R,   R"   R%   RK   RR   RL   R   R   (    (    (    s   /usr/share/yum-cli/utils.pyt   <module>   s8   

					$			&	