Sindbad~EG File Manager
3
�<�e�W�@slddlZddljZddlZddlZddlZddlmZddl Z ddl
mZmZej
j�ZGdd�de�ZdS)�N)�commands)�Popen�PIPEc@s&eZdZdZdd�Zdd�Zdd�Zedd ��Ze d
d��Z
e dd
��Ze dd��Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zejfd2d3�Z d4d5�Z!d6d7�Z"d8d9�Z#ejfd:d;�Z$d<d=�Z%d>d?�Z&ejfd@dA�Z'dBdC�Z(dDdE�Z)dFdG�Z*dHdI�Z+dJdK�Z,dLdM�Z-d|dOdP�Z.d}dQdR�Z/d~dSdT�Z0ddUdV�Z1dWdX�Z2dYdZ�Z3d[d\�Z4d]d^�Z5d_d`�Z6d�dbdc�Z7d�ddde�Z8dfdg�Z9dhdi�Z:djdk�Z;d�dldm�Z<d�dndo�Z=dpdq�Z>drds�Z?dtdu�Z@d�dvdw�ZAd�dxdy�ZBdzd{�ZCdNS)��Plugina
Base class for all plugins.
Plugins change various system settings in order to get desired performance or power
saving. Plugins use Monitor objects to get information from the running system.
Intentionally a lot of logic is included in the plugin to increase plugin flexibility.
c Csn|j|jj�|_||_||_||_||_||_t j
�|_|j�||_
||_d|_d|_|j�|_t�|_dS)zPlugin constructor.FN)�create� __class__�__name__�_storageZ_monitors_repositoryZ_hardware_inventory�_device_matcher�_device_matcher_udev�_instance_factory�collections�OrderedDict�
_instances�_init_commands�_global_cfg�
_variables�_has_dynamic_options�_devices_inited�#_get_config_options_used_by_dynamic�_options_used_by_dynamicr�_cmd) �selfZmonitors_repositoryZstorage_factoryZhardware_inventoryZdevice_matcherZdevice_matcher_udevZinstance_factoryZ
global_cfg� variables�r�/usr/lib/python3.6/base.py�__init__s
zPlugin.__init__cCs|j�dS)N)�destroy_instances)rrrr�cleanup,szPlugin.cleanupcCs|js|j�d|_dS)NT)r�
_init_devices)rrrr�init_devices/szPlugin.init_devicescCs|jjjd�djdd�dS)N�.��_���)r�
__module__�split)rrrr�name4szPlugin.namecCsiS)z-Default configuration options for the plugin.r)rrrr�_get_config_options<szPlugin._get_config_optionscCsiS)z*Explanation of each config option functionr)�clsrrr�get_config_options_hintsAszPlugin.get_config_options_hintscCsgS)znList of config options used by dynamic tuning. Their previous values will be automatically saved and restored.r)rrrrrFsz*Plugin._get_config_options_used_by_dynamiccCsP|j�j�}x>|D]6}||ks$|jr2||||<qtjd||jjf�qW|S)z3Merge provided options with plugin default options.z$Unknown option '%s' for plugin '%s'.)r(�copyr�log�warnrr)r�optionsZ effective�keyrrr�_get_effective_optionsKs
zPlugin._get_effective_optionscCs,t|�tkr|St|�j�}|dkp*|dkS)N�true�1)�type�bool�str�lower)r�valuerrr�_option_boolVszPlugin._option_boolc CsF||jkrtd|��|j|�}|jj|||||||�}||j|<|S)z8Create new instance of the plugin and seize the devices.z.Plugin instance with name '%s' already exists.)r� Exceptionr0rr) rr'�devices_expression�devices_udev_regex�
script_pre�script_postr.Zeffective_options�instancerrr�create_instance`s
zPlugin.create_instancecCsV|j|krtd||f��|j|jkr2td|��|j|j}|j|�|j|j=dS)zDestroy existing instance.z9Plugin instance '%s' does not belong to this plugin '%s'.z+Plugin instance '%s' was already destroyed.N)Z_pluginr9r'r�_destroy_instance)rr>rrr�destroy_instancels
zPlugin.destroy_instancecCs$tjd|j|jf�|j|�dS)zInitialize an instance.zinitializing instance %s (%s)N)r,�debugr'�_instance_init)rr>rrr�initialize_instancewszPlugin.initialize_instancecCsFx6t|jj��D]$}tjd|j|jf�|j|�qW|jj�dS)zDestroy all instances.zdestroying instance %s (%s)N)�listr�valuesr,rBr'r@�clear)rr>rrrr|szPlugin.destroy_instancescCs|j|�|j|�dS)N)�release_devices�_instance_cleanup)rr>rrrr@�s
zPlugin._destroy_instancecCs
t��dS)N)�NotImplementedError)rr>rrrrC�szPlugin._instance_initcCs
t��dS)N)rJ)rr>rrrrI�szPlugin._instance_cleanupcCsd|_t�|_t�|_dS)NF)�_devices_supported�set�_assigned_devices�
_free_devices)rrrrr�szPlugin._init_devicescCsdS)z�Override this in a subclass to transform a list of device names (e.g. ['sda'])
to a list of pyudev.Device objects, if your plugin supports itNr)r�devicesrrr�_get_device_objects�szPlugin._get_device_objectscCsj|jdkrt|jj|j|��S|j|�}|dkrDtjd|j�t�S|j j|j|�}tdd�|D��SdS)Nz<Plugin '%s' does not support the 'devices_udev_regex' optioncSsg|]
}|j�qSr)Zsys_name)�.0�xrrr�
<listcomp>�sz0Plugin._get_matching_devices.<locals>.<listcomp>)
r;rLr
Z
match_listr:rPr,�errorr'r)rr>rOZudev_devicesrrr�_get_matching_devices�s
zPlugin._get_matching_devicescCs�|js
dStjd|j�|j||j�}t|�dk|_|jsNtjd|j�n`|j}|j|jkrn|d|j7}tj d|dj
|�f�|jj|�|j
|O_
|j|8_dS)Nz assigning devices to instance %srz*instance %s: no matching devices availablez (%s)z!instance %s: assigning devices %sz, )rKr,rBr'rUrN�len�activer-�info�join�assigned_devices�updaterM)rr>Z to_assignr'rrr�assign_free_devices�szPlugin.assign_free_devicescCsV|js
dS|j|jB|j@}d|_|jj�|jj�|j|8_|j|O_dS)NF)rK�processed_devicesrZrMrWrGrN)rr>Z
to_releaserrrrH�s
zPlugin.release_devicescCs(|jsdg}x|D]}|||�qWdS)N)rK)rr>�callbackrO�devicerrr�_run_for_each_device�s
zPlugin._run_for_each_devicecCsdS)Nr)rr>�enablingrrr�_instance_pre_static�szPlugin._instance_pre_staticcCsdS)Nr)rr>rarrr�_instance_post_static�szPlugin._instance_post_staticcCsn|dkrdSt|�dkr0tjd|j|f�dS|jd�sHtjd�dStjj|�}d}�x|D�]}tj } | j
|jj��|g}
|t
jkr�|
jd�|
j|�tjd |t|
�f�tjd
tt| j����yVt|g|
ttd| |dd�}|j�\}}
|j�r$tjd||j|
dd�f�d}Wq`ttfk
�rd}ztjd||f�d}WYdd}~Xq`Xq`W|S)Nrz1Instance '%s': no device to call script '%s' for.�/z<Relative paths cannot be used in script_pre or script_post. zUse ${i:PROFILE_DIR}.FTZ
full_rollbackz'calling script '%s' with arguments '%s'zusing environment '%s')�stdout�stderrZ close_fds�env�cwdZuniversal_newlineszscript '%s' error: %d, '%s'r"zscript '%s' error: %szQRelative paths cannot be used in script_pre or script_post. Use ${i:PROFILE_DIR}.r$)rVr,r-r'�
startswithrT�os�path�dirname�environr[rZget_env�constsZ
ROLLBACK_FULL�appendrXr5rBrE�itemsrrZcommunicate�
returncode�OSError�IOError)rr>Zscript�oprO�rollbackZdir_name�retZdevrmZ arguments�proc�out�err�errr�_call_device_script�sB
zPlugin._call_device_scriptcCs�|js
dS|jrZ|j||jd|j�|j|d�|j|�|j|d�|j||jd|j�|j r�|j
jtj
tj�r�|j||j|j�|jj|j�|jj�dS)zG
Apply static and dynamic tuning if the plugin instance is active.
NZapplyT)rW�has_static_tuningr{r<rZrb�_instance_apply_staticrcr=�has_dynamic_tuningr�getrn�CFG_DYNAMIC_TUNING�CFG_DEF_DYNAMIC_TUNINGr`�_instance_apply_dynamicr]r[rG)rr>rrr�instance_apply_tuning�s
zPlugin.instance_apply_tuningcCs�|js
dSt|j�dkr.tjddj|j��|jj�}|jr�|j ||j
d|�dkrXdS|j|||�dkrndS|j ||jd|�dkr�dSdSdSdS)z<
Verify static tuning if the plugin instance is active.
Nrz)BUG: Some devices have not been tuned: %sz, ZverifyFT)
rWrVrZr,rTrYr]r+r|r{r<�_instance_verify_staticr=)rr>�ignore_missingrOrrr�instance_verify_tunings
zPlugin.instance_verify_tuningcCs<|js
dS|jr8|jjtjtj�r8|j||j|j j
��dS)z<
Apply dynamic tuning if the plugin instance is active.
N)rWr~rrrnr�r�r`�_instance_update_dynamicr]r+)rr>rrr�instance_update_tuning$szPlugin.instance_update_tuningcCs�|tjkrdS|jr8|jjtjtj�r8|j||j|j �|j
r�|j||jd|j |d�|j
|d�|j||�|j|d�|j||jd|j |d�dS)z8
Remove all tunings applied by the plugin instance.
NZunapply)ruF)rnZ
ROLLBACK_NONEr~rrr�r�r`�_instance_unapply_dynamicr]r|r{r=rb�_instance_unapply_staticrcr<)rr>rurrr�instance_unapply_tuning-s
zPlugin.instance_unapply_tuningcCs|j|�|j||j�dS)N)� _execute_all_non_device_commands�_execute_all_device_commandsrZ)rr>rrrr}?s
zPlugin._instance_apply_staticcCs2d}|j||�dkrd}|j|||�dkr.d}|S)NTF)�_verify_all_non_device_commands�_verify_all_device_commands)rr>r�rOrvrrrr�CszPlugin._instance_verify_staticcCs|j||j�|j|�dS)N)�_cleanup_all_device_commandsr]� _cleanup_all_non_device_commands)rr>rurrrr�KszPlugin._instance_unapply_staticcsFx4���fdd��jD�D]}�j��j|��qW�j���dS)Ncs(g|] }�j��j|��dkr|�qS)N)�_storage_get� _commands)rQ�opt)r_r>rrrrSQsz2Plugin._instance_apply_dynamic.<locals>.<listcomp>)r�_check_and_save_valuer�r�)rr>r_Zoptionr)r_r>rrr�PszPlugin._instance_apply_dynamiccCs
t��dS)N)rJ)rr>r_rrrr�Vsz Plugin._instance_unapply_dynamiccCs
t��dS)N)rJ)rr>r_rrrr�YszPlugin._instance_update_dynamiccCstj�|_|j�|j�dS)z
Initialize commands.
N)r
rr��_autoregister_commands�_check_commands)rrrrr`s
zPlugin._init_commandscCs�x�|jjD]�}|jd�rq
t||�}t|d�s0q
|jd}|jj|d|i�}d|jkr�d|d<||d<|jd|d<|jd|d<nBd |jkr�||d <n.d|jkr�||d<|jd|d<|jd|d<||j|<q
Wtj t
t|jj��d
d�d��|_dS)
zd
Register all commands marked using @command_set, @command_get, and @command_custom decorators.
�__�_commandr'rLN�custom�
per_device�priorityrcSs|ddS)Nr"r�r)Z name_inforrr�<lambda>�sz/Plugin._autoregister_commands.<locals>.<lambda>)r/)
r�__dict__ri�getattr�hasattrr�r�rr
r�sorted�iterrp)r�member_name�member�command_namerXrrrr�hs*
zPlugin._autoregister_commandscCsJxDt|jj��D]2\}}|jdd�r&qd|ks6d|krtd|��qWdS)z2
Check if all commands are defined correctly.
r�FrrLz,Plugin command '%s' is not defined correctlyN)rEr�rpr� TypeError)rr��commandrrrr��s
zPlugin._check_commandsNcCsJt|�j}|dkrdn|}|dkr&dn|}|dkr6dn|}d||||fS)N�z%s/%s/%s/%s)r3r)rZ
instance_namer��device_name�
class_namerrr�_storage_key�s
zPlugin._storage_keycCs&|j|j|d|�}|jj||�dS)Nr')r�r'r rL)rr>r�r7r�r/rrr�_storage_set�szPlugin._storage_setcCs |j|j|d|�}|jj|�S)Nr')r�r'r r)rr>r�r�r/rrrr��szPlugin._storage_getcCs |j|j|d|�}|jj|�S)Nr')r�r'r Zunset)rr>r�r�r/rrr�_storage_unset�szPlugin._storage_unsetcCsVxPdd�t|jj��D�D]4}|jj|jj|dd��}|dk r|j|||�qWdS)NcSsg|]}|ds|�qS)r�r)rQr�rrrrS�sz;Plugin._execute_all_non_device_commands.<locals>.<listcomp>r')rEr�rFr�expandr.r�_execute_non_device_command)rr>r�� new_valuerrrr��sz'Plugin._execute_all_non_device_commandscCshxbdd�t|jj��D�D]F}|jj|jj|dd��}|dkrBqx|D]}|j||||�qHWqWdS)NcSsg|]}|dr|�qS)r�r)rQr�rrrrS�sz7Plugin._execute_all_device_commands.<locals>.<listcomp>r')rEr�rFrr�r.r�_execute_device_command)rr>rOr�r�r_rrrr��s
z#Plugin._execute_all_device_commandscCsdd}xZdd�t|jj��D�D]>}|jj|jj|dd��}|dk r|j||||�dkrd}qW|S)NTcSsg|]}|ds|�qS)r�r)rQr�rrrrS�sz:Plugin._verify_all_non_device_commands.<locals>.<listcomp>r'F)rEr�rFrr�r.r�_verify_non_device_command)rr>r�rvr�r�rrrr��sz&Plugin._verify_all_non_device_commandscCsnd}xddd�t|jj��D�D]H}|jj|dd�}|dkr>qx&|D]}|j|||||�dkrDd}qDWqW|S)NTcSsg|]}|dr|�qS)r�r)rQr�rrrrS�sz6Plugin._verify_all_device_commands.<locals>.<listcomp>r'F)rEr�rFr.r�_verify_device_command)rr>rOr�rvr�r�r_rrrr��s
z"Plugin._verify_all_device_commandscCs�|dk r�t|�}t|�dkr |S|dd�}|dd�}|dkrP|dkrL|S|SyF|dkrtt|�t|�krn|SdSn |dkr�t|�t|�kr�|SdSWn*tk
r�tjd||||f�YnX|S)Nr"�<�>zhcannot compare new value '%s' with current value '%s' by operator '%s', using '%s' directly as new value)r�r�)r5rV�int�
ValueErrorr,r-)rr��
current_valueZnwsrt�valrrr�_process_assignment_modifiers�s(z$Plugin._process_assignment_modifiersFcCs&|dk r|d||d�S|d�SdS)Nr)r�r)rr�r_r�rrr�_get_current_value�szPlugin._get_current_valuecCs<|j||�}|j||�}|dk r8|dk r8|j||||�|S)N)r�r�r�)rr>r�r_r�r�rrrr��s
zPlugin._check_and_save_valuecCsR|ddk r"|dd||dd�n,|j||||�}|dk rN|d||ddd�dS)Nr�TFrL)�sim�remove)r�)rr>r�r_r�rrrr��s
zPlugin._execute_device_commandcCsN|ddk r |dd|dd�n*|j||d|�}|dk rJ|d|ddd�dS)Nr�TFrL)r�r�)r�)rr>r�r�rrrr��s
z"Plugin._execute_non_device_commandcCs.|jjt|��}tjd|�r*tjdd|�S|S)Nz\s*(0+,?)+([\da-fA-F]*,?)*\s*$z^\s*(0+,?)+r�)rZunquoter5�re�match�sub)rr7�vrrr�_norm_valueszPlugin._norm_valuec Cs(|dkrdSd}|dkrN|rN|dkr6tjtj|�ntjtj||f�dS|dk �r|j|�}|j|�}yt|�t|�k}Wn�tk
�ryt|d�t|d�k}Wn^tk
�rt|�t|�k}|�st|�j d�}x"|D]}|j
�}||k}|r�Pq�WYnXYnX|j|||||d�|S)NFT��|)r_)r,rXrnZ STR_VERIFY_PROFILE_VALUE_MISSINGZ'STR_VERIFY_PROFILE_DEVICE_VALUE_MISSINGr�r�r�r5r&�strip�_log_verification_result) rr'r�r�r�r_rv�valsr�rrr�
_verify_value
s8
zPlugin._verify_valuecCs�|rL|dkr*tjtj|t|�j�f�ntjtj||t|�j�f�dS|dkr|tjtj|t|�j�t|�j�f�n(tjtj ||t|�j�t|�j�f�dSdS)NTF)
r,rXrnZSTR_VERIFY_PROFILE_VALUE_OKr5r�Z"STR_VERIFY_PROFILE_DEVICE_VALUE_OKrTZSTR_VERIFY_PROFILE_VALUE_FAILZ$STR_VERIFY_PROFILE_DEVICE_VALUE_FAIL)rr'�successr�r�r_rrrr�-s((zPlugin._log_verification_resultcCsp|ddk r |dd||d|�S|j|||d�}|j||�}|dkrHdS|d||dd�}|j|d||||�S)Nr�T)r�rLFr')r�r�r�)rr>r�r_r�r�r�rrrr�<szPlugin._verify_device_commandcCsd|ddk r|dd|d|�S|j|�}|j||�}|dkr@dS|d|dd�}|j|d|||�S)Nr�TrLFr')r�r�r�)rr>r�r�r�r�rrrr�Fs
z!Plugin._verify_non_device_commandcCsZxTtdd�t|jj��D��D]4}|jj|dd�dk sF|d|jkr|j||�qWdS)NcSsg|]}|ds|�qS)r�r)rQr�rrrrSQsz;Plugin._cleanup_all_non_device_commands.<locals>.<listcomp>r')�reversedrEr�rFr.rr�_cleanup_non_device_command)rr>r�rrrr�Ps"$z'Plugin._cleanup_all_non_device_commandscCslxftdd�t|jj��D��D]F}|jj|dd�dk sF|d|jkrx|D]}|j||||�qLWqWdS)NcSsg|]}|dr|�qS)r�r)rQr�rrrrSVsz7Plugin._cleanup_all_device_commands.<locals>.<listcomp>r')r�rEr�rFr.rr�_cleanup_device_command)rr>rOr�r�r_rrrr�Us"$
z#Plugin._cleanup_all_device_commandscCs^|ddk r"|ddd|dd�n8|j|||�}|dk rL|d||d|d�|j|||�dS)Nr�FrL)r�r�)r�r�)rr>r�r_r�� old_valuerrrr�[szPlugin._cleanup_device_commandcCsV|ddk r |ddddd�n2|j||�}|dk rF|d|ddd�|j||�dS)Nr�FrL)r�r�)r�r�)rr>r�r�rrrr�dsz"Plugin._cleanup_non_device_command)NNN)N)N)N)NF)NN)N)N)F)F)Drr%�__qualname__�__doc__rrr �propertyr'�classmethodr(r*rr0r8r?rArDrr@rCrIrrPrUr\rHr`rbrcrnZ
ROLLBACK_SOFTr{r�r�r�r�r}r�r�r�r�r�rr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrrs|
#
!
r)r�Ztuned.constsrnZtuned.profiles.variablesZtunedZ
tuned.logsr
Ztuned.utils.commandsrrj�
subprocessrrZlogsrr,�objectrrrrr�<module>s
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists