Skip to content

Commit

Permalink
Optionally allow a cwd for get_distribution_version_string (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
DMRobertson authored Jun 7, 2022
1 parent 163f768 commit 4b0a3e7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dev =
# for type checking
mypy == 0.910
# for linting
black == 21.9b0
black == 22.3.0
flake8 == 4.0.1
isort == 5.9.3

Expand Down
17 changes: 14 additions & 3 deletions src/matrix_common/versionstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import functools
import logging
import subprocess
from typing import Optional

try:
from importlib.metadata import distribution
Expand All @@ -27,7 +28,9 @@


@functools.lru_cache()
def get_distribution_version_string(distribution_name: str) -> str:
def get_distribution_version_string(
distribution_name: str, cwd: Optional[str] = None
) -> str:
"""Calculate a git-aware version string for a distribution package.
A "distribution package" is a thing that you can e.g. install and manage with pip.
Expand All @@ -44,6 +47,11 @@ def get_distribution_version_string(distribution_name: str) -> str:
Args:
distribution_name: The name of the distribution package to check the version of
cwd: if provided, the directory to run all git commands in. `cwd` may also be
the path to a file, in which case `os.path.dirname(cwd)` is used instead.
If omitted, the function will attempt to locate the distribution's source
on disk and use that location instead---but this fallback is not reliable.
Raises:
importlib.metadata.PackageNotFoundError if the given distribution name doesn't
exist.
Expand All @@ -54,8 +62,11 @@ def get_distribution_version_string(distribution_name: str) -> str:

dist = distribution(distribution_name)
version_string = dist.version
cwd = dist.locate_file(".")

if cwd is None:
# This used to work for Synapse, but seems to have broken between versions 1.56
# and 1.57. I suspect that the cause is a difference in the metadata generated
# by `setuptools` and `poetry-core` at package-install time.
cwd = dist.locate_file(".").__fspath__()
try:

def _run_git_command(prefix: str, *params: str) -> str:
Expand Down

0 comments on commit 4b0a3e7

Please sign in to comment.