Your IP : 3.147.75.217


Current Path : /usr/lib/python3.6/site-packages/tuned/utils/__pycache__/
Upload File :
Current File : //usr/lib/python3.6/site-packages/tuned/utils/__pycache__/commands.cpython-36.opt-1.pyc

3

�@ag�@�@srddlZddlZddlZddlZddlZddlZddljZddl	Z	ddl
TddlmZej
j�ZGdd�d�ZdS)�N)�*)�TunedExceptionc@s�eZdZdbdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Zdcdd�Z	dddd�Z
dd�Zdd�Zdd�Z
dedd�Zdfdd�Zdgd d!�Zdhd#d$�Zdid%d&�Zdjd'd(�Zdkd)d*�Zdld+d,�Zd-d.�Zd/d0�Zdmd1d2�Zd3d4�Zd5d6�Zdnd7d8�Zddigdfd9d:�Zdod;d<�Zd=d>�Zd?d@�ZdAdB�Z dpdDdE�Z!dFdG�Z"dHdI�Z#dJdK�Z$dLdM�Z%dqdNdO�Z&dPdQ�Z'dRdS�Z(dTdU�Z)dVdW�Z*dXdY�Z+dZd[�Z,d\d]�Z-d^d_�Z.d`da�Z/dS)r�commandsTcCs
||_dS)N)�_logging)�selfZlogging�r�/usr/lib/python3.6/commands.py�__init__szcommands.__init__cCs|jrtj|�dS)N)r�log�error)r�msgrrr�_errorszcommands._errorcCs|jrtj|�dS)N)rr
�debug)rrrrr�_debugszcommands._debugc	Cs.t|�j�j�}ddddddddd�j||�S)N�1�0)�YZYES�TZTRUE�NZNO�FZFALSE)�str�upper�strip�get)r�value�vrrr�get_boolszcommands.get_boolcCstjddt|��j�S)Nz\s+� )�re�subrr)r�srrr�	remove_wsszcommands.remove_wscCstjdd|�S)Nz^"(.*)"$z\1)rr)rrrrr�unquote"szcommands.unquote�\cCs|j|d||f�S)Nz%s%s)�replace)rr Zwhat_escapeZ	escape_byrrr�escape&szcommands.escapecCs|j|d�S)N�)r$)rr Zescape_charrrr�unescape*szcommands.unescapecCs|d|t|�|S)Nr)�len)r�s1�pos�s2rrr�	align_str.szcommands.align_strcCs2g}|dk	r.x t|j��D]}|t|�7}qW|S)N)�sorted�items�list)r�d�l�irrr�	dict2list4s
zcommands.dict2listcCs(|dkrdStjddjt|j����S)Nz(%s)z)|()r�compile�joinr/�keys)rr0rrr�re_lookup_compile<szcommands.re_lookup_compileNrcsV�dkr|dkr.|Snt��dks*|dkr.|S|dkr@|j��}|j�fdd�||�S)Nrcst�j��|jdS)N�)r/�values�	lastindex)�mo)r0rr�<lambda>Msz.commands.multiple_re_replace.<locals>.<lambda>)r(r7r)rr0r �r�flagsr)r0r�multiple_re_replaceDs
zcommands.multiple_re_replacecCsRt|�dks|dkrdS|dkr*|j|�}|j|�}|rNt|j��|jdSdS)Nrr8)r(r7�searchr/r9r:)rr0r r=r;rrr�	re_lookupSs

