15 个版本 (5 个重大更新)
0.6.1 | 2024 年 7 月 30 日 |
---|---|
0.5.0 | 2024 年 4 月 22 日 |
0.4.2 | 2024 年 2 月 18 日 |
0.2.3 | 2023 年 9 月 27 日 |
0.1.1 | 2023 年 3 月 29 日 |
#98 in 开发工具
67,749 每月下载量
用于 7 个 crate (5 个直接使用)
150KB
3K SLoC
Rust 中的依赖说明符 (PEP 508)
一个用于 Python 依赖说明符 的库,更广为人知的是 PEP 508.
用法
Rust 中
use std::str::FromStr;
use pep508_rs::Requirement;
let marker = r#"requests [security,tests] >= 2.8.1, == 2.8.* ; python_version > "3.8""#;
let dependency_specification = Requirement::from_str(marker).unwrap();
assert_eq!(dependency_specification.name, "requests");
assert_eq!(dependency_specification.extras, Some(vec!["security".to_string(), "tests".to_string()]));
Python 中
from pep508_rs import Requirement
requests = Requirement(
'requests [security,tests] >= 2.8.1, == 2.8.* ; python_version > "3.8"'
)
assert requests.name == "requests"
assert requests.extras == ["security", "tests"]
assert [str(i) for i in requests.version_or_url] == [">= 2.8.1", "== 2.8.*"]
Python 绑定是用 maturin 构建的,但您也可以使用正常的 pip install .
Version
和 VersionSpecifier
从 pep440_rs 中重新导出,以避免类型不匹配。
标记
标记允许您只在特定环境中安装依赖项(Python 版本、操作系统、架构等)或当特定功能被激活时安装依赖项。例如,您可以说 importlib-metadata ; python_version < "3.8"
或 itsdangerous (>=1.1.0) ; extra == 'security'
。不幸的是,标记语法有一些疏漏(例如 https://github.com/pypa/packaging.python.org/pull/1181)并且比较的设计(PEP 440 比较与字典序回退)导致了令人困惑的结果。此实现试图仔细验证一切,并在遇到不正确语义的比较时发出警告。
在 Python 中,警告默认发送到正常的 Python 日志基础设施
from pep508_rs import Requirement, MarkerEnvironment
env = MarkerEnvironment.current()
assert not Requirement("numpy; extra == 'science'").evaluate_markers(env, [])
assert Requirement("numpy; extra == 'science'").evaluate_markers(env, ["science"])
assert not Requirement(
"numpy; extra == 'science' and extra == 'arrays'"
).evaluate_markers(env, ["science"])
assert Requirement(
"numpy; extra == 'science' or extra == 'arrays'"
).evaluate_markers(env, ["science"])
from pep508_rs import Requirement, MarkerEnvironment
env = MarkerEnvironment.current()
Requirement("numpy; python_version >= '3.9.'").evaluate_markers(env, [])
# This will log:
# "Expected PEP 440 version to compare with python_version, found '3.9.', "
# "evaluating to false: Version `3.9.` doesn't match PEP 440 rules"
依赖项
~5–7.5MB
~165K SLoC