summaryrefslogtreecommitdiff
path: root/src/setuptools_scm
diff options
context:
space:
mode:
authorRonny Pfannschmidt <opensource@ronnypfannschmidt.de>2023-03-09 20:00:27 +0100
committerRonny Pfannschmidt <opensource@ronnypfannschmidt.de>2023-03-11 22:06:31 +0100
commit594964b3b21ef78fc7c6faaadfffecf553c25b08 (patch)
treeacce44893cc788511052d1e2df6d48795c6711ea /src/setuptools_scm
parent7d851ecb653d0d8cac3aa332b7a43b6e786d4c79 (diff)
downloadsetuptools-scm-594964b3b21ef78fc7c6faaadfffecf553c25b08.tar.gz
refactor: command running - migrate more code to it
Diffstat (limited to 'src/setuptools_scm')
-rw-r--r--src/setuptools_scm/__init__.py2
-rw-r--r--src/setuptools_scm/_run_cmd.py3
-rw-r--r--src/setuptools_scm/hg.py23
3 files changed, 15 insertions, 13 deletions
diff --git a/src/setuptools_scm/__init__.py b/src/setuptools_scm/__init__.py
index e15eeb1..eccc68f 100644
--- a/src/setuptools_scm/__init__.py
+++ b/src/setuptools_scm/__init__.py
@@ -106,7 +106,7 @@ def _version_missing(config: Configuration) -> NoReturn:
def get_version(
- root: str = ".",
+ root: _t.PathT = ".",
version_scheme: _t.VERSION_SCHEME = DEFAULT_VERSION_SCHEME,
local_scheme: _t.VERSION_SCHEME = DEFAULT_LOCAL_SCHEME,
write_to: _t.PathT | None = None,
diff --git a/src/setuptools_scm/_run_cmd.py b/src/setuptools_scm/_run_cmd.py
index 14fda9c..bcefb1f 100644
--- a/src/setuptools_scm/_run_cmd.py
+++ b/src/setuptools_scm/_run_cmd.py
@@ -65,6 +65,7 @@ def run(
strip: bool = True,
trace: bool = True,
timeout: int = 20,
+ check: bool = False,
) -> subprocess.CompletedProcess[str]:
if isinstance(cmd, str):
cmd = shlex.split(cmd)
@@ -96,4 +97,6 @@ def run(
_trace.trace("err:\n", res.stderr, indent=True)
if res.returncode:
_trace.trace("ret:", res.returncode)
+ if check:
+ res.check_returncode()
return res
diff --git a/src/setuptools_scm/hg.py b/src/setuptools_scm/hg.py
index c325d0f..e7295bc 100644
--- a/src/setuptools_scm/hg.py
+++ b/src/setuptools_scm/hg.py
@@ -10,7 +10,6 @@ from ._trace import trace
from ._version_cls import Version
from .scm_workdir import Workdir
from .utils import data_from_mime
-from .utils import do_ex
from .utils import require_command
from .version import meta
from .version import ScmVersion
@@ -19,6 +18,8 @@ from .version import tag_to_version
if TYPE_CHECKING:
from . import _types as _t
+from ._run_cmd import run as _run
+
class HgWorkdir(Workdir):
COMMAND = "hg"
@@ -26,10 +27,10 @@ class HgWorkdir(Workdir):
@classmethod
def from_potential_worktree(cls, wd: _t.PathT) -> HgWorkdir | None:
require_command(cls.COMMAND)
- root, err, ret = do_ex("hg root", wd)
- if ret:
+ res = _run("hg root", wd)
+ if res.returncode:
return None
- return cls(root)
+ return cls(res.stdout)
def get_meta(self, config: Configuration) -> ScmVersion | None:
node: str
@@ -48,10 +49,7 @@ class HgWorkdir(Workdir):
["hg", "id", "-T", "{branch}\n{if(dirty, 1, 0)}\n{date|shortdate}"]
).split("\n")
dirty = bool(int(dirty_str))
- # todo: fromiso
- node_date = datetime.date(
- *map(int, (dirty_date if dirty else node_date_str).split("-"))
- )
+ node_date = datetime.date.fromisoformat(dirty_date if dirty else node_date_str)
if node.count("0") == len(node):
trace("initial node", self.path)
@@ -106,7 +104,8 @@ class HgWorkdir(Workdir):
def hg_log(self, revset: str, template: str) -> str:
cmd = ["hg", "log", "-r", revset, "-T", template]
- return self.do(cmd)
+
+ return _run(cmd, cwd=self.path, check=True).stdout
def get_latest_normalizable_tag(self) -> str | None:
# Gets all tags containing a '.' (see #229) from oldest to newest
@@ -141,9 +140,9 @@ class HgWorkdir(Workdir):
def parse(root: _t.PathT, config: Configuration) -> ScmVersion | None:
if os.path.exists(os.path.join(root, ".hg/git")):
- paths, _, ret = do_ex("hg path", root)
- if not ret:
- for line in paths.split("\n"):
+ res = _run(["hg", "path"], root)
+ if not res.returncode:
+ for line in res.stdout.split("\n"):
if line.startswith("default ="):
path = Path(line.split()[2])
if path.name.endswith(".git") or (path / ".git").exists():