uec @sddlmZddlZddlZddlZddlmZmZmZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlmZddlmZddlZdZddZdddddZdddZdZdZdedddeeddd ZddZd dddZdej fdYZ!de"fdYZ#de$fdYZ%dS( i(twith_statementN(tPopentPIPEtSTDOUT(tclosing(tdequecCsRt|d=}tj|j|kr>|j| dn|jSWdQXdS(s,Returns the last number_of_bytes of filenametrbiN(topentoststattst_sizetseektread(tfilenametnumber_of_bytestf((s1/usr/lib/python2.7/site-packages/sos/utilities.pyttailstrcCsut|tjrgyt||SWqqtk rctjd}|jd|ttj SXn t|SdS(s@Returns a file-like object that can be used as a context managertsossfileobj: %s could not be openedN( t isinstancetsixt string_typesRtIOErrortloggingt getLoggertdebugRtStringIO(t path_or_filetmodetlog((s1/usr/lib/python2.7/site-packages/sos/utilities.pytfileobj&s ii iii(cCsxt|}||kr$d||S||kr<d||S||krTd||S||krld||Sd|SdS(sCConverts a number of bytes to a shorter, more human friendly formats%.1fTs%.1fGs%.1fMs%.1fKs%dN(tfloat(tbytes_tKtMtGtTtfn((s1/usr/lib/python2.7/site-packages/sos/utilities.pyt convert_bytes3s         c cs|r4tjj|jtjj}||7}nxtj|D]\}}}|r{|jtjj|kr{|2n|rtj|| rqDnx.tj||D]}tjj||VqWqDWdS(sGenerator function to find files recursively. Usage:: for filename in find("*.properties", "/var/log/foobar"): print filename N( Rtpathtdirnametcounttseptwalktfnmatchtfiltertjoin( t file_patternttop_dirt max_deptht path_patternt base_depthR'tdirlisttfilelisttname((s1/usr/lib/python2.7/site-packages/sos/utilities.pytfindBs! !csMg}x@|D]8}t|$}|jfd|DWdQXq W|S(sReturns lines matched in fnames, where fnames can either be pathnames to files to grep through or open file objects to grep through line by linec3s'|]}tj|r|VqdS(N(tretmatch(t.0tline(tpattern(s1/usr/lib/python2.7/site-packages/sos/utilities.pys _sN(Rtextend(R<tfiles_or_pathstmatchestfoptfo((R<s1/usr/lib/python2.7/site-packages/sos/utilities.pytgrepXs  'cCsgtjjddjtjj}|gg|D]}tjj||^q1}td|DS(s6Returns if a command matches an executable on the PATHtPATHtcss$|]}tj|tjVqdS(N(RtaccesstX_OK(R:R'((s1/usr/lib/python2.7/site-packages/sos/utilities.pys is(RtenvirontgettsplitR'tpathsepR.tany(tcommandtpathstpt candidates((s1/usr/lib/python2.7/site-packages/sos/utilities.pyt is_executableds$/i,c sefd} tjj} d| d<|rtx@|jD]/\} } | r]| | | | j| dq>Wn|rtdrd|rdnd||f}ntjs|j dd }nt j |}g}x@|D]8}t j |}|r |j |q|j|qWyt|d td td |rBtntd dd| dtd| }t|j||}| rx9|jr| r|jtntjdqWn|j}x|jdkrqWWn=tk r}|jtjkridd6dd6S|nX|j dks>|j dkrPtj!d}ni|j d6|d6S(sExecute a command and return a dictionary of status and output, optionally changing root or current working directory before executing command. cs0rtjnr,tjndS(N(Rtchroottchdir((RRRQ(s1/usr/lib/python2.7/site-packages/sos/utilities.pyt_child_prep_fnvstCtLC_ALLttimeoutstimeout %s %ds %ss --foregroundRDsutf-8tignoretshelltstdouttstderrtbufsizeitenvt close_fdst preexec_fng{Gz?itstatustoutputi~N("RRGtcopytitemstpoptNoneRPRtPY3tencodetshlexRItglobR=tappendRtFalseRRtTruet AsyncReaderRYtrunningt terminatetSoSTimeoutErrorttimetsleept get_contentstpolltOSErrorterrnotENOENTt returncodet binary_type(RLRVRZRQRRR\t foregroundtbinaryt sizelimittpollerRStcmd_envtkeytvaluetargst expanded_argstargt expanded_argRNtreaderRYte((RRRQs1/usr/lib/python2.7/site-packages/sos/utilities.pytsos_get_command_outputls\              cCs|jdd}t|tt|g}gtj|tjD]!\}}|j|krG|^qG}|rg|D]}t||r{|^q{}n|S(sImports the module module_fqname and returns a list of defined classes from that module. If superclasses is defined then the classes returned will be subclasses of the specified superclass or superclasses. If superclasses is plural it must be a tuple of classes.t.i( t rpartitiont __import__tglobalstlocalstinspectt getmemberstisclasst __module__t issubclass(t module_fqnamet superclassest module_nametmoduletcnametclass_tmodulestm((s1/usr/lib/python2.7/site-packages/sos/utilities.pyt import_modules+cCs t|d|d|d|dS(seShell out to an external command and return the output or the empty string in case of error. RVRQRRR`(R(tcmdRVRQtrunat((s1/usr/lib/python2.7/site-packages/sos/utilities.pyt shell_outsRlcBs)eZdZdZdZdZRS(sUsed to limit command output to a given size without deadlocking sos. Takes a sizelimit value in MB, and will compile stdout from Popen into a string that is limited to the given sizelimit. cCstt|j||_||_d|_d}|rZ|d}t||j}ntd||_t |_ |j dS(Niitmaxlen( tsuperRlt__init__tchanRzt chunksizeRdtintRRkRmtstart(tselftchannelR{Rztslots((s1/usr/lib/python2.7/site-packages/sos/utilities.pyRs     cCsgy@x9tr>|jj|j}|s+Pn|jj|qWWnttfk rYnXt|_ dS(slReads from the channel (pipe) that is the output pipe for a called Popen. As we are reading from the pipe, the output is added to a deque. After the size of the deque exceeds the sizelimit earlier (older) entries are removed. This means the returned output is chunksize-sensitive, but is not really byte-sensitive. N( RkRR RRRit ValueErrorRRjRm(RR;((s1/usr/lib/python2.7/site-packages/sos/utilities.pytruns  cCs^x|jrtjdqW|js@djd|jDSdjd|jDSdS(s-Returns the contents of the deque as a stringg{Gz?RDcss!|]}|jddVqdS(sutf-8RWN(tdecode(R:tln((s1/usr/lib/python2.7/site-packages/sos/utilities.pys scss|] }|VqdS(N((R:R((s1/usr/lib/python2.7/site-packages/sos/utilities.pys sN(RmRpRqRzR.R(R((s1/usr/lib/python2.7/site-packages/sos/utilities.pyRrs   (t__name__Rt__doc__RRRr(((s1/usr/lib/python2.7/site-packages/sos/utilities.pyRls tImporterHelpercBs;eZdZdZdZdZdZdZRS(sProvides a list of modules that can be imported in a package. Importable modules are located along the module __path__ list and modules are files that end in .py. cCs ||_dS(sopackage is a package module import my.package.module helper = ImporterHelper(my.package.module)N(tpackage(RR((s1/usr/lib/python2.7/site-packages/sos/utilities.pyRscCs.tjj|}tjj|\}}|S(s-Returns the plugin module name given the path(RR'tbasenametsplitext(RR'tbaseR6text((s1/usr/lib/python2.7/site-packages/sos/utilities.pyt _plugin_namescCsKg|D]0}d|kr|jdr|j|^q}|j|S(NRs.py(tendswithRtsort(Rtlist_tplugintplugins((s1/usr/lib/python2.7/site-packages/sos/utilities.pyt_get_plugins_from_lists  0 cCsKtjj|rGttd|}|j|}|r@|SgSndS(Ns*.py(RR'texiststlistR7R(RR'tpy_filestpnames((s1/usr/lib/python2.7/site-packages/sos/utilities.pyt_find_plugins_in_dir!s cCsLg}x?|jjD]1}tjj|r|j|j|qqW|S(sQReturns the list of importable modules in the configured python package. (Rt__path__RR'tisdirR=R(RRR'((s1/usr/lib/python2.7/site-packages/sos/utilities.pyt get_modules*s (RRRRRRRR(((s1/usr/lib/python2.7/site-packages/sos/utilities.pyRs     RocBseZRS((RR(((s1/usr/lib/python2.7/site-packages/sos/utilities.pyRo5siii@l(&t __future__RRR8Rt subprocessRRRRR,RuRgRht threadingRpt contextlibRt collectionsRRRRR&RdR7RBRPRjRRRtThreadRltobjectRRtRo(((s1/usr/lib/python2.7/site-packages/sos/utilities.pyt s8               L 6-