diff options
author | Ronny Pfannschmidt <opensource@ronnypfannschmidt.de> | 2023-03-24 10:57:59 +0100 |
---|---|---|
committer | Ronny Pfannschmidt <opensource@ronnypfannschmidt.de> | 2023-03-29 17:04:12 +0200 |
commit | 71e839f1d7a74ce7f2f24d3661ef76524d2ed814 (patch) | |
tree | d6553535a5afbecde68cd2a5e36bb66b41e50294 /src/setuptools_scm | |
parent | b9ed7bb7f36c2954a2861ea01adbb241bb49455b (diff) | |
download | setuptools-scm-71e839f1d7a74ce7f2f24d3661ef76524d2ed814.tar.gz |
remove the utils module
move command utils to _run_cmd
move data_from_mime to integration
Diffstat (limited to 'src/setuptools_scm')
-rw-r--r-- | src/setuptools_scm/_file_finders/git.py | 2 | ||||
-rw-r--r-- | src/setuptools_scm/_file_finders/hg.py | 2 | ||||
-rw-r--r-- | src/setuptools_scm/_run_cmd.py | 24 | ||||
-rw-r--r-- | src/setuptools_scm/fallbacks.py | 2 | ||||
-rw-r--r-- | src/setuptools_scm/git.py | 4 | ||||
-rw-r--r-- | src/setuptools_scm/hg.py | 5 | ||||
-rw-r--r-- | src/setuptools_scm/hg_git.py | 4 | ||||
-rw-r--r-- | src/setuptools_scm/integration.py | 13 | ||||
-rw-r--r-- | src/setuptools_scm/scm_workdir.py | 4 | ||||
-rw-r--r-- | src/setuptools_scm/utils.py | 51 |
10 files changed, 46 insertions, 65 deletions
diff --git a/src/setuptools_scm/_file_finders/git.py b/src/setuptools_scm/_file_finders/git.py index 917ed1e..873b4ba 100644 --- a/src/setuptools_scm/_file_finders/git.py +++ b/src/setuptools_scm/_file_finders/git.py @@ -10,7 +10,7 @@ from . import is_toplevel_acceptable from . import scm_find_files from .. import _types as _t from .._run_cmd import run as _run -from ..utils import data_from_mime +from ..integration import data_from_mime log = logging.getLogger(__name__) diff --git a/src/setuptools_scm/_file_finders/hg.py b/src/setuptools_scm/_file_finders/hg.py index e1aa1bd..ec8604a 100644 --- a/src/setuptools_scm/_file_finders/hg.py +++ b/src/setuptools_scm/_file_finders/hg.py @@ -8,7 +8,7 @@ from .. import _types as _t from .._file_finders import is_toplevel_acceptable from .._file_finders import scm_find_files from .._run_cmd import run as _run -from ..utils import data_from_mime +from ..integration import data_from_mime log = logging.getLogger(__name__) diff --git a/src/setuptools_scm/_run_cmd.py b/src/setuptools_scm/_run_cmd.py index 39898b0..3f66f56 100644 --- a/src/setuptools_scm/_run_cmd.py +++ b/src/setuptools_scm/_run_cmd.py @@ -4,7 +4,9 @@ import os import shlex import subprocess import textwrap +import warnings from typing import Mapping +from typing import Sequence from . import _log from . import _types as _t @@ -111,3 +113,25 @@ def _unsafe_quote_for_display(item: _t.PathT) -> str: # give better results than shlex.join in our cases text = os.fspath(item) return text if all(c not in text for c in " {[:") else f'"{text}"' + + +def has_command(name: str, args: Sequence[str] = ["help"], warn: bool = True) -> bool: + try: + p = run([name, *args], cwd=".", timeout=5) + except OSError as e: + log.warning("command %s missing: %s", name, e) + res = False + except subprocess.TimeoutExpired as e: + log.warning("command %s timed out %s", name, e) + res = False + + else: + res = not p.returncode + if not res and warn: + warnings.warn("%r was not found" % name, category=RuntimeWarning) + return res + + +def require_command(name: str) -> None: + if not has_command(name, warn=False): + raise OSError(f"{name!r} was not found") diff --git a/src/setuptools_scm/fallbacks.py b/src/setuptools_scm/fallbacks.py index 818c09d..e1ea60c 100644 --- a/src/setuptools_scm/fallbacks.py +++ b/src/setuptools_scm/fallbacks.py @@ -8,7 +8,7 @@ from typing import TYPE_CHECKING if TYPE_CHECKING: from . import _types as _t from . import Configuration -from .utils import data_from_mime +from .integration import data_from_mime from .version import meta from .version import ScmVersion from .version import tag_to_version diff --git a/src/setuptools_scm/git.py b/src/setuptools_scm/git.py index dc80fa3..e2a5a79 100644 --- a/src/setuptools_scm/git.py +++ b/src/setuptools_scm/git.py @@ -17,10 +17,10 @@ from typing import TYPE_CHECKING from . import _types as _t from . import Configuration +from ._run_cmd import require_command from ._run_cmd import run +from .integration import data_from_mime from .scm_workdir import Workdir -from .utils import data_from_mime -from .utils import require_command from .version import meta from .version import ScmVersion from .version import tag_to_version diff --git a/src/setuptools_scm/hg.py b/src/setuptools_scm/hg.py index a3b7d59..cb45e03 100644 --- a/src/setuptools_scm/hg.py +++ b/src/setuptools_scm/hg.py @@ -8,9 +8,8 @@ from typing import TYPE_CHECKING from . import Configuration from ._version_cls import Version +from .integration import data_from_mime from .scm_workdir import Workdir -from .utils import data_from_mime -from .utils import require_command from .version import meta from .version import ScmVersion from .version import tag_to_version @@ -18,7 +17,7 @@ from .version import tag_to_version if TYPE_CHECKING: from . import _types as _t -from ._run_cmd import run as _run +from ._run_cmd import run as _run, require_command log = logging.getLogger(__name__) diff --git a/src/setuptools_scm/hg_git.py b/src/setuptools_scm/hg_git.py index 884c290..f2ea360 100644 --- a/src/setuptools_scm/hg_git.py +++ b/src/setuptools_scm/hg_git.py @@ -8,10 +8,10 @@ from datetime import datetime from subprocess import CompletedProcess from . import _types as _t +from ._run_cmd import require_command from ._run_cmd import run as _run from .git import GitWorkdir from .hg import HgWorkdir -from .utils import require_command log = logging.getLogger(__name__) @@ -27,7 +27,7 @@ class GitWorkdirHgClient(GitWorkdir, HgWorkdir): @classmethod def from_potential_worktree(cls, wd: _t.PathT) -> GitWorkdirHgClient | None: - require_command(cls.COMMAND) + require_command("hg") res = _run(["hg", "root"], cwd=wd) if res.returncode: return None diff --git a/src/setuptools_scm/integration.py b/src/setuptools_scm/integration.py index 4c7cb25..9df4f3e 100644 --- a/src/setuptools_scm/integration.py +++ b/src/setuptools_scm/integration.py @@ -2,7 +2,9 @@ from __future__ import annotations import logging import os +import textwrap import warnings +from pathlib import Path from typing import Any from typing import Callable from typing import TYPE_CHECKING @@ -10,6 +12,7 @@ from typing import TYPE_CHECKING import setuptools from . import _get_version +from . import _types as _t from . import _version_missing from . import Configuration from ._integration.setuptools import ( @@ -119,3 +122,13 @@ def infer_version(dist: setuptools.Distribution) -> None: log.exception(e) else: _assign_version(dist, config) + + +def data_from_mime(path: _t.PathT) -> dict[str, str]: + content = Path(path).read_text(encoding="utf-8") + log.debug("mime %s content:\n%s", path, textwrap.indent(content, " ")) + # the complex conditions come from reading pseudo-mime-messages + data = dict(x.split(": ", 1) for x in content.splitlines() if ": " in x) + + log.debug("mime %s data:\n%s", path, data) + return data diff --git a/src/setuptools_scm/scm_workdir.py b/src/setuptools_scm/scm_workdir.py index 12935f1..8f6a1cd 100644 --- a/src/setuptools_scm/scm_workdir.py +++ b/src/setuptools_scm/scm_workdir.py @@ -1,19 +1,15 @@ from __future__ import annotations from pathlib import Path -from typing import ClassVar from typing import TYPE_CHECKING -from .utils import require_command if TYPE_CHECKING: from . import _types as _t class Workdir: - COMMAND: ClassVar[str] path: Path def __init__(self, path: _t.PathT): - require_command(self.COMMAND) self.path = Path(path) diff --git a/src/setuptools_scm/utils.py b/src/setuptools_scm/utils.py deleted file mode 100644 index bb313fc..0000000 --- a/src/setuptools_scm/utils.py +++ /dev/null @@ -1,51 +0,0 @@ -""" -utils -""" -from __future__ import annotations - -import logging -import subprocess -import textwrap -import warnings -from pathlib import Path -from typing import Sequence -from typing import TYPE_CHECKING - -from . import _run_cmd - -if TYPE_CHECKING: - from . import _types as _t - -log = logging.getLogger(__name__) - - -def data_from_mime(path: _t.PathT) -> dict[str, str]: - content = Path(path).read_text(encoding="utf-8") - log.debug("mime %s content:\n%s", path, textwrap.indent(content, " ")) - # the complex conditions come from reading pseudo-mime-messages - data = dict(x.split(": ", 1) for x in content.splitlines() if ": " in x) - - log.debug("mime %s data:\n%s", path, data) - return data - - -def has_command(name: str, args: Sequence[str] = ["help"], warn: bool = True) -> bool: - try: - p = _run_cmd.run([name, *args], cwd=".", timeout=5) - except OSError as e: - log.warning("command %s missing: %s", name, e) - res = False - except subprocess.TimeoutExpired as e: - log.warning("command %s timed out %s", name, e) - res = False - - else: - res = not p.returncode - if not res and warn: - warnings.warn("%r was not found" % name, category=RuntimeWarning) - return res - - -def require_command(name: str) -> None: - if not has_command(name, warn=False): - raise OSError(f"{name!r} was not found") |