o
    _w9                     @   st   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 dlmZ d dl	Z	d dl
T d dlmZ ej ZG dd dZdS )    N)*)TunedExceptionc                   @   s  e Zd Zd[ddZdd Zdd Zdd	 Zd
d Zdd Zd\ddZ	d]ddZ
dd Zdd Zdd Zd^ddZd_ddZd`d d!Zdad#d$Zdbd%d&Zdbd'd(Zdbd)d*Zdbd+d,Zd-d. Zd/d0 Zd[d1d2Zd3d4 Zd5d6 Zd[d7d8Zddi g dfd9d:Zd[d;d<Zd=d> Zd?d@ ZdAdB Z dCdD Z!dEdF Z"dGdH Z#dIdJ Z$dKdL Z%dMdN Z&dOdP Z'dQdR Z(dSdT Z)dUdV Z*dWdX Z+dYdZ Z,dS )ccommandsTc                 C   s
   || _ d S N)_logging)selflogging r	   6/usr/lib/python3/dist-packages/tuned/utils/commands.py__init__   s   
zcommands.__init__c                 C      | j r
t| d S d S r   )r   logerrorr   msgr	   r	   r
   _error      zcommands._errorc                 C   r   r   )r   r   debugr   r	   r	   r
   _debug   r   zcommands._debugc              	   C   s.   t |  }ddddddddd||S )N10)YYESTTRUENNOFFALSE)strupperstripget)r   valuevr	   r	   r
   get_bool   s   zcommands.get_boolc                 C   s   t ddt| S )Nz\s+ )resubr   r!   )r   sr	   r	   r
   	remove_ws   s   zcommands.remove_wsc                 C   s   t dd|S )Nz^"(.*)"$z\1)r'   r(   )r   r$   r	   r	   r
   unquote"   s   zcommands.unquote\c                 C   s   | |d||f S )Nz%s%sreplace)r   r)   what_escape	escape_byr	   r	   r
   escape&      zcommands.escapec                 C   s   | |dS )N r-   )r   r)   escape_charr	   r	   r
   unescape*   s   zcommands.unescapec                 C   s   |d|t |   | S )Nr&   )len)r   s1poss2r	   r	   r
   	align_str.   s   zcommands.align_strc                 C   s.   g }|d urt | D ]}|t|7 }q|S r   )sorteditemslist)r   dlir	   r	   r
   	dict2list4   s
   zcommands.dict2listc                 C   s(   |d u rd S t ddt|  S )Nz(%s)z)|()r'   compilejoinr=   keys)r   r>   r	   r	   r
   re_lookup_compile<   s   zcommands.re_lookup_compileNr   c                    sV    d u r|d u r
|S nt  dks|d u r|S |d u r |  }| fdd||S )Nr   c                    s   t   | jd  S )N   )r=   values	lastindex)mor>   r	   r
   <lambda>M   s    z.commands.multiple_re_replace.<locals>.<lambda>)r6   rE   r(   )r   r>   r)   rflagsr	   rJ   r
   multiple_re_replaceD   s   
zcommands.multiple_re_replacec                 C   sR   t |dks
|d u rd S |d u r| |}||}|r't| |jd  S d S Nr   rF   )r6   rE   searchr=   rG   rH   )r   r>   r)   rL   rI   r	   r	   r
   	re_lookupS   s   

zcommands.re_lookupFc           	   
   C   s   |  d||f  |rtj|}tj|rd}z|r!t| t|d}|t| |	  d}W |S  t
tfy^ } zd}|sS| d||f  W Y d }~|S W Y d }~|S d }~ww )NzWriting to file: '%s' < '%s'FwTz Writing to file '%s' error: '%s')r   ospathdirnameisdirmakedirsopenwriter   closeOSErrorIOErrorr   )	r   fdatamakedirno_errorr>   fdrcer	   r	   r
   write_to_file]   s,   


zcommands.write_to_filer3   c              
   C   sx   |}zt |d}| }|  W n ttfy0 } z|s&| d||f  W Y d }~nd }~ww | d||f  |S )NrL   z"Error when reading file '%s': '%s'z Read data from file: '%s' > '%s')rX   readrZ   r[   r\   r   r   )r   r]   err_retr`   	old_valuerc   r	   r	   r
   	read_filep   s   
zcommands.read_filec              
   C   sr   |  d|  tj|r7z	t|| W dS  ty6 } z|s+td|t	|f  W Y d }~dS d }~ww dS )NzRemoving tree: '%s'zcannot remove tree '%s': '%s'FT)
