Skip to content

Commit

Permalink
Stop relying on .egg-info directories
Browse files Browse the repository at this point in the history
Fix gh-2567.

Signed-off-by: Juan Luis Cano Rodríguez <juan_luis_cano@mckinsey.com>
  • Loading branch information
astrojuanlu committed May 29, 2023
1 parent 0a497b3 commit c1f5b6a
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions kedro/framework/cli/micropkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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"
Expand All @@ -172,7 +169,7 @@ def _pull_package(
_install_files(
metadata,
package_name,
egg_info_file.parent,
project_root_dir,
env,
alias,
destination,
Expand Down

0 comments on commit c1f5b6a

Please sign in to comment.