diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index 647d244..4ec148a 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -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: diff --git a/version_query/py_query.py b/version_query/py_query.py index 967e086..e4a8dcf 100644 --- a/version_query/py_query.py +++ b/version_query/py_query.py @@ -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')) @@ -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}') diff --git a/version_query/query.py b/version_query/query.py index fe6d8ae..253c3bb 100644 --- a/version_query/query.py +++ b/version_query/query.py @@ -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 == '', here here = here.parent assert here.is_dir(), here _LOG.debug('found directory "%s"', here)