zcommands.re_lookupFc	Cs�|jd||f�|r2tjj|�}tjj|�r2d}y6|rBtj|�t|d�}|jt|��|j	�d}Wn`t
tfk
r�}z@d}t|t
�r�|s�t|t�r�|j|kr�|jd||f�WYdd}~XnX|S)aWrite data to a file.

		Parameters:
		f -- filename where to write
		data -- data to write
		makedir -- if True and the path doesn't exist, it will be created
		no_error -- if True errors are silenced, it can be also list of ignored errnos

		Return:
		bool -- True on success
		zWriting to file: '%s' < '%s'F�wTz Writing to file '%s' error: '%s'N)r�os�path�dirname�isdir�makedirs�open�writer�close�OSError�IOError�
isinstance�boolr/�errnor
)	r�f�dataZmakedir�no_errorr0�fdZrc�errr�
write_to_file]s$

$zcommands.write_to_filer&cCsv|}yt|d�}|j�}|j�Wn<ttfk
r^}z|sN|jd||f�WYdd}~XnX|jd||f�|S)Nr=z"Error when reading file '%s': '%s'z Read data from file: '%s' > '%s')rH�readrJrKrLr
r)rrPZerr_retrRZ	old_valuerTrrr�	read_file|s
$zcommands.read_filecCsj|jd|�tjj|�rfytj||�Wn:tk
rd}z|sVtjd|t	|�f�dSd}~XnXdS)NzRemoving tree: '%s'zcannot remove tree '%s': '%s'FT)
rrCrD�exists�shutil�rmtreerKr
rr)rrPrRrrrrrZ�szcommands.rmtreecCsh|jd|�tjj|�rdytj|�Wn:tk
rb}z|sTtjd|t|�f�dSd}~XnXdS)NzRemoving file: '%s'zcannot remove file '%s': '%s'FT)	rrCrDrX�unlinkrKr
rr)rrPrRrrrrr[�szcommands.unlinkcCsd|jd||f�ytj||�Wn<tk
r^}z |sPtjd||t|�f�dSd}~XnXdS)NzRenaming file '%s' to '%s'z%cannot rename file '%s' to '%s': '%s'FT)rrC�renamerKr
rr)r�src�dstrRrrrrr\�szcommands.renamecCs`y"tjd||f�tj||�dStk
rZ}z|sLtjd|||f�dSd}~XnXdS)Nzcopying file '%s' to '%s'Tz!cannot copy file '%s' to '%s': %sF)r
rrY�copyrLr)rr]r^rRrTrrrr_�sz
commands.copycCs6|j|�}t|�dkrdS|j|tj|||tjd��S)NrF)r>)rWr(rUrr�	MULTILINE)rrP�pattern�replrQrrr�replace_in_file�s
zcommands.replace_in_filecCs4|j|�}t|�dkrdS|j||j||tjd��S)NrF)r>)rWr(rUr?rr`)rrPr0rQrrr�multiple_replace_in_file�s
z!commands.multiple_replace_in_filecCs�|j|�}x�|D]�}t|�}t||�}tjd|d|tjd�dkr||r�t|�dkrj|ddkrj|d7}|d||f7}qtjd|d	d
|j|�d|tjd�}qW|j||�S)Nz\bz\s*=.*$)r>rr8�
z%s="%s"
z\b(z\s*=).*$z\1�"���z\1")	rWrrr@r`r(rr%rU)rrPr0�addrQ�opt�orrrr�add_modify_option_in_file�s

.z"commands.add_modify_option_in_filecCs"|j|�}tjt|�jd��j�S)Nzutf-8)rW�hashlibZmd5r�encode�	hexdigest)rrPrQrrr�md5sum�s
zcommands.md5sumcCs"|j|�}tjt|�jd��j�S)Nzutf-8)rWrlZsha256rrmrn)rrPrQrrr�	sha256sum�s
zcommands.sha256sumcCs|jtj|�j�S)N)rW�constsZMACHINE_ID_FILEr)rrRrrr�get_machine_id�szcommands.get_machine_idcCsVd}tjj�}d|d<|j|�|jdt|��d}	d}
y�t|tt|||ddd�}|j�\}	}|j	}|r�||kr�d|kr�|dd�}
t
|
�dkr�|	dd�}
d	d
j|�|
f}
|s�|j|
�Wnxt
tfk
�r8}zV|jdk	r�|jnd
}t|�|k�r(d|k�r(d	d
j|�|f}
|�s(|j|
�WYdd}~XnX|�rJ||	|
fS||	fSdS)Nr�C�LC_ALLz
Executing %s.r&T)�stdout�stderr�env�shell�cwdZ	close_fdsZuniversal_newlinesr8zExecuting '%s' error: %srrgrgrg)rC�environr_�updaterr�Popen�PIPEZcommunicate�
returncoder(r5r
rKrLrO�abs)r�argsrxryrwZ	no_errorsZ
return_errZretcodeZ_environment�outZerr_msg�proc�errZerr_outrTrrr�execute�s>


