ó
æ”\c           @  sÝ   d  d l  m Z d  d l Z d  d l m Z d  d l m Z d d l	 m
 Z
 d  d l m Z d  d l j 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	 e f d
 „  ƒ  YZ d S(   iÿÿÿÿ(   t   print_functionN(   t   commands(   t   Locatori   (   t   TunedAdminDBusException(   t   TunedException(   t   ProfileRecommendert   Adminc           B  s”  e  Z e e e e j e j 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 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 „  Z% d „  Z& d  „  Z' d! „  Z( d" „  Z) d# „  Z* d$ e d% „ Z+ d$ e d& „ Z, e d' „ Z- e d( „ Z. RS()   c         C  s  | |  _  | |  _ | |  _ | |  _ t | ƒ |  _ t t j ƒ |  _	 t
 j ƒ  |  _ d |  _ t |  _ d |  _ d  |  _ d  |  _ | |  _ t ƒ  |  _ |  j  rt j j t j t j t j | ƒ |  _ y |  j j t j |  j ƒ Wqt  k
 r} |  j! | ƒ t" |  _  qXn  d  S(   Nt    (#   t   _dbust   _debugt   _asynct   _timeoutR   t   _cmdt   profiles_locatort   constst   LOAD_DIRECTORIESt   _profiles_locatort	   threadingt   Eventt   _daemon_action_finishedt   _daemon_action_profilet   Truet   _daemon_action_resultt   _daemon_action_errstrt   Nonet   _controllert
   _log_tokent
   _log_levelR   t   _profile_recommendert   tunedt   admint   DBusControllert   DBUS_BUSt   DBUS_INTERFACEt   DBUS_OBJECTt   set_signal_handlert   DBUS_SIGNAL_PROFILE_CHANGEDt   _signal_profile_changed_cbR   t   _errort   False(   t   selft   dbust   debugt   asyncot   timeoutt	   log_levelt   e(    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   __init__   s*    											'c         C  s   t  | d t j ƒd  S(   Nt   file(   t   printt   syst   stderr(   R(   t   message(    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyR&   +   s    c         C  s>   |  j  j ƒ  s: | |  _ | |  _ | |  _ |  j  j ƒ  n  d  S(   N(   R   t   is_setR   R   R   t   set(   R(   t   profile_namet   resultt   errstr(    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyR%   .   s
    			c         C  si   y) t  j t |  j j t j ƒ ƒ d ƒ Wn9 t k
 rK } | j t j	 k St
 t f k
 rd } t SXt S(   Ni    (   t   ost   killt   intR   t	   read_fileR   t   PID_FILEt   OSErrort   errnot   EPERMt
   ValueErrort   IOErrorR'   R   (   R(   R.   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _tuned_is_running6   s    )c         O  sL  | d  k s | d k r t Sd  } d  } t } y t |  d | ƒ } Wn t k
 rc } t |  _ n Xy t |  d | ƒ } Wn; t k
 r¸ } |  j s¹ |  j t | ƒ d | ƒ t Sn X|  j r-y< |  j j |  j	 ƒ |  j j
 | | | Ž |  j j ƒ  } Wq-t k
 r)} |  j | ƒ t |  _ q-Xn  |  j sH| | | Ž  } n  | S(   NR   t   _action_dbus_t   _action_s    , action '%s' is not implemented(   R   R'   t   getattrt   AttributeErrorR   R&   t   strR   t   set_on_exit_actiont   _log_capture_finisht
   set_actiont   runR   (   R(   t   action_namet   argst   kwargst   actiont   action_dbust   resR.   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyRQ   @   s6    			
	c      	   C  s€   t  d ƒ xo | D]g } | d d  k	 rf | d d k rf t  |  j j d | d d d | d ƒ ƒ q t  d | d ƒ q Wd  S(   Ns   Available profiles:i   R   s   - %si    i   (   R1   R   R   t	   align_str(   R(   t   profile_namest   profile(    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _print_profiles^   s
    
 /c         C  sx   y |  j  j ƒ  } Wn; t k
 rP } g  |  j  j ƒ  D] } | d f ^ q5 } n X|  j | ƒ |  j ƒ  |  j  j t ƒ S(   NR   (   R   t	   profiles2R   t   profilesRW   t   _action_dbus_activet   exitR   (   R(   RU   R.   RV   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _action_dbus_list_profilesf   s    ,
c         C  s$   |  j  |  j j ƒ  ƒ |  j ƒ  t S(   N(   RW   R   t   get_known_names_summaryt   _action_activeR   (   R(   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _action_list_profilesp   s    
c         C  s8   |  j  j ƒ  } | d k r$ d  } n  |  j  j t ƒ | S(   NR   (   R   t   active_profileR   R[   R   (   R(   R7   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _dbus_get_active_profileu   s
    	c         C  s   |  j  j ƒ  \ } } | S(   N(   R   t   get_active_profile(   R(   R7   t   manual(    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _get_active_profile|   s    c         C  sD   |  j  j ƒ  \ } } | d  k r0 | d  k	 } n  | r= t j St j S(   N(   R   Rb   R   R   t   ACTIVE_PROFILE_MANUALt   ACTIVE_PROFILE_AUTO(   R(   RV   Rc   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _get_profile_mode€   s    c         C  s€   | d t  k rj t d ƒ t | d ƒ t ƒ  t d ƒ t | d ƒ t ƒ  t d ƒ t | d ƒ t  St d | ƒ t Sd  S(	   Ni    s   Profile name:i   s   Profile summary:i   s   Profile description:i   s,   Unable to get information about profile '%s'(   R   R1   R'   (   R(   RV   t   profile_info(    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _print_profile_info†   s    


R   c         C  sC   | d k r |  j  ƒ  } n  |  j j |  j | |  j j | ƒ ƒ ƒ S(   NR   (   Ra   R   R[   Ri   Rh   (   R(   RV   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _action_dbus_profile_info•   s    c         C  s—   | d k rc y* |  j  ƒ  } | d  k r5 t d ƒ t SWqc t k
 r_ } |  j t | ƒ ƒ t SXn  |  j | |  j j	 | t
 j t
 j g d d g ƒ ƒ S(   NR   s   No current active profile.(   Rd   R   R1   R'   R   R&   RI   Ri   R   t   get_profile_attrsR   t   PROFILE_ATTR_SUMMARYt   PROFILE_ATTR_DESCRIPTION(   R(   RV   R.   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _action_profile_infoš   s    
c         C  s,   | d  k r t d ƒ t St d | ƒ t S(   Ns   No current active profile.s   Current active profile: %s(   R   R1   R'   R   (   R(   R7   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _print_profile_name¦   s
    
c         C  s   |  j  j |  j |  j ƒ  ƒ ƒ S(   N(   R   R[   Ro   Ra   (   R(   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyRZ   ®   s    c         C  s|   y |  j  ƒ  } Wn' t k
 r9 } |  j t | ƒ ƒ t SX| d  k	 ro |  j ƒ  ro t d ƒ t d | ƒ t S|  j	 | ƒ S(   NsK   It seems that tuned daemon is not running, preset profile is not activated.s   Preset profile: %s(
   Rd   R   R&   RI   R'   R   RD   R1   R   Ro   (   R(   R7   R.   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyR^   ±   s    
c         C  s   t  d | ƒ d  S(   Ns   Profile selection mode: (   R1   (   R(   t   mode(    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _print_profile_mode½   s    c         C  s[   |  j  j ƒ  \ } } |  j | ƒ | d k rK |  j | ƒ |  j  j t ƒ S|  j  j t ƒ S(   NR   (   R   t   profile_modeRq   R&   R[   R'   R   (   R(   Rp   t   error(    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _action_dbus_profile_modeÀ   s    c         C  sO   y! |  j  ƒ  } |  j | ƒ t SWn' t k
 rJ } |  j t | ƒ ƒ t SXd  S(   N(   Rg   Rq   R   R   R&   RI   R'   (   R(   Rp   R.   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _action_profile_modeÈ   s    c         C  sP   | r? |  j  j ƒ  rL |  j  j ƒ  rL |  j d ƒ t } qL n |  j | ƒ | S(   Ns   Cannot enable the tuning.(   R   t
   is_runningt   startR&   R'   (   R(   t   rett   msg(    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _profile_print_statusÑ   s     c         C  sœ   t  j  ƒ  |  j |  j k r= t d |  j ƒ |  j j t ƒ S|  j j ƒ  r˜ |  j	 | k r˜ |  j
 s… t d |  j ƒ |  j j t ƒ S|  j j t ƒ Sn  t S(   Ns‰   Operation timed out after waiting %d seconds(s), you may try to increase timeout by using --timeout command line option or using --async.s   Error changing profile: %s(   t   timet
   _timestampR   R1   R   R[   R'   R   t   isSetR   R   R   R   (   R(   R7   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _action_dbus_wait_profileÚ   s    	c         C  s‘   |  j  d  k s |  j  d k r" d  SyH |  j j |  j  ƒ } d  |  _  t | d d d t j ƒt j j ƒ  Wn  t k
 rŒ } |  j	 d ƒ n Xd  S(   NR   t   endR0   sU   Error: Failed to stop log capture. Restart the Tuned daemon to prevent a memory leak.(
   R   R   R   t   log_capture_finishR1   R2   R3   t   flushR   R&   (   R(   t   log_msgsR.   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyRK   æ   s    		c         C  s  t  | ƒ d k r |  j ƒ  Sd j | ƒ } | d k rG |  j j t ƒ S|  j j ƒ  |  j rœ |  j	 d  k	 rœ |  j d d } |  j j |  j	 | ƒ |  _ n  |  j j | ƒ \ } } |  j sÄ | rà |  j j |  j | | ƒ ƒ St j ƒ  |  _ |  j j |  j | ƒ |  j | | ƒ S(   Ni    t    R   i   i   (   t   lent   _action_dbus_listt   joinR   R[   R'   R   t   clearR
   R   R   R   t   log_capture_startR   t   switch_profileRz   R{   R|   RL   R~   (   R(   RY   R7   R,   Rx   Ry   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _action_dbus_profileò   s     
	c         C  sR   t  d ƒ |  j j d d d g ƒ \ } } | d k rD t  d ƒ n
 t  d ƒ d  S(   Ns   Trying to (re)start tuned...t   serviceR   t   restarti    s#   Tuned (re)started, changes applied.sQ   Tuned (re)start failed, you need to (re)start tuned by hand for changes to apply.(   R1   R   t   execute(   R(   Rx   Ry   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _restart_tuned  s
    
!c         C  s   | |  j  j ƒ  k rt y% |  j j | | ƒ |  j ƒ  t SWq‰ t k
 rp } |  j t | ƒ ƒ |  j d ƒ t	 SXn |  j d | ƒ t	 Sd  S(   Ns   Unable to switch profile.s%   Requested profile '%s' doesn't exist.(
   R   t   get_known_namesR   t   save_active_profileRŽ   R   R   R&   RI   R'   (   R(   R7   Rc   R.   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _set_profile  s    
c         C  sK   t  | ƒ d k r |  j ƒ  Sd j | ƒ } | d k r; t S|  j | t ƒ S(   Ni    Rƒ   R   (   R„   R_   R†   R'   R‘   R   (   R(   RY   R7   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _action_profile  s    
c         C  sÚ   |  j  j ƒ  } |  j j ƒ  |  j rd |  j d  k	 rd |  j d d } |  j  j |  j | ƒ |  _	 n  |  j  j
 ƒ  \ } } |  j s‰ | r¥ |  j  j |  j | | ƒ ƒ St j ƒ  |  _ |  j  j |  j | ƒ |  j | | ƒ S(   Ni   i   (   R   t   recommend_profileR   R‡   R
   R   R   R   Rˆ   R   t   auto_profileR[   Rz   R{   R|   RL   R~   (   R(   R7   R,   Rx   Ry   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _action_dbus_auto_profile$  s    	c         C  s   |  j  j ƒ  } |  j | t ƒ S(   N(   R   t	   recommendR‘   R'   (   R(   R7   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _action_auto_profile4  s    c         C  s#   t  |  j j ƒ  ƒ |  j j t ƒ S(   N(   R1   R   R“   R[   R   (   R(   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _action_dbus_recommend_profile8  s    c         C  s   t  |  j j ƒ  ƒ t S(   N(   R1   R   R–   R   (   R(   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _action_recommend_profile<  s    c         C  s—   | r |  j  j ƒ  } n |  j  j ƒ  } | r: t d ƒ n< t d ƒ t d ƒ t d ƒ t d ƒ t d ƒ t d ƒ t d t j ƒ |  j  j | ƒ S(	   NsH   Verfication succeeded, current system settings match the preset profile.sL   Verification failed, current system settings differ from the preset profile.s=   You can mostly fix this by restarting the Tuned daemon, e.g.:s     systemctl restart tunedt   ors     service tuned restartsN   Sometimes (if some plugins like bootloader are used) a reboot may be required.s&   See tuned log file ('%s') for details.(   R   t   verify_profile_ignore_missingt   verify_profileR1   R   t   LOG_FILER[   (   R(   t   ignore_missingRx   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _action_dbus_verify_profile@  s    





c         C  s   t  d ƒ t S(   Ns    Not supported in no_daemon mode.(   R1   R'   (   R(   Rž   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _action_verify_profileQ  s    
c         C  sV   d } |  j  j |  j | ƒ |  _ |  j  j ƒ  } | sF |  j d ƒ n  |  j  j | ƒ S(   Ni   i   s   Cannot disable active profile.i   (   R   Rˆ   R   R   t   offR&   R[   (   R(   R,   Rx   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _action_dbus_offU  s    	c         C  s   t  d ƒ t S(   Ns    Not supported in no_daemon mode.(   R1   R'   (   R(   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _action_off_  s    
RY   c         C  s6   | d k r |  j  ƒ  S| d k r2 |  j d | ƒ Sd S(   sD  Print accessible profiles or plugins got from tuned dbus api

		Keyword arguments:
		list_choice -- argument from command line deciding what will be listed
		verbose -- if True then list plugin's config options and their hints
			if possible. Functional only with plugin listing, with profiles
			this argument is omitted
		RY   t   pluginst   verboseN(   R\   t   _action_dbus_list_plugins(   R(   t   list_choiceR¥   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyR…   c  s    	
c         C  s6   | d k r |  j  ƒ  S| d k r2 |  j d | ƒ Sd S(   sa  Print accessible profiles or plugins with no daemon mode

		Keyword arguments:
		list_choice -- argument from command line deciding what will be listed
		verbose -- Plugins cannot be listed in this mode, so verbose argument
			is here only because argparse module always supplies verbose
			option and if verbose was not here it would result in error
		RY   R¤   R¥   N(   R_   t   _action_list_plugins(   R(   R§   R¥   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   _action_listq  s    	
c         C  sÁ   |  j  j ƒ  } xŸ | j ƒ  D]‘ } t | ƒ | s t | | ƒ d k rO q n  |  j  j | ƒ } xI | | D]= } t d | ƒ | j | d ƒ } | rl t d | ƒ ql ql Wq W|  j  j t	 ƒ S(   sv   Print accessible plugins

		Keyword arguments:
		verbose -- if is set to True then parameters and hints are printed
		i    s   	%ss   		%sN(
   R   t   get_pluginst   keysR1   R„   t   get_plugin_hintst   getR   R[   R   (   R(   R¥   R¤   t   plugint   hintst	   parametert   hint(    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyR¦     s    
c         C  s   t  d ƒ t S(   Ns    Not supported in no_daemon mode.(   R1   R'   (   R(   R¥   (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyR¨   ’  s    
(/   t   __name__t
   __module__R   R'   R   t   ADMIN_TIMEOUTt   loggingt   ERRORR/   R&   R%   RD   RQ   RW   R\   R_   Ra   Rd   Rg   Ri   Rj   Rn   Ro   RZ   R^   Rq   Rt   Ru   Rz   R~   RK   RŠ   RŽ   R‘   R’   R•   R—   R˜   R™   RŸ   R    R¢   R£   R…   R©   R¦   R¨   (    (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyR      sR   				
			
																											
	(   t
   __future__R    t   tuned.adminR   t   tuned.utils.commandsR   t   tuned.profilesR   R   t
   exceptionsR   t   tuned.exceptionsR   t   tuned.constsR   t   tuned.utils.profile_recommenderR   R:   R2   R@   R{   R   Rµ   t   objectR   (    (    (    s5   /usr/lib/python2.7/site-packages/tuned/admin/admin.pyt   <module>   s   