r   rS   rT   existsshutilrmtreer[   r   r   r   r   r]   r`   r   r	   r	   r
   rk   |   s   zcommands.rmtreec              
   C   sp   |  d|  tj|r6zt| W dS  ty5 } z|s*td|t|f  W Y d }~dS d }~ww dS )NzRemoving file: '%s'zcannot remove file '%s': '%s'FT)	r   rS   rT   ri   unlinkr[   r   r   r   rl   r	   r	   r
   rm      s   zcommands.unlinkc              
   C   sh   |  d||f  z	t|| W dS  ty3 } z|s(td||t|f  W Y d }~dS d }~ww )NzRenaming file '%s' to '%s'z%cannot rename file '%s' to '%s': '%s'FT)r   rS   renamer[   r   r   r   )r   srcdstr`   r   r	   r	   r
   rn      s   zcommands.renamec              
   C   sd   zt d||f  t|| W dS  ty1 } z|s&t d|||f  W Y d }~dS d }~ww )Nzcopying file '%s' to '%s'Tz!cannot copy file '%s' to '%s': %sF)r   r   rj   copyr\   r   )r   ro   rp   r`   rc   r	   r	   r
   rq      s   zcommands.copyc              	   C   s6   |  |}t|dkrdS | |tj|||tjdS Nr   FrM   )rh   r6   rd   r'   r(   	MULTILINE)r   r]   patternreplr^   r	   r	   r
   replace_in_file   s   
zcommands.replace_in_filec                 C   s4   |  |}t|dkrdS | || j||tjdS rr   )rh   r6   rd   rN   r'   rt   )r   r]   r>   r^   r	   r	   r
   multiple_replace_in_file   s   
z!commands.multiple_replace_in_filec                 C   s   |  |}|D ]H}t|}t|| }tjd| d |tjdd u r=|r<t|dkr4|d dkr4|d7 }|d||f 7 }qtjd| d	 d
| d |tjd}q| ||S )Nz\bz\s*=.*$rs   r   
z%s="%s"
z\b(z\s*=).*$z\1"")rh   r   r'   rP   rt   r6   r(   rd   )r   r]   r>   addr^   optor$   r	   r	   r
   add_modify_option_in_file   s   
&z"commands.add_modify_option_in_filec                 C   "   |  |}tt|d S Nzutf-8)rh   hashlibmd5r   encode	hexdigestr   r]   r^   r	   r	   r
   md5sum      
zcommands.md5sumc                 C   r   r   )rh   r   sha256r   r   r   r   r	   r	   r
   	sha256sum   r   zcommands.sha256sumc                 C   s   |  tj| S r   )rh   constsMACHINE_ID_FILEr!   )r   r`   r	   r	   r
   get_machine_id   s   zcommands.get_machine_idc              
   C   sF  d}t j }d|d< || | dt|  d}	d }
zBt|tt|||ddd}| \}	}|j	}|r^||vr^d|vr^|d d }t
|dkrO|	d d }d	|d |f }
|s^| |
 W n8 ttfy } z*|jd urr|j nd}t||vrd|vrd	|d |f }
|s| |
 W Y d }~nd }~ww |r||	|
fS ||	fS )
Nr   CLC_ALLzExecuting %s.r3   T)stdoutstderrenvshellcwd	close_fdsuniversal_newlinesry   zExecuting %s error: %s)rS   environrq   updater   r   PopenPIPEcommunicate
returncoder6   r   r[   r\   errnoabs)r   argsr   r   r   	no_errors
return_errretcode_environmentouterr_msgprocerrerr_outrc   r	   r	   r
   execute   sF   




zcommands.executec                 C   s.   t d|}|r|dS |r| d S |S )Nz.*\[([^\]]+)\].*rF   r   )r'   matchgroupsplit)r   optionsdosplitmr	   r	   r
   get_active_option   s   
