File size: 9,472 Bytes
71687cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
"""Backported exports for conda-build."""

from __future__ import annotations

from builtins import input  # noqa: UP029
from io import StringIO
from typing import TYPE_CHECKING

from . import CondaError
from .base.constants import (
    DEFAULT_CHANNELS,
    DEFAULT_CHANNELS_UNIX,
    DEFAULT_CHANNELS_WIN,
    PREFIX_PLACEHOLDER,
)
from .base.context import (  # noqa: F401
    context,
    non_x86_machines,
    reset_context,
    sys_rc_path,
)
from .cli.common import spec_from_line, specs_from_args, specs_from_url  # noqa: F401
from .cli.conda_argparse import ArgumentParser  # noqa: F401
from .cli.helpers import (  # noqa: F401
    add_parser_channels,
    add_parser_prefix,
)
from .common import compat  # noqa: F401
from .common.serialize.json import CondaJSONEncoder
from .common.toposort import _toposort  # noqa: F401
from .core.index import (
    Index,
    dist_str_in_index,  # noqa: F401
)
from .core.package_cache_data import ProgressiveFetchExtract  # noqa: F401
from .core.prefix_data import delete_prefix_from_linked_data
from .core.solve import Solver  # noqa: F401
from .core.subdir_data import cache_fn_url  # noqa: F401
from .deprecations import deprecated
from .exceptions import (
    CondaHTTPError,
    CondaOSError,
    LinkError,
    LockError,
    PaddingError,
    PathNotFoundError,
    UnsatisfiableError,
)
from .gateways.connection.download import TmpDownload  # noqa: F401
from .gateways.connection.download import download as _download
from .gateways.connection.session import CondaSession  # noqa: F401
from .gateways.disk.create import TemporaryDirectory  # noqa: F401
from .gateways.disk.delete import delete_trash  # noqa: F401
from .gateways.disk.link import lchmod  # noqa: F401
from .gateways.subprocess import ACTIVE_SUBPROCESSES, subprocess_call  # noqa: F401
from .misc import untracked, walk_prefix  # noqa: F401
from .models.channel import Channel, get_conda_build_local_url  # noqa: F401
from .models.dist import Dist
from .models.enums import FileMode, PathEnum  # noqa: F401
from .models.version import VersionOrder, normalized_version  # noqa: F401
from .resolve import (  # noqa: F401
    MatchSpec,
    Resolve,
    ResolvePackageNotFound,
    Unsatisfiable,
)
from .utils import human_bytes, url_path  # noqa: F401

if TYPE_CHECKING:
    from typing import Any

reset_context()  # initialize context when conda.exports is imported


NoPackagesFound = NoPackagesFoundError = ResolvePackageNotFound
non_x86_linux_machines = non_x86_machines
arch_name = context.arch_name
binstar_upload = context.anaconda_upload
bits = context.bits
default_prefix = context.default_prefix
default_python = context.default_python
envs_dirs = context.envs_dirs
pkgs_dirs = context.pkgs_dirs
platform = context.platform
root_dir = context.root_prefix
root_writable = context.root_writable
subdir = context.subdir
conda_build = context.conda_build
get_rc_urls = lambda: list(context.channels)
get_local_urls = lambda: list(get_conda_build_local_url()) or []
load_condarc = lambda fn: reset_context([fn])
PaddingError = PaddingError
LinkError = LinkError
CondaOSError = CondaOSError
# Replacements for six exports for compatibility


# FUTURE: conda 26.3+ remove this
def __getattr__(name: str) -> Any:
    # lazy load the deprecated module
    if name == "plan":
        from . import plan

        return plan

    raise AttributeError(f"module '{__name__}' has no attribute '{name}'")


deprecated.constant(
    "26.3",
    "26.9",
    "EntityEncoder",
    CondaJSONEncoder,
    addendum="Use `conda.common.serialize.json.CondaJSONEncoder` instead.",
)
del CondaJSONEncoder

deprecated.constant(
    "26.9",
    "27.3",
    "input",
    input,
    addendum="Use `builtins.input` instead.",
)
del input

deprecated.constant(
    "26.9",
    "27.3",
    "StringIO",
    StringIO,
    addendum="Use `io.StringIO` instead.",
)
del StringIO

deprecated.constant(
    "26.9",
    "27.3",
    "PY3",
    True,
    addendum="Python 2 is no longer supported.",
)

deprecated.constant(
    "26.9",
    "27.3",
    "string_types",
    str,
    addendum="Use `str` instead.",
)

deprecated.constant(
    "26.9",
    "27.3",
    "text_type",
    str,
    addendum="Use `str` instead.",
)

deprecated.constant(
    "26.9",
    "27.3",
    "DEFAULT_CHANNELS",
    DEFAULT_CHANNELS,
    addendum="Use `conda.base.constants.DEFAULT_CHANNELS` instead.",
)
del DEFAULT_CHANNELS

deprecated.constant(
    "26.9",
    "27.3",
    "DEFAULT_CHANNELS_UNIX",
    DEFAULT_CHANNELS_UNIX,
    addendum="Use `conda.base.constants.DEFAULT_CHANNELS_UNIX` instead.",
)
del DEFAULT_CHANNELS_UNIX

