Skip to content

Commit

Permalink
fix: handle weird multipath thing
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Jun 28, 2024
1 parent 729ec25 commit 46fd9a5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
10 changes: 4 additions & 6 deletions src/ape/managers/project.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import importlib.resources as resources
import json
import random
import shutil
Expand Down Expand Up @@ -35,6 +34,7 @@
create_tempdir,
get_all_files_in_directory,
get_full_extension,
get_package_path,
get_relative_path,
in_tempdir,
path_match,
Expand Down Expand Up @@ -1539,11 +1539,9 @@ def from_python_library(
:class:`~ape.managers.project.LocalProject`
"""
try:
file_result = resources.files(package_name)
except ModuleNotFoundError as err:
raise ProjectError(f"Package '{package_name}' not found in site-packages.") from err

pkg_path = Path(str(file_result)).resolve()
pkg_path = get_package_path(package_name)
except ValueError as err:
raise ProjectError(str(err)) from err

# Treat site-package as a local-project.
return LocalProject(pkg_path, config_override=config_override)
Expand Down
2 changes: 2 additions & 0 deletions src/ape/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
expand_environment_variables,
get_all_files_in_directory,
get_full_extension,
get_package_path,
get_relative_path,
in_tempdir,
path_match,
Expand Down Expand Up @@ -98,6 +99,7 @@
"get_all_files_in_directory",
"get_current_timestamp_ms",
"get_full_extension",
"get_package_path",
"pragma_str_to_specifier_set",
"in_tempdir",
"injected_before_use",
Expand Down
31 changes: 31 additions & 0 deletions src/ape/utils/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from collections.abc import Callable, Iterator
from contextlib import contextmanager
from fnmatch import fnmatch
from importlib.resources import files
from pathlib import Path
from re import Pattern
from tempfile import TemporaryDirectory, gettempdir
Expand Down Expand Up @@ -302,3 +303,33 @@ def clean_path(path: Path) -> str:
return f"$HOME{os.path.sep}{path.relative_to(home)}"

return f"{path}"


def get_package_path(package_name: str) -> Path:
"""
Get the path to a package from site-packages.
Args:
package_name (str): The name of the package.
Returns:
Path
"""
try:
file_result = files(package_name)
except ModuleNotFoundError as err:
raise ValueError(f"Package '{package_name}' not found in site-packages.") from err

if not file_result:
raise ValueError(f"Package '{package_name}' not found in site-packages.")

# There is no easy way to get a real-path from a MultiplexedPath
for path in file_result.iterdir():
if not isinstance(path, Path):
continue

for parent in path.parents:
if parent.name == package_name:
return parent

raise ValueError(f"Unable to get package path for '{package_name}'.")
12 changes: 5 additions & 7 deletions src/ape_pm/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import shutil
from collections.abc import Iterable
from functools import cached_property
from importlib import metadata, resources
from importlib import metadata
from pathlib import Path
from typing import Optional, Union

Expand All @@ -13,7 +13,7 @@
from ape.exceptions import ProjectError
from ape.logging import logger
from ape.managers.project import _version_to_options
from ape.utils import ManagerAccessMixin, clean_path, in_tempdir
from ape.utils import ManagerAccessMixin, clean_path, get_package_path, in_tempdir
from ape.utils._github import _GithubClient, github_client


Expand Down Expand Up @@ -414,11 +414,9 @@ def validate_model(cls, values):
@cached_property
def path(self) -> Path:
try:
file_result = resources.files(self.python)
except ModuleNotFoundError as err:
raise ProjectError(f"Dependency '{self.python}' not found installed.") from err

return Path(str(file_result)).resolve()
return get_package_path(self.python)
except ValueError as err:
raise ProjectError(str(err)) from err

@property
def package_id(self) -> str:
Expand Down

0 comments on commit 46fd9a5

Please sign in to comment.