zcommands.executecCs.tjd|�}|r|jd�S|r*|j�dS|S)Nz.*\[([^\]]+)\].*r8r)r�match�group�split)rZoptionsZdosplit�mrrr�get_active_options
zcommands.get_active_optioncCs*t|�}|dkp(|jd|dd�j�dkS)Nrz$/sys/devices/system/cpu/cpu%s/onlineT)rRr)rrWr)r�cpuZscpurrr�
is_cpu_onlineszcommands.is_cpu_onlinecCs\|dkrdSt|�jdd�}yt|d�}Wn&tk
rPtjdt|��gSX|j|�S)N�,r&�zinvalid hexadecimal mask '%s')rr$�int�
ValueErrorr
r�bitmask2cpulist)r�maskr�rrr�hex2cpulistszcommands.hex2cpulistcCs<d}g}x.|dkr6|d@r$|j|�|dL}|d7}q
W|S)Nrr8)�append)rr�r��cpusrrrr�&s

zcommands.bitmask2cpulist�'"cCs^g}|dkr|S|}t|�tk	rD|dk	r6t|�j|�}t|�jd�}g}g}d}d}�x|D�]}	t|	�}
|r�t|
�dkr�d}|j|�d}n||
7}q\|
dd�j�dkr�d}|
}q\|
o�|
ddks�|
dd	k�rP|
d
d�jd�}yPt|�d
k�r|ttt	|d�t	|d
�d
��7}n|jt	|
d
d���Wnt
k
�rLgSXq\t|
�dkr\|j|
�q\Wt|�dk�r�|j|�x�|D]�}	|	jd�}|	dd�j�dk�r�||j|	�7}ndyLt|�d
k�r�|ttt	|d�t	|d
�d
��7}n|jt	|d��Wnt
k
�rgSX�q�Wttt
|���}
x"|D]}||
k�r<|
j|��q<W|
S)Nr�Fr&r�Z0xT�^�!r8�-)�typer/rrr�r(r��lower�ranger�r�r�r-�set�remove)rr1Zstrip_chars�rlZllZll2Z
negation_listZhexmaskZhvrZsv�nlZvlZcpu_listr�rrr�cpulist_unpack9sh





(

zcommands.cpulist_unpackcCs�|j|�}|dkst|�dkr"|Sd}|}g}xz|dt|�kr�||d||dkr�||kr�|jt||�dt||��n|jt||��|d}|d7}q0W|dt|�kr�|jt||�dt|d��n|jt|d��|S)Nrr8r�rgrg)r�r(r�r)rr1r2�jr�rrr�cpulist_pack}s"
$$zcommands.cpulist_packcCs.|j|�}|j|jd��}tt|�t|��S)Nz/sys/devices/system/cpu/online)r�rWr/r�)rr1r�Zonlinerrr�cpulist_invert�s
zcommands.cpulist_invertcs�|dkrdS|j|�}|dkr"dS|j|�}d|�t��}|ddkrX|d|d7}�j|��dj�fdd�tdt��d�D��S)Nz%x�rr�c3s|]}�||d�VqdS)r�Nr)�.0r2)r rr�	<genexpr>�sz'commands.cpulist2hex.<locals>.<genexpr>)r��cpulist2bitmaskr(�zfillr5r�)rr1Zulr�Zlsr)r r�cpulist2hex�s


