<s> '''<STR_LIT>''' <EOL> from __future__ import print_function <EOL> import os <EOL> import re <EOL> import sys <EOL> import stat <EOL> import pwd <EOL> import time <EOL> import pty <EOL> import errno <EOL> import subprocess <EOL> import multiprocessing <EOL> import json <EOL> from collections import defaultdict <EOL> from collections import namedtuple <EOL> from optparse import OptionParser <EOL> try : <EOL> import curses , _curses <EOL> except ImportError : <EOL> print ( "<STR_LIT>" , file = sys . stderr ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> def cmd_exists ( cmd ) : <EOL> try : <EOL> return subprocess . call ( [ "<STR_LIT>" , cmd ] , stdout = subprocess . PIPE , stderr = subprocess . PIPE ) == <NUM_LIT:0> <EOL> except OSError : <EOL> return False <EOL> HAS_LXC = cmd_exists ( '<STR_LIT>' ) <EOL> HAS_DOCKER = cmd_exists ( '<STR_LIT>' ) <EOL> HAS_OPENVZ = cmd_exists ( '<STR_LIT>' ) <EOL> regexp_ovz_container = re . compile ( '<STR_LIT>' ) <EOL> HIDE_EMPTY_CGROUP = True <EOL> CGROUP_MOUNTPOINTS = { } <EOL> CONFIGURATION = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : <NUM_LIT:1.0> , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : '<STR_LIT:/>' , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT:type>' : [ ] , <EOL> } <EOL> Column = namedtuple ( '<STR_LIT>' , [ '<STR_LIT:title>' , '<STR_LIT:width>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> COLUMNS = [ ] <EOL> COLUMNS_MANDATORY = [ '<STR_LIT:name>' ] <EOL> COLUMNS_AVAILABLE = { <EOL> '<STR_LIT>' : Column ( "<STR_LIT>" , <NUM_LIT:10> , '<STR_LIT:<>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT:type>' : Column ( "<STR_LIT>" , <NUM_LIT:10> , '<STR_LIT:<>' , '<STR_LIT>' , '<STR_LIT:type>' , '<STR_LIT:type>' ) , <EOL> '<STR_LIT>' : Column ( "<STR_LIT>" , <NUM_LIT:11> , '<STR_LIT:>>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : Column ( "<STR_LIT>" , <NUM_LIT> , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : Column ( "<STR_LIT>" , <NUM_LIT:5> , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : Column ( "<STR_LIT>" , <NUM_LIT:5> , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : Column ( "<STR_LIT>" , <NUM_LIT:10> , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : Column ( "<STR_LIT>" , <NUM_LIT> , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT:name>' : Column ( "<STR_LIT>" , '<STR_LIT>' , '<STR_LIT:<>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> } <EOL> DOCKER_PREFIXES = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> def strip_prefix ( prefix , text ) : <EOL> if text . startswith ( prefix ) : <EOL> return text [ len ( prefix ) : ] <EOL> return text <EOL> def docker_container_name ( container_id , default , cache = dict ( ) ) : <EOL> cached_name = cache . get ( container_id ) <EOL> if cached_name : <EOL> return cached_name <EOL> try : <EOL> sp = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' , container_id ] , <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . PIPE ) <EOL> except OSError : <EOL> cache [ container_id ] = default <EOL> return default <EOL> for _ in range ( <NUM_LIT:10> ) : <EOL> sp . poll ( ) <EOL> if sp . returncode is not None : <EOL> break <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> if sp . returncode is None : <EOL> try : <EOL> sp . kill ( ) <EOL> except OSError : <EOL> pass <EOL> elif sp . returncode == <NUM_LIT:0> : <EOL> stdout , _stderr = sp . communicate ( ) <EOL> try : <EOL> containers = json . loads ( stdout ) <EOL> if len ( containers ) == <NUM_LIT:1> : <EOL> container = containers [ <NUM_LIT:0> ] <EOL> container_name = container [ '<STR_LIT:Name>' ] . lstrip ( '<STR_LIT:/>' ) <EOL> name = '<STR_LIT>' + container_name <EOL> cache [ container_id ] = name <EOL> return name <EOL> except Exception : <EOL> pass <EOL> cache [ container_id ] = default <EOL> return default <EOL> def to_human ( num , suffix = '<STR_LIT:B>' ) : <EOL> num = int ( num ) <EOL> for unit in [ '<STR_LIT:U+0020>' , '<STR_LIT>' , '<STR_LIT:M>' , '<STR_LIT>' , '<STR_LIT:T>' , '<STR_LIT:P>' , '<STR_LIT:E>' , '<STR_LIT>' ] : <EOL> if abs ( num ) < <NUM_LIT> : <EOL> return "<STR_LIT>" . format ( num , unit , suffix ) <EOL> num /= <NUM_LIT> <EOL> return "<STR_LIT>" % ( num , '<STR_LIT:Y>' , suffix ) <EOL> def div ( num , by ) : <EOL> res = num / by <EOL> mod = num % by <EOL> return res , mod <EOL> def to_human_time ( seconds ) : <EOL> minutes , seconds = div ( seconds , <NUM_LIT> ) <EOL> hours , minutes = div ( minutes , <NUM_LIT> ) <EOL> days , hours = div ( hours , <NUM_LIT> ) <EOL> if days : <EOL> return '<STR_LIT>' % ( days , hours , minutes , seconds ) <EOL> else : <EOL> return '<STR_LIT>' % ( hours , minutes , seconds ) <EOL> def get_total_memory ( ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> with open ( '<STR_LIT>' ) as f : <EOL> content = f . read ( ) <EOL> except OSError : <EOL> content = '<STR_LIT>' <EOL> for line in content . split ( '<STR_LIT:\n>' ) : <EOL> fields = re . split ( '<STR_LIT>' , line ) <EOL> if fields [ <NUM_LIT:0> ] . strip ( ) == "<STR_LIT>" : <EOL> return int ( fields [ <NUM_LIT:1> ] ) * <NUM_LIT> <EOL> return - <NUM_LIT:1> <EOL> def run ( user , cmd , interactive = False ) : <EOL> '''<STR_LIT>''' <EOL> prefix = [ ] <EOL> cur_uid = os . getuid ( ) <EOL> try : <EOL> cur_user = pwd . getpwuid ( cur_uid ) . pw_name <EOL> except : <EOL> cur_user = cur_uid <EOL> if user != cur_user and user != - <NUM_LIT:2> : <EOL> if cur_uid == <NUM_LIT:0> : <EOL> prefix = [ '<STR_LIT>' , user ] <EOL> if user == '<STR_LIT:root>' : <EOL> prefix = [ '<STR_LIT>' ] <EOL> else : <EOL> prefix = [ '<STR_LIT>' , '<STR_LIT>' , user ] <EOL> if interactive : <EOL> curses . savetty ( ) <EOL> curses . nocbreak ( ) <EOL> curses . echo ( ) <EOL> curses . endwin ( ) <EOL> pty . spawn ( prefix + cmd ) <EOL> init_screen ( ) <EOL> curses . resetty ( ) <EOL> else : <EOL> with open ( '<STR_LIT>' , '<STR_LIT:w>' ) as dev_null : <EOL> subprocess . Popen ( <EOL> prefix + cmd , <EOL> stdout = dev_null , <EOL> stderr = dev_null , <EOL> close_fds = True , <EOL> ) <EOL> class Cgroup ( object ) : <EOL> def __init__ ( self , path , base_path ) : <EOL> self . path = path <EOL> self . base_path = base_path <EOL> @ property <EOL> def short_path ( self ) : <EOL> return self . path [ len ( self . base_path ) : ] or '<STR_LIT:/>' <EOL> @ property <EOL> def name ( self ) : <EOL> if HAS_DOCKER and self . type == '<STR_LIT>' : <EOL> container_id = self . short_path <EOL> for prefix in DOCKER_PREFIXES : <EOL> container_id = strip_prefix ( prefix , container_id ) <EOL> return docker_container_name ( container_id , default = self . short_path ) <EOL> return self . short_path <EOL> @ property <EOL> def owner ( self ) : <EOL> path = os . path . join ( self . base_path , self . path , '<STR_LIT>' ) <EOL> uid = os . stat ( path ) . st_uid <EOL> try : <EOL> return pwd . getpwuid ( uid ) . pw_name <EOL> except : <EOL> return uid <EOL> @ property <EOL> def type ( self ) : <EOL> path = self . short_path <EOL> if any ( path . startswith ( prefix ) for prefix in DOCKER_PREFIXES ) : <EOL> return '<STR_LIT>' <EOL> elif path . startswith ( '<STR_LIT>' ) : <EOL> return '<STR_LIT>' <EOL> elif path . startswith ( '<STR_LIT>' ) : <EOL> _ , parent , name = path . rsplit ( '<STR_LIT:/>' , <NUM_LIT:2> ) <EOL> if parent . endswith ( '<STR_LIT>' ) : <EOL> if os . path . isdir ( '<STR_LIT>' % ( self . owner , name ) ) : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT>' <EOL> elif path == '<STR_LIT>' or path == '<STR_LIT>' or path . startswith ( '<STR_LIT>' ) : <EOL> return '<STR_LIT>' <EOL> elif regexp_ovz_container . match ( path ) and path != '<STR_LIT>' and HAS_OPENVZ : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT:->' <EOL> def _coerce ( self , value ) : <EOL> try : <EOL> return int ( value ) <EOL> except : <EOL> pass <EOL> try : <EOL> return float ( value ) <EOL> except : <EOL> pass <EOL> return value <EOL> def __getitem__ ( self , name ) : <EOL> path = os . path . join ( self . base_path , self . path , name ) <EOL> with open ( path ) as f : <EOL> content = f . read ( ) . strip ( ) <EOL> if name == '<STR_LIT>' or '<STR_LIT:\n>' in content or '<STR_LIT:U+0020>' in content : <EOL> content = content . split ( '<STR_LIT:\n>' ) <EOL> if '<STR_LIT:U+0020>' in content [ <NUM_LIT:0> ] : <EOL> content = dict ( ( re . split ( '<STR_LIT>' , l , <NUM_LIT:1> ) for l in content if content ) ) <EOL> for k , v in content . items ( ) : <EOL> content [ k ] = self . _coerce ( v ) <EOL> else : <EOL> content = [ self . _coerce ( v ) for v in content if v ] <EOL> else : <EOL> content = self . _coerce ( content ) <EOL> return content <EOL> def cgroups ( base_path ) : <EOL> '''<STR_LIT>''' <EOL> for cgroup_path , dirs , files in os . walk ( base_path ) : <EOL> yield Cgroup ( cgroup_path , base_path ) <EOL> def init ( ) : <EOL> with open ( "<STR_LIT>" ) as f : <EOL> cgroups = f . read ( ) . strip ( ) <EOL> subsystems = [ ] <EOL> for cgroup in cgroups . split ( '<STR_LIT:\n>' ) : <EOL> if cgroup [ <NUM_LIT:0> ] == '<STR_LIT:#>' : continue <EOL> subsystems . append ( cgroup . split ( ) [ <NUM_LIT:0> ] ) <EOL> with open ( "<STR_LIT>" ) as f : <EOL> mounts = f . read ( ) . strip ( ) <EOL> for mount in mounts . split ( '<STR_LIT:\n>' ) : <EOL> mount = mount . split ( '<STR_LIT:U+0020>' ) <EOL> if mount [ <NUM_LIT:2> ] != "<STR_LIT>" : <EOL> continue <EOL> for arg in mount [ <NUM_LIT:3> ] . split ( '<STR_LIT:U+002C>' ) : <EOL> if arg in subsystems and arg not in CGROUP_MOUNTPOINTS : <EOL> CGROUP_MOUNTPOINTS [ arg ] = mount [ <NUM_LIT:1> ] <EOL> def collect_ensure_common ( data , cgroup ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT>' in data : <EOL> return <EOL> data [ '<STR_LIT>' ] = cgroup [ '<STR_LIT>' ] <EOL> data [ '<STR_LIT>' ] = cgroup . owner <EOL> data [ '<STR_LIT:type>' ] = cgroup . type <EOL> def get_user_beacounts ( ) : <EOL> '''<STR_LIT>''' <EOL> prefix = [ ] <EOL> if os . getuid ( ) != <NUM_LIT:0> : <EOL> prefix = [ '<STR_LIT>' ] <EOL> command = prefix + [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> output , err = subprocess . Popen ( command , shell = False , stdout = subprocess . PIPE ) . communicate ( ) <EOL> return output <EOL> def collect ( measures ) : <EOL> cur = defaultdict ( dict ) <EOL> prev = measures [ '<STR_LIT:data>' ] <EOL> if '<STR_LIT>' in CGROUP_MOUNTPOINTS : <EOL> for cgroup in cgroups ( CGROUP_MOUNTPOINTS [ '<STR_LIT>' ] ) : <EOL> collect_ensure_common ( cur [ cgroup . name ] , cgroup ) <EOL> cur [ cgroup . name ] [ '<STR_LIT>' ] = cgroup [ '<STR_LIT>' ] <EOL> cur [ cgroup . name ] [ '<STR_LIT>' ] = { '<STR_LIT:user>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:0> } <EOL> if cgroup . name in prev : <EOL> for key , value in cur [ cgroup . name ] [ '<STR_LIT>' ] . items ( ) : <EOL> cur [ cgroup . name ] [ '<STR_LIT>' ] [ key ] = value - prev [ cgroup . name ] [ '<STR_LIT>' ] [ key ] <EOL> if '<STR_LIT>' in CGROUP_MOUNTPOINTS : <EOL> for cgroup in cgroups ( CGROUP_MOUNTPOINTS [ '<STR_LIT>' ] ) : <EOL> collect_ensure_common ( cur [ cgroup . name ] , cgroup ) <EOL> try : <EOL> cur [ cgroup . name ] [ '<STR_LIT>' ] = cgroup [ '<STR_LIT>' ] <EOL> cur [ cgroup . name ] [ '<STR_LIT>' ] = { '<STR_LIT>' : <NUM_LIT:0> } <EOL> except IOError as e : <EOL> if e . errno == errno . ENOENT : <EOL> continue <EOL> raise <EOL> if cgroup . name in prev : <EOL> cur_val = cur [ cgroup . name ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> prev_val = prev [ cgroup . name ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> cur [ cgroup . name ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] = cur_val - prev_val <EOL> if '<STR_LIT>' in CGROUP_MOUNTPOINTS : <EOL> for cgroup in cgroups ( CGROUP_MOUNTPOINTS [ '<STR_LIT>' ] ) : <EOL> collect_ensure_common ( cur [ cgroup . name ] , cgroup ) <EOL> cur [ cgroup . name ] [ '<STR_LIT>' ] = cgroup [ '<STR_LIT>' ] <EOL> cur [ cgroup . name ] [ '<STR_LIT>' ] = min ( int ( cgroup [ '<STR_LIT>' ] ) , measures [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in CGROUP_MOUNTPOINTS : <EOL> for cgroup in cgroups ( CGROUP_MOUNTPOINTS [ '<STR_LIT>' ] ) : <EOL> if cgroup . name == "<STR_LIT:/>" : <EOL> continue <EOL> collect_ensure_common ( cur [ cgroup . name ] , cgroup ) <EOL> cur [ cgroup . name ] [ '<STR_LIT>' ] = cgroup [ '<STR_LIT>' ] <EOL> if HAS_OPENVZ : <EOL> user_beancounters = get_user_beacounts ( ) <EOL> for line in user_beancounters . split ( '<STR_LIT:\n>' ) : <EOL> if line == '<STR_LIT>' : <EOL> continue <EOL> line = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , line ) <EOL> splited_line = re . split ( '<STR_LIT>' , line ) <EOL> if len ( splited_line ) != <NUM_LIT:3> : <EOL> continue <EOL> ctid , privvmpages , limit = splited_line <EOL> ctid = '<STR_LIT:/>' + ctid <EOL> if '<STR_LIT>' not in cur [ ctid ] : <EOL> continue <EOL> privvmpages = int ( privvmpages ) <EOL> privvmpages = privvmpages * <NUM_LIT> <EOL> limit = int ( limit ) <EOL> limit = limit * <NUM_LIT> <EOL> cur [ ctid ] [ '<STR_LIT>' ] = privvmpages <EOL> cur [ ctid ] [ '<STR_LIT>' ] = min ( limit , measures [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> if not len ( cur ) : <EOL> raise KeyboardInterrupt ( ) <EOL> measures [ '<STR_LIT:data>' ] = cur <EOL> def built_statistics ( measures , conf ) : <EOL> prev_time = measures [ '<STR_LIT>' ] . get ( '<STR_LIT:time>' , - <NUM_LIT:1> ) <EOL> cur_time = time . time ( ) <EOL> time_delta = cur_time - prev_time <EOL> measures [ '<STR_LIT>' ] [ '<STR_LIT:time>' ] = cur_time <EOL> cpu_to_percent = measures [ '<STR_LIT>' ] [ '<STR_LIT>' ] * measures [ '<STR_LIT>' ] [ '<STR_LIT>' ] * time_delta <EOL> results = [ ] <EOL> for cgroup , data in measures [ '<STR_LIT:data>' ] . items ( ) : <EOL> cpu_usage = data . get ( '<STR_LIT>' , { } ) <EOL> line = { <EOL> '<STR_LIT>' : str ( data . get ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> '<STR_LIT:type>' : str ( data . get ( '<STR_LIT:type>' , '<STR_LIT>' ) ) , <EOL> '<STR_LIT>' : len ( data [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : data . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : data . get ( '<STR_LIT>' , <NUM_LIT:0> ) , <EOL> '<STR_LIT>' : data . get ( '<STR_LIT>' , measures [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : data . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , <NUM_LIT:0> ) + data . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT:user>' , <NUM_LIT:0> ) , <EOL> '<STR_LIT>' : cpu_usage . get ( '<STR_LIT>' , <NUM_LIT:0> ) / cpu_to_percent , <EOL> '<STR_LIT>' : cpu_usage . get ( '<STR_LIT:user>' , <NUM_LIT:0> ) / cpu_to_percent , <EOL> '<STR_LIT>' : data . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , <NUM_LIT:0> ) , <EOL> '<STR_LIT>' : cgroup , <EOL> } <EOL> line [ '<STR_LIT>' ] = line [ '<STR_LIT>' ] + line [ '<STR_LIT>' ] , <EOL> line [ '<STR_LIT>' ] = to_human_time ( line [ '<STR_LIT>' ] ) <EOL> line [ '<STR_LIT>' ] = line [ '<STR_LIT>' ] / line [ '<STR_LIT>' ] <EOL> line [ '<STR_LIT>' ] = "<STR_LIT>" . format ( to_human ( line [ '<STR_LIT>' ] ) , to_human ( line [ '<STR_LIT>' ] ) ) <EOL> line [ '<STR_LIT>' ] = "<STR_LIT>" . format ( line [ '<STR_LIT>' ] , line [ '<STR_LIT>' ] ) <EOL> line [ '<STR_LIT>' ] = to_human ( line [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> results . append ( line ) <EOL> return results <EOL> def render_tree ( results , tree , level = <NUM_LIT:0> , prefix = [ ] , node = '<STR_LIT:/>' ) : <EOL> if node not in tree : <EOL> return <EOL> for i , line in enumerate ( tree [ node ] ) : <EOL> cgroup = line [ '<STR_LIT>' ] <EOL> if i == len ( tree [ node ] ) - <NUM_LIT:1> : <EOL> line [ '<STR_LIT>' ] = prefix + [ curses . ACS_LLCORNER , curses . ACS_HLINE , '<STR_LIT:U+0020>' ] <EOL> _child_prefix = prefix + [ '<STR_LIT:U+0020>' , '<STR_LIT:U+0020>' , '<STR_LIT:U+0020>' ] <EOL> else : <EOL> line [ '<STR_LIT>' ] = prefix + [ curses . ACS_LTEE , curses . ACS_HLINE , '<STR_LIT:U+0020>' ] <EOL> _child_prefix = prefix + [ curses . ACS_VLINE , '<STR_LIT:U+0020>' , '<STR_LIT:U+0020>' ] <EOL> results . append ( line ) <EOL> if cgroup not in CONFIGURATION [ '<STR_LIT>' ] : <EOL> render_tree ( results , tree , level + <NUM_LIT:1> , _child_prefix , cgroup ) <EOL> else : <EOL> line [ '<STR_LIT>' ] [ - <NUM_LIT:2> ] = '<STR_LIT:+>' <EOL> def filter_tree ( tree , keep , node = '<STR_LIT:/>' ) : <EOL> '''<STR_LIT>''' <EOL> filtered = [ ] <EOL> for cgroup in tree . get ( node , [ ] ) : <EOL> if filter_tree ( tree , keep , cgroup [ '<STR_LIT>' ] ) : <EOL> filtered . append ( cgroup ) <EOL> elif cgroup [ '<STR_LIT:type>' ] in keep : <EOL> filtered . append ( cgroup ) <EOL> if filtered : <EOL> tree [ node ] = filtered <EOL> else : <EOL> tree . pop ( node , None ) <EOL> return bool ( filtered ) <EOL> def prepare_tree ( results ) : <EOL> '''<STR_LIT>''' <EOL> if not CONFIGURATION [ '<STR_LIT>' ] : <EOL> if not CONFIGURATION [ '<STR_LIT:type>' ] : <EOL> return results <EOL> return [ l for l in results if l [ '<STR_LIT:type>' ] in CONFIGURATION [ '<STR_LIT:type>' ] ] <EOL> tree = { } <EOL> rendered = [ ] <EOL> for line in results : <EOL> cgroup = line [ '<STR_LIT>' ] <EOL> parent = os . path . dirname ( cgroup ) <EOL> if parent == cgroup : <EOL> rendered . append ( line ) <EOL> continue <EOL> if parent not in tree : <EOL> tree [ parent ] = [ ] <EOL> tree [ parent ] . append ( line ) <EOL> if CONFIGURATION [ '<STR_LIT:type>' ] : <EOL> filter_tree ( tree , CONFIGURATION [ '<STR_LIT:type>' ] ) <EOL> render_tree ( rendered , tree ) <EOL> return rendered <EOL> def display ( scr , results , conf ) : <EOL> results = sorted ( results , key = lambda line : line . get ( conf [ '<STR_LIT>' ] , <NUM_LIT:0> ) , reverse = not conf [ '<STR_LIT>' ] ) <EOL> results = prepare_tree ( results ) <EOL> CONFIGURATION [ '<STR_LIT>' ] = [ cgroup [ '<STR_LIT>' ] for cgroup in results ] <EOL> if CONFIGURATION [ '<STR_LIT>' ] : <EOL> while True : <EOL> try : <EOL> i = CONFIGURATION [ '<STR_LIT>' ] . index ( CONFIGURATION [ '<STR_LIT>' ] ) <EOL> CONFIGURATION [ '<STR_LIT>' ] = i <EOL> break <EOL> except : <EOL> CONFIGURATION [ '<STR_LIT>' ] = os . path . dirname ( CONFIGURATION [ '<STR_LIT>' ] ) <EOL> else : <EOL> CONFIGURATION [ '<STR_LIT>' ] = min ( len ( results ) - <NUM_LIT:1> , CONFIGURATION [ '<STR_LIT>' ] ) <EOL> CONFIGURATION [ '<STR_LIT>' ] = CONFIGURATION [ '<STR_LIT>' ] [ CONFIGURATION [ '<STR_LIT>' ] ] <EOL> CONFIGURATION [ '<STR_LIT>' ] = results [ CONFIGURATION [ '<STR_LIT>' ] ] <EOL> height , width = scr . getmaxyx ( ) <EOL> list_height = height - <NUM_LIT:2> <EOL> max_offset = max ( <NUM_LIT:0> , len ( results ) - list_height ) <EOL> if CONFIGURATION [ '<STR_LIT>' ] < CONFIGURATION [ '<STR_LIT>' ] : <EOL> CONFIGURATION [ '<STR_LIT>' ] = CONFIGURATION [ '<STR_LIT>' ] <EOL> elif CONFIGURATION [ '<STR_LIT>' ] - CONFIGURATION [ '<STR_LIT>' ] > list_height - <NUM_LIT:1> : <EOL> CONFIGURATION [ '<STR_LIT>' ] = CONFIGURATION [ '<STR_LIT>' ] - list_height + <NUM_LIT:1> <EOL> elif CONFIGURATION [ '<STR_LIT>' ] > max_offset : <EOL> CONFIGURATION [ '<STR_LIT>' ] = max_offset <EOL> scr . clear ( ) <EOL> x = <NUM_LIT:0> <EOL> line_tpl = [ ] <EOL> scr . addstr ( <NUM_LIT:0> , <NUM_LIT:0> , '<STR_LIT:U+0020>' * width , curses . color_pair ( <NUM_LIT:1> ) ) <EOL> for col in COLUMNS : <EOL> title_fmt = '<STR_LIT>' % ( col . align , col . width ) <EOL> line_tpl . append ( col . col_fmt % ( col . width ) ) <EOL> color = <NUM_LIT:2> if col . col_sort == conf [ '<STR_LIT>' ] else <NUM_LIT:1> <EOL> try : <EOL> scr . addstr ( <NUM_LIT:0> , x , title_fmt . format ( col . title ) + '<STR_LIT:U+0020>' , curses . color_pair ( color ) ) <EOL> except : <EOL> break <EOL> if col . width : <EOL> x += col . width + <NUM_LIT:1> <EOL> lineno = <NUM_LIT:1> <EOL> for line in results [ CONFIGURATION [ '<STR_LIT>' ] : ] : <EOL> y = <NUM_LIT:0> <EOL> if lineno - <NUM_LIT:1> == CONFIGURATION [ '<STR_LIT>' ] - CONFIGURATION [ '<STR_LIT>' ] : <EOL> col_reg , col_tree = curses . color_pair ( <NUM_LIT:2> ) , curses . color_pair ( <NUM_LIT:2> ) <EOL> else : <EOL> col_reg , col_tree = colors = curses . color_pair ( <NUM_LIT:0> ) , curses . color_pair ( <NUM_LIT:4> ) <EOL> try : <EOL> scr . addstr ( lineno , <NUM_LIT:0> , '<STR_LIT:U+0020>' * width , col_reg ) <EOL> except _curses . error : <EOL> break <EOL> try : <EOL> for col in COLUMNS : <EOL> cell_tpl = col . col_fmt % ( col . width if col . width else <NUM_LIT:1> ) <EOL> data_point = line . get ( col . col_data , '<STR_LIT>' ) <EOL> if col . title == '<STR_LIT>' and CONFIGURATION [ '<STR_LIT>' ] : <EOL> data_point = os . path . basename ( data_point ) or '<STR_LIT>' <EOL> for c in line . get ( '<STR_LIT>' , [ ] ) : <EOL> scr . addch ( c , col_tree ) <EOL> y += <NUM_LIT:1> <EOL> scr . addstr ( lineno , y , cell_tpl . format ( data_point ) + '<STR_LIT:U+0020>' , col_reg ) <EOL> if col . width : <EOL> y += col . width + <NUM_LIT:1> <EOL> except _curses . error : <EOL> pass <EOL> lineno += <NUM_LIT:1> <EOL> else : <EOL> try : scr . addstr ( lineno , <NUM_LIT:0> , '<STR_LIT:U+0020>' * width ) <EOL> except _curses . error : pass <EOL> try : <EOL> color = curses . color_pair ( <NUM_LIT:2> ) <EOL> try : <EOL> scr . addstr ( height - <NUM_LIT:1> , <NUM_LIT:0> , '<STR_LIT:U+0020>' * ( width ) , color ) <EOL> except : <EOL> pass <EOL> selected = results [ CONFIGURATION [ '<STR_LIT>' ] ] <EOL> scr . addstr ( height - <NUM_LIT:1> , <NUM_LIT:0> , "<STR_LIT>" , color ) <EOL> scr . addch ( curses . ACS_VLINE , color ) <EOL> scr . addstr ( "<STR_LIT>" + ( '<STR_LIT>' if CONFIGURATION [ '<STR_LIT>' ] else '<STR_LIT>' ) , color ) <EOL> scr . addch ( curses . ACS_VLINE , color ) <EOL> scr . addstr ( "<STR_LIT>" + ( '<STR_LIT>' if CONFIGURATION [ '<STR_LIT>' ] else '<STR_LIT>' ) , color ) <EOL> scr . addch ( curses . ACS_VLINE , color ) <EOL> scr . addstr ( "<STR_LIT>" % ( '<STR_LIT:list>' if CONFIGURATION [ '<STR_LIT>' ] else '<STR_LIT>' ) , color ) <EOL> scr . addch ( curses . ACS_VLINE , color ) <EOL> if CONFIGURATION [ '<STR_LIT>' ] : <EOL> scr . addstr ( "<STR_LIT>" % ( '<STR_LIT>' if selected [ '<STR_LIT>' ] in CONFIGURATION [ '<STR_LIT>' ] else '<STR_LIT>' ) , color ) <EOL> scr . addch ( curses . ACS_VLINE , color ) <EOL> selected_type = selected [ '<STR_LIT:type>' ] <EOL> if selected_type == '<STR_LIT>' and HAS_DOCKER or selected_type in [ '<STR_LIT>' , '<STR_LIT>' ] and HAS_LXC or selected_type == '<STR_LIT>' and HAS_OPENVZ : <EOL> if selected_type == '<STR_LIT>' : <EOL> scr . addstr ( "<STR_LIT>" , color ) <EOL> else : <EOL> scr . addstr ( "<STR_LIT>" , color ) <EOL> scr . addch ( curses . ACS_VLINE , color ) <EOL> scr . addstr ( "<STR_LIT>" , color ) <EOL> except _curses . error : <EOL> pass <EOL> scr . refresh ( ) <EOL> def set_sort_col ( sort_by ) : <EOL> if CONFIGURATION [ '<STR_LIT>' ] == sort_by : <EOL> CONFIGURATION [ '<STR_LIT>' ] = not CONFIGURATION [ '<STR_LIT>' ] <EOL> else : <EOL> CONFIGURATION [ '<STR_LIT>' ] = sort_by <EOL> def on_keyboard ( c ) : <EOL> '''<STR_LIT>''' <EOL> if c == ord ( '<STR_LIT:q>' ) : <EOL> raise KeyboardInterrupt ( ) <EOL> elif c == ord ( '<STR_LIT:p>' ) : <EOL> CONFIGURATION [ '<STR_LIT>' ] = not CONFIGURATION [ '<STR_LIT>' ] <EOL> elif c == ord ( '<STR_LIT:f>' ) : <EOL> CONFIGURATION [ '<STR_LIT>' ] = not CONFIGURATION [ '<STR_LIT>' ] <EOL> return <NUM_LIT:2> <EOL> elif c == ord ( '<STR_LIT:+>' ) or c == ord ( '<STR_LIT:->' ) : <EOL> cgroup = CONFIGURATION [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if cgroup in CONFIGURATION [ '<STR_LIT>' ] : <EOL> CONFIGURATION [ '<STR_LIT>' ] . remove ( cgroup ) <EOL> else : <EOL> CONFIGURATION [ '<STR_LIT>' ] . append ( cgroup ) <EOL> return <NUM_LIT:2> <EOL> elif c == ord ( '<STR_LIT:a>' ) : <EOL> selected = CONFIGURATION [ '<STR_LIT>' ] <EOL> selected_name = os . path . basename ( selected [ '<STR_LIT>' ] ) <EOL> if selected [ '<STR_LIT:type>' ] == '<STR_LIT>' and HAS_DOCKER : <EOL> if selected_name . startswith ( '<STR_LIT>' ) : <EOL> selected_name = selected_name [ <NUM_LIT:7> : - <NUM_LIT:6> ] <EOL> run ( - <NUM_LIT:2> , [ '<STR_LIT>' , '<STR_LIT>' , selected_name ] , interactive = True ) <EOL> elif selected [ '<STR_LIT:type>' ] in [ '<STR_LIT>' , '<STR_LIT>' ] and HAS_LXC : <EOL> run ( selected [ '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT>' , selected_name , '<STR_LIT>' , '<STR_LIT>' ] , interactive = True ) <EOL> elif selected [ '<STR_LIT:type>' ] == '<STR_LIT>' and HAS_OPENVZ : <EOL> run ( selected [ '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT>' , selected_name ] , interactive = True ) <EOL> return <NUM_LIT:2> <EOL> elif c == ord ( '<STR_LIT:e>' ) : <EOL> selected = CONFIGURATION [ '<STR_LIT>' ] <EOL> selected_name = os . path . basename ( selected [ '<STR_LIT>' ] ) <EOL> if selected [ '<STR_LIT:type>' ] == '<STR_LIT>' and HAS_DOCKER : <EOL> if selected_name . startswith ( '<STR_LIT>' ) : <EOL> selected_name = selected_name [ <NUM_LIT:7> : - <NUM_LIT:6> ] <EOL> run ( - <NUM_LIT:2> , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , selected_name , '<STR_LIT>' ] , interactive = True ) <EOL> elif selected [ '<STR_LIT:type>' ] in [ '<STR_LIT>' , '<STR_LIT>' ] and HAS_LXC : <EOL> run ( selected [ '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT>' , selected_name , '<STR_LIT>' , '<STR_LIT>' ] , interactive = True ) <EOL> elif selected [ '<STR_LIT:type>' ] == '<STR_LIT>' and HAS_OPENVZ : <EOL> run ( selected [ '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT>' , selected_name ] , interactive = True ) <EOL> return <NUM_LIT:2> <EOL> elif c == ord ( '<STR_LIT:s>' ) : <EOL> selected = CONFIGURATION [ '<STR_LIT>' ] <EOL> selected_name = os . path . basename ( selected [ '<STR_LIT>' ] ) <EOL> if selected [ '<STR_LIT:type>' ] == '<STR_LIT>' and HAS_DOCKER : <EOL> if selected_name . startswith ( '<STR_LIT>' ) : <EOL> selected_name = selected_name [ <NUM_LIT:7> : - <NUM_LIT:6> ] <EOL> run ( - <NUM_LIT:2> , [ '<STR_LIT>' , '<STR_LIT>' , selected_name ] ) <EOL> elif selected [ '<STR_LIT:type>' ] in [ '<STR_LIT>' , '<STR_LIT>' ] and HAS_LXC : <EOL> run ( selected [ '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT>' , selected_name , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> elif selected [ '<STR_LIT:type>' ] == '<STR_LIT>' and HAS_OPENVZ : <EOL> run ( selected [ '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT>' , selected_name ] ) <EOL> return <NUM_LIT:1> <EOL> elif c == ord ( '<STR_LIT:c>' ) : <EOL> selected = CONFIGURATION [ '<STR_LIT>' ] <EOL> selected_name = os . path . basename ( selected [ '<STR_LIT>' ] ) <EOL> if selected [ '<STR_LIT:type>' ] == '<STR_LIT>' and HAS_OPENVZ : <EOL> run ( selected [ '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT>' , selected_name ] ) <EOL> return <NUM_LIT:1> <EOL> elif c == ord ( '<STR_LIT:k>' ) : <EOL> selected = CONFIGURATION [ '<STR_LIT>' ] <EOL> selected_name = os . path . basename ( selected [ '<STR_LIT>' ] ) <EOL> if selected [ '<STR_LIT:type>' ] == '<STR_LIT>' and HAS_DOCKER : <EOL> if selected_name . startswith ( '<STR_LIT>' ) : <EOL> selected_name = selected_name [ <NUM_LIT:7> : - <NUM_LIT:6> ] <EOL> run ( - <NUM_LIT:2> , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:0>' , selected_name ] ) <EOL> elif selected [ '<STR_LIT:type>' ] in [ '<STR_LIT>' , '<STR_LIT>' ] and HAS_LXC : <EOL> run ( selected [ '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , selected_name , '<STR_LIT>' ] ) <EOL> elif selected [ '<STR_LIT:type>' ] == '<STR_LIT>' and HAS_OPENVZ : <EOL> run ( selected [ '<STR_LIT>' ] , [ '<STR_LIT>' , '<STR_LIT>' , selected_name , '<STR_LIT>' ] ) <EOL> return <NUM_LIT:2> <EOL> elif c == <NUM_LIT> : <EOL> CONFIGURATION [ '<STR_LIT>' ] = not CONFIGURATION [ '<STR_LIT>' ] <EOL> return <NUM_LIT:2> <EOL> elif c == curses . KEY_DOWN : <EOL> if CONFIGURATION [ '<STR_LIT>' ] : <EOL> i = CONFIGURATION [ '<STR_LIT>' ] . index ( CONFIGURATION [ '<STR_LIT>' ] ) <EOL> else : <EOL> i = CONFIGURATION [ '<STR_LIT>' ] <EOL> i = min ( i + <NUM_LIT:1> , len ( CONFIGURATION [ '<STR_LIT>' ] ) - <NUM_LIT:1> ) <EOL> CONFIGURATION [ '<STR_LIT>' ] = i <EOL> CONFIGURATION [ '<STR_LIT>' ] = CONFIGURATION [ '<STR_LIT>' ] [ i ] <EOL> return <NUM_LIT:2> <EOL> elif c == curses . KEY_UP : <EOL> if CONFIGURATION [ '<STR_LIT>' ] : <EOL> i = CONFIGURATION [ '<STR_LIT>' ] . index ( CONFIGURATION [ '<STR_LIT>' ] ) <EOL> else : <EOL> i = CONFIGURATION [ '<STR_LIT>' ] <EOL> i = max ( i - <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> CONFIGURATION [ '<STR_LIT>' ] = i <EOL> CONFIGURATION [ '<STR_LIT>' ] = CONFIGURATION [ '<STR_LIT>' ] [ i ] <EOL> return <NUM_LIT:2> <EOL> return <NUM_LIT:1> <EOL> def on_mouse ( ) : <EOL> '''<STR_LIT>''' <EOL> _ , x , y , z , bstate = curses . getmouse ( ) <EOL> if bstate & curses . BUTTON1_CLICKED : <EOL> if y == <NUM_LIT:0> : <EOL> x_max = <NUM_LIT:0> <EOL> for col in COLUMNS : <EOL> if not col . width : <EOL> set_sort_col ( col . col_sort ) <EOL> elif x < x_max + col . width : <EOL> set_sort_col ( col . col_sort ) <EOL> else : <EOL> x_max += col . width + <NUM_LIT:1> <EOL> continue <EOL> return <NUM_LIT:2> <EOL> elif y <= len ( CONFIGURATION [ '<STR_LIT>' ] ) : <EOL> if CONFIGURATION [ '<STR_LIT>' ] : <EOL> CONFIGURATION [ '<STR_LIT>' ] = CONFIGURATION [ '<STR_LIT>' ] [ y - <NUM_LIT:1> ] <EOL> else : <EOL> CONFIGURATION [ '<STR_LIT>' ] = y - <NUM_LIT:1> <EOL> return <NUM_LIT:2> <EOL> return <NUM_LIT:1> <EOL> def on_resize ( ) : <EOL> '''<STR_LIT>''' <EOL> return <NUM_LIT:2> <EOL> def event_listener ( scr , timeout ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> scr . timeout ( timeout ) <EOL> c = scr . getch ( ) <EOL> if c == - <NUM_LIT:1> : <EOL> return <NUM_LIT:1> <EOL> elif c == curses . KEY_MOUSE : <EOL> return on_mouse ( ) <EOL> elif c == curses . KEY_RESIZE : <EOL> return on_resize ( ) <EOL> else : <EOL> return on_keyboard ( c ) <EOL> except _curses . error : <EOL> return <NUM_LIT:0> <EOL> def rebuild_columns ( ) : <EOL> del COLUMNS [ : ] <EOL> for col in CONFIGURATION [ '<STR_LIT>' ] + COLUMNS_MANDATORY : <EOL> COLUMNS . append ( COLUMNS_AVAILABLE [ col ] ) <EOL> def diagnose ( ) : <EOL> devnull = open ( os . devnull , '<STR_LIT:w>' ) <EOL> if os . path . isfile ( '<STR_LIT>' ) : <EOL> print ( """<STR_LIT>""" , file = sys . stderr ) <EOL> if cmd_exists ( '<STR_LIT>' ) : <EOL> print ( """<STR_LIT>""" , file = sys . stderr ) <EOL> devnull . close ( ) <EOL> def init_screen ( ) : <EOL> curses . start_color ( ) <EOL> curses . use_default_colors ( ) <EOL> curses . noecho ( ) <EOL> curses . cbreak ( ) <EOL> curses . mousemask ( curses . ALL_MOUSE_EVENTS ) <EOL> if hasattr ( curses , '<STR_LIT>' ) : <EOL> try : <EOL> curses . curs_set ( <NUM_LIT:0> ) <EOL> except : <EOL> pass <EOL> def main ( ) : <EOL> parser = OptionParser ( ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store_true>" , default = False , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:int>" , default = <NUM_LIT:1> , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:string>" , default = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:string>" , default = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:string>" , default = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> options , args = parser . parse_args ( ) <EOL> CONFIGURATION [ '<STR_LIT>' ] = options . tree <EOL> CONFIGURATION [ '<STR_LIT>' ] = float ( options . refresh ) <EOL> CONFIGURATION [ '<STR_LIT>' ] = [ ] <EOL> CONFIGURATION [ '<STR_LIT>' ] = options . fold or list ( ) <EOL> CONFIGURATION [ '<STR_LIT:type>' ] = options . type or list ( ) <EOL> if options . follow : <EOL> CONFIGURATION [ '<STR_LIT>' ] = options . follow <EOL> CONFIGURATION [ '<STR_LIT>' ] = True <EOL> for col in options . columns . split ( '<STR_LIT:U+002C>' ) : <EOL> col = col . strip ( ) <EOL> if col in COLUMNS_MANDATORY : <EOL> continue <EOL> if not col in COLUMNS_AVAILABLE : <EOL> print ( "<STR_LIT>" , col , file = sys . stderr ) <EOL> print ( __doc__ ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> CONFIGURATION [ '<STR_LIT>' ] . append ( col ) <EOL> rebuild_columns ( ) <EOL> if options . sort_col not in COLUMNS_AVAILABLE : <EOL> print ( "<STR_LIT>" , options . sort_col , file = sys . stderr ) <EOL> print ( __doc__ ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> CONFIGURATION [ '<STR_LIT>' ] = COLUMNS_AVAILABLE [ options . sort_col ] . col_sort <EOL> measures = { <EOL> '<STR_LIT:data>' : defaultdict ( dict ) , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : multiprocessing . cpu_count ( ) , <EOL> '<STR_LIT>' : get_total_memory ( ) , <EOL> '<STR_LIT>' : os . sysconf ( '<STR_LIT>' ) , <EOL> } <EOL> } <EOL> init ( ) <EOL> if not CGROUP_MOUNTPOINTS : <EOL> print ( "<STR_LIT>" , file = sys . stderr ) <EOL> diagnose ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> results = None <EOL> try : <EOL> stdscr = curses . initscr ( ) <EOL> init_screen ( ) <EOL> stdscr . keypad ( <NUM_LIT:1> ) <EOL> curses . init_pair ( <NUM_LIT:1> , curses . COLOR_BLACK , curses . COLOR_GREEN ) <EOL> curses . init_pair ( <NUM_LIT:2> , curses . COLOR_BLACK , curses . COLOR_CYAN ) <EOL> curses . init_pair ( <NUM_LIT:3> , curses . COLOR_WHITE , - <NUM_LIT:1> ) <EOL> curses . init_pair ( <NUM_LIT:4> , curses . COLOR_CYAN , - <NUM_LIT:1> ) <EOL> while True : <EOL> collect ( measures ) <EOL> results = built_statistics ( measures , CONFIGURATION ) <EOL> display ( stdscr , results , CONFIGURATION ) <EOL> sleep_start = time . time ( ) <EOL> while CONFIGURATION [ '<STR_LIT>' ] or time . time ( ) < sleep_start + CONFIGURATION [ '<STR_LIT>' ] : <EOL> if CONFIGURATION [ '<STR_LIT>' ] : <EOL> to_sleep = - <NUM_LIT:1> <EOL> else : <EOL> to_sleep = int ( ( sleep_start + CONFIGURATION [ '<STR_LIT>' ] - time . time ( ) ) * <NUM_LIT:1000> ) <EOL> ret = event_listener ( stdscr , to_sleep ) <EOL> if ret == <NUM_LIT:2> : <EOL> display ( stdscr , results , CONFIGURATION ) <EOL> except KeyboardInterrupt : <EOL> pass <EOL> finally : <EOL> curses . nocbreak ( ) <EOL> stdscr . keypad ( <NUM_LIT:0> ) <EOL> curses . echo ( ) <EOL> curses . endwin ( ) <EOL> if results is not None and len ( results ) < <NUM_LIT:2> : <EOL> print ( "<STR_LIT>" , file = sys . stderr ) <EOL> diagnose ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
