Skip to content

Commit

Permalink
Merge pull request #26 from mbdevpl/feature/improve-metadata-finding
Browse files Browse the repository at this point in the history
improve Python package metadata finding when running outside of git repositories
  • Loading branch information
mbdevpl authored Feb 16, 2025
2 parents a7b99f1 + 4b2e6e0 commit 80b3593
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
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_pkg_info(path: pathlib.Path) -> Version:

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])
if not metadata_json_paths and len(pkg_info_paths) == 1:
return query_pkg_info(pkg_info_paths[0])
_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 @@ def query_package_folder(path: pathlib.Path, search_parent_directories: bool = F
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

0 comments on commit 80b3593

Please sign in to comment.