3 Yd3=@srddlZddlZddlZddlZddlZddlZddljZddl Z ddl Tddl m Z ej jZGdddZdS)N)*)TunedExceptionc@seZdZd^ddZddZddZdd Zd d Zd d Zd_ddZ d`ddZ ddZ ddZ ddZ daddZdbddZdcd d!Zddd#d$Zded%d&Zdfd'd(Zdgd)d*Zdhd+d,Zd-d.Zd/d0Zdid1d2Zd3d4Zd5d6Zdjd7d8Zddigdfd9d:Zdkd;d<Zd=d>Zd?d@ZdAdBZ dldDdEZ!dFdGZ"dHdIZ#dJdKZ$dLdMZ%dmdNdOZ&dPdQZ'dRdSZ(dTdUZ)dVdWZ*dXdYZ+dZd[Z,d\d]Z-dS)ncommandsTcCs ||_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}|s|j d||fWYdd}~XnX|S)NzWriting to file: '%s' < '%s'FwTz Writing to file '%s' error: '%s') rospathdirnameisdirmakedirsopenwritercloseOSErrorIOErrorr ) 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)rrMZerr_retrOZ old_valuerQrrr read_fileps  $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)rrMrOr rrrrW|s zcommands.rmtreecCsh|jd|tjj|rdytj|Wn:tk rb}z|sTtjd|t|fdSd}~XnXdS)NzRemoving file: '%s'zcannot remove file '%s': '%s'FT) rrCrDrUunlinkrKr r r)rrMrOr rrrrXs 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)rsrcdstrOr rrrrYszcommands.renamecCs`y"tjd||ftj||dStk rZ}z|sLtjd|||fdSd}~XnXdS)Nzcopying file '%s' to '%s'Tz!cannot copy file '%s' to '%s': %sF)r rrVcopyrLr )rrZr[rOrQrrrr\s z commands.copycCs6|j|}t|dkrdS|j|tj|||tjdS)NrF)r>)rTr(rRrr MULTILINE)rrMpatternreplrNrrrreplace_in_files  zcommands.replace_in_filecCs4|j|}t|dkrdS|j||j||tjdS)NrF)r>)rTr(rRr?rr])rrMr0rNrrrmultiple_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") rTrrr@r]r(rr%rR)rrMr0addrNoptorrrradd_modify_option_in_files   .z"commands.add_modify_option_in_filecCs"|j|}tjt|jdjS)Nzutf-8)rThashlibZmd5rencode hexdigest)rrMrNrrrmd5sums zcommands.md5sumcCs"|j|}tjt|jdjS)Nzutf-8)rTriZsha256rrjrk)rrMrNrrr sha256sums zcommands.sha256sumcCs|jtj|jS)N)rTconstsZMACHINE_ID_FILEr)rrOrrrget_machine_idszcommands.get_machine_idcCsRd}tjj}d|d<|j||jdt|d} d} yt|tt|||ddd} | j\} } | j }|r||krd|kr| dd } t | dkr| dd } d |d| f} |s|j | Wnvt t fk r4}zT|jdk r|j nd }t||kr$d|kr$d |d|f} |s$|j | WYdd}~XnX|rF|| | fS|| fSdS) NrCLC_ALLz Executing %s.r&T)stdoutstderrenvshellcwdZ close_fdsZuniversal_newlinesr8zExecuting %s error: %srdrdrd)rCenvironr\updaterrPopenPIPEZ communicate returncoder(r rKrLerrnoabs)rargsrurvrtZ no_errorsZ return_errZretcodeZ _environmentoutZerr_msgprocerrZerr_outrQrrrexecutes>       zcommands.executecCs.tjd|}|r|jdS|r*|jdS|S)Nz.*\[([^\]]+)\].*r8r)rmatchgroupsplit)rZoptionsZdosplitmrrrget_active_options    zcommands.get_active_optioncCs*t|}|dkp(|jd|ddjdkS)Nrz$/sys/devices/system/cpu/cpu%s/onlineT)rOr)rrTr)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)rrrcpusrrrrs   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