Repository: kedro-org/kedro

Issue:
kedro micropkg pull from pypi is not working in 0.18.2
## Description

A micro-packaged pipeline which is uploaded to a PyPi repository can not be successfully pulled.

We have been using kedro very actively, so thanks for your support with this issue!

## Context
I was trying to push and pull a micro package via a private PyPi repository to share and reuse pipelines.

## Steps to Reproduce
1. kedro pipeline create <pipeline_name>
2. kedro micropkg package pipeline.<pipeline_name>
3. twine upload -dist/* 
4. kedro micropkg pull <pipeline_name>

## Expected Result
The micro-package should be downloaded via pip and successfully pulled via `kedro micropkg pull`


## Actual Result
The micro-package is downloaded via pip but there seems to be an isse here:
https://github.com/kedro-org/kedro/blob/4843fadfc887c410bb03ba3e54f6737019eeef3b/kedro/framework/cli/micropkg.py#L147

`Path(package_path).name` is expected to be a tar.gz file, but in my case it is actually the name of the pip package that was downloaded. As a result `egg_info_file` will be an empty list.

This is the error:

```
kedro.framework.cli.utils.KedroCliError: More than 1 or no egg-info files found from <pipeline_name>. There has to be exactly one egg-info directory.
```

I can bypass the issue by running this:
1. `python -m pip download --no-deps --dest <destination>  <pipeline_name>`
2. Copying the tar.gz path of the downloaded package `<micro-package-path>`
3. ` kedro micropkg pull <micro-package-path>`

## Your Environment

* Kedro version used (`pip show kedro` or `kedro -V`): 0.18.2
* Python version used (`python -V`): 3.9.12
* Operating system and version: Ubuntu 20.04.3 and MacOS 12.5



Discussion:
I think I know (part of) the problem: `.rstrip(".tar.gz") ` does not remove the suffix, but strips the ensemble of letters `'.', 'a', 'g', 'r', 't', 'z'`, which in case of our package removes a letter.

Also, the version seems to be missing. I put a breakpoint, and at this point there is a directory with `package_name-0.1` and not `package-name`. Note also that there is an underscore `_` in the real directory, but this line expects an hyphen `-`
(sorry for the noise, I put what I found here in case I do not have time to make a PR).
- In the case of `.tar.gz` file, the `package_name` is correctly the name of the package with underscores and the version  (which is what stops the `.rstrip` from stripping package name's letters) which is the name of the folder untarred (not sure this is a word)
- In the case of a `pip` package, the `package_name` is the name of the package with dashes `-` and no version.

They cannot be treated the same way here.

Maybe use `list(temp_dir_path.rglob("*.egg-info"))` which may work in both cases?

Resolving patch:
diff --git a/kedro/framework/cli/micropkg.py b/kedro/framework/cli/micropkg.py
--- a/kedro/framework/cli/micropkg.py
+++ b/kedro/framework/cli/micropkg.py
@@ -141,18 +141,17 @@ def _pull_package(
 ):
     with tempfile.TemporaryDirectory() as temp_dir:
         temp_dir_path = Path(temp_dir).resolve()
-
         _unpack_sdist(package_path, temp_dir_path, fs_args)
 
-        sdist_file_name = Path(package_path).name.rstrip(".tar.gz")
-        egg_info_file = list((temp_dir_path / sdist_file_name).glob("*.egg-info"))
-        if len(egg_info_file) != 1:
+        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."
             )
-        package_name = egg_info_file[0].stem
-        package_requirements = temp_dir_path / sdist_file_name / "setup.py"
+        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"
@@ -172,7 +171,7 @@ def _pull_package(
         _install_files(
             metadata,
             package_name,
-            temp_dir_path / sdist_file_name,
+            egg_info_file.parent,
             env,
             alias,
             destination,


Test patch:
diff --git a/tests/framework/cli/micropkg/test_micropkg_pull.py b/tests/framework/cli/micropkg/test_micropkg_pull.py
--- a/tests/framework/cli/micropkg/test_micropkg_pull.py
+++ b/tests/framework/cli/micropkg/test_micropkg_pull.py
@@ -627,9 +627,12 @@ def test_pull_from_pypi(
 
         options = ["-e", env] if env else []
         options += ["--alias", alias] if alias else []
+
+        package_name = "my-pipeline"
+
         result = CliRunner().invoke(
             fake_project_cli,
-            ["micropkg", "pull", f"{PIPELINE_NAME}-{version}", *options],
+            ["micropkg", "pull", package_name, *options],
             obj=fake_metadata,
         )
         assert result.exit_code == 0
@@ -642,7 +645,7 @@ def test_pull_from_pypi(
                 "--no-deps",
                 "--dest",
                 str(tmp_path),
-                f"{PIPELINE_NAME}-{version}",
+                package_name,
             ],
         )