conda
packages (can) have a summary and description in their metadata. I'd like to read those somehow using conda
, to help decide what packages to install. However, the most information I can find is from conda search -i
, e.g.
$ conda search -i optionsfactory
Loading channels: done
optionsfactory 1.0.3 py_0
-------------------------
file name : optionsfactory-1.0.3-py_0.tar.bz2
name : optionsfactory
version : 1.0.3
build : py_0
build number: 0
size : 22 KB
license : BSD-3-Clause
subdir : noarch
url : https://conda.anaconda.org/conda-forge/noarch/optionsfactory-1.0.3-py_0.tar.bz2
md5 : f8db4dd5600ad38b5f6ecae5f6f7d2ad
timestamp : 2020-05-13 08:04:25 UTC
dependencies:
- python >=3.6
which isn't helpful for finding out what the package actually does.
This seems to be a bug in conda
. The info-*.tar.zst/about.json
contains the summary
field that contains information about what a packages does.
It seems conda search
and conda info <package_name>
only output information that are saved in the .json
files created by conda index
which lack this information.
You could manually load the package and extract this part of the metadata yourself, like this:
#!/usr/bin/env python3
# extract_summary.py Extract the summary of a conda file
# Note does not support the old .tar.bz2 conda files, change the zipfile below if need this
import json
import tarfile
import zipfile
import sys
from collections.abc import Iterable
from pathlib import Path
import zstandard # need to run pip install zstandard first
def get_conda_pkg_info_files(
conda_pkg_file: Path,
) -> Iterable[tuple[str, dict[str, bytes]]]:
"""Extract conda package info files from conda pkg file"""
with zipfile.ZipFile(conda_pkg_file, "r") as conda_file:
for level1_file in conda_file.namelist():
if level1_file.startswith("info") and level1_file.endswith(".tar.zst"):
with conda_file.open(level1_file) as f_level1:
unzstd = zstandard.ZstdDecompressor()
with unzstd.stream_reader(f_level1) as stream:
with tarfile.open(mode="r|", fileobj=stream) as tf:
yield level1_file, {
member.name: tf.extractfile(member).read()
for member in tf
if not member.isdir()
}
def extract_summary(conda_pkg_file: Path) -> None:
"""Print summary found in conda_pkg"""
for file, tar_file in get_conda_pkg_info_files(conda_pkg_file):
json_file = tar_file["info/about.json"]
summary = json.loads(json_file)["summary"]
print(f"{conda_pkg_file}: {file} {summary}")
if __name__ == "__main__":
extract_summary(Path(sys.argv[1]))
To get the summary, you need first to download the *.conda
that is given as URL
in the conda search -i
result and the execute the script against:
$ wget https://conda.anaconda.org/conda-forge/noarch/optionsfactory-1.0.11-pyhd8ed1ab_0.conda
$ ./extract_summary.py optionsfactory-1.0.11-pyhd8ed1ab_0.conda
optionsfactory-1.0.11-pyhd8ed1ab_0.conda: info-optionsfactory-1.0.11-pyhd8ed1ab_0.tar.zst Python package for handling user-provided options with flexible defaults, documentation and checking