diff --git a/kedro/framework/cli/micropkg.py b/kedro/framework/cli/micropkg.py index 222ded2531..fb0d202187 100644 --- a/kedro/framework/cli/micropkg.py +++ b/kedro/framework/cli/micropkg.py @@ -11,6 +11,7 @@ from typing import Iterable, List, Tuple, Union import click +from build.util import project_wheel_metadata from packaging.requirements import InvalidRequirement, Requirement from rope.base.project import Project from rope.contrib import generate @@ -131,6 +132,17 @@ def pull_package( # pylint:disable=unused-argument, too-many-arguments click.secho(message, fg="green") +def _get_library_reqs(metadata): + # https://discuss.python.org/t/\ + # programmatically-getting-non-optional-requirements-of-current-directory/26963/2 + return [ + str(req) + for dep_str in metadata.get_all("Requires-Dist") + for req in (Requirement(dep_str),) + if not req.marker or req.marker.evaluate() + ] + + # pylint: disable=too-many-arguments, too-many-locals def _pull_package( package_path: str, @@ -144,25 +156,10 @@ def _pull_package( temp_dir_path = Path(temp_dir).resolve() _unpack_sdist(package_path, temp_dir_path, fs_args) - egg_info_files = list((temp_dir_path).rglob("*.egg-info")) - if len(egg_info_files) != 1: - raise KedroCliError( - f"More than 1 or no egg-info files found from {package_path}. " - f"There has to be exactly one egg-info directory." - ) - egg_info_file = egg_info_files[0] - package_name = egg_info_file.stem - package_requirements = egg_info_file.parent / "setup.py" - - # Finds a string representation of 'install_requires' list from setup.py - reqs_list_pattern = r"install_requires\=(.*?)\,\n" - list_reqs = re.findall( - reqs_list_pattern, package_requirements.read_text(encoding="utf-8") - ) - - # Finds all elements from the above string representation of a list - reqs_element_pattern = r"\'(.*?)\'" - package_reqs = re.findall(reqs_element_pattern, list_reqs[0]) + project_root_dir = temp_dir_path + meta = project_wheel_metadata(project_root_dir) + package_name = meta.get("Name") + package_reqs = _get_library_reqs(meta) if package_reqs: requirements_txt = metadata.source_dir / "requirements.txt" @@ -172,7 +169,7 @@ def _pull_package( _install_files( metadata, package_name, - egg_info_file.parent, + project_root_dir, env, alias, destination,