From 51f08cd10675b3ba0e7c2cd65460610ab313c2a5 Mon Sep 17 00:00:00 2001 From: Xuehai Pan Date: Fri, 12 Jul 2024 17:08:59 +0000 Subject: [PATCH] refactor: remove unnecessary global constants --- nvitop/gui/library/utils.py | 46 ++++++++++++++++++------------------- nvitop/select.py | 12 +++------- 2 files changed, 26 insertions(+), 32 deletions(-) diff --git a/nvitop/gui/library/utils.py b/nvitop/gui/library/utils.py index b07ccf57..5ab64a70 100644 --- a/nvitop/gui/library/utils.py +++ b/nvitop/gui/library/utils.py @@ -11,6 +11,29 @@ from nvitop.gui.library.widestring import WideString +USERNAME = 'N/A' +with contextlib.suppress(ImportError, OSError): + USERNAME = host.getuser() + +SUPERUSER = False +with contextlib.suppress(AttributeError, OSError): + if host.WINDOWS: + import ctypes + + SUPERUSER = bool(ctypes.windll.shell32.IsUserAnAdmin()) + else: + try: + SUPERUSER = os.geteuid() == 0 + except AttributeError: + SUPERUSER = os.getuid() == 0 + +HOSTNAME = host.hostname() +if host.WSL: + HOSTNAME = f'{HOSTNAME} (WSL)' + +USERCONTEXT = f'{USERNAME}@{HOSTNAME}' + + LARGE_INTEGER = 65536 @@ -53,26 +76,3 @@ def make_bar(prefix, percent, width, *, extra_text=''): if extra_text and len(f'{bar} {text} {extra_text}') <= width: return f'{bar} {text}'.ljust(width - len(extra_text) - 1) + f' {extra_text}' return f'{bar} {text}'.ljust(width) - - -USERNAME = 'N/A' -with contextlib.suppress(ImportError, OSError): - USERNAME = host.getuser() - -SUPERUSER = False -with contextlib.suppress(AttributeError, OSError): - if host.WINDOWS: - import ctypes - - SUPERUSER = bool(ctypes.windll.shell32.IsUserAnAdmin()) - else: - try: - SUPERUSER = os.geteuid() == 0 - except AttributeError: - SUPERUSER = os.getuid() == 0 - -HOSTNAME = host.hostname() -if host.WSL: - HOSTNAME = f'{HOSTNAME} (WSL)' - -USERCONTEXT = f'{USERNAME}@{HOSTNAME}' diff --git a/nvitop/select.py b/nvitop/select.py index 902bf2a7..8d88516c 100644 --- a/nvitop/select.py +++ b/nvitop/select.py @@ -75,13 +75,6 @@ __all__ = ['select_devices'] -USERNAME = 'N/A' -with contextlib.suppress(ImportError, OSError): - USERNAME = host.getuser() - -TTY = sys.stdout.isatty() - - @overload def select_devices( # pylint: disable=too-many-arguments devices: Iterable[Device] | None, @@ -534,7 +527,8 @@ def non_negint(argstring: str) -> int: args.sep = '\0' if args.free_accounts is not None and len(args.free_accounts) == 0: - args.free_accounts.append(USERNAME) + with contextlib.suppress(ImportError, OSError): + args.free_accounts.append(host.getuser()) return args @@ -574,7 +568,7 @@ def main() -> int: identifiers = list(map(str, identifiers)) result = args.sep.join(identifiers) - if not TTY: + if not sys.stdout.isatty(): print('CUDA_VISIBLE_DEVICES="{}"'.format(','.join(identifiers)), file=sys.stderr) retval = 0