Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): handle exceptions for function getpass.getuser() #130

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ repos:
- id: debug-statements
- id: double-quote-string-fixer
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.0
rev: v0.5.1
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

-
- Handle exceptions for function `getpass.getuser()` by [@XuehaiPan](https://github.com/XuehaiPan) in [#130](https://github.com/XuehaiPan/nvitop/pull/130). Issued by [@landgraf](https://github.com/landgraf).

### Changed

Expand Down
1 change: 1 addition & 0 deletions docs/source/spelling_wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,4 @@ pytorch
api
utils
GpuStatsLogger
hostname
22 changes: 21 additions & 1 deletion nvitop/api/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
from __future__ import annotations

import os as _os
import time as _time
from typing import Callable as _Callable

import psutil as _psutil
from psutil import * # noqa: F403 # pylint: disable=wildcard-import,unused-wildcard-import,redefined-builtin


__all__ = [name for name in _psutil.__all__ if not name.startswith('_')] + [
'getuser',
'hostname',
'load_average',
'uptime',
'memory_percent',
Expand Down Expand Up @@ -60,6 +61,23 @@
MACOS = _psutil.MACOS


def getuser() -> str:
"""Get the current username from the environment or password database."""
import getpass # pylint: disable=import-outside-toplevel

try:
return getpass.getuser()
except (ModuleNotFoundError, OSError):
return _os.getlogin()


def hostname() -> str:
"""Get the hostname of the machine."""
import platform # pylint: disable=import-outside-toplevel

return platform.node()


if hasattr(_psutil, 'getloadavg'):

def load_average() -> tuple[float, float, float]:
Expand All @@ -75,6 +93,8 @@ def load_average() -> None: # type: ignore[misc]

def uptime() -> float:
"""Get the system uptime."""
import time as _time # pylint: disable=import-outside-toplevel

return _time.time() - _psutil.boot_time()


Expand Down
29 changes: 13 additions & 16 deletions nvitop/gui/library/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

# pylint: disable=missing-module-docstring,missing-function-docstring

import getpass
import contextlib
import math
import os
import platform

from nvitop.api import NA, colored, host, set_color # noqa: F401 # pylint: disable=unused-import
from nvitop.gui.library.widestring import WideString
Expand Down Expand Up @@ -56,25 +55,23 @@ def make_bar(prefix, percent, width, *, extra_text=''):
return f'{bar} {text}'.ljust(width)


try:
USERNAME = getpass.getuser()
except ModuleNotFoundError:
USERNAME = os.getlogin()
USERNAME = 'N/A'
with contextlib.suppress(ImportError, OSError):
USERNAME = host.getuser()

if host.WINDOWS:
import ctypes
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 = bool(ctypes.windll.shell32.IsUserAnAdmin())
else:
try:
SUPERUSER = os.getuid() == 0
SUPERUSER = os.geteuid() == 0
except AttributeError:
SUPERUSER = False
SUPERUSER = os.getuid() == 0

HOSTNAME = platform.node()
HOSTNAME = host.hostname()
if host.WSL:
HOSTNAME = f'{HOSTNAME} (WSL)'

Expand Down
12 changes: 6 additions & 6 deletions nvitop/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@
from __future__ import annotations

import argparse
import getpass
import contextlib
import math
import os
import sys
import warnings
from typing import TYPE_CHECKING, Any, Callable, Iterable, Sequence, overload

from nvitop.api import Device, GpuProcess, Snapshot, colored, human2bytes, libnvml
from nvitop.api import Device, GpuProcess, Snapshot, colored, host, human2bytes, libnvml
from nvitop.version import __version__


Expand All @@ -74,10 +74,10 @@

__all__ = ['select_devices']

try:
USERNAME = getpass.getuser()
except ModuleNotFoundError:
USERNAME = os.getlogin()

USERNAME = 'N/A'
with contextlib.suppress(ImportError, OSError):
USERNAME = host.getuser()

TTY = sys.stdout.isatty()

Expand Down