3 Zd?d@ZdAdBZ dndDdEZ!dFdGZ"dHdIZ#dJdKZ$dLdMZ%dodNdOZ&dPdQZ'dRdSZ(dTdUZ)dVdWZ*dXdYZ+dZd[Z,d\d]Z-d^d_Z.dS)pcommandsTcCs ||_dS)N)_logging)selfZloggingr/usr/lib/python3.6/commands.py__init__szcommands.__init__cCs|jrtj|dS)N)rlogerror)rmsgrrr_errorszcommands._errorcCs|jrtj|dS)N)rr debug)rr rrr_debugszcommands._debugc Cs.t|jj}dddddddddj||S)N10)YZYESTZTRUENZNOFZFALSE)strupperstripget)rvaluevrrrget_boolszcommands.get_boolcCstjddt|jS)Nz\s+ )resubrr)rsrrr remove_wsszcommands.remove_wscCstjdd|S)Nz^"(.*)"$z\1)rr)rrrrrunquote"szcommands.unquote\cCs|j|d||fS)Nz%s%s)replace)rr Z what_escapeZ escape_byrrrescape&szcommands.escapecCs |j|dS)N)r$)rr Z escape_charrrrunescape*szcommands.unescapecCs|d|t||S)Nr)len)rs1poss2rrr align_str.szcommands.align_strcCs2g}|dk r.x t|jD]}|t|7}qW|S)N)sorteditemslist)rdlirrr dict2list4s zcommands.dict2listcCs(|dkr dStjddjt|jS)Nz(%s)z)|()rcompilejoinr/keys)rr0rrrre_lookup_compile<szcommands.re_lookup_compileNrcsVdkr|dkr.|Sntdks*|dkr.|S|dkr@|j}|jfdd||S)Nrcstj|jdS)N)r/values lastindex)mo)r0rrMsz.commands.multiple_re_replace..)r(r7r)rr0r rflagsr)r0rmultiple_re_replaceDs zcommands.multiple_re_replacecCsRt|dks|dkrdS|dkr*|j|}|j|}|rNt|j|jdSdS)Nrr8)r(r7searchr/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 t fk r}z@d}t |t r| st |tr|j|kr|jd||fWYdd}~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'FwTz Writing to file '%s' error: '%s'N)rospathdirnameisdirmakedirsopenwritercloseOSErrorIOError isinstanceboolr/errnor ) rfdataZmakedirno_errorr0fdZrcerrr write_to_file]s$     $zcommands.write_to_filer&cCsv|}yt|d}|j}|jWn<ttfk r^}z|sN|jd||fWYdd}~XnX|jd||f|S)Nr=z"Error when reading file '%s': '%s'z Read data from file: '%s' > '%s')rHreadrJrKrLr r)rrPZerr_retrRZ old_valuerTrrr read_file|s  $zcommands.read_filecCsj|jd|tjj|rfytj||Wn:tk rd}z|sVtjd|t |fdSd}~XnXdS)NzRemoving tree: '%s'zcannot remove tree '%s': '%s'FT) rrCrDexistsshutilrmtreerKr r r)rrPrRr rrrrZs zcommands.rmtreecCsh|jd|tjj|rdytj|Wn:tk rb}z|sTtjd|t|fdSd}~XnXdS)NzRemoving file: '%s'zcannot remove file '%s': '%s'FT) rrCrDrXunlinkrKr r r)rrPrRr rrrr[s zcommands.unlinkcCsd|jd||fytj||Wn<tk r^}z |sPtjd||t|fdSd}~XnXdS)NzRenaming file '%s' to '%s'z%cannot rename file '%s' to '%s': '%s'FT)rrCrenamerKr r r)rsrcdstrRr rrrr\szcommands.renamecCs`y"tjd||ftj||dStk rZ}z|sLtjd|||fdSd}~XnXdS)Nzcopying file '%s' to '%s'Tz!cannot copy file '%s' to '%s': %sF)r rrYcopyrLr )rr]r^rRrTrrrr_s z commands.copycCs6|j|}t|dkrdS|j|tj|||tjdS)NrF)r>)rWr(rUrr MULTILINE)rrPpatternreplrQrrrreplace_in_files  zcommands.replace_in_filecCs4|j|}t|dkrdS|j||j||tjdS)NrF)r>)rWr(rUr?rr`)rrPr0rQrrrmultiple_replace_in_files  z!commands.multiple_replace_in_filecCs|j|}x|D]}t|}t||}tjd|d|tjddkr||rt|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)rrPr0addrQoptorrrradd_modify_option_in_files   .z"commands.add_modify_option_in_filecCs"|j|}tjt|jdjS)Nzutf-8)rWhashlibZmd5rencode hexdigest)rrPrQrrrmd5sums zcommands.md5sumcCs"|j|}tjt|jdjS)Nzutf-8)rWrlZsha256rrmrn)rrPrQrrr sha256sums zcommands.sha256sumcCs|jtj|jS)N)rWconstsZMACHINE_ID_FILEr)rrRrrrget_machine_idszcommands.get_machine_idcCsVd}tjj}d|d<|j||jdt|d} d} yt|tt|||ddd} | j\} } | j }|r||krd|kr| dd } t | dkr| dd } d d j || f} |s|j | Wnxt tfk r8}zV|jdk r|j nd }t||kr(d|kr(d d j ||f} |s(|j | WYdd}~XnX|rJ|| | fS|| fSdS)NrCLC_ALLz Executing %s.r&T)stdoutstderrenvshellcwdZ close_fdsZuniversal_newlinesr8zExecuting '%s' error: %srrgrgrg)rCenvironr_updaterrPopenPIPEZ communicate returncoder(r5r rKrLrOabs)rargsrxryrwZ no_errorsZ return_errZretcodeZ _environmentoutZerr_msgprocerrZerr_outrTrrrexecutes>       zcommands.executecCs.tjd|}|r|jdS|r*|jdS|S)Nz.*\[([^\]]+)\].*r8r)rmatchgroupsplit)rZoptionsZdosplitmrrrget_active_option s    zcommands.get_active_optioncCs*t|}|dkp(|jd|ddjdkS)Nrz$/sys/devices/system/cpu/cpu%s/onlineT)rRr)rrWr)rcpuZscpurrr is_cpu_onlineszcommands.is_cpu_onlinec Cs\|dkr dSt|jdd}yt|d}Wn&tk rPtjdt|gSX|j|S)N,r&zinvalid hexadecimal mask '%s')rr$int ValueErrorr r bitmask2cpulist)rmaskrrrr hex2cpulistszcommands.hex2cpulistcCs<d}g}x.|dkr6|d@r$|j||dL}|d7}q W|S)Nrr8)append)rrrcpusrrrr&s   zcommands.bitmask2cpulist'"cCs^g}|dkr|S|}t|tk rD|dk r6t|j|}t|jd}g}g}d}d}x|D]} t| } |rt| dkrd}|j|d}n|| 7}q\| ddjdkrd}| }q\| o| ddks| dd krP| d djd } yPt| d kr|ttt | dt | d d 7}n|jt | d dWnt k rLgSXq\t| dkr\|j| q\Wt|dkr|j|x|D]} | jd } | ddjdkr||j | 7}ndyLt| d kr|ttt | dt | d d 7}n|jt | dWnt k rgSXqWt tt |} x"|D]}|| kr<| j|qsz'commands.cpulist2hex..)rcpulist2bitmaskr(zfillr5r)rr1ZulrZlsr)r r cpulist2hexs    zcommands.cpulist2hexcCs$d}x|D]}|td|O}q W|S)Nrr)pow)rr1rrrrrrs zcommands.cpulist2bitmaskcsdjfdd|DS)Nrc3s|]}t|VqdS)N)r)rr)prefixrrrsz*commands.cpulist2string..)r5)rr1rr)rrcpulist2stringszcommands.cpulist2stringcCsb|ddjdkrH|dd}|ddjdkrHdd|j|DStjd|}dd|DS)Nrrzcpulist:cSsg|]}dt|qS)r)r)rrrrr sz(commands.devstr2devs..z\s*(?s