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

Add better support for packages with files outside site-packages #482

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 20 additions & 4 deletions importlib_metadata/__init__.py
Expand Up @@ -562,15 +562,31 @@ def _read_files_egginfo_installed(self):
if not text or not subdir:
return

site_path = self.locate_file('').resolve()
paths = (
(subdir / name)
.resolve()
.relative_to(self.locate_file('').resolve())
.as_posix()
self._relative_to(
(subdir / name).resolve(),
site_path,
).as_posix()
for name in text.splitlines()
)
return map('"{}"'.format, paths)

def _relative_to(self, path, root):
"""
Workaround for https://bugs.python.org/issue23082 where ".."
dan-blanchard marked this conversation as resolved.
Show resolved Hide resolved
isn't added by pathlib.Path.relative_to() when path is not
a subpath of root.

One example of such a package is dask-labextension, which uses
jupyter-packaging to install JupyterLab javascript files outside
of site-packages.
"""
try:
return path.relative_to(root)
except ValueError:
return pathlib.Path(os.path.relpath(path, root))

def _read_files_egginfo_sources(self):
"""
Read SOURCES.txt and return lines in a similar CSV-parsable
Expand Down
2 changes: 2 additions & 0 deletions importlib_metadata/_meta.py
Expand Up @@ -65,3 +65,5 @@ def read_text(self, encoding=None) -> str: ... # pragma: no cover
def read_bytes(self) -> bytes: ... # pragma: no cover

def exists(self) -> bool: ... # pragma: no cover

def resolve(self) -> bool: ... # pragma: no cover
34 changes: 34 additions & 0 deletions tests/fixtures.py
Expand Up @@ -250,6 +250,40 @@ def main():
}


class EggInfoPkgPipInstalledExternalDataFiles(OnSysPath, SiteBuilder):
files: FilesSpec = {
"egg_with_module_pkg.egg-info": {
"PKG-INFO": "Name: egg_with_module-pkg",
# SOURCES.txt is made from the source archive, and contains files
# (setup.py) that are not present after installation.
"SOURCES.txt": """
egg_with_module.py
setup.py
egg_with_module.json
egg_with_module_pkg.egg-info/PKG-INFO
egg_with_module_pkg.egg-info/SOURCES.txt
egg_with_module_pkg.egg-info/top_level.txt
""",
# installed-files.txt is written by pip, and is a strictly more
# accurate source than SOURCES.txt as to the installed contents of
# the package.
"installed-files.txt": """
../../../etc/jupyter/jupyter_notebook_config.d/relative.json
/etc/jupyter/jupyter_notebook_config.d/absolute.json
../egg_with_module.py
PKG-INFO
SOURCES.txt
top_level.txt
""",
# missing top_level.txt (to trigger fallback to installed-files.txt)
},
"egg_with_module.py": """
def main():
print("hello world")
""",
}


class EggInfoPkgPipInstalledNoModules(OnSysPath, SiteBuilder):
files: FilesSpec = {
"egg_with_no_modules_pkg.egg-info": {
Expand Down
1 change: 1 addition & 0 deletions tests/test_api.py
Expand Up @@ -29,6 +29,7 @@ class APITests(
fixtures.EggInfoPkg,
fixtures.EggInfoPkgPipInstalledNoToplevel,
fixtures.EggInfoPkgPipInstalledNoModules,
fixtures.EggInfoPkgPipInstalledExternalDataFiles,
fixtures.EggInfoPkgSourcesFallback,
fixtures.DistInfoPkg,
fixtures.DistInfoPkgWithDot,
Expand Down