zcommands.cpulist2hexcCs$d}x|D]}|td|�O}q
W|S)Nrr�)�pow)rr1r�rrrrr��s
zcommands.cpulist2bitmaskcsdj�fdd�|D��S)Nr�c3s|]}�t|�VqdS)N)r)r�r)�prefixrrr��sz*commands.cpulist2string.<locals>.<genexpr>)r5)rr1r�r)r�r�cpulist2string�szcommands.cpulist2stringcCsb|dd�j�dkrH|dd�}|dd�j�dkrHdd�|j|�D�Stjd|�}dd�|D�S)Nrr�zcpulist:cSsg|]}dt|��qS)r�)r)r�rrrr�
<listcomp>�sz(commands.devstr2devs.<locals>.<listcomp>z\s*(?<!\\),\s*cSsg|]}t|�jdd��qS)z\,r�)rr$)r�rrrrr��s)r�r�rr�)rr r1rrr�devstr2devs�szcommands.devstr2devsc	Cs"y|j|d�S|j|�SdS)NF)�wait)rZ	terminateZtimerrrr��sz
commands.waitcCs�t|�j�j�}x�dD]�}|j|�}|dkr.qybt|d|��}||d�}|dkr^|d9}n0|dkrp|d	9}n|dkr�|d9}n|dkr�d}|Stk
r�dSXqWdS)N�KB�MB�GBr&r8i)r�r�r�r&rgiii@)rrr�rfindr�r�)rr ZunitZunit_ix�val�urrr�get_size�s&




zcommands.get_sizecCs�d}d}y(ttjd��}|j�j�}WdQRXWnjtk
rj}z|jtjkrZtd|��WYdd}~Xn2t	t
fk
r�}ztd|��WYdd}~XnXyHttjd��2}|j�j�}|dtjtj
gkr�tdtj��WdQRXWnptk
�r"}z |jtjk�rtd|��WYdd}~Xn4t	t
fk
�rT}ztd|��WYdd}~XnX|dk�rfd}n
|tj
k}|dk�r~d}||fS)Nr&r=z!Failed to read active profile: %szInvalid value in file %s.zFailed to read profile mode: %s)rHrq�ACTIVE_PROFILE_FILErVrrLrO�ENOENTrrK�EOFError�PROFILE_MODE_FILE�ACTIVE_PROFILE_AUTO�ACTIVE_PROFILE_MANUAL)r�profile_name�moderPrT�manualrrr�get_active_profile�s4


zcommands.get_active_profilecCs�y2ttjd��}|dk	r&|j|d�WdQRXWn4ttfk
rf}ztd|j��WYdd}~XnXy:ttjd��$}|r�tj	ntj
}|j|d�WdQRXWn4ttfk
r�}ztd|j��WYdd}~XnXdS)NrBrez!Failed to save active profile: %szFailed to save profile mode: %s)rHrqr�rIrKrLr�strerrorr�r�r�)rr�r�rPrTr�rrr�save_active_profile�s zcommands.save_active_profilecCs�d}y(ttjd��}|j�j�}WdQRXWnjtk
rf}z|jtjkrVtd|��WYdd}~Xn2t	t
fk
r�}ztd|��WYdd}~XnX|dkr�d}|S)Nr&r=z1Failed to read the active post-loaded profile: %s)rHrq�POST_LOADED_PROFILE_FILErVrrLrOr�rrKr�)rr�rPrTrrr�get_post_loaded_profilesz commands.get_post_loaded_profilecCsly2ttjd��}|dk	r&|j|d�WdQRXWn4ttfk
rf}ztd|j��WYdd}~XnXdS)NrBrez1Failed to save the active post-loaded profile: %s)rHrqr�rIrKrLrr�)rr�rPrTrrr�save_post_loaded_profilesz!commands.save_post_loaded_profilecCsDytj||�}Wn(tk
r8ddl}|j||�}YnX|j|�S)Nr)r�	maketrans�AttributeError�string�	translate)r�textZsource_charsZ
dest_charsZtransr�rrr�trszcommands.trcCs&|p$tdd�|D��o$t|�tjkS)Ncss|]}|tjkVqdS)N)rqZNAMES_ALLOWED_CHARS)r��crrrr�&sz)commands.is_valid_name.<locals>.<genexpr>)�allr(rqZNAMES_MAX_LENGTH)r�namerrr�
is_valid_name%szcommands.is_valid_name)T)r#r#)r#)Nr)N)FF)r&F)F)F)F)F)T)T)T)r�)r&)0�__name__�
__module__�__qualname__r	r
rrr!r"r%r'r,r3r7r?rArUrWrZr[r\r_rcrdrkrorprrr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrrsZ
















'
	
D
		r)rOrlZ
tuned.logsZtunedr_rCrYZtuned.constsrqr�
subprocessZtuned.exceptionsrZlogsrr
rrrrr�<module>s



?>