|
23 | 23 | import psutil
|
24 | 24 | # wildcard import because the name is too long
|
25 | 25 | from pynvml import *
|
| 26 | +from blessings import Terminal |
26 | 27 |
|
27 | 28 | __version__ = '0.4.0.dev'
|
28 | 29 |
|
29 | 30 |
|
30 |
| -class ANSIColors: |
31 |
| - RESET = '\033[0m' |
32 |
| - WHITE = '\033[1m' |
33 |
| - RED = '\033[0;31m' |
34 |
| - GREEN = '\033[0;32m' |
35 |
| - YELLOW = '\033[0;33m' |
36 |
| - BLUE = '\033[0;34m' |
37 |
| - MAGENTA = '\033[0;35m' |
38 |
| - CYAN = '\033[0;36m' |
39 |
| - GRAY = '\033[1;30m' |
40 |
| - BOLD_RED = '\033[1;31m' |
41 |
| - BOLD_GREEN = '\033[1;32m' |
42 |
| - BOLD_YELLOW = '\033[1;33m' |
43 |
| - |
44 | 31 | NOT_SUPPPORTED = 'Not Supported'
|
45 | 32 |
|
46 |
| -@staticmethod |
47 |
| -def wrap(color, msg): |
48 |
| - return (color + msg + ANSIColors.RESET) |
49 |
| - |
| 33 | +term = Terminal() |
50 | 34 |
|
51 | 35 | def execute_process(command_shell):
|
52 | 36 | stdout = check_output(command_shell, shell=True).strip()
|
@@ -142,24 +126,24 @@ def print_to(self, fp,
|
142 | 126 | # color settings
|
143 | 127 | colors = {}
|
144 | 128 | def _conditional(cond_fn, true_value, false_value,
|
145 |
| - error_value=ANSIColors.GRAY): |
| 129 | + error_value=term.gray): |
146 | 130 | try:
|
147 | 131 | if cond_fn(): return true_value
|
148 | 132 | else: return false_value
|
149 | 133 | except:
|
150 | 134 | return error_value
|
151 | 135 |
|
152 |
| - colors['C0'] = ANSIColors.RESET |
153 |
| - colors['C1'] = ANSIColors.CYAN |
154 |
| - colors['CName'] = ANSIColors.BLUE |
| 136 | + colors['C0'] = term.normal |
| 137 | + colors['C1'] = term.cyan |
| 138 | + colors['CName'] = term.blue |
155 | 139 | colors['CTemp'] = _conditional(lambda: int(self.entry['temperature.gpu']) < 50,
|
156 |
| - ANSIColors.RED, ANSIColors.BOLD_RED) |
157 |
| - colors['CMemU'] = ANSIColors.BOLD_YELLOW |
158 |
| - colors['CMemT'] = ANSIColors.YELLOW |
159 |
| - colors['CMemP'] = ANSIColors.YELLOW |
160 |
| - colors['CUser'] = ANSIColors.GRAY |
| 140 | + term.red, term.bold_red) |
| 141 | + colors['CMemU'] = term.bold_yellow |
| 142 | + colors['CMemT'] = term.yellow |
| 143 | + colors['CMemP'] = term.yellow |
| 144 | + colors['CUser'] = term.gray |
161 | 145 | colors['CUtil'] = _conditional(lambda: int(self.entry['utilization.gpu']) < 30,
|
162 |
| - ANSIColors.GREEN, ANSIColors.BOLD_GREEN) |
| 146 | + term.green, term.bold_green) |
163 | 147 |
|
164 | 148 | if not with_colors:
|
165 | 149 | for k in list(colors.keys()):
|
@@ -312,11 +296,11 @@ def print_formatted(self, fp=sys.stdout, no_color=False,
|
312 | 296 | ):
|
313 | 297 | # header
|
314 | 298 | time_format = locale.nl_langinfo(locale.D_T_FMT)
|
315 |
| - header_msg = '%(WHITE)s{hostname}%(RESET)s {timestr}'.format(**{ |
| 299 | + header_msg = '{t.white}{hostname}{t.normal} {timestr}'.format(**{ |
316 | 300 | 'hostname' : self.hostname,
|
317 |
| - 'timestr' : self.query_time.strftime(time_format) |
318 |
| - |
319 |
| - }) % (defaultdict(str) if no_color else ANSIColors.__dict__) |
| 301 | + 'timestr' : self.query_time.strftime(time_format), |
| 302 | + 't': term, |
| 303 | + }) |
320 | 304 |
|
321 | 305 | print(header_msg)
|
322 | 306 |
|
@@ -354,27 +338,6 @@ def date_handler(obj):
|
354 | 338 | fp.flush()
|
355 | 339 |
|
356 | 340 |
|
357 |
| -def self_test(): |
358 |
| - gpu_stats = GPUStatCollection.new_query() |
359 |
| - print('# of GPUS:', len(gpu_stats)) |
360 |
| - for g in gpu_stats: |
361 |
| - print(g) |
362 |
| - |
363 |
| - process_entries = GPUStatCollection.running_processes() |
364 |
| - print('---Entries---') |
365 |
| - print(process_entries) |
366 |
| - |
367 |
| - print('-------------') |
368 |
| - |
369 |
| - |
370 |
| -def new_query(): |
371 |
| - ''' |
372 |
| - Obtain a new GPUStatCollection instance by querying nvidia-smi |
373 |
| - to get the list of GPUs and running process information. |
374 |
| - ''' |
375 |
| - return GPUStatCollection.new_query() |
376 |
| - |
377 |
| - |
378 | 341 | def print_gpustat(json=False, debug=False, **args):
|
379 | 342 | '''
|
380 | 343 | Display the GPU query results into standard output.
|
|
0 commit comments