Skip to content

Commit

Permalink
Fix MetaPathFinder issue
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleknap committed Jan 30, 2024
1 parent b2fbe9b commit 9dec2ae
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion tests/dependencies/test_closure.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import fnmatch
import functools
import importlib.metadata
import json
import os
from typing import Dict, Iterator, List, Tuple
import site

import pytest
from packaging.requirements import Requirement

_NESTED_STR_DICT = Dict[str, "_NESTED_STR_DICT"]
_SITE_PACKAGES = site.getsitepackages()[0]


@pytest.fixture()
Expand All @@ -42,7 +46,7 @@ def _get_runtime_closure(self) -> "DependencyClosure":
return closure

def _get_runtime_requirements(self) -> List[Requirement]:
req_strings = importlib.metadata.distribution(self.name).requires
req_strings = self._get_distribution(self.name).requires
if req_strings is None:
return []
return [Requirement(req_string) for req_string in req_strings]
Expand All @@ -60,6 +64,33 @@ def _requirement_applies_to_environment(
return False
return True

def _get_distribution(self, name: str) -> importlib.metadata.Distribution:
# For v2, we inject our own MetaPathFinder to handle
# botocore/s3transfer import aliases. However for the typical
# importlib.metadata.distribution(), it extends the built-in
# MetaPathFinder to include its own find_distributions() method
# to search for distribution directories. Read more here:
# https://docs.python.org/3/library/importlib.metadata.html#extending-the-search-algorithm
#
# Our MetaPathFinder class does not implement this method, which
# causes importlib.metadata.distribution() to not find the "awscli"
# package. So instead, this helper method is implemented to locate the
# dist-info directories based off our current site-packages
# and explicitly provide the directory to avoid needing to use
# the MetaPathFinder and thus avoid this issue.

# Packages names may have a "-". These get converted to "_" for
# their respective directory names in the site packages directory.
snake_case_name = name.replace("-", "_")
for filename in os.listdir(_SITE_PACKAGES):
if fnmatch.fnmatch(filename, f"{snake_case_name}-*.dist-info"):
return importlib.metadata.Distribution.at(
os.path.join(_SITE_PACKAGES, filename)
)
raise ValueError(
f'Could not find .dist-info directory for {self.name}'
)


class DependencyClosure:
def __init__(self) -> None:
Expand Down

0 comments on commit 9dec2ae

Please sign in to comment.