Skip to content

Commit

Permalink
Check VCS root (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep authored Jul 13, 2021
1 parent f85f6be commit d0acbc2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
48 changes: 40 additions & 8 deletions get_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from enum import Enum
from functools import partial
from pathlib import Path
from subprocess import run
from textwrap import indent
from typing import Union, Optional, List

Expand Down Expand Up @@ -94,15 +95,17 @@ def get_version_from_dirname(parent: Path) -> Optional[str]:
return match["version"]


def dunamai_get_from_vcs(dir_: Path):
from dunamai import Version

with working_dir(dir_):
return Version.from_any_vcs(f"(?x)v?{RE_PEP440_VERSION.pattern}")


def get_version_from_vcs(parent: Path) -> Optional[str]:
parent = parent.resolve()
vcs_root = find_vcs_root(parent)
if vcs_root is None:
raise NoVersionFound(
Source.vcs, f"could not find VCS from directory “{parent}”."
)
if parent != vcs_root:
raise NoVersionFound(
Source.vcs, f"directory “{parent}” does not match VCS root “{vcs_root}”."
)
try:
version = dunamai_get_from_vcs(parent)
except (RuntimeError, ImportError) as e:
Expand All @@ -113,6 +116,35 @@ def get_version_from_vcs(parent: Path) -> Optional[str]:
return version.serialize(dirty=not ON_RTD)


def find_vcs_root(start: Path) -> Optional[Path]:
from dunamai import _detect_vcs, Vcs

with working_dir(start):
try:
vcs = _detect_vcs()
except RuntimeError:
return None
if vcs is Vcs.Git:
cmd = ["git", "rev-parse", "--show-toplevel"]
elif vcs is Vcs.Mercurial:
cmd = ["hg", "root"]
else:
raise NotImplementedError(
f"Please file a feature request to implement support for {vcs.value}."
)
ret = run(cmd, cwd=start, capture_output=True)
if ret.returncode != 0:
return None # Swallow stderr. Maybe we should logging.debug() it instead?
return Path(os.fsdecode(ret.stdout.rstrip(b"\n")))


def dunamai_get_from_vcs(dir_: Path):
from dunamai import Version

with working_dir(dir_):
return Version.from_any_vcs(f"(?x)v?{RE_PEP440_VERSION.pattern}")


def get_version_from_metadata(
name: str, parent: Optional[Path] = None
) -> Optional[str]:
Expand Down Expand Up @@ -143,7 +175,7 @@ def get_pkg_paths(pkg: Distribution) -> List[Path]:
mods = {
f.parts[0] if len(f.parts) > 1 else f.with_suffix("").name
for f in pkg.files
if f.suffix == ".py"
if f.suffix == ".py" or Path(pkg.locate_file(f)).is_symlink()
}
if not mods:
raise RuntimeError(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def test_metadata():
re.compile(
r"^No version found:\n"
r"- Directory name:.*mod_dev_dir” does not contain a valid version\.\n"
r"- VCS:.*mod_dev_dir.*Unable to detect version control system\.\n"
r"- VCS: could not find VCS from directory.*mod_dev_dir”\.\n"
r"- Package metadata: could not find distribution “mod”\.$"
),
),
Expand Down

0 comments on commit d0acbc2

Please sign in to comment.