<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import psutil <EOL> if not hasattr ( psutil . Process , '<STR_LIT>' ) or os . name != '<STR_LIT>' : <EOL> sys . exit ( '<STR_LIT>' ) <EOL> import time <EOL> import curses <EOL> import atexit <EOL> def tear_down ( ) : <EOL> win . keypad ( <NUM_LIT:0> ) <EOL> curses . nocbreak ( ) <EOL> curses . echo ( ) <EOL> curses . endwin ( ) <EOL> win = curses . initscr ( ) <EOL> atexit . register ( tear_down ) <EOL> curses . endwin ( ) <EOL> lineno = <NUM_LIT:0> <EOL> def print_line ( line , highlight = False ) : <EOL> """<STR_LIT>""" <EOL> global lineno <EOL> try : <EOL> if highlight : <EOL> line += "<STR_LIT:U+0020>" * ( win . getmaxyx ( ) [ <NUM_LIT:1> ] - len ( line ) ) <EOL> win . addstr ( lineno , <NUM_LIT:0> , line , curses . A_REVERSE ) <EOL> else : <EOL> win . addstr ( lineno , <NUM_LIT:0> , line , <NUM_LIT:0> ) <EOL> except curses . error : <EOL> lineno = <NUM_LIT:0> <EOL> win . refresh ( ) <EOL> raise <EOL> else : <EOL> lineno += <NUM_LIT:1> <EOL> def bytes2human ( n ) : <EOL> """<STR_LIT>""" <EOL> symbols = ( '<STR_LIT>' , '<STR_LIT:M>' , '<STR_LIT>' , '<STR_LIT:T>' , '<STR_LIT:P>' , '<STR_LIT:E>' , '<STR_LIT>' , '<STR_LIT:Y>' ) <EOL> prefix = { } <EOL> for i , s in enumerate ( symbols ) : <EOL> prefix [ s ] = <NUM_LIT:1> << ( i + <NUM_LIT:1> ) * <NUM_LIT:10> <EOL> for s in reversed ( symbols ) : <EOL> if n >= prefix [ s ] : <EOL> value = float ( n ) / prefix [ s ] <EOL> return '<STR_LIT>' % ( value , s ) <EOL> return '<STR_LIT>' % ( n ) <EOL> def poll ( interval ) : <EOL> """<STR_LIT>""" <EOL> procs = [ p for p in psutil . process_iter ( ) ] <EOL> for p in procs [ : ] : <EOL> try : <EOL> p . _before = p . get_io_counters ( ) <EOL> except psutil . Error : <EOL> procs . remove ( p ) <EOL> continue <EOL> disks_before = psutil . disk_io_counters ( ) <EOL> time . sleep ( interval ) <EOL> for p in procs [ : ] : <EOL> try : <EOL> p . _after = p . get_io_counters ( ) <EOL> p . _cmdline = '<STR_LIT:U+0020>' . join ( p . cmdline ) <EOL> if not p . _cmdline : <EOL> p . _cmdline = p . name <EOL> p . _username = p . username <EOL> except psutil . NoSuchProcess : <EOL> procs . remove ( p ) <EOL> disks_after = psutil . disk_io_counters ( ) <EOL> for p in procs : <EOL> p . _read_per_sec = p . _after . read_bytes - p . _before . read_bytes <EOL> p . _write_per_sec = p . _after . write_bytes - p . _before . write_bytes <EOL> p . _total = p . _read_per_sec + p . _write_per_sec <EOL> disks_read_per_sec = disks_after . read_bytes - disks_before . read_bytes <EOL> disks_write_per_sec = disks_after . write_bytes - disks_before . write_bytes <EOL> processes = sorted ( procs , key = lambda p : p . _total , reverse = True ) <EOL> return ( processes , disks_read_per_sec , disks_write_per_sec ) <EOL> def refresh_window ( procs , disks_read , disks_write ) : <EOL> """<STR_LIT>""" <EOL> curses . endwin ( ) <EOL> templ = "<STR_LIT>" <EOL> win . erase ( ) <EOL> disks_tot = "<STR_LIT>" % ( bytes2human ( disks_read ) , bytes2human ( disks_write ) ) <EOL> print_line ( disks_tot ) <EOL> header = templ % ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> print_line ( header , highlight = True ) <EOL> for p in procs : <EOL> line = templ % ( p . pid , <EOL> p . _username [ : <NUM_LIT:7> ] , <EOL> bytes2human ( p . _read_per_sec ) , <EOL> bytes2human ( p . _write_per_sec ) , <EOL> p . _cmdline ) <EOL> try : <EOL> print_line ( line ) <EOL> except curses . error : <EOL> break <EOL> win . refresh ( ) <EOL> def main ( ) : <EOL> try : <EOL> interval = <NUM_LIT:0> <EOL> while <NUM_LIT:1> : <EOL> args = poll ( interval ) <EOL> refresh_window ( * args ) <EOL> interval = <NUM_LIT:1> <EOL> except ( KeyboardInterrupt , SystemExit ) : <EOL> pass <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