deprecated.constant(
    "26.9",
    "27.3",
    "DEFAULT_CHANNELS_WIN",
    DEFAULT_CHANNELS_WIN,
    addendum="Use `conda.base.constants.DEFAULT_CHANNELS_WIN` instead.",
)
del DEFAULT_CHANNELS_WIN

deprecated.constant(
    "26.9",
    "27.3",
    "PREFIX_PLACEHOLDER",
    PREFIX_PLACEHOLDER,
    addendum="Use `conda.base.constants.PREFIX_PLACEHOLDER` instead.",
)

deprecated.constant(
    "26.9",
    "27.3",
    "_PREFIX_PLACEHOLDER",
    PREFIX_PLACEHOLDER,
    addendum="Use `conda.base.constants.PREFIX_PLACEHOLDER` instead.",
)

deprecated.constant(
    "26.9",
    "27.3",
    "prefix_placeholder",
    PREFIX_PLACEHOLDER,
    addendum="Use `conda.base.constants.PREFIX_PLACEHOLDER` instead.",
)
del PREFIX_PLACEHOLDER

deprecated.constant(
    "26.9",
    "27.3",
    "CondaError",
    CondaError,
    addendum="Use `conda.CondaError` instead.",
)
del CondaError

deprecated.constant(
    "26.9",
    "27.3",
    "CondaHTTPError",
    CondaHTTPError,
    addendum="Use `conda.exceptions.CondaHTTPError` instead.",
)
del CondaHTTPError

deprecated.constant(
    "26.9",
    "27.3",
    "CondaOSError",
    CondaOSError,
    addendum="Use `conda.exceptions.CondaOSError` instead.",
)
del CondaOSError

deprecated.constant(
    "26.9",
    "27.3",
    "LinkError",
    LinkError,
    addendum="Use `conda.exceptions.LinkError` instead.",
)
del LinkError

deprecated.constant(
    "26.9",
    "27.3",
    "LockError",
    LockError,
    addendum="Use `conda.exceptions.LockError` instead.",
)
del LockError

deprecated.constant(
    "26.9",
    "27.3",
    "PaddingError",
    PaddingError,
    addendum="Use `conda.exceptions.PaddingError` instead.",
)
del PaddingError

deprecated.constant(
    "26.9",
    "27.3",
    "PathNotFoundError",
    PathNotFoundError,
    addendum="Use `conda.exceptions.PathNotFoundError` instead.",
)

deprecated.constant(
    "26.9",
    "27.3",
    "CondaFileNotFoundError",
    PathNotFoundError,
    addendum="Use `conda.exceptions.PathNotFoundError` instead.",
)
del PathNotFoundError

deprecated.constant(
    "26.9",
    "27.3",
    "UnsatisfiableError",
    UnsatisfiableError,
    addendum="Use `conda.exceptions.UnsatisfiableError` instead.",
)
del UnsatisfiableError


def get_default_urls():
    from .base.constants import DEFAULT_CHANNELS

    return DEFAULT_CHANNELS


def rm_rf(path, max_retries=5, trash=True):
    from .gateways.disk.delete import rm_rf

    rm_rf(path)
    delete_prefix_from_linked_data(path)


def get_index(
    channel_urls=(),
    prepend=True,
    platform=None,
    use_local=False,
    use_cache=False,
    unknown=None,
    prefix=None,
):
    index = Index(
        channel_urls, prepend, platform, use_local, use_cache, unknown, prefix
    )
    return {Dist(prec): prec for prec in index.values()}


def package_cache():
    from .core.package_cache_data import PackageCacheData

    class package_cache:
        def __contains__(self, dist):
            return bool(
                PackageCacheData.first_writable().get(Dist(dist).to_package_ref(), None)
            )

        def keys(self):
            return (Dist(v) for v in PackageCacheData.first_writable().values())

        def __delitem__(self, dist):
            PackageCacheData.first_writable().remove(Dist(dist).to_package_ref())

    return package_cache()


def linked_data(prefix, ignore_channels=False):
    """Return a dictionary of the linked packages in prefix."""
    from .core.prefix_data import PrefixData
    from .models.dist import Dist

    pd = PrefixData(prefix)
    return {
        Dist(prefix_record): prefix_record
        for prefix_record in pd._prefix_records.values()
    }


def linked(prefix, ignore_channels=False):
    """Return the Dists of linked packages in prefix."""
    from .models.enums import PackageType

    conda_package_types = PackageType.conda_package_types()
    ld = linked_data(prefix, ignore_channels=ignore_channels).items()
    return {
        dist
        for dist, prefix_rec in ld
        if prefix_rec.package_type in conda_package_types
    }


# exports
def is_linked(prefix, dist):
    """
    Return the install metadata for a linked package in a prefix, or None
    if the package is not linked in the prefix.
    """
    # FIXME Functions that begin with `is_` should return True/False
    from .core.prefix_data import PrefixData

    pd = PrefixData(prefix)
    prefix_record = pd.get(dist.name, None)
    if prefix_record is None:
        return None
    elif MatchSpec(dist).match(prefix_record):
        return prefix_record
    else:
        return None


def download(
    url,
    dst_path,
    session=None,
    md5sum=None,
    urlstxt=False,
    retries=3,
    sha256=None,
    size=None,
):
    return _download(url, dst_path, md5=md5sum, sha256=sha256, size=size)