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
//! Import name → PyPI distribution name mapping for the `requirements` generator.
//!
//! Most PyPI distributions use the same name as their top-level import — we
//! default to identity. This table lists the common exceptions where the
//! import name differs from the distribution name.
//!
//! Users can override these via the `mapping` config field; user entries win.
/// Resolve a Python import name to a PyPI distribution name using the curated
/// table. Returns the distribution name if the import is mapped, or the
/// original import name otherwise. Callers should consult the user's
/// configured mapping first.
pub(crate) fn resolve_distribution(import_name: &str) -> &str {
MAPPINGS.binary_search_by_key(&import_name, |&(k, _)| k)
.ok()
.map(|i| MAPPINGS[i].1)
.unwrap_or(import_name)
}
/// Sorted list of (import_name, distribution_name) pairs. Must stay sorted —
/// `resolve_distribution` relies on binary search.
const MAPPINGS: &[(&str, &str)] = &[
("PIL", "Pillow"),
("attr", "attrs"),
("bs4", "beautifulsoup4"),
("cv2", "opencv-python"),
("dateutil", "python-dateutil"),
("discord", "discord.py"),
("dns", "dnspython"),
("docx", "python-docx"),
("dotenv", "python-dotenv"),
("fitz", "PyMuPDF"),
("git", "GitPython"),
("google", "google-api-python-client"),
("grpc", "grpcio"),
("gym", "gymnasium"),
("jwt", "PyJWT"),
("magic", "python-magic"),
("mpl_toolkits", "matplotlib"),
("mx", "egenix-mx-base"),
("nacl", "PyNaCl"),
("pptx", "python-pptx"),
("psycopg2", "psycopg2-binary"),
("pycountry", "pycountry"),
("pycryptodome", "pycryptodome"),
("serial", "pyserial"),
("skimage", "scikit-image"),
("sklearn", "scikit-learn"),
("slugify", "python-slugify"),
("socks", "PySocks"),
("tensorflow_datasets", "tensorflow-datasets"),
("tensorflow_hub", "tensorflow-hub"),
("tensorflow_probability", "tensorflow-probability"),
("uvicorn", "uvicorn"),
("win32api", "pywin32"),
("win32com", "pywin32"),
("win32con", "pywin32"),
("wx", "wxPython"),
("yaml", "PyYAML"),
("zmq", "pyzmq"),
];
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn mappings_are_sorted() {
for pair in MAPPINGS.windows(2) {
assert!(pair[0].0 < pair[1].0, "MAPPINGS not sorted: {} >= {}", pair[0].0, pair[1].0);
}
}
#[test]
fn known_mappings() {
assert_eq!(resolve_distribution("cv2"), "opencv-python");
assert_eq!(resolve_distribution("yaml"), "PyYAML");
assert_eq!(resolve_distribution("PIL"), "Pillow");
assert_eq!(resolve_distribution("sklearn"), "scikit-learn");
}
#[test]
fn unmapped_returns_identity() {
assert_eq!(resolve_distribution("requests"), "requests");
assert_eq!(resolve_distribution("numpy"), "numpy");
}
}