U =_jfN@s8dZddlmZmZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl mZddl mZddlmZddlmZddlmZzdd lmZWnek rdZYnXzdd lmZWnek rdZYnXe jd krddlZndZe jdd kZd ddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;dd?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZgNZejd[kZejd\kZe jd]Ze jd^Z e Z!e jd_Z"e jd`Z#e jdaZ$e"pe#pe$Z%e jdbZ&e jdcZ'ddZ(deZ)dfZ*dgZ+dhZ,diZ-djZ.dkZ/dlZ0dmZ1dnZ2doZ3dpZ4dqZ5drZ6dsZ7dtZ8duZ9dvZ:dwZ;dxZd{Z?d|Z@d}ZAedkrd~ZBdZCdZDn GdddejEZFeGHeFjIedkrdZJdZKn GdddejEZLeGHeLjIe MZNesdZOn2z e PZOWn$eQk rHer@dndZOYnXedHddddddgZRedDddddgZSedBddddddgZTedCddddddgZUedEddddddddgZVedIdddddgZWed@dddddddgZXedFdddddgZYedGddddgZZedAddddgZ[eddddgZ\edddddgZ]eddddgZ^edddgZ_ed8ddddgZ`ed=ddgZaed>dddgZbed?dddgZced:dddgZded;ddddgZeedt||}|SdS)z5Calculate percentage usage of 'used' against 'total'.dgN)floatZeroDivisionErrorround)rnrmZround_rrdrdrerOus cs2tfdd}fdd}i||_|S)aA simple memoize decorator for functions supporting (hashable) positional arguments. It also provides a cache_clear() function for clearing the cache: >>> @memoize ... def foo() ... return 1 ... >>> foo() 1 >>> foo.cache_clear() >>> csN|tt|f}z |WStk rH||}|<|YSXdSr) frozensetsorteditemsKeyError)rkwargskeyrcachefunrdrewrappers  zmemoize..wrappercs dS)z Clear cache.N)clearrd)rrdre cache_clearszmemoize..cache_clear) functoolswrapsr)rrrrdrrerLs  cs6tfdd}dd}dd}||_||_|S)aA memoize decorator which is disabled by default. It can be activated and deactivated on request. For efficiency reasons it can be used only against class methods accepting no arguments. >>> class Foo: ... @memoize ... def foo() ... print(1) ... >>> f = Foo() >>> # deactivated (default) >>> foo() 1 >>> foo() 1 >>> >>> # activated >>> foo.cache_activate(self) >>> foo() 1 >>> foo() >>> foo() >>> csTz|j}Wn@tk r*|YStk rN|}|j<YnX|Sr)_cacheAttributeErrorrrrrdrers z'memoize_when_activated..wrappercSs i|_dS)zjActivate cache. Expects a Process instance. Cache will be stored as a "_cache" instance attribute.N)rprocrdrdrecache_activatesz.memoize_when_activated..cache_activatecSs"z|`Wntk rYnXdS)zDeactivate and clear cache.N)rrrrdrdrecache_deactivatesz0memoize_when_activated..cache_deactivate)rrrr)rrrrrdrrememoize_when_activateds rc Cs\zt|}Wn<tk rJ}z|jtjtjfkr4WYdSd}~XYnXt|jSdS)zSame as os.path.isfile() but does not swallow EACCES / EPERM exceptions, see: http://mail.python.org/pipermail/python-dev/2012-June/120787.html FN)osstatOSErrorerrnoEPERMEACCESS_ISREGst_mode)rsterrrdrdrerKsc CsTzt|Wn<tk rJ}z|jtjtjfkr4WYdSd}~XYnXdSdS)zSame as os.path.exists() but does not swallow EACCES / EPERM exceptions, see: http://mail.python.org/pipermail/python-dev/2012-June/120787.html FNT)rrrrrr)rrrdrdrerNsc CsdtjrtdkrdSz4tttj}t||dW5QRXWdStjk r^YdSXdS)z2Return True if IPv6 is supported on this platform.NF)z::1rT)socketZhas_ipv6r r contextlibclosingZbinderror)ZsockrdrdrerPs cCsvi}d}t}|d|}||kr"qr|d||}||krh|||}||d|}|r`|}|||<|d}q |S)zCParse a C environ block of environment variables into a dictionary.r=r_)rfindupper)datarposZWINDOWS_Znext_posZ equal_posrrrdrdrerMs   cCs6tdkr |Sz t|WStk r0|YSXdS)zConvert a numeric socket family value to an IntEnum member. If it's not a known member, return the numeric value itself. N)enumrZ AddressFamily ValueErrorZnumrdrdrerQ"s  cCs6tdkr |Sz t|WStk r0|YSXdS)zConvert a numeric socket type value to an IntEnum member. If it's not a known member, return the numeric value itself. N)rrZ SocketKindrrrdrdrerR/s  cCs|tjtfkr&|rt|}|r&t|}|tjkrJ|ttfkrJ||t}nt}t|}t|}|dkrxt ||||||St |||||||SdS)z2Convert a raw connection tuple to a proper ntuple.N) rrr rrgetrrQrRr6r?)rqZfamZtype_rsrtruZ status_maprprdrdrerU<scsfdd}|S)zA decorator which can be used to mark a method as deprecated 'replcement' is the method name which will be called instead. cs:d|jf|jdkr|_t|fdd}|S)Nz8%s() is deprecated and will be removed; use %s() insteadcs tjtddt|||S)Nr^)category stacklevel)warningswarnDeprecationWarninggetattr)rrr)r replacementrdreinnerYsz/deprecated_method..outer..inner)rarrr)rrrrreouterSs z deprecated_method..outerrd)rrrdrrerJOs c@sBeZdZdZddZddZddZdd Zdd d Zd dZ d S) _WrapNumberszNWatches numbers so that they don't overflow and wrap (reset to zero). cCs t|_i|_i|_i|_dSr) threadingZLocklockr reminders reminder_keysrrdrdrerfs z_WrapNumbers.__init__cCsT||jkst||jkst||jks*t||j|<tt|j|<tt|j|<dSr)rAssertionErrorrrrintset)r input_dictrordrdre _add_dictls  z_WrapNumbers._add_dictcCs\|j|}t|t|}|D]0}|j||D]}|j||=q8|j||=q&dS)zIn case the number of keys changed between calls (e.g. a disk disappears) this removes the entry from self.reminders. N)rrkeysrr)rrroold_dictZ gone_keysZgone_keyremkeyrdrdre_remove_dead_remindersts  z#_WrapNumbers._remove_dead_remindersc Cs ||jkr||||S||||j|}i}|D]}||}z ||}Wn tk rt|||<YqClear the internal cache, optionally only for function 'name'.N)rrrrrpop)rrordrdrers   z_WrapNumbers.cache_clearc Cs.|j|j|j|jfW5QRSQRXdS)z5Return internal cache dicts as a tuple of 3 elements.N)rrrrrrdrdre cache_infosz_WrapNumbers.cache_info)N) rarbrcrrrrrrrrdrdrdreras ' rc Cs*tjt||W5QRSQRXdS)zGiven an `input_dict` and a function `name`, adjust the numbers which "wrap" (restart from zero) across different calls by adding "old value" to "new value" and return an updated dict. N)_wnrr)rrordrdrerSscKst|df|S)Nrb)openfnamerrdrdre open_binarysrcKs*tr|dt|dtt|df|S)zOn Python 3 opens a file in text mode by using fs encoding and a proper en/decoding errors handler. On Python 2 this is just an alias for open(name, 'rt'). encodingerrorsZrt)PY3 setdefaultr4r5rrrdrdre open_texts  r%(value).1f%(symbol)scCsd}i}t|ddD]\}}d|dd>||<qt|ddD].}|||krFt|||}|tSqF|t|d|dS)zUsed by various scripts. See: http://goo.gl/zeJZl >>> bytes2human(10000) '9.8K' >>> bytes2human(100001221) '95.4M' ) BKMGTPEZYr_N r)symbolr) enumeratereversedrlocalsdict)nformatZsymbolsprefixrsrrrdrdrerTs  cCs tjdjS)z+Return updated psutil.PROCFS_PATH constant.r)sysmodulesZ PROCFS_PATHrdrdrdreget_procfs_pathsrcCs|jttdS)N)rr)decoder4r5rrdrdrersrcCs|SrrdrrdrdrerscCs`tjdkrdSz2ddl}|s$t||ddks>tWntk rVYdSXdSdS)Nr[TrcolorsF)rrocursesisattyrZ setuptermZtigetnumr)filerrdrdrerXs  Fc Csts |Sg}tddddddddd d }d |d <z ||}Wn(tk rdtd t|YnX|||r~|ddd||fS)z*Return an highlighted version of 'string'.Z32Z91Z33Z93Z34Z35Z36Z37Z30) greenredbrownyellowZblueZvioletZ lightblueZgreyZdarkgreyZ29Nz#invalid color %r; choose between %s1z [%sm%s;)rXrrrlistrrr)rcolorboldattrrrdrdrerW s(     c Cstst||dntr.tt||||dnddl}d}|jjj}|jjj}t ddddd}||d<z ||}Wn,t k rt d |t | fYnX|r|dkr|d 7}|tjkrd nd } |j|_|| } || |zt||dW5|| |XdS) z$Print a colorized version of string.r!rNr^r )r"r#r$r%z#invalid color %r; choose between %rii)rXprintrrWctypesZwindllZKernel32 GetStdHandleSetConsoleTextAttributerrrr(rrstderrZc_ulongZrestype) rr)r*r!r1Z DEFAULT_COLORr2r3rZ handle_idZhandlerdrdrerY s6     Z PSUTIL_DEBUGcCs6ttj\}}}}}td|||ftjddS)z@If PSUTIL_DEBUG env var is set, print a debug message to stderr.zpsutil-debug [%s:%s]> %sr,N)inspectZ getframeinfoZ currentframef_backr0rr4)rrlinenoZ func_namelinesindexrdrdrerVFs cCsdSrrdrrdrdrerVMs)N)N)r)NF)rZ __future__rrrrrrrrrrr collectionsrrrrrr ImportErrorr version_inforr__all__rorrplatform startswithrrrr rrrrZAIXr*r+r'r,r.r2r&r0r1r(r)r/r-r3rr!r rrr"rrrrrrr#r$r%IntEnumr`globalsupdate __members__rirjrhgetfilesystemencodingr4r5getfilesystemencodeerrorsrrGrCrArBrDrHr?rErFr@rvryrzr{r7r<r=r>r9r:r;r8r6rrIrrrrrrrOrLrrKrNrPrMrQrRrUrJrrSrrrrrrTrrstdoutrXrWrYboolgetenvr5rVrdrdrdres.         $                                 9   W     #