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 4f46184
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 50 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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

-
- Refactor setup scripts by [@XuehaiPan](https://github.com/XuehaiPan).

### Fixed

Expand Down
6 changes: 3 additions & 3 deletions nvitop-exporter/nvitop_exporter/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,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,7 +43,7 @@
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
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
6 changes: 3 additions & 3 deletions nvitop/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,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,7 +43,7 @@
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
Expand Down
67 changes: 47 additions & 20 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,77 @@

"""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__,
extras_require={
'lint': [
'black >= 22.6.0',
'black >= 24.0.0, < 25.0.0a0',
'isort',
'pylint[spelling] >= 2.16.0',
'pylint[spelling]',
'mypy',
'typing-extensions',
'pre-commit',
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 4f46184

Please sign in to comment.