Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally allow a cwd for get_distribution_version_string #25

Merged
merged 2 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Comment on lines +50 to +51
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I definitely had code which did this. Must have lost it when force pushing. Will fix on main branch and do another release.

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