Skip to content

Commit 80b3593

Browse files
authored
Merge pull request #26 from mbdevpl/feature/improve-metadata-finding
improve Python package metadata finding when running outside of git repositories
2 parents a7b99f1 + 4b2e6e0 commit 80b3593

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

.github/workflows/python.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ jobs:
5151
- run: python -m coverage run --append --branch --source . -m unittest -v test.test_version
5252
env:
5353
LOGGING_LEVEL: critical
54+
- run: python -m pip install -e .
55+
- run: python -m coverage run --append --branch --source . -m pip install --no-build-isolation --no-binary ebrains-drive ebrains-drive==0.6.0
5456
- run: python -m coverage report --show-missing
5557
- run: codecov
5658
publish:

version_query/py_query.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,26 @@ def query_pkg_info(path: pathlib.Path) -> Version:
2929

3030
def query_package_folder(path: pathlib.Path, search_parent_directories: bool = False) -> Version:
3131
"""Get version from Python package folder."""
32+
global_metadata_json_paths, global_pkg_info_paths = [], []
33+
if path.joinpath('pyproject.toml').exists() or path.joinpath('setup.py').exists():
34+
metadata_json_paths = list(path.glob('*.dist-info/metadata.json'))
35+
pkg_info_paths = list(path.glob('*.egg-info/PKG-INFO'))
36+
pkg_info_paths += list(path.glob('*.dist-info/METADATA'))
37+
if len(metadata_json_paths) == 1 and not pkg_info_paths:
38+
return query_metadata_json(metadata_json_paths[0])
39+
if not metadata_json_paths and len(pkg_info_paths) == 1:
40+
return query_pkg_info(pkg_info_paths[0])
41+
_LOG.debug(
42+
'in %s found pyproject.toml or setup.py, as well as'
43+
' %i JSON metadata: %s and %i PKG-INFO metadata: %s'
44+
' - unable to infer package metadata, continuing search',
45+
path, len(metadata_json_paths), metadata_json_paths, len(pkg_info_paths),
46+
pkg_info_paths)
47+
global_metadata_json_paths.extend(metadata_json_paths)
48+
global_pkg_info_paths.extend(pkg_info_paths)
3249
paths = [path]
3350
if search_parent_directories:
3451
paths += path.parents
35-
metadata_json_paths, pkg_info_paths = None, None
3652
for pth in paths:
3753
metadata_json_paths = list(pth.parent.glob(f'{pth.name}*.dist-info/metadata.json'))
3854
pkg_info_paths = list(pth.parent.glob(f'{pth.name}*.egg-info/PKG-INFO'))
@@ -41,4 +57,13 @@ def query_package_folder(path: pathlib.Path, search_parent_directories: bool = F
4157
return query_metadata_json(metadata_json_paths[0])
4258
if not metadata_json_paths and len(pkg_info_paths) == 1:
4359
return query_pkg_info(pkg_info_paths[0])
44-
raise ValueError(paths, metadata_json_paths, pkg_info_paths)
60+
_LOG.debug(
61+
'in %s found %i JSON metadata: %s and %i PKG-INFO metadata: %s'
62+
' - unable to infer package metadata, continuing search',
63+
pth, len(metadata_json_paths), metadata_json_paths, len(pkg_info_paths), pkg_info_paths)
64+
global_metadata_json_paths.extend(metadata_json_paths)
65+
global_pkg_info_paths.extend(pkg_info_paths)
66+
raise ValueError(
67+
f'unable to infer package metadata from the following paths {paths} '
68+
f'- found {len(global_metadata_json_paths)} JSON metadata: {global_metadata_json_paths}'
69+
f' and {len(global_pkg_info_paths)} PKG-INFO metadata: {global_pkg_info_paths}')

version_query/query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def _caller_folder(stack_level: int = 1) -> pathlib.Path:
1919
caller_path = frame_info[1] # frame_info.filename
2020

2121
here = pathlib.Path(caller_path).absolute().resolve()
22-
assert here.is_file(), here
22+
assert here.is_file() or here.name == '<string>', here
2323
here = here.parent
2424
assert here.is_dir(), here
2525
_LOG.debug('found directory "%s"', here)

0 commit comments

Comments
 (0)