eaiovnaovbqoebvqoeavibavo 3 fM'@sddlZddlZddlZddlmZmZmZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlmZddlmZyddlmZWn ek rddlmZYnXdZyddlZejedZWnPeefk r,ejd Zdd l m!Z!d Z"ej#d e!e"d ddd YnXdZ$ddddddddddddddddd d!d"d#d$d%d&gZ%d'd(Z&dPd)d*Z'd+d&Z(dQd-dZ)dVd3d4Z*d5dZ+dWd6dZ,d7dZ-dXd8dZ.e$dddddddddf d9d%Z/dYd:dZ0dZd;d$Z1d[d=dZ2ej3fd>d?Z4d@dZ5dAdZ6dBd Z7dCd!Z8dDdZ9ej:dEdFd"Z;dGdZGdKdLdLe j?Z@GdMddeAZBGdNddZCGdOddeDZEdS)\N)PopenPIPESTDOUT)closing)deque)parse) parse_versionFTsos)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)Zreplace_whitespacei,TIMEOUT_DEFAULTImporterHelperSoSTimeoutError TempFileUtilboldfile_is_binaryfileobjfindget_human_readablegrep import_module is_executablelistdirr path_exists path_isdir path_isfile path_islink path_joinrecursive_dict_values_by_key shell_outsos_get_command_outputtailcCstjd}yB|j|dd\}}}|s(|Stjdd|jd}|d|}|Stk r}ztjd|d ||Sd }~XnXd S) z< Convert the version into a PEP440 compliant version scheme.z2^([0-9][0-9.]*(?:(?:a|b|rc|.post|.dev)[0-9]+)*)\+?)maxsplitz[+~]+.-+zUnable to format z to pep440 format: N)recompilesplitsubstrip Exceptionlogdebug)verZpublic_version_re_ZpublicZlocalZsanitized_localZpep440_versionerrr3/usr/lib/python3.6/utilities.pyformat_version_to_pep440Lsr5cCs|rt|}t|St|S)z6 Converts the version to PEP440 format before parsing )r5r)r0Zpep440Z ver_pep440r3r3r4sos_parse_version]sr6c Cs<t|d(}tj|j|kr*|j| d|jSQRXdS)z,Returns the last number_of_bytes of filenamerbN)openosstatst_sizeseekread)filenameZnumber_of_bytesfr3r3r4r"fs rc CsXt|trLy t||Stk rHtjd}|jd|ttj SXnt|SdS)z@Returns a file-like object that can be used as a context managerr zfileobj: %s could not be openedN) isinstancestrr9IOErrorlogging getLoggerr/rioStringIO)Z path_or_filemoder.r3r3r4rns   r# (cCsdt|}||krd||S||kr0d||S||krDd||S||krXd||Sd|SdS)zCConverts a number of bytes to a shorter, more human friendly formatz%.1fTz%.1fGz%.1fMz%.1fKz%dN)float)Zbytes_KMGTfnr3r3r4 convert_bytes{s    rTcstrNy4tj|ddg}jdko8tfdd|D Stk rLYnXt|d*}y|jddStk r|d SXWd QRXd S) 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/binaryc3s|]}jj|VqdS)N)Z mime_type startswith).0Z_mt)_ftupr3r4 sz!file_is_binary..Ztrr#FTN) magic_modmagicdetect_from_filenameencodinganyr-r9r>UnicodeDecodeError)fnameZ_mimesZtfiler3)rXr4rs     c cs|r"tjj|jtjj}||7}xrtj|D]d\}}}|rX|jtjj|krX|dd=|rltj|| rlq.x$tj||D]}tjj||VqzWq.WdS)zGenerator function to find files recursively. Usage:: for filename in find("*.properties", "/var/log/foobar"): print filename N) r:pathdirnamecountsepwalkfnmatchfilterjoin) Z file_patternZtop_dirZ max_depthZ path_patternZ base_depthraZdirlistZfilelistnamer3r3r4rs c sBg}x8|D]0}t|}|jfdd|DWdQRXq W|S)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 linec3s|]}tj|r|VqdS)N)r(match)rWline)patternr3r4rYszgrep..N)rextend)rlZfiles_or_pathsZmatchesZfopZfor3)rlr4rs   &cs\tjjddjtjj}gfdd|D}rJ|fdd|D7}tdd|DS)z6Returns if a command matches an executable on the PATHPATHcsg|]}tjj|qSr3)r:rarh)rWp)commandr3r4 sz!is_executable..cs g|]}tjj|jdqS)/)r:rarhlstrip)rWc)sysrootr3r4rrscss|]}tj|tjVqdS)N)r:accessX_OK)rWrar3r3r4rYsz is_executable..)r:environgetr*rapathsepr^)rqrvpathsZ candidatesr3)rqrvr4rs c sbfdd} fdd} tjj} d| d<|r`x.|jD]"\}}|rP|| |<q:| j|dq:W|rtdrd |rvd nd ||f}tj|}g}xN|D]F}|jd rd |krt j |}|r|j |q|j |q|j |qW| rt | d}nt }yt|d||rtnt d| d| d}| s.t|j||}n t||}rXxr|jrT| |q@WnZy|j|rh|ndWn@tk r|j| r|jd|_d|j|jdSX| r|jx|jdkrԐqW|j}|j}WnPtk r6}z2| r|j|jtjkr"dd d dS|WYdd}~XnX|jdksP|jdkrTd}|j||dS)zExecute a command and return a dictionary of status and output, optionally changing root or current working directory before executing command. cs(rdkrtjr$tjdS)Nrs)r:chrootchdirr3)r~r}r3r4_child_prep_fns  z.sos_get_command_output.._child_prep_fncs,s|jdkr|jttjddS)N|g{Gz?)poll terminatertimesleep)proc)pollerr3r4 _check_pollersz-sos_get_command_output.._check_pollerzC.UTF-8LC_ALLNtimeoutztimeout %s %ds %sz --foregroundrors*wFr#T)shellstdoutstderrbufsizeenvZ close_fdsZ preexec_fnr)Zstatusoutput truncated~)r:rycopyitemspoprshlexr*rVglobrmappendr9rrr AsyncReaderr FakeReaderrunningwaitr-rclose get_contentsis_fullrOSErrorerrnoENOENT returncode)rqrrr}r~rZ foregroundrU sizelimitrZto_filerrZcmd_envkeyvalueargsZ expanded_argsargZ expanded_argZ_outputrpreaderrrer3)r~r}rr4r!s~                  csjdd }yttt|g}Wn>tk rd}z"tdd|jj|WYdd}~XnXfddtj |tj D}rfdd|D}|S) 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%r#z"Error while trying to load module z: Ncsg|]\}}|jkr|qSr3) __module__)rWZcnameZclass_) module_fqnamer3r4rrWsz!import_module..csg|]}t|r|qSr3) issubclass)rWm) superclassesr3r4rr[sr) rpartition __import__globalslocals ImportErrorprint __class____name__inspectZ getmembersZisclass)rrZ module_namemodulermodulesr3)rrr4rKs cCst||||ddS)zeShell out to an external command and return the output or the empty string in case of error. )rr}r~r)r!)cmdrr}Zrunatr3r3r4r `sr8cCsJdddddg}d}x$|dkr6|dkr6|d 7}|d }qWd ||||fS) NBZKiBZMiBZGiBZTiBrir#g@z%.*f%sr3)sizeZ precisionsuffixesZ suffixindexr3r3r4rhs  cCs>|r,|tjkr,|j|s,tjj||jd}t||}||S)Nrs)r:rdrVrarhrtgetattr)rarvmethodrZ_methr3r3r4 _os_wrapperrs   rcCs$d|krt||dtdSt||dS)Nrr)rexists)rr)rarvr3r3r4rzscCs t||dS)Nisdir)r)rarvr3r3r4rscCs t||dS)Nisfile)r)rarvr3r3r4rscCs t||dS)Nislink)r)rarvr3r3r4rscCst||dtS)Nr)rr:)rarvr3r3r4rs)rvcGs8|r&|j| r&tjj||jtj}tjj|f|S)N)rVr:rarhrtrd)rarvrpr3r3r4rscCs d|dS)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`` zzr3)textr3r3r4rs c sg}g|j|t|trx|jD]\}j|| sF||krV|jtq&ttr&y$|jtfdd|DdWntk rYnXjjq&Wn |j|fdd|DS)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`` c3s|]}|kr|VqdS)Nr3)rWr)vr3r4rYsz/recursive_dict_values_by_key..rcsg|]}|kr|qSr3r3)rWd)_filtr3r4rrsz0recursive_dict_values_by_key..)rmrBdictrrr IndexErrorkeys)ZdobjrZ_itemskr3)rrr4rs&      c@s8eZdZdZddZeddZddZedd Zd S) 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. cCs||_||_dS)N)processrU)selfrrUr3r3r4__init__szFakeReader.__init__cCsdS)NFr3)rr3r3r4rszFakeReader.is_fullcCs|js dSdS)Nror)rU)rr3r3r4rszFakeReader.get_contentscCs|jjdkS)N)rr)rr3r3r4rszFakeReader.runningN) rr __qualname____doc__rpropertyrrrr3r3r3r4rs  rcs<eZdZdZfddZddZddZedd ZZ 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. csbtt|j||_||_d|_d|_|rB|d}t||j|_t|jd|_d|_ |j dS)Nii)maxlenT) superrrchanrU chunksizeslotsintrrstart)rZchannelrrU)rr3r4rszAsyncReader.__init__c CsNy*x$|jj|j}|sP|jj|qWWnttfk rBYnXd|_dS)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)rr>rrr ValueErrorrDr)rrkr3r3r4runs zAsyncReader.runcCsLx|jrtjdqW|js2djdd|jDSdjdd|jDSdS)z-Returns the contents of the deque as a stringg{Gz?rocss|]}|jddVqdS)zutf-8ignoreN)decode)rWlnr3r3r4rY sz+AsyncReader.get_contents..rcss|] }|VqdS)Nr3)rWrr3r3r4rY"sN)rrrrUrhr)rr3r3r4rs zAsyncReader.get_contentscCs|js dSt|j|jkS)z?Checks if the deque is full, implying that output was truncatedF)rlenr)rr3r3r4r$szAsyncReader.is_full) rrrrrrrrr __classcell__r3r3)rr4rs   rc@s8eZdZdZddZddZddZdd Zd d Zd S) rzProvides 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)zopackage is a package module import my.package.module helper = ImporterHelper(my.package.module)N)package)rrr3r3r4r2szImporterHelper.__init__cCs tjj|}tjj|\}}|S)z-Returns the plugin module name given the path)r:rabasenamesplitext)rrabaseriZextr3r3r4 _plugin_name8s zImporterHelper._plugin_namecsfdd|D}|j|S)Ncs(g|] }d|kr|jdrj|qS)rz.py)endswithr)rWZplugin)rr3r4rr?sz9ImporterHelper._get_plugins_from_list..)sort)rZlist_pluginsr3)rr4_get_plugins_from_list>s z%ImporterHelper._get_plugins_from_listcCs0tjj|r,ttd|}|j|}|r,|SgS)Nz*.py)r:rarlistrr)rraZpy_filesZpnamesr3r3r4_find_plugins_in_dirEs   z#ImporterHelper._find_plugins_in_dircCs6g}x,|jjD] }tjj|r|j|j|qW|S)zQReturns the list of importable modules in the configured python package. )r__path__r:rarrmr)rrrar3r3r4 get_modulesMs  zImporterHelper.get_modulesN) rrrrrrrrrr3r3r3r4r,s c@s$eZdZddZddZddZdS)rcCs||_g|_dS)N)tmp_dirfiles)rrr3r3r4rZszTempFileUtil.__init__cCs2tj|jd\}}tj|d}|jj||f|S)N)dirzw+)tempfileZmkstemprr:fdopenrr)rfdr`Zfobjr3r3r4new^s zTempFileUtil.newcCslx`|jD]V\}}y|j|jWntk r8YnXytj|Wqtk r\YqXqWg|_dS)N)rflushrr-r:unlink)rr`r@r3r3r4cleanes  zTempFileUtil.cleanN)rrrrrrr3r3r3r4rXsc@s eZdZdS)rN)rrrr3r3r3r4rss)T)rA@)rrrr)NN)N)N)rLNN)r8)Fr:r(r subprocessrrrrErfrrrrZ threadingrrG contextlibr collectionsrZpackaging.versionrrrZ pkg_resourcesrZr[r\__file__AttributeErrorrFr.textwrapr msgZwarningr __all__r5r6r"rrTrrrrr!rr rrarrrrrrrdrrrrZThreadrobjectrrrrr3r3r3r4 s     "  "   n    6=,