a h`C@stddlZddlZddlZddlZddlZddlZddlmZddl Z ddl Tddl m Z ej ZGdddZdS)N)*)TunedExceptionc@seZdZdfddZddZddZdd Zd d Zd d ZdgddZ dhddZ ddZ ddZ ddZ diddZdjddZdkd d!Zdld#d$Zdmd%d&Zdnd'd(Zdod)d*Zdpd+d,Zd-d.Zd/d0Zdqd1d2Zd3d4Zd5d6Zdrd7d8Zddigdfd9d:Zdsd;d<Zd=d>Zd?d@ZdAdBZ dtdDdEZ!dFdGZ"dHdIZ#dJdKZ$dLdMZ%dudNdOZ&dPdQZ'dRdSZ(dTdUZ)dVdWZ*dXdYZ+dZd[Z,d\d]Z-d^d_Z.d`daZ/dbdcZ0dddeZ1dS)vcommandsTcCs ||_dSN)_logging)selfZloggingr8/usr/lib/python3.9/site-packages/tuned/utils/commands.py__init__szcommands.__init__cCs|jrt|dSr)rlogerrorrmsgrrr _errorszcommands._errorcCs|jrt|dSr)rr debugr rrr _debugszcommands._debugc Cs.t|}ddddddddd||S)N10)YZYESTZTRUENZNOFZFALSE)strupperstripget)rvaluevrrr get_boolszcommands.get_boolcCstddt|S)Nz\s+ )resubrr)rsrrr remove_wsszcommands.remove_wscCstdd|S)Nz^"(.*)"$z\1)r r!)rrrrr unquote"szcommands.unquote\cCs||d||fS)Nz%s%sreplace)rr"Z what_escapeZ escape_byrrr escape&szcommands.escapecCs ||dS)Nr&)rr"Z escape_charrrr unescape*szcommands.unescapecCs|d|t||S)Nr)len)rs1poss2rrr align_str.szcommands.align_strcCs.g}|dur*t|D]}|t|7}q|Sr)sorteditemslist)rdlirrr dict2list4s zcommands.dict2listcCs(|dur dStddt|S)Nz(%s)z)|()r compilejoinr2keys)rr3rrr re_lookup_compile<szcommands.re_lookup_compileNrcsVdur|dur.|Sntdks*|dur.|S|dur@|}|fdd||S)Nrcst|jdS)N)r2values lastindex)mor3rr Mz.commands.multiple_re_replace..)r+r:r!)rr3r"rflagsrr?r multiple_re_replaceDs zcommands.multiple_re_replacecCsRt|dks|durdS|dur*||}||}|rNt||jdSdSNrr;)r+r:searchr2r<r=)rr3r"rBr>rrr re_lookupSs  zcommands.re_lookupFc Cs|d||f|r2tj|}tj|r2d}zh|rBt||rt|j|ddt|krt|d|WdSt |d}| t|| d}Wn`t t fy} zDd}t|tr|rt|tr| j|vr|d|| fWYd} ~ n d} ~ 00|S) ahWrite 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 ignore_same -- if True and the write would not change the file, it is skipped Return: bool -- True on success zWriting to file: '%s' < '%s'FT)no_errorz=Skipping the write to file '%s', the content would not changewz Writing to file '%s' error: '%s'N)rospathdirnameisdirmakedirs read_filerropenwritecloseOSErrorIOError isinstanceboolr2errnor) rfdataZmakedirrHZ ignore_samer3fdrcerrr write_to_file]s0     (zcommands.write_to_filer)c Csx|}zt|d}|}|Wn>ttfy`}z"|sL|d||fWYd}~n d}~00|d||f|S)NrBz"Error when reading file '%s': '%s'z Read data from file: '%s' > '%s')rPreadrRrSrTrr)rrXerr_retrH old_valuerZr\rrr rOs  (zcommands.read_filec Csp|d|tj|rlzt||Wn@tyj}z(|sTtd|t |fWYd}~dSd}~00dS)NzRemoving tree: '%s'zcannot remove tree '%s': '%s'FT) rrJrKexistsshutilrmtreerSr r rrrXrHr rrr rcs zcommands.rmtreec Csn|d|tj|rjzt|Wn@tyh}z(|sRtd|t|fWYd}~dSd}~00dS)NzRemoving file: '%s'zcannot remove file '%s': '%s'FT) rrJrKraunlinkrSr r rrdrrr res zcommands.unlinkc Csj|d||fzt||WnBtyd}z*|sNtd||t|fWYd}~dSd}~00dS)NzRenaming file '%s' to '%s'z%cannot rename file '%s' to '%s': '%s'FT)rrJrenamerSr r r)rsrcdstrHr rrr rfszcommands.renamec Cshz$td||ft||WdStyb}z&|sLtd|||fWYd}~dSd}~00dS)Nzcopying file '%s' to '%s'Tz!cannot copy file '%s' to '%s': %sF)r rrbcopyrTr )rrgrhrHr\rrr ris z commands.copyc Cs6||}t|dkrdS||tj|||tjdSNrFrC)rOr+r]r r! MULTILINE)rrXpatternreplrYrrr replace_in_files  zcommands.replace_in_filecCs4||}t|dkrdS|||j||tjdSrj)rOr+r]rDr rl)rrXr3rYrrr multiple_replace_in_files  z!commands.multiple_replace_in_filecCs||}|D]}t|}t||}tjd|d|tjddurz|rt|dkrh|ddkrh|d7}|d||f7}qtjd|d d ||d |tjd}q|||S) Nz\bz\s*=.*$rkr z%s="%s" z\b(z\s*=).*$z\1"") rOrr rFrlr+r!r(r])rrXr3addrYoptorrrr add_modify_option_in_files  ,z"commands.add_modify_option_in_filecCs"||}tt|dSNzutf-8)rOhashlibZmd5rencode hexdigestrrXrYrrr md5sums zcommands.md5sumcCs"||}tt|dSrx)rOryZsha256rrzr{r|rrr sha256sums zcommands.sha256sumcCs|tj|Sr)rOconstsZMACHINE_ID_FILEr)rrHrrr get_machine_idszcommands.get_machine_idc CsXd}tj}d|d<|||dt|d} d} zt|tt|||ddd} | \} } | j }|r||vrd|vr| dd} t | dkr| dd} d d || f} |s| | Wnzt tfy:}z\|jdur|j nd}t||vr&d|vr&d d ||f} |s&| | WYd}~n d}~00|rL|| | fS|| fSdS) NrCLC_ALLz Executing %s.r)T)stdoutstderrenvshellcwdZ close_fdsZuniversal_newlinesrqzExecuting '%s' error: %sr)rJenvironriupdaterrPopenPIPEZ communicate returncoder+r8rrSrTrWabs)rargsrrrZ no_errorsZ return_errZretcodeZ _environmentouterr_msgprocerrZerr_outr\rrr executes@        zcommands.executecCs.td|}|r|dS|r*|dS|S)Nz.*\[([^\]]+)\].*r;r)r matchgroupsplit)roptionsZdosplitmrrr get_active_options    zcommands.get_active_optioncCs,t|}|dkp*|jd|ddddkS)Nrz$/sys/devices/system/cpu/cpu%s/onlinerT)r_rH)rrOr)rcpuZscpurrr is_cpu_onlineszcommands.is_cpu_onlinecCs^|dur dSt|dd}zt|d}Wn(tyRtdt|gYS0||S)N,r)zinvalid hexadecimal mask '%s')rr'int ValueErrorr r bitmask2cpulist)rmaskrrrr hex2cpulists  zcommands.hex2cpulistcCs8d}g}|dkr4|d@r"|||dL}|d7}q|SrE)append)rrrcpusrrr r*s  zcommands.bitmask2cpulist'"c Cs^g}|dur|S|}t|turD|dur6t||}t|d}g}g}d}d}|D]} t| } |rt| dkrd}||d}n|| 7}qX| dddkrd}| }qX| rT| ddks| dd krT| d dd } zPt| d kr|ttt | dt | d d 7}n|t | d dWnt yPgYS0qXt| dkrX|| qXt|dkr|||D]} | d } | dddkr|| | 7}njzLt| d kr|ttt | dt | d d 7}n|t | dWnt y$gYS0qt tt |} |D]}|| vr>| |q>| S) NrFr)r0xT^!r;-)typer2rrrr+rlowerrangerrrr0setremove)rr4Z strip_charsrlZllZll2Z negation_listZhexmaskZhvrsvnlZvlZcpu_listrrrr cpulist_unpack=sj            ( zcommands.cpulist_unpackcCs||}|dust|dkr"|Sd}|}g}|dt|kr||d||dkr||kr|t||dt||n|t|||d}|d7}q.|dt|kr|t||dt|dn|t|d|S)Nrr;rrq)rr+rr)rr4r5jrrrr cpulist_packs" $ $zcommands.cpulist_packcCs.||}||d}tt|t|S)Nz/sys/devices/system/cpu/online)rrOr2r)rr4rZonlinerrr cpulist_inverts zcommands.cpulist_invertcs|dur dS||}|dur"dS||}d|t}|ddkrX|d|d7}|dfddtdtdDS)Nz%xrrc3s|]}||dVqdS)rNr).0r5r"rr rAz'commands.cpulist2hex..)rcpulist2bitmaskr+zfillr8r)rr4ZulrZlsrrr cpulist2hexs    zcommands.cpulist2hexcCs d}|D]}|td|O}q|S)Nrr)pow)rr4rrrrr rszcommands.cpulist2bitmaskcsdfdd|DS)Nrc3s|]}t|VqdSrrrrprefixrr rrAz*commands.cpulist2string..)r8)rr4rrrr cpulist2stringszcommands.cpulist2stringcCsb|dddkrH|dd}|dddkrHdd||DStd|}dd|DS)Nrrzcpulist:cSsg|]}dt|qS)rrrrrr rAz(commands.devstr2devs..z\s*(?td|WYd}~n>d}~0t t fy}ztd|WYd}~n d}~00|dkrd}n |tj k}|dkrd}||fS)Nr)rBz!Failed to read active profile: %szInvalid value in file %s.zFailed to read profile mode: %s)rPrACTIVE_PROFILE_FILEr^rrTrWENOENTrrSEOFErrorPROFILE_MODE_FILEACTIVE_PROFILE_AUTOACTIVE_PROFILE_MANUAL)r profile_namemoderXr\manualrrr get_active_profiles4.  " 2 "   zcommands.get_active_profilec Cs zFttjd&}|dur&||dWdn1s:0YWn6ttfy|}ztd|jWYd}~n d}~00zNttjd.}|rtj ntj }||dWdn1s0YWn8ttfy}ztd|jWYd}~n d}~00dS)NrIrrz!Failed to save active profile: %szFailed to save profile mode: %s) rPrrrQrSrTrstrerrorrrr)rrrrXr\rrrr save_active_profiles0$0zcommands.save_active_profilec Csd}z.)allr+rZNAMES_MAX_LENGTH)rnamerrr is_valid_name)szcommands.is_valid_namecCstd|gS)Ngetconf)Z check_outputdecoder)rvariablerrr r,szcommands.getconfcCs |tj}|r||SdgSr)rOrZSYSFS_CPUS_PRESENT_PATHr)rrrrr get_cpus0s zcommands.get_cpus)T)r%r%)r%)Nr)N)FFF)r)F)F)F)F)F)T)T)T)r)r))2__name__ __module__ __qualname__r rrrr#r$r(r*r/r6r:rDrGr]rOrcrerfrirorprwr}r~rrrrrrrrrrrrrrrrrrrrrrrrrrr rs^     #   '   D    r)rWryZ tuned.logsZtunedrirJrbZ tuned.constsrr subprocessZtuned.exceptionsrZlogsrr rrrrr s