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

improve Python package metadata finding when running outside of git repositories #26

Merged
merged 4 commits into from
Feb 16, 2025
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: 2 additions & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ jobs:
- run: python -m coverage run --append --branch --source . -m unittest -v test.test_version
env:
LOGGING_LEVEL: critical
- run: python -m pip install -e .
- run: python -m coverage run --append --branch --source . -m pip install --no-build-isolation --no-binary ebrains-drive ebrains-drive==0.6.0
- run: python -m coverage report --show-missing
- run: codecov
publish:
Expand Down
29 changes: 27 additions & 2 deletions version_query/py_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,26 @@

def query_package_folder(path: pathlib.Path, search_parent_directories: bool = False) -> Version:
"""Get version from Python package folder."""
global_metadata_json_paths, global_pkg_info_paths = [], []
if path.joinpath('pyproject.toml').exists() or path.joinpath('setup.py').exists():
metadata_json_paths = list(path.glob('*.dist-info/metadata.json'))
pkg_info_paths = list(path.glob('*.egg-info/PKG-INFO'))
pkg_info_paths += list(path.glob('*.dist-info/METADATA'))
if len(metadata_json_paths) == 1 and not pkg_info_paths:
return query_metadata_json(metadata_json_paths[0])

Check warning on line 38 in version_query/py_query.py

View check run for this annotation

Codecov / codecov/patch

version_query/py_query.py#L38

Added line #L38 was not covered by tests
if not metadata_json_paths and len(pkg_info_paths) == 1:
return query_pkg_info(pkg_info_paths[0])

Check warning on line 40 in version_query/py_query.py

View check run for this annotation

Codecov / codecov/patch

version_query/py_query.py#L40

Added line #L40 was not covered by tests
_LOG.debug(
'in %s found pyproject.toml or setup.py, as well as'
' %i JSON metadata: %s and %i PKG-INFO metadata: %s'
' - unable to infer package metadata, continuing search',
path, len(metadata_json_paths), metadata_json_paths, len(pkg_info_paths),
pkg_info_paths)
global_metadata_json_paths.extend(metadata_json_paths)
global_pkg_info_paths.extend(pkg_info_paths)
paths = [path]
if search_parent_directories:
paths += path.parents
metadata_json_paths, pkg_info_paths = None, None
for pth in paths:
metadata_json_paths = list(pth.parent.glob(f'{pth.name}*.dist-info/metadata.json'))
pkg_info_paths = list(pth.parent.glob(f'{pth.name}*.egg-info/PKG-INFO'))
Expand All @@ -41,4 +57,13 @@
return query_metadata_json(metadata_json_paths[0])
if not metadata_json_paths and len(pkg_info_paths) == 1:
return query_pkg_info(pkg_info_paths[0])
raise ValueError(paths, metadata_json_paths, pkg_info_paths)
_LOG.debug(
'in %s found %i JSON metadata: %s and %i PKG-INFO metadata: %s'
' - unable to infer package metadata, continuing search',
pth, len(metadata_json_paths), metadata_json_paths, len(pkg_info_paths), pkg_info_paths)
global_metadata_json_paths.extend(metadata_json_paths)
global_pkg_info_paths.extend(pkg_info_paths)
raise ValueError(
f'unable to infer package metadata from the following paths {paths} '
f'- found {len(global_metadata_json_paths)} JSON metadata: {global_metadata_json_paths}'
f' and {len(global_pkg_info_paths)} PKG-INFO metadata: {global_pkg_info_paths}')
2 changes: 1 addition & 1 deletion version_query/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def _caller_folder(stack_level: int = 1) -> pathlib.Path:
caller_path = frame_info[1] # frame_info.filename

here = pathlib.Path(caller_path).absolute().resolve()
assert here.is_file(), here
assert here.is_file() or here.name == '<string>', here
here = here.parent
assert here.is_dir(), here
_LOG.debug('found directory "%s"', here)
Expand Down