如何查看 conda 包的摘要或说明?



conda包(可以(在其元数据中包含摘要和描述。我想以某种方式使用conda阅读这些,以帮助决定要安装哪些软件包。但是,我能找到的最多信息来自conda search -i,例如

$ 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

这对找出包的实际作用没有帮助。

这似乎是conda中的一个错误。info-*.tar.zst/about.json包含summary字段,其中包含有关包功能的信息。

它似乎conda search并且conda info <package_name>仅输出保存在缺少此信息的conda index创建的.json文件中的信息。

您可以手动加载包并自己提取这部分元数据,如下所示:

#!/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]))

要获取摘要,首先需要下载conda search -i结果中作为URL给出的*.conda,并针对以下条件执行脚本:

$ 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

最新更新