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

Merged
merged 12 commits into from
Jun 23, 2024
26 changes: 21 additions & 5 deletions importlib_metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def at(path: str | os.PathLike[str]) -> Distribution:
:param path: a string or path-like object
:return: a concrete Distribution instance for the path
"""
return PathDistribution(pathlib.Path(path))
return PathDistribution(cast(SimplePath, pathlib.Path(path)))
jaraco marked this conversation as resolved.
Show resolved Hide resolved

@staticmethod
def _discover_resolvers():
Expand Down 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://github.com/python/cpython/issues/67271 where ".."
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))

jaraco marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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
jaraco marked this conversation as resolved.
Show resolved Hide resolved
34 changes: 34 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class APITests(
fixtures.EggInfoPkg,
fixtures.EggInfoPkgPipInstalledNoToplevel,
fixtures.EggInfoPkgPipInstalledNoModules,
fixtures.EggInfoPkgPipInstalledExternalDataFiles,
fixtures.EggInfoPkgSourcesFallback,
fixtures.DistInfoPkg,
fixtures.DistInfoPkgWithDot,
Expand Down
Loading