Skip to content

Commit

Permalink
refactor(setup): refactor setup scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
XuehaiPan committed Jul 4, 2024
1 parent 4833f20 commit f0c5129
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 49 deletions.
6 changes: 3 additions & 3 deletions .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.4.7
rev: v0.5.0
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand All @@ -38,12 +38,12 @@ repos:
hooks:
- id: black
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.2
rev: v3.16.0
hooks:
- id: pyupgrade
args: [--py37-plus] # sync with requires-python
- repo: https://github.com/pycqa/flake8
rev: 7.0.0
rev: 7.1.0
hooks:
- id: flake8
additional_dependencies:
Expand Down
8 changes: 5 additions & 3 deletions nvitop-exporter/nvitop_exporter/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"""Prometheus exporter built on top of ``nvitop``."""

__version__ = '1.3.2'
__version_tuple__ = tuple(map(int, __version__.split('.')))
__license__ = 'Apache-2.0'
__author__ = __maintainer__ = 'Xuehai Pan'
__email__ = '[email protected]'
Expand All @@ -28,8 +29,8 @@

try:
prefix, sep, suffix = (
subprocess.check_output(
['git', 'describe', '--abbrev=7'], # noqa: S603,S607
subprocess.check_output( # noqa: S603
['git', 'describe', '--abbrev=7'], # noqa: S607
cwd=os.path.dirname(os.path.abspath(__file__)),
stderr=subprocess.DEVNULL,
text=True,
Expand All @@ -43,10 +44,11 @@
if sep:
version_prefix, dot, version_tail = prefix.rpartition('.')
prefix = f'{version_prefix}{dot}{int(version_tail) + 1}'
__version__ = sep.join((prefix, suffix))
__version__ = f'{prefix}{sep}{suffix}'
del version_prefix, dot, version_tail
else:
__version__ = prefix
__version_tuple__ = tuple(map(int, prefix.split('.')))
del prefix, sep, suffix
except (OSError, subprocess.CalledProcessError):
pass
Expand Down
63 changes: 45 additions & 18 deletions nvitop-exporter/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,70 @@

"""Setup script for ``nvitop-exporter``."""

import pathlib
from __future__ import annotations

import contextlib
import re
import sys
from importlib.util import module_from_spec, spec_from_file_location
from pathlib import Path
from typing import TYPE_CHECKING, Generator

from setuptools import setup


HERE = pathlib.Path(__file__).absolute().parent
VERSION_FILE = HERE / 'nvitop_exporter' / 'version.py'
if TYPE_CHECKING:
from types import ModuleType


HERE = Path(__file__).absolute().parent

sys.path.insert(0, str(VERSION_FILE.parent))
# pylint: disable-next=import-error,wrong-import-position
import version # noqa

@contextlib.contextmanager
def vcs_version(name: str, path: Path | str) -> Generator[ModuleType, None, None]:
"""Context manager to update version string in a version module."""
path = Path(path).absolute()
assert path.is_file()
module_spec = spec_from_file_location(name=name, location=path)
assert module_spec is not None
assert module_spec.loader is not None
module = sys.modules.get(name)
if module is None:
module = module_from_spec(module_spec)
sys.modules[name] = module
module_spec.loader.exec_module(module)

VERSION_CONTENT = None
if module.__release__:
yield module
return

try:
if not version.__release__:
content = None
try:
try:
VERSION_CONTENT = VERSION_FILE.read_text(encoding='utf-8')
VERSION_FILE.write_text(
content = path.read_text(encoding='utf-8')
path.write_text(
data=re.sub(
r"""__version__\s*=\s*('[^']+'|"[^"]+")""",
f'__version__ = {version.__version__!r}',
string=VERSION_CONTENT,
f'__version__ = {module.__version__!r}',
string=content,
),
encoding='utf-8',
)
except OSError:
VERSION_CONTENT = None
content = None

yield module
finally:
if content is not None:
with path.open(mode='wt', encoding='utf-8', newline='') as file:
file.write(content)


with vcs_version(
name='nvitop_exporter.version',
path=(HERE / 'nvitop_exporter' / 'version.py'),
) as version:
setup(
name='nvitop-exporter',
version=version.__version__,
)
finally:
if VERSION_CONTENT is not None:
with VERSION_FILE.open(mode='wt', encoding='utf-8', newline='') as file:
file.write(VERSION_CONTENT)
4 changes: 2 additions & 2 deletions nvitop/api/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -3126,8 +3126,8 @@ def _parse_cuda_visible_devices( # pylint: disable=too-many-branches,too-many-s

try:
raw_uuids = (
subprocess.check_output(
[ # noqa: S603
subprocess.check_output( # noqa: S603
[
sys.executable,
'-c',
textwrap.dedent(
Expand Down
5 changes: 3 additions & 2 deletions nvitop/api/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def command_join(cmdline: list[str]) -> str:

_RAISE = object()
_USE_FALLBACK_WHEN_RAISE = threading.local() # see also `GpuProcess.failsafe`
_USE_FALLBACK_WHEN_RAISE.value = False


def auto_garbage_clean(
Expand Down Expand Up @@ -131,7 +132,7 @@ def wrapped(self: GpuProcess, *args: Any, **kwargs: Any) -> Any:
except KeyError:
pass
# See also `GpuProcess.failsafe`
if fallback is _RAISE or not getattr(_USE_FALLBACK_WHEN_RAISE, 'value', False):
if fallback is _RAISE or not _USE_FALLBACK_WHEN_RAISE.value:
raise
if isinstance(fallback, tuple):
if isinstance(ex, host.AccessDenied) and fallback == ('No Such Process',):
Expand Down Expand Up @@ -1049,7 +1050,7 @@ def failsafe(cls) -> Generator[None, None, None]:
""" # pylint: disable=line-too-long
global _USE_FALLBACK_WHEN_RAISE # pylint: disable=global-statement,global-variable-not-assigned

prev_value = getattr(_USE_FALLBACK_WHEN_RAISE, 'value', False)
prev_value = _USE_FALLBACK_WHEN_RAISE.value
try:
_USE_FALLBACK_WHEN_RAISE.value = True
yield
Expand Down
8 changes: 5 additions & 3 deletions nvitop/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"""An interactive NVIDIA-GPU process viewer and beyond, the one-stop solution for GPU process management."""

__version__ = '1.3.2'
__version_tuple__ = tuple(map(int, __version__.split('.')))
__license__ = 'GPL-3.0-only AND Apache-2.0'
__author__ = __maintainer__ = 'Xuehai Pan'
__email__ = '[email protected]'
Expand All @@ -28,8 +29,8 @@

try:
prefix, sep, suffix = (
subprocess.check_output(
['git', 'describe', '--abbrev=7'], # noqa: S603,S607
subprocess.check_output( # noqa: S603
['git', 'describe', '--abbrev=7'], # noqa: S607
cwd=os.path.dirname(os.path.abspath(__file__)),
stderr=subprocess.DEVNULL,
text=True,
Expand All @@ -43,10 +44,11 @@
if sep:
version_prefix, dot, version_tail = prefix.rpartition('.')
prefix = f'{version_prefix}{dot}{int(version_tail) + 1}'
__version__ = sep.join((prefix, suffix))
__version__ = f'{prefix}{sep}{suffix}'
del version_prefix, dot, version_tail
else:
__version__ = prefix
__version_tuple__ = tuple(map(int, prefix.split('.')))
del prefix, sep, suffix
except (OSError, subprocess.CalledProcessError):
pass
Expand Down
63 changes: 45 additions & 18 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,69 @@

"""Setup script for ``nvitop``."""

import pathlib
from __future__ import annotations

import contextlib
import re
import sys
from importlib.util import module_from_spec, spec_from_file_location
from pathlib import Path
from typing import TYPE_CHECKING, Generator

from setuptools import setup


HERE = pathlib.Path(__file__).absolute().parent
VERSION_FILE = HERE / 'nvitop' / 'version.py'
if TYPE_CHECKING:
from types import ModuleType


HERE = Path(__file__).absolute().parent

sys.path.insert(0, str(VERSION_FILE.parent))
# pylint: disable-next=import-error,wrong-import-position
import version # noqa

@contextlib.contextmanager
def vcs_version(name: str, path: Path | str) -> Generator[ModuleType, None, None]:
"""Context manager to update version string in a version module."""
path = Path(path).absolute()
assert path.is_file()
module_spec = spec_from_file_location(name=name, location=path)
assert module_spec is not None
assert module_spec.loader is not None
module = sys.modules.get(name)
if module is None:
module = module_from_spec(module_spec)
sys.modules[name] = module
module_spec.loader.exec_module(module)

VERSION_CONTENT = None
if module.__release__:
yield module
return

try:
if not version.__release__:
content = None
try:
try:
VERSION_CONTENT = VERSION_FILE.read_text(encoding='utf-8')
VERSION_FILE.write_text(
content = path.read_text(encoding='utf-8')
path.write_text(
data=re.sub(
r"""__version__\s*=\s*('[^']+'|"[^"]+")""",
f'__version__ = {version.__version__!r}',
string=VERSION_CONTENT,
f'__version__ = {module.__version__!r}',
string=content,
),
encoding='utf-8',
)
except OSError:
VERSION_CONTENT = None
content = None

yield module
finally:
if content is not None:
with path.open(mode='wt', encoding='utf-8', newline='') as file:
file.write(content)


with vcs_version(
name='nvitop.version',
path=HERE / 'nvitop' / 'version.py',
) as version:
setup(
name='nvitop',
version=version.__version__,
Expand All @@ -63,7 +94,3 @@
},
},
)
finally:
if VERSION_CONTENT is not None:
with VERSION_FILE.open(mode='wt', encoding='utf-8', newline='') as file:
file.write(VERSION_CONTENT)

0 comments on commit f0c5129

Please sign in to comment.