Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Shared cache cannot support overlapping directory structures #2106

Merged
merged 1 commit into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/2105.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug that shared cache cannot support overlapping namespace packages.
23 changes: 14 additions & 9 deletions src/pdm/installers/installers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ def _is_python_package(root: str | Path) -> bool:
return False


_namespace_package_lines = frozenset(
[
# pkg_resources style
"__import__('pkg_resources').declare_namespace(__name__)",
"pkg_resources.declare_namespace(__name__)",
# pkgutil style
"__path__ = __import__('pkgutil').extend_path(__path__, __name__)",
"__path__ = pkgutil.extend_path(__path__, __name__)",
]
)
_namespace_package_lines = _namespace_package_lines.union(line.replace("'", '"') for line in _namespace_package_lines)


@lru_cache()
def _is_namespace_package(root: str) -> bool:
if not _is_python_package(root):
Expand All @@ -52,15 +65,7 @@ def _is_namespace_package(root: str) -> bool:
return True
with Path(root, "__init__.py").open(encoding="utf-8") as f:
init_py_lines = [line.strip() for line in f if line.strip() and not line.strip().startswith("#")]
namespace_identifiers = [
# pkg_resources style
"__import__('pkg_resources').declare_namespace(__name__)",
# pkgutil style
"__path__ = __import__('pkgutil').extend_path(__path__, __name__)",
]
checker = namespace_identifiers[:]
checker.extend(item.replace("'", '"') for item in namespace_identifiers)
return any(line in checker for line in init_py_lines)
return not _namespace_package_lines.isdisjoint(init_py_lines)


def _create_symlinks_recursively(source: str, destination: str) -> Iterable[str]:
Expand Down