id~H ddlZddlZddlZddlmZmZmZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlmZddlmZ ddlmZdZ ddlZej8edZdZ&dZ'd0dZ(d1dZ)dZ*d2dZ+dZ,d3dZ-e&dddddddddf dZ.d3dZ/d4dZ0d5dZ1ejdfdZ3dZ4dZ5dZ6dZ7d Z8ejrd!d"Z:d#Z;gfd$Zej@d Z!dd l"m#Z#d Z$e!jKd e#e$d dzd zYwxYw)6N)PopenPIPESTDOUT)closing)deque) parse_version)parseFTsos)fillzWARNING: Failed to load 'magic' module version >= 0.4.20 which sos aims to use for detecting binary files. A less effective method will be used. It is recommended to install proper python3-magic package with the module.  H)replace_whitespacei,ct|d5}tj|j|kDr|j | d|j cdddS#1swYyxYw)z,Returns the last number_of_bytes of filenamerbN)openosstatst_sizeseekread)filenamenumber_of_bytesfs //usr/lib/python3/dist-packages/sos/utilities.pytailr2sT h  778  $ $ 6 FFO#Q 'vvxs AAA%ct|tr t||St|S#t$rIt j d}|j d|zttjcYSwxYw)z@Returns a file-like object that can be used as a context managerr zfileobj: %s could not be opened) isinstancestrrIOErrorlogging getLoggerdebugrioStringIO) path_or_filemodelogs rfileobjr):so,$ * d+ + |$$  *##E*C II7,F G2;;=) ) *s )AA;:A;ct|}||k\rd||z zS||k\rd||z zS||k\rd||z zS||k\rd||z zSd|zS)zCConverts a number of bytes to a shorter, more human friendly formatz%.1fTz%.1fGz%.1fMz%.1fKz%d)float)bytes_KMGTfns r convert_bytesr2Gsm vB {"q&!! 1"q&!! 1"q&!! 1"q&!!f}cFtr@ tj|ddg}jdk(xrt fd|D St |d5} |jd dddy#t $rY4wxYw#t$r Ydddy wxYw#1swYyxYw) azHelper to determine if a given file contains binary content or not. This is especially helpful for `sos clean`, which cannot obfuscate binary data and instead, by default, will remove binary files. :param fname: The full path of the file to check binaryness of :type fname: ``str`` :returns: True if binary, else False :rtype: ``bool`` ztext/zinode/binaryc3TK|]}jj|!ywN) mime_type startswith).0_mt_ftups r z!file_is_binary..hs FEOO..s3Fs%(trNFT) magic_modmagicdetect_from_filenameencodingany ExceptionrrUnicodeDecodeError)fname_mimestfiler<s @rfile_is_binaryrJVs ..u5Ex(F(*GFvFF0G  eT e  JJqM     "    s;>A1BB1 A=<A= B BBBB c#K|rKtjj|jtjj}||z }tj |D]\}}}|r1|jtjj|k\r|dd=|rt j ||sSt j||D]$}tjj||&yw)zGenerator function to find files recursively. Usage:: for filename in find("*.properties", "/var/log/foobar"): print filename N) rpathdirnamecountsepwalkfnmatchfilterjoin) file_patterntop_dir max_depth path_pattern base_depthrLdirlistfilelistnames rfindr\xsWW__W-33BGGKK@ Z #%777#3+gx BGGKK0I= l C NN8\: +D'',,tT* * ++sC6C8cg}|D]0}t|5}|jfd|Dddd2|S#1swY>xYw)zReturns lines matched in fnames, where fnames can either be pathnames to files to grep through or open file objects to grep through line by linec3PK|]}tj|s|ywr7)rematch)r:linepatterns rr=zgrep..sKT288GT3JDKs&&N)r)extend)rbfiles_or_pathsmatchesfopfos` rgreprhs\GM S\ MR NNKRK L M MM N M Ms ;A c tjjddjtjj }|g|Dcgc]"}tjj ||$c}z}|r@||Dcgc]1}tjj ||jd3c}z }td|DScc}wcc}w)z6Returns if a command matches an executable on the PATHPATH/c3dK|](}tj|tj*ywr7)raccessX_OK)r:rLs rr=z is_executable..s?Dryyrww'?s.0) renvirongetsplitrLpathseprSlstriprD)commandsysrootpathsp candidatescs r is_executabler{s JJNN62 & , ,RWW__ =EF1bggll1g6FFJ:D 56BGGLL!((3- 0   ?J? ?? G s 'C6Cc l fd} fd} tjj} d| d<|r2|jD]\}}|r|| |<| j |d!|rt dr d|rdnd ||fz}t j|}g}|D]c}|jd r?d |vr;tj|}|r|j|A|j|S|j|e| r t| d }nt} t|d ||rtntd| d| }| st!|j"||}n t%||} r"|j&r,| ||j&rn |j)|r|nd| r|j/|j5 |j5|j1}|j2}|j<dk(s|j<dk(rd}|j<||dS#t*$rJ|j-| r|j/d |_d|j1|j2dcYSwxYw#t6$rA}| r|j/|j8t8j:k(r dd d dcYd}~S|d}~wwxYw)zExecute a command and return a dictionary of status and output, optionally changing root or current working directory before executing command. cnrdk7rtjrtjyyNrl)rchrootchdir)rrsr_child_prep_fnz.sos_get_command_output.._child_prep_fns+ fm IIf   HHUO r3cs|jdk(r|jttjdy)N|{Gz?)poll terminateSoSTimeoutErrortimesleep)procpollers r _check_pollerz-sos_get_command_output.._check_pollers1 8tyy{c) NN ! ! 4r3zC.UTF-8LC_ALLNtimeoutztimeout %s %ds %sz --foregroundrkrl*wFT)shellstdoutstderrbufsizeenv close_fds preexec_fnr)statusoutput truncated~r3)rrpcopyitemspopr{shlexrrr9globrcappendrrrr AsyncReaderr FakeReaderrunningwaitrErclose get_contentsis_fullrOSErrorerrnoENOENT returncode)rurrrrr foregroundr5 sizelimitrto_filerrcmd_envkeyvalueargs expanded_argsarg expanded_arg_outputrxreaderrres `` ` rsos_get_command_outputrs  jjooG!GH ))+ 'JC$  C&  ' =+%(Nb  )   ;;w DM& >># 3#:99S>L$$\2$$S)   %&w$, -uW#)t'T+ -  9f=F6*F ..a .. 5'wt4  MMOffh ffh$$&NN  ||sallc1,, = 5 MMO"'"%1D1D1F%+^^55 5&   MMO 77ell "!RbA AG sVA'I)<I)>H3I)I)AI&#I)%I&&I)) J324J.&J3,J..J3cH|jdd}t|tt|g}t j |tj Dcgc]\}}|j|k(r|}}}|r|Dcgc]}t||s|}}|Scc}}wcc}w)aImports 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..r) rpartition __import__globalslocalsinspect getmembersisclass __module__ issubclass) module_fqname superclasses module_namemodulecnameclass_modulesms r import_modulers  **3/3K  wy&([M JF!!&'//:6-%##}46G6%EA|)D1EE N 6FsB?BBc&t||||dS)zeShell out to an external command and return the output or the empty string in case of error. )rrrr)r)cmdrrrunats r shell_outr's$ "#w)/u >>F HHr3cbgd}d}|dkDr|dkr|dz }|dz }|dkDr|dkrd||||fzS)N)BKiBMiBGiBTiBrr?g@z%.*f%s)size precisionsuffixes suffixindexs rget_human_readabler/sT0HK ++/q F{ ++/ y$(=> >>r3c|rS|tjk7r@|j|s/tjj ||j d}t ||}||Sr~)rrOr9rLrSrtgetattr)rLrvmethodr_meths r _os_wrapperr9sO7bff$w'77<<S)9:D FF #E ;r3cJd|vrt||dtSt||dS)Nrr)rexists)rrrLrvs r path_existsrAs) d{4&>> tWh //r3ct||dS)Nisdirrrs r path_isdirrGs tWg ..r3ct||dS)Nisfilerrs r path_isfilerK tWh //r3ct||dS)Nislinkrrs r path_islinkrOrr3c&t||dtS)Nlistdir)rrrs rrrSs tWi 44r3)rvc|rN|j|s=tjj||j tj }tjj|g|Sr7)r9rrLrSrtrO)rLrvrxs r path_joinrWsItw/ww||GT[[%89 77<< !q !!r3cd|zdzS)a"Helper to make text bold in console output, without pulling in dependencies to the project unneccessarily. :param text: The text to make bold :type text: ``str`` :returns: The text wrapped in the ASCII codes to display as bold :rtype: ``str`` zzr)texts rboldr]s t i ''r3cg}g}|j|t|tr|jD]\}|j ||r||vr|jt 8ttsI |jt fd|Dd|jjn|j||Dcgc] }||vs| c}S#t $rYSwxYwcc}w)a Recursively compile all elements of a potentially nested dict by a set of keys. If a given key is a dict within ``dobj``, then _all_ elements within that dict, regardless of child keys, will be returned. For example, if a Plugin searches the devices dict for the 'storage' key, then all storage devices under the that dict (e.g. block, fibre, etc...) will be returned. However, if the Plugin specifies 'block' via ``keys``, then only the block devices within the devices['storage'] dict will be returned. Any elements passed here that are _not_ keys within the dict or any nested dicts will also be returned. :param dobj: The 'top-level' dict to intially search by :type dobj: ``dict`` :param keys: Which keys to compile elements from within ``dobj``. If no keys are given, all nested elements are returned :param keys: ``list`` of ``str`` :returns: All elements within the dict and any nested dicts :rtype: ``list`` c32K|]}|vs|ywr7r)r:rvs rr=z/recursive_dict_values_by_key..s 5'*cQhAcF5s  r)rcrdictrrrecursive_dict_values_by_key IndexErrorkeys)dobjr_items_filtkdrs @rrrjs0F E MM$$JJL 'DAq LLO19 :1=>At$ MM45.25 QVVX&% '*  d 0!%A 00" 1s&C/ C>(C>/ C;:C;ct|S)zParse the version string ) version_parse)versions rrrs  !!r3c<eZdZdZdZedZdZedZy)rzUsed as a replacement AsyncReader for when we are writing directly to disk, and allows us to keep more simplified flows for executing, monitoring, and collecting command output. c ||_||_yr7)processr5)selfrr5s r__init__zFakeReader.__init__s  r3cy)NFrrs rrzFakeReader.is_fullsr3c"|jsdSdS)Nrkr3)r5rs rrzFakeReader.get_contentssr-#-r3c:|jjduSr7)rrrs rrzFakeReader.runnings||  "d**r3N) __name__r __qualname____doc__rpropertyrrrrr3rrrs9 .++r3rc>eZdZdZfdZdZdZedZxZ S)rzUsed 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. ctt| ||_||_d|_d|_|r"|dz}t||j z |_t|j |_d|_ |jy)Ni)maxlenT) superrrchanr5 chunksizeslotsintrrstart)rchannelrr5 __class__s rrzAsyncReader.__init__sn k4)+   !G+IY78DJ$**-   r3c |jj|j}|sn|jj |D d|_y#t t f$r Yd|_ywxYw)alReads 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. FN)rrr rr ValueErrorr r)rras rrunzAsyncReader.runsk yy~~dnn5 !!$'   G$    sAAA)(A)c|jr"tjd|jr"|js"dj d|j DSdj d|j DS)z-Returns the contents of the deque as a stringrrkc3@K|]}|jddyw)zutf-8ignoreN)decoder:lns rr=z+AsyncReader.get_contents..sMB299Wh7Msr3c3 K|]}|ywr7rr-s rr=z+AsyncReader.get_contents..s42B4s )rrrr5rSrrs rrzAsyncReader.get_contentssYll JJt ll{{77M$**MM M88444 4r3c`|jsyt|j|jk(S)z?Checks if the deque is full, implying that output was truncatedF)r!lenrrs rrzAsyncReader.is_fulls%zz4::$**,,r3) rrrrrr(rrr __classcell__)r%s@rrrs+ * 5--r3rc.eZdZdZdZdZdZdZdZy)ImporterHelperzProvides 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. c||_y)zopackage is a package module import my.package.module helper = ImporterHelper(my.package.module)N)package)rr6s rrzImporterHelper.__init__s  r3ctjj|}tjj|\}}|S)z-Returns the plugin module name given the path)rrLbasenamesplitext)rrLbaser[exts r _plugin_namezImporterHelper._plugin_names3ww%GG$$T* c r3c|Dcgc](}d|vr"|jdr|j|*}}|j|Scc}w)Nrz.py)endswithr<sort)rlist_pluginpluginss r_get_plugins_from_listz%ImporterHelper._get_plugins_from_list sV!&K .6??53I$$V,KK   Ks-Actjj|r,tt d|}|j |}|r|SgSy)Nz*.py)rrLrlistr\rC)rrLpy_filespnamess r_find_plugins_in_dirz#ImporterHelper._find_plugins_in_dirsE 77>>$ D./H00:F  r3cg}|jjD]B}tjj |s#|j |j |D|S)zQReturns the list of importable modules in the configured python package. )r6__path__rrLrrcrH)rrBrLs r get_moduleszImporterHelper.get_modulessRLL)) @Dww}}T"t88>? @r3N) rrrrrr<rCrHrKrr3rr4r4s    r3r4ceZdZdZdZdZy) TempFileUtilc ||_g|_yr7)tmp_dirfiles)rrOs rrzTempFileUtil.__init__(s  r3ctj|j\}}tj|d}|j j ||f|S)N)dirzw+)tempfilemkstemprOrfdopenrPr)rfdrGfobjs rnewzTempFileUtil.new,sE$$6 EyyT" 5$-( r3c|jD]<\}} |j|j t j |>g|_y#t$rY+wxYw#t$rYawxYwr7)rPflushrrErunlink)rrGrs rcleanzTempFileUtil.clean3sq HE1     %       s" AA# A A # A/.A/N)rrrrrXr\rr3rrMrM&s r3rMc eZdZy)rN)rrrrr3rrrAsr3r)r)rri@l)NNr7)NN)r)Err_r subprocessrrrr!rQrrrrS threadingrr$ contextlibr collectionsr pkg_resourcesrr  SyntaxErrorpackaging.versionr r@rArB__file__ ImportErrorAttributeErrorr"r(textwrapr msgwarningTIMEOUT_DEFAULTrr)r2rJr\rhr{rrrrrLrrrrrrrOrrrrThreadrobjectr4rMrrrr3rrps **  9<   GEx(I % D+,  @->EFGs%DD( D%$D%(AE-,E-