Sindbad~EG File Manager
3
�<�e�>�@s�ddlmZddlZddlmZddlmZddl m
Z
ddlmZddl
jZddlmZddlZddlZddlZddlZddlZddlZGd d
�d
e�ZdS)�)�print_functionN)�commands)�Locator�)�TunedAdminDBusException)�TunedException)�ProfileRecommenderc@s�eZdZdddejejfdd�Zdd�Zdd�Z d d
�Z
dd�Zd
d�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdfd d!�Zdgd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Z d8d9�Z!d:d;�Z"d<d=�Z#d>d?�Z$d@dA�Z%dBdC�Z&dDdE�Z'dFdG�Z(dHdI�Z)dJdK�Z*dLdM�Z+dNdO�Z,dhdQdR�Z-didSdT�Z.djdUdV�Z/dkdWdX�Z0dYdZ�Z1d[d\�Z2d]d^�Z3d_d`�Z4dadb�Z5dcdd�Z6deS)l�AdminTFcCs�||_||_||_||_t|�|_ttj�|_ t
j�|_d|_
d|_d|_d|_d|_||_t�|_|jr�tjjtjtjtj|�|_y|jjtj|j�Wn2tk
r�}z|j|�d|_WYdd}~XnXdS)N�TF) �_dbusZ_debug�_async�_timeoutr�_cmd�profiles_locator�constsZLOAD_DIRECTORIES�_profiles_locator� threadingZEvent�_daemon_action_finished�_daemon_action_profile�_daemon_action_result�_daemon_action_errstr�_controller�
_log_token�
_log_levelr�_profile_recommender�tunedZadminZDBusControllerZDBUS_BUSZDBUS_INTERFACEZDBUS_OBJECTZset_signal_handlerZSIGNAL_PROFILE_CHANGED�_signal_profile_changed_cbr�_error)�selfZdbus�debugZasynco�timeoutZ log_level�e�r"�/usr/lib/python3.6/admin.py�__init__s*
zAdmin.__init__cCst|tjd�dS)N)�file)�print�sys�stderr)r�messager"r"r#r+szAdmin._errorcCs*|jj�s&||_||_||_|jj�dS)N)rZis_setrrr�set)r�profile_name�resultZerrstrr"r"r#r.s
z Admin._signal_profile_changed_cbcCsnytjt|jjtj��d�WnJtk
rF}z|jtj kSd}~Xn$t
tfk
rh}zdSd}~XnXdS)NrFT)�os�kill�intrZ read_filerZPID_FILE�OSError�errnoZEPERM�
ValueError�IOError)rr!r"r"r#�_tuned_is_running6szAdmin._tuned_is_runningc-Os6|dks|dkrdSd}d}d}yt|d|�}Wn(tk
rZ}zd|_WYdd}~XnXyt|d|�}WnBtk
r�}z&|js�|jt|�d|�dSWYdd}~XnX|j�r y0|jj|j�|jj|f|�|�|jj �}Wn4t
k
�r}z|j|�d|_WYdd}~XnX|j�s2|||�}|S)Nr
FZ
_action_dbus_Z_action_z , action '%s' is not implemented)�getattr�AttributeErrorrr�strrZset_on_exit_action�_log_capture_finish�
set_actionZrunr)rZaction_name�args�kwargs�actionZaction_dbus�resr!r"r"r#r<@s6
zAdmin.actioncCshtd�xZ|D]R}|ddk rP|ddkrPt|jjd|ddd|d��qtd|d�qWdS)NzAvailable profiles:rr
z- %sr�)r&rZ align_str)r�
profile_names�profiler"r"r#�_print_profiles^s
&zAdmin._print_profilescCsdy|jj�}Wn6tk
rD}zdd�|jj�D�}WYdd}~XnX|j|�|j�|jjd�S)NcSsg|]}|df�qS)r
r")�.0r@r"r"r#�
<listcomp>ksz4Admin._action_dbus_list_profiles.<locals>.<listcomp>T)rZ profiles2r�profilesrA�_action_dbus_active�exit)rr?r!r"r"r#�_action_dbus_list_profilesfs&
z Admin._action_dbus_list_profilescCs|j|jj��|j�dS)NT)rArZget_known_names_summary�_action_active)rr"r"r#�_action_list_profilespszAdmin._action_list_profilescCs&|jj�}|dkrd}|jjd�|S)Nr
T)r�active_profilerF)rr+r"r"r#�_dbus_get_active_profileus
zAdmin._dbus_get_active_profilecCs|jj�\}}|S)N)r�get_active_profile)rr+�manualr"r"r#�_get_active_profile|szAdmin._get_active_profilecCs.|jj�\}}|dkr|dk }|r(tjStjS)N)rrLrZACTIVE_PROFILE_MANUALZACTIVE_PROFILE_AUTO)rr@rMr"r"r#�_get_profile_mode�szAdmin._get_profile_modecCs|jj�}|dkrd}|S)Nr
)r�post_loaded_profile)rr+r"r"r#�_dbus_get_post_loaded_profile�s
z#Admin._dbus_get_post_loaded_profilecCs|jj�}|S)N)rZget_post_loaded_profile)rr+r"r"r#�_get_post_loaded_profile�s
zAdmin._get_post_loaded_profilecCsl|ddkrXtd�t|d�t�td�t|d�t�td�t|d�dStd |�d
SdS)NrTz
Profile name:rzProfile summary:�zProfile description:�z,Unable to get information about profile '%s'F)r&)rr@�profile_infor"r"r#�_print_profile_info�szAdmin._print_profile_infor
cCsB|dkr|j�}|r*|j||jj|��}ntd�d}|jj|�S)Nr
zNo current active profile.F)rKrVrrUr&rF)rr@r=r"r"r#�_action_dbus_profile_info�szAdmin._action_dbus_profile_infocCs||dkrXy |j�}|dkr&td�dSWn.tk
rV}z|jt|��dSd}~XnX|j||jj|tj tj
gddg��S)Nr
zNo current active profile.F)rNr&rrr7rVrZget_profile_attrsrZPROFILE_ATTR_SUMMARYZPROFILE_ATTR_DESCRIPTION)rr@r!r"r"r#�_action_profile_info�szAdmin._action_profile_infocCs$|dkrtd�dStd|�dS)NzNo current active profile.FzCurrent active profile: %sT)r&)rr+r"r"r#�_print_profile_name�s
zAdmin._print_profile_namecCs|rtd|�dS)NzCurrent post-loaded profile: %s)r&)rr+r"r"r#�_print_post_loaded_profile�sz Admin._print_post_loaded_profilecCs4|j�}|j|�}|r(|j�}|j|�|jj|�S)N)rKrYrQrZrrF)rrJr=rPr"r"r#rE�s
zAdmin._action_dbus_activecCs�y2|j�}|j�}|r0|r$|d7}nd}||7}Wn.tk
r`}z|jt|��dSd}~XnX|dk r�|j�r�td�td|�|r�td|�dS|j|�}|j|�|S)N� r
FzKIt seems that tuned daemon is not running, preset profile is not activated.zPreset profile: %szPreset post-loaded profile: %sT) rNrRrrr7r4r&rYrZ)rr+rPr!r=r"r"r#rH�s(
zAdmin._action_activecCstd|�dS)NzProfile selection mode: )r&)r�moder"r"r#�_print_profile_mode�szAdmin._print_profile_modecCsB|jj�\}}|j|�|dkr6|j|�|jjd�S|jjd�S)Nr
FT)rZprofile_moder]rrF)rr\�errorr"r"r#�_action_dbus_profile_mode�s
zAdmin._action_dbus_profile_modecCsJy|j�}|j|�dStk
rD}z|jt|��dSd}~XnXdS)NTF)rOr]rrr7)rr\r!r"r"r#�_action_profile_mode�s
zAdmin._action_profile_modecCs>|r,|jj�r:|jj�r:|jd�d}n|jd|�|S)NzCannot enable the tuning.FzUnable to switch profile: %s)rZ
is_running�startr)r�ret�msgr"r"r#�_profile_print_status�s
zAdmin._profile_print_statuscCsrtj�|j|jkr.td|j�|jjd�S|jj�rn|j|krn|j sbtd|j
�|jjd�S|jjd�SdS)Nz�Operation timed out after waiting %d seconds(s), you may try to increase timeout by using --timeout command line option or using --async.FzError changing profile: %sT)�time�
_timestampr
r&rrFrZisSetrrr)rr+r"r"r#�_action_dbus_wait_profile�s
zAdmin._action_dbus_wait_profilecCs||jdks|jdkrdSy2|jj|j�}d|_t|dtjd�tjj�Wn,tk
rv}z|jd�WYdd}~XnXdS)Nr
)�endr%zUError: Failed to stop log capture. Restart the TuneD daemon to prevent a memory leak.) rrZlog_capture_finishr&r'r(�flushrr)rZlog_msgsr!r"r"r#r8szAdmin._log_capture_finishcCs�t|�dkr|j�Sdj|�}|dkr2|jjd�S|jj�|jrn|jdk rn|j dd}|jj
|j|�|_|jj|�\}}|js�|r�|jj|j
||��Stj�|_|jj|j|�|j
||�S)Nrr[r
F��)�len�_action_dbus_list�joinrrFr�clearrrr
�log_capture_startrZswitch_profilerdrerfr9rg)rrDr+r rbrcr"r"r#�_action_dbus_profiles
zAdmin._action_dbus_profilecCs<td�|jjdddg�\}}|dkr0td�ntd�dS)NzTrying to (re)start tuned...ZservicerZrestartrz#TuneD (re)started, changes applied.zQTuneD (re)start failed, you need to (re)start TuneD by hand for changes to apply.)r&rZexecute)rrbrcr"r"r#�_restart_tuned+s
zAdmin._restart_tunedcCsz||jj�krdy|jj||�|j�dStk
r`}z|jt|��|jd�dSd}~XqvXn|jd|�dSdS)NTzUnable to switch profile.Fz%Requested profile '%s' doesn't exist.)rZget_known_namesrZsave_active_profilerrrrr7)rr+rMr!r"r"r#�_set_profile3s
zAdmin._set_profilecCs6t|�dkr|j�Sdj|�}|dkr*dS|j|d�S)Nrr[r
FT)rlrIrnrs)rrDr+r"r"r#�_action_profileAs
zAdmin._action_profilecCs�|jj�}|jj�|jrF|jdk rF|jdd}|jj|j|�|_|jj �\}}|js`|rt|jj
|j||��Stj�|_
|jj|j|�|j||�S)Nrjrk)r�recommend_profilerrorrr
rprZauto_profilerFrdrerfr9rg)rr+r rbrcr"r"r#�_action_dbus_auto_profileIs
zAdmin._action_dbus_auto_profilecCs|jj�}|j|d�S)NF)r� recommendrs)rr+r"r"r#�_action_auto_profileYs
zAdmin._action_auto_profilecCst|jj��|jjd�S)NT)r&rrurF)rr"r"r#�_action_dbus_recommend_profile]sz$Admin._action_dbus_recommend_profilecCst|jj��dS)NT)r&rrw)rr"r"r#�_action_recommend_profileaszAdmin._action_recommend_profilecCsr|r|jj�}n
|jj�}|r(td�n0td�td�td�td�td�td�tdtj�|jj|�S) NzIVerification succeeded, current system settings match the preset profile.zLVerification failed, current system settings differ from the preset profile.z=You can mostly fix this by restarting the TuneD daemon, e.g.:z systemctl restart tuned�orz service tuned restartzNSometimes (if some plugins like bootloader are used) a reboot may be required.z&See TuneD log file ('%s') for details.)rZverify_profile_ignore_missingZverify_profiler&rZLOG_FILErF)r�ignore_missingrbr"r"r#�_action_dbus_verify_profilees
z!Admin._action_dbus_verify_profilecCstd�dS)Nz Not supported in no_daemon mode.F)r&)rr|r"r"r#�_action_verify_profilevszAdmin._action_verify_profilecCs:d}|jj|j|�|_|jj�}|s.|jd�|jj|�S)NrjrkzCannot disable active profile.r>)rrprrZoffrrF)rr rbr"r"r#�_action_dbus_offzs
zAdmin._action_dbus_offcCstd�dS)Nz Not supported in no_daemon mode.F)r&)rr"r"r#�_action_off�szAdmin._action_offrDcCs(|dkr|j�S|dkr$|j|d�SdS)aDPrint 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
rD�plugins)�verboseN)rG�_action_dbus_list_plugins)r�list_choicer�r"r"r#rm�s zAdmin._action_dbus_listcCs(|dkr|j�S|dkr$|j|d�SdS)aaPrint 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
rDr�)r�N)rI�_action_list_plugins)rr�r�r"r"r#�_action_list�s zAdmin._action_listcCs�|jj�}xv|j�D]j}t|�|st||�dkr8q|jj|�}x8||D],}td|�|j|d�}|rNtd|�qNWqW|jjd�S)zvPrint accessible plugins
Keyword arguments:
verbose -- if is set to True then parameters and hints are printed
rz %sNz %sT)rZget_plugins�keysr&rlZget_plugin_hints�getrF)rr�r��pluginZhintsZ parameterZhintr"r"r#r��s
zAdmin._action_dbus_list_pluginscCstd�dS)Nz Not supported in no_daemon mode.F)r&)rr�r"r"r#r��szAdmin._action_list_pluginscCs0|jj||�\}}|s$|jd|�|jj|�S)NzUnable to acquire devices: %s)rZinstance_acquire_devicesrrF)r�devices�instancerbrcr"r"r#�%_action_dbus_instance_acquire_devices�sz+Admin._action_dbus_instance_acquire_devicescCstd�dS)Nz Not supported in no_daemon mode.F)r&)rr�r�r"r"r#� _action_instance_acquire_devices�sz&Admin._action_instance_acquire_devicescCs^|jj|�\}}}|s0|jd|�|jjd�Sx |D]\}}td||f�q6W|jjd�S)NzUnable to list instances: %sFz%s (%s)T)rZ
get_instancesrrFr&)r�plugin_namerbrcZpairsr�r�r"r"r#�_action_dbus_get_instances�sz Admin._action_dbus_get_instancescCstd�dS)Nz Not supported in no_daemon mode.F)r&)rr�r"r"r#�_action_get_instances�szAdmin._action_get_instancescCsR|jj|�\}}}|s0|jd|�|jjd�Sx|D]}t|�q6W|jjd�S)NzUnable to list devices: %sFT)rZinstance_get_devicesrrFr&)rr�rbrcr�Zdevicer"r"r#�!_action_dbus_instance_get_devices�s
z'Admin._action_dbus_instance_get_devicescCstd�dS)Nz Not supported in no_daemon mode.F)r&)rr�r"r"r#�_action_instance_get_devices�sz"Admin._action_instance_get_devicesN)r
)r
)rDF)rDF)F)F)7�__name__�
__module__�__qualname__rZ
ADMIN_TIMEOUT�loggingZERRORr$rrr4r<rArGrIrKrNrOrQrRrVrWrXrYrZrErHr]r_r`rdrgr8rqrrrsrtrvrxryrzr}r~rr�rmr�r�r�r�r�r�r�r�r�r"r"r"r#r sd
r )Z
__future__rZtuned.adminrZtuned.utils.commandsrZtuned.profilesrr�
exceptionsrZtuned.exceptionsrZtuned.constsrZtuned.utils.profile_recommenderrr-r'r1rerr��objectr r"r"r"r#�<module>s
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists