Skip to content

Commit

Permalink
refactor: remove unnecessary global constants
Browse files Browse the repository at this point in the history
  • Loading branch information
XuehaiPan committed Jul 12, 2024
1 parent c00b80d commit 51f08cd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
46 changes: 23 additions & 23 deletions nvitop/gui/library/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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}'
12 changes: 3 additions & 9 deletions nvitop/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 51f08cd

Please sign in to comment.