zcommands.get_active_optionc                 C   s*   t |}|dkp| jd| dd dkS )Nr   z$/sys/devices/system/cpu/cpu%s/onlineT)r`   r   )r   rh   r!   )r   cpuscpur	   r	   r
   is_cpu_online  s   "zcommands.is_cpu_onlinec                 C   s^   |d u rd S t |dd}zt|d}W n ty)   tdt |  g  Y S w | |S )N,r3      zinvalid hexadecimal mask '%s')r   r.   int
ValueErrorr   r   bitmask2cpulist)r   maskr   r	   r	   r
   hex2cpulist  s   
zcommands.hex2cpulistc                 C   s>   d}g }|dkr|d@ r| | |dL }|d7 }|dks|S rO   )append)r   r   r   cpusr	   r	   r
   r     s   
zcommands.bitmask2cpulistc              	   C   s2  g }|d u r|S t |tu r|}nt|d}g }g }d}d}|D ]}t|}	|r?t|	dkr:d}|| d}q"||	7 }q"|	dd  dkrNd}|	}q"|	r|	d dks\|	d d	kr|	d
d  d}
z't|
d
kr|ttt|
d t|
d
 d
 7 }n|t|	d
d   W q" t	y   g  Y   S w t|	dkr||	 q"t|dkr|| |D ]K}|d}|dd  dkr|| 
|7 }qz%t|d
kr|ttt|d t|d
 d
 7 }n	|t|d  W q t	y   g  Y   S w ttt|}|D ]}||v r|| q	|S )Nr   Fr3   r      0xT^!rF   -)typer=   r   r   r6   r   lowerranger   r   r   r;   setremove)r   r?   rlllll2negation_listhexmaskhvr$   svnlvlcpu_listr   r	   r	   r
   cpulist_unpack*  sr   






(

zcommands.cpulist_unpackc                 C   s   |  |}|d u st|dkr|S d}|}g }|d t|k rZ||d  ||  dkrN||krA|t|| d t||   n	|t||  |d }|d7 }|d t|k s|d t|k ru|t|| d t|d   |S |t|d  |S )Nr   rF   r   ry   )r   r6   r   r   )r   r?   r@   jr   r	   r	   r
   cpulist_packm  s&   
$"zcommands.cpulist_packc                 C   s.   |  |}|  | d}tt|t| S )Nz/sys/devices/system/cpu/online)r   rh   r=   r   )r   r?   r   onliner	   r	   r
   cpulist_invert  s   
zcommands.cpulist_invertc                    s   |d u rd S |  |}|d u rd S | |}d|  t }|d dkr,|d|d  7 } | d fddtdt dD S )Nz%x   r   r   c                 3   s     | ]} ||d   V  qdS )r   Nr	   ).0r@   r)   r	   r
   	<genexpr>  s    z'commands.cpulist2hex.<locals>.<genexpr>)r   cpulist2bitmaskr6   zfillrC   r   )r   r?   ulr   lsr	   r   r
   cpulist2hex  s   


$zcommands.cpulist2hexc                 C   s    d}|D ]	}|t d|O }q|S )Nr   r   )pow)r   r?   r   r$   r	   r	   r
   r     s   zcommands.cpulist2bitmaskc                 C   s   d dd |D S )Nr   c                 s   s    | ]}t |V  qd S r   )r   )r   r$   r	   r	   r
   r     s    z*commands.cpulist2string.<locals>.<genexpr>)rC   )r   r?   r	   r	   r
   cpulist2string  r2   zcommands.cpulist2stringc                 C   s$   z| |dW S    | | Y S )NF)wait)r   	terminatetimer	   r	   r
   r     s   zcommands.waitc              	   C   s   t |  }dD ]K}||}|dkrq
z4t|d | }||d  }|dkr.|d9 }n|dkr7|d9 }n|dkr@|d9 }n|d	krFd }|W   S  tyU   Y  d S w d S )
N)KBMBGBr3   ry   r   i   r   i   r   i   @r3   )r   r!   r    rfindr   r   )r   r)   unitunit_ixvalur	   r	   r
   get_size  s*   




zcommands.get_sizec              
   C   s  d}d}zt tjd}|  }W d    n1 sw   Y  W n1 ty@ } z|jtjkr6td| W Y d }~nd }~w t	t
fyS } ztd| d }~ww z.t tjd}|  }|dtjtjfvrrtdtj W d    n1 s|w   Y  W n1 ty } z|jtjkrtd| W Y d }~nd }~w t	t
fy } ztd| d }~ww |dkrd }n|tjk}|dkrd }||fS )Nr3   rL   z!Failed to read active profile: %szInvalid value in file %s.zFailed to read profile mode: %s)rX   r   ACTIVE_PROFILE_FILEre   r!   r\   r   ENOENTr   r[   EOFErrorPROFILE_MODE_FILEACTIVE_PROFILE_AUTOACTIVE_PROFILE_MANUAL)r   profile_namemoder]   rc   manualr	   r	   r
   get_active_profile  sN   
zcommands.get_active_profilec              
   C   s   z#t tjd}|d ur||d  W d    n1 sw   Y  W n ttfy8 } ztd|j d }~ww z*t tjd}|rFtj	ntj
}||d  W d    W d S 1 s\w   Y  W d S  ttfyx } ztd|j d }~ww )NrR   rz   z!Failed to save active profile: %szFailed to save profile mode: %s)rX   r   r   rY   r[   r\   r   strerrorr   r   r   )r   r   r   r]   rc   r   r	   r	   r
   save_active_profile  s(   &zcommands.save_active_profilec              
   C   s   d}zt tjd}|  }W d    n1 sw   Y  W n1 ty> } z|jtjkr4td| W Y d }~nd }~w t	t
fyQ } ztd| d }~ww |dkrXd }|S )Nr3   rL   z1Failed to read the active post-loaded profile: %s)rX   r   POST_LOADED_PROFILE_FILEre   r!   r\   r   r   r   r[   r   r   r   r]   rc   r	   r	   r
   get_post_loaded_profile  s$   z commands.get_post_loaded_profilec              
   C   s   z/t tjd}|d ur||d  W d    W d S W d    W d S 1 s(w   Y  W d S  ttfyD } ztd|j d }~ww )NrR   rz   z1Failed to save the active post-loaded profile: %s)rX   r   r   rY   r[   r\   r   r   r   r	   r	   r
   save_post_loaded_profile  s   &z!commands.save_post_loaded_profile)T)r,   r,   )r,   )Nr   r   )FF)r3   F)F)-__name__
__module____qualname__r   r   r   r%   r*   r+   r1   r5   r:   rA   rE   rN   rQ   rd   rh   rk   rm   rn   rq   rw   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	   r	   r	   r
   r      sV    

















'	Cr   )r   r   
tuned.logstunedrq   rS   rj   tuned.constsr   r'   
subprocesstuned.exceptionsr   logsr"   r   r   r	   r	   r	   r
   <module>   s    
