:py:mod:`shards` ================ .. py:module:: conda._private.shards.shards .. autoapi-nested-parse:: Models for sharded repodata, and to make monolithic repodata look like sharded repodata. Classes ------- .. autoapisummary:: conda._private.shards.shards.ShardFetch conda._private.shards.shards.ShardBase conda._private.shards.shards.ShardLike conda._private.shards.shards.Shards Functions --------- .. autoapisummary:: conda._private.shards.shards.shard_mentioned_packages conda._private.shards.shards.shard_mentioned_package_extras conda._private.shards.shards._shards_base_url conda._private.shards.shards._repodata_shards conda._private.shards.shards._shards_from_bytes conda._private.shards.shards.fetch_shards_index conda._private.shards.shards.batch_retrieve_from_cache conda._private.shards.shards.batch_retrieve_from_network conda._private.shards.shards.fetch_channels .. py:class:: ShardFetch(shardbase: ShardBase, package: str, shard_cache: conda._private.shards.cache.ShardCache | None = None) Wrapper class that encapsulates fetching and caching of individual shards. Handles deferred fetching: shards can be requested via this class but will only be actually retrieved from the network when fetch() is called. This allows batching and coordinating shard retrieval across multiple channels. Initialize a ShardFetch wrapper. :param shardbase: The ShardBase (Shards or ShardLike) instance :param package: The package name to fetch :param shard_cache: Optional cache to use for storage (required for Shards) .. py:attribute:: shardbase .. py:attribute:: package .. py:attribute:: url .. py:attribute:: shard_cache :value: None .. py:attribute:: _shard :type: conda._private.shards.typing.ShardDict | None :value: None .. py:attribute:: _fetched :value: False .. py:method:: fetch() -> conda._private.shards.typing.ShardDict Fetch the shard from the network or return cached result. For Shards, performs the actual network fetch. For ShardLike, returns the shard immediately since it's in memory. .. py:method:: _fetch_from_shards() -> conda._private.shards.typing.ShardDict Fetch a single shard from a Shards instance. .. py:method:: _fetch_shards_impl(packages: collections.abc.Iterable[str]) -> dict[str, conda._private.shards.typing.ShardDict] Fetch multiple shards for a Shards instance. Implements the core fetching logic for Shards, handling network requests, caching, and decompression. .. py:method:: _process_fetch_result(future, url, package, results, shards) Process a single fetched shard result. .. py:method:: fetch_batch(shard_fetches: collections.abc.Iterable[ShardFetch]) -> None :staticmethod: Batch fetch multiple ShardFetch objects, grouping by ShardBase. This efficiently fetches shards from multiple sources by grouping requests by their ShardBase instance and making coordinated network calls. .. py:function:: shard_mentioned_packages(shard: conda._private.shards.typing.ShardDict, extra: collections.abc.Iterable[str] = (), spec_to_package_name=spec_to_package_name, repodata_version: int = 1) -> collections.abc.Iterable[str] Return all dependency names mentioned in a shard, not including the shard's own package name. Callers are responsible for calling ``conda._private.shards.misc.filter_redundant_packages`` on ``shard`` first if "prefer .conda over .tar.bz2" semantics are required; `conda._private.shards.subset` already does this for every shard before invoking this function. .. py:function:: shard_mentioned_package_extras(shard: conda._private.shards.typing.ShardDict, extras: collections.abc.Iterable[str] = (), include_depends: bool = True, spec_to_package_name=spec_to_package_name, repodata_version: int = 1) -> collections.abc.Iterable[tuple[str, frozenset[str]]] Return ``(name, extras)`` for each dependency spec in a shard, where ``extras`` are the CEP 44 extras explicitly requested by that spec (e.g. ``pkg[extras=more]`` -> ``("pkg", frozenset({"more"}))``). Specs come from every record's ``depends`` (when ``include_depends`` is true) and from the ``extra_depends`` groups named by ``extras``. Groups are unioned across all builds/versions in the shard. Only groups named in ``extras`` are followed. Other groups stay inactive. Callers are responsible for calling ``conda._private.shards.misc.filter_redundant_packages`` on ``shard`` first. .. py:class:: ShardBase Bases: :py:obj:`abc.ABC` Abstract base class for shard-like objects. Defines the common interface for both sharded repodata (Shards) and monolithic repodata presented as shards (ShardLike). .. py:attribute:: url :type: str .. py:attribute:: repodata_no_packages :type: conda._private.shards.typing.RepodataDict .. py:attribute:: visited :type: dict[str, conda._private.shards.typing.ShardDict | None] .. py:attribute:: _base_url :type: str .. py:property:: package_names :type: collections.abc.KeysView[str] :abstractmethod: Return the names of all packages available in this shard collection. .. py:property:: base_url :type: str Return self.url joined with base_url from repodata, or self.url if no base_url was present. Packages are found here. Note base_url can be a relative or an absolute url. Uses _safe_urljoin_with_slash to handle non-HTTP schemes (s3://, etc.). .. py:method:: __contains__(package: str) -> bool Check if a package is available in this shard collection. .. py:method:: shard_url(package: str) -> str :abstractmethod: Return shard URL for a given package. For monolithic repodata, should not be fetched but is a unique identifier. Raise KeyError if package is not in the index. .. py:method:: shard_loaded(package: str) -> bool :abstractmethod: Return True if the given package's shard is in memory. .. py:method:: visit_package(package: str) -> conda._private.shards.typing.ShardDict Return a shard that is already loaded in memory and mark as visited. .. py:method:: visit_shard(package: str, shard: conda._private.shards.typing.ShardDict) Store new shard data in the visited dict. .. py:method:: build_repodata() -> conda._private.shards.typing.RepodataDict Return monolithic repodata including all visited shards. Does not return "v3" repodata. Prefer iter_records_v3() over this method. .. py:method:: iter_records() -> collections.abc.Iterable[tuple[str, dict]] Yield (key, record) tuples for all packages in visited shards, including v3 records. For classic packages, *key* is the filename. For v3 packages, *key* is the shard key (not necessarily the download filename; see ``record["fn"]``). Prefer :meth:`iter_records_v3` when section information is needed. .. py:method:: iter_records_v3() -> collections.abc.Iterable[tuple[tuple[str, str], dict]] Yield ((key, section), record) tuples for all packages in visited shards. Section can be: "packages" for .tar.bz2 packages, "packages.conda" for .conda packages, "v3.whl", "v3.conda", "v3.tar.bz2" for v3 packages. key is the same as the filename for "packages", "packages.conda" but is different from the filename for v3 packages. .. py:class:: ShardLike(repodata: conda._private.shards.typing.RepodataDict, url: str = '') Bases: :py:obj:`ShardBase` Present a "classic" repodata.json as per-package shards. url: must be unique for all ShardLike used together. .. py:attribute:: repodata_no_packages :type: conda._private.shards.typing.RepodataDict .. py:attribute:: url :value: '' .. py:attribute:: shards :type: dict[str, conda._private.shards.typing.ShardDict] .. py:attribute:: visited :type: dict[str, conda._private.shards.typing.ShardDict | None] .. py:method:: __repr__() .. py:property:: package_names :type: collections.abc.KeysView[str] Return the names of all packages available in this shard collection. .. py:method:: shard_url(package: str) -> str Return shard URL for a given package. Raise KeyError if package is not in the index. .. py:method:: shard_loaded(package: str) -> bool Return True if the given package's shard is in memory. .. py:method:: visit_package(package: str) -> conda._private.shards.typing.ShardDict Return a shard that is already in memory and mark as visited. .. py:function:: _shards_base_url(url, shards_base_url) -> str Return shards_base_url joined with base_url and url. Note shards_base_url can be a relative or an absolute url. Uses _safe_urljoin_with_slash to handle non-HTTP schemes (s3://, etc.). .. py:class:: Shards(shards_index: conda._private.shards.typing.ShardsIndexDict, url: str) Bases: :py:obj:`ShardBase` Handle repodata_shards.msgpack.zst and individual per-package shards. :param shards_index: raw parsed msgpack dict. Don't change it or base_url, :param shards_base_url will be wrong.: :param url: URL of repodata_shards.msgpack.zst .. py:attribute:: _shards_base_url :type: str .. py:attribute:: shards_index .. py:attribute:: url .. py:attribute:: _base_url .. py:attribute:: session .. py:attribute:: repodata_no_packages .. py:attribute:: visited :type: dict[str, conda._private.shards.typing.ShardDict | None] .. py:attribute:: _shard_url_cache :type: dict[str, str] .. py:property:: package_names Return the names of all packages available in this shard collection. .. py:property:: packages_index .. py:property:: shards_base_url :type: str Return self.url joined with shards_base_url. Note shards_base_url can be a relative or an absolute url. .. py:method:: shard_url(package: str) -> str Return shard URL for a given package. Raise KeyError if package is not in the index. .. py:method:: shard_loaded(package: str) -> bool Return True if the given package's shard is in memory. .. py:method:: visit_package(package: str) -> conda._private.shards.typing.ShardDict Return a shard that is already in memory and mark as visited. .. py:function:: _repodata_shards(url, cache: conda.gateways.repodata.RepodataCache) -> bytes Fetch shards index with cache. Update cache state. Return shards data, either newly fetched or from cache. In offline mode, returns cached data even if expired. If no cache exists in offline mode, raises RepodataIsEmpty to signal unavailability. .. py:function:: _shards_from_bytes(shards_data: bytes, shards_index_url: str) -> Shards Helper func to generate shards from shards byte data .. py:function:: fetch_shards_index(sd: conda.core.subdir_data.SubdirData) -> Shards | None Check a SubdirData's URL for shards. Return shards index bytes from cache or network. Return None if not found; caller should fetch normal repodata. TODO: If this function fails to retrieve the sharded repodata index file, it will mark it is as not supporting this feature in cache. This can problematic because sometimes server errors can happen which will lead it to wrongly assuming the channel doesn't support sharding. We need to rethink our logic for determining shard support. .. py:function:: batch_retrieve_from_cache(shardlikes: collections.abc.Sequence[ShardBase], packages: list[str], shard_cache: conda._private.shards.cache.ShardCache) -> list[ShardFetch] Given a list of ShardBase objects and a list of package names, fetch all URLs from a shared local cache, and update shardlikes with those per-package shards. Return ShardFetch objects for items not found in cache (to be fetched from network). .. py:function:: batch_retrieve_from_network(wanted: list[ShardFetch]) Fetch all shards in the wanted list from the network. Coordinate batch fetching across multiple ShardBase instances. .. py:function:: fetch_channels(url_to_channel: dict[str, conda.models.channel.Channel], *, require_shards: bool = True) -> dict[str, ShardBase] | None :param url_to_channel: not modified, must already be expanded to subdirs. :param require_shards: Return None unless at least one channel provides shards. Attempt to fetch the sharded index first and then fall back to retrieving a monolithic `repodata.json` file. :returns: A dict mapping channel URLs to `Shard` or `ShardLike` objects. If `require_shards` is true, return None when no channels have shards. This dict preserves the key order of the input `url_to_channel`.