|
6 | 6 | import os
|
7 | 7 | from functools import partial
|
8 | 8 | from importlib import import_module # noqa: F401
|
| 9 | +from pathlib import Path |
| 10 | +from typing import Iterable |
9 | 11 |
|
10 | 12 | from conda import __version__ as CONDA_VERSION # noqa: F401
|
11 | 13 | from conda.auxlib.packaging import ( # noqa: F401
|
|
53 | 55 | human_bytes,
|
54 | 56 | input,
|
55 | 57 | install_actions,
|
56 |
| - is_linked, |
57 | 58 | lchmod,
|
58 | 59 | linked,
|
59 | 60 | linked_data,
|
|
75 | 76 | )
|
76 | 77 | from conda.models.channel import get_conda_build_local_url # noqa: F401
|
77 | 78 | from conda.models.dist import Dist # noqa: F401
|
78 |
| -from conda.models.records import PackageRecord |
| 79 | +from conda.models.records import PackageRecord, PrefixRecord |
79 | 80 |
|
80 | 81 | from .deprecations import deprecated
|
81 | 82 |
|
@@ -125,46 +126,36 @@ class SignatureError(Exception):
|
125 | 126 | pass
|
126 | 127 |
|
127 | 128 |
|
128 |
| -@deprecated("3.28.0", "4.0.0") |
129 |
| -def which_package(path): |
130 |
| - """ |
131 |
| - Given the path (of a (presumably) conda installed file) iterate over |
132 |
| - the conda packages the file came from. Usually the iteration yields |
133 |
| - only one package. |
134 |
| - """ |
135 |
| - from os.path import abspath, join |
| 129 | +@deprecated( |
| 130 | + "3.28.0", |
| 131 | + "4.0.0", |
| 132 | + addendum="Use `conda_build.inspect_pkg.which_package` instead.", |
| 133 | +) |
| 134 | +def which_package(path: str | os.PathLike | Path) -> Iterable[PrefixRecord]: |
| 135 | + from .inspect_pkg import which_package |
136 | 136 |
|
137 |
| - path = abspath(path) |
138 |
| - prefix = which_prefix(path) |
139 |
| - if prefix is None: |
140 |
| - raise RuntimeError("could not determine conda prefix from: %s" % path) |
141 |
| - for dist in linked(prefix): |
142 |
| - meta = is_linked(prefix, dist) |
143 |
| - if any(abspath(join(prefix, f)) == path for f in meta["files"]): |
144 |
| - yield dist |
| 137 | + return which_package(path, which_prefix(path)) |
145 | 138 |
|
146 | 139 |
|
147 | 140 | @deprecated("3.28.0", "4.0.0")
|
148 |
| -def which_prefix(path): |
| 141 | +def which_prefix(path: str | os.PathLike | Path) -> Path: |
149 | 142 | """
|
150 | 143 | Given the path (to a (presumably) conda installed file) return the
|
151 | 144 | environment prefix in which the file in located
|
152 | 145 | """
|
153 |
| - from os.path import abspath, dirname, isdir, join |
| 146 | + from conda.gateways.disk.test import is_conda_environment |
154 | 147 |
|
155 |
| - prefix = abspath(path) |
156 |
| - iteration = 0 |
157 |
| - while iteration < 20: |
158 |
| - if isdir(join(prefix, "conda-meta")): |
159 |
| - # we found it, so let's return it |
160 |
| - break |
161 |
| - if prefix == dirname(prefix): |
| 148 | + prefix = Path(path) |
| 149 | + for _ in range(20): |
| 150 | + if is_conda_environment(prefix): |
| 151 | + return prefix |
| 152 | + elif prefix == (parent := prefix.parent): |
162 | 153 | # we cannot chop off any more directories, so we didn't find it
|
163 |
| - prefix = None |
164 | 154 | break
|
165 |
| - prefix = dirname(prefix) |
166 |
| - iteration += 1 |
167 |
| - return prefix |
| 155 | + else: |
| 156 | + prefix = parent |
| 157 | + |
| 158 | + raise RuntimeError("could not determine conda prefix from: %s" % path) |
168 | 159 |
|
169 | 160 |
|
170 | 161 | @deprecated("3.28.0", "4.0.0")
|
|
0 commit comments