| 1 |
use crate::PyProjectToml; |
| 2 |
use anyhow::{bail, format_err, Context, Result}; |
| 3 |
use fs_err as fs; |
| 4 |
use indexmap::IndexMap; |
| 5 |
use normpath::PathExt; |
| 6 |
use pep440_rs::{Version, VersionSpecifiers}; |
| 7 |
use pep508_rs::{ |
| 8 |
ExtraName, ExtraOperator, MarkerExpression, MarkerTree, MarkerValueExtra, Requirement, |
| 9 |
}; |
| 10 |
use pyproject_toml::{check_pep639_glob, License}; |
| 11 |
use regex::Regex; |
| 12 |
use serde::{Deserialize, Serialize}; |
| 13 |
use std::collections::{HashMap, HashSet}; |
| 14 |
use std::fmt::Write as _; |
| 15 |
use std::path::{Path, PathBuf}; |
| 16 |
use std::str; |
| 17 |
use std::str::FromStr; |
| 18 |
use tracing::debug; |
| 19 |
|
| 20 |
/// The metadata required to generate the .dist-info directory |
| 21 |
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)] |
| 22 |
pub struct WheelMetadata { |
| 23 |
/// Python Package Metadata 2.4 |
| 24 |
pub metadata24: Metadata24, |
| 25 |
/// The `[console_scripts]` for the entry_points.txt |
| 26 |
pub scripts: HashMap<String, String>, |
| 27 |
/// The name of the module can be distinct from the package name, mostly |
| 28 |
/// because package names normally contain minuses while module names |
| 29 |
/// have underscores. The package name is part of metadata24 |
| 30 |
pub module_name: String, |
| 31 |
} |
| 32 |
|
| 33 |
/// Python Package Metadata 2.4 as specified in |
| 34 |
/// https://packaging.python.org/specifications/core-metadata/ |
| 35 |
/// Maturin writes static metadata and does not support dynamic fields atm. |
| 36 |
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)] |
| 37 |
#[serde(rename_all = "kebab-case")] |
| 38 |
#[allow(missing_docs)] |
| 39 |
pub struct Metadata24 { |
| 40 |
// Mandatory fields |
| 41 |
pub metadata_version: String, |
| 42 |
pub name: String, |
| 43 |
pub version: Version, |
| 44 |
// Optional fields |
| 45 |
pub platform: Vec<String>, |
| 46 |
pub supported_platform: Vec<String>, |
| 47 |
pub summary: Option<String>, |
| 48 |
pub description: Option<String>, |
| 49 |
pub description_content_type: Option<String>, |
| 50 |
pub keywords: Option<String>, |
| 51 |
pub home_page: Option<String>, |
| 52 |
pub download_url: Option<String>, |
| 53 |
pub author: Option<String>, |
| 54 |
pub author_email: Option<String>, |
| 55 |
pub maintainer: Option<String>, |
| 56 |
pub maintainer_email: Option<String>, |
| 57 |
pub license: Option<String>, |
| 58 |
pub license_expression: Option<String>, |
| 59 |
pub license_files: Vec<PathBuf>, |
| 60 |
pub classifiers: Vec<String>, |
| 61 |
pub requires_dist: Vec<Requirement>, |
| 62 |
pub provides_dist: Vec<String>, |
| 63 |
pub obsoletes_dist: Vec<String>, |
| 64 |
pub requires_python: Option<VersionSpecifiers>, |
| 65 |
pub requires_external: Vec<String>, |
| 66 |
pub project_url: IndexMap<String, String>, |
| 67 |
pub provides_extra: Vec<String>, |
| 68 |
pub scripts: IndexMap<String, String>, |
| 69 |
pub gui_scripts: IndexMap<String, String>, |
| 70 |
pub entry_points: IndexMap<String, IndexMap<String, String>>, |
| 71 |
} |
| 72 |
|
| 73 |
impl Metadata24 { |
| 74 |
/// Initializes with name, version and otherwise the defaults |
| 75 |
pub fn new(name: String, version: Version) -> Self { |
| 76 |
Self { |
| 77 |
metadata_version: "2.4".to_string(), |
| 78 |
name, |
| 79 |
version, |
| 80 |
platform: vec![], |
| 81 |
supported_platform: vec![], |
| 82 |
summary: None, |
| 83 |
description: None, |
| 84 |
description_content_type: None, |
| 85 |
keywords: None, |
| 86 |
home_page: None, |
| 87 |
download_url: None, |
| 88 |
author: None, |
| 89 |
author_email: None, |
| 90 |
maintainer: None, |
| 91 |
maintainer_email: None, |
| 92 |
license: None, |
| 93 |
license_expression: None, |
| 94 |
license_files: vec![], |
| 95 |
classifiers: vec![], |
| 96 |
requires_dist: vec![], |
| 97 |
provides_dist: vec![], |
| 98 |
obsoletes_dist: vec![], |
| 99 |
requires_python: None, |
| 100 |
requires_external: vec![], |
| 101 |
project_url: Default::default(), |
| 102 |
provides_extra: vec![], |
| 103 |
scripts: Default::default(), |
| 104 |
gui_scripts: Default::default(), |
| 105 |
entry_points: Default::default(), |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
const PLAINTEXT_CONTENT_TYPE: &str = "text/plain; charset=UTF-8"; |
| 111 |
const GFM_CONTENT_TYPE: &str = "text/markdown; charset=UTF-8; variant=GFM"; |
| 112 |
|
| 113 |
/// Guess a Description-Content-Type based on the file extension, |
| 114 |
/// defaulting to plaintext if extension is unknown or empty. |
| 115 |
/// |
| 116 |
/// See https://packaging.python.org/specifications/core-metadata/#description-content-type |
| 117 |
fn path_to_content_type(path: &Path) -> String { |
| 118 |
path.extension() |
| 119 |
.map_or(String::from(PLAINTEXT_CONTENT_TYPE), |ext| { |
| 120 |
let ext = ext.to_string_lossy().to_lowercase(); |
| 121 |
let type_str = match ext.as_str() { |
| 122 |
"rst" => "text/x-rst; charset=UTF-8", |
| 123 |
"md" => GFM_CONTENT_TYPE, |
| 124 |
"markdown" => GFM_CONTENT_TYPE, |
| 125 |
_ => PLAINTEXT_CONTENT_TYPE, |
| 126 |
}; |
| 127 |
String::from(type_str) |
| 128 |
}) |
| 129 |
} |
| 130 |
|
| 131 |
impl Metadata24 { |
| 132 |
/// Merge metadata with pyproject.toml, where pyproject.toml takes precedence |
| 133 |
/// |
| 134 |
/// pyproject_dir must be the directory containing pyproject.toml |
| 135 |
pub fn merge_pyproject_toml( |
| 136 |
&mut self, |
| 137 |
pyproject_dir: impl AsRef<Path>, |
| 138 |
pyproject_toml: &PyProjectToml, |
| 139 |
) -> Result<()> { |
| 140 |
let pyproject_dir = pyproject_dir.as_ref(); |
| 141 |
if let Some(project) = &pyproject_toml.project { |
| 142 |
let dynamic: HashSet<&str> = project |
| 143 |
.dynamic |
| 144 |
.as_ref() |
| 145 |
.map(|x| x.iter().map(AsRef::as_ref).collect()) |
| 146 |
.unwrap_or_default(); |
| 147 |
if dynamic.contains("name") { |
| 148 |
bail!("`project.dynamic` must not specify `name` in pyproject.toml"); |
| 149 |
} |
| 150 |
|
| 151 |
// According to PEP 621, build backends must not add metadata fields |
| 152 |
// that are not declared in the dynamic list. Clear fields from Cargo.toml |
| 153 |
// that are not in the dynamic list. |
| 154 |
if !dynamic.contains("description") { |
| 155 |
self.summary = None; |
| 156 |
} |
| 157 |
if !dynamic.contains("authors") { |
| 158 |
self.author = None; |
| 159 |
self.author_email = None; |
| 160 |
} |
| 161 |
if !dynamic.contains("maintainers") { |
| 162 |
self.maintainer = None; |
| 163 |
self.maintainer_email = None; |
| 164 |
} |
| 165 |
if !dynamic.contains("keywords") { |
| 166 |
self.keywords = None; |
| 167 |
} |
| 168 |
if !dynamic.contains("urls") { |
| 169 |
self.project_url.clear(); |
| 170 |
} |
| 171 |
if !dynamic.contains("license") { |
| 172 |
self.license = None; |
| 173 |
// Don't clear license_files as they may come from auto-discovery |
| 174 |
} |
| 175 |
if !dynamic.contains("classifiers") { |
| 176 |
self.classifiers.clear(); |
| 177 |
} |
| 178 |
if !dynamic.contains("readme") { |
| 179 |
self.description = None; |
| 180 |
self.description_content_type = None; |
| 181 |
} |
| 182 |
if !dynamic.contains("requires-python") { |
| 183 |
self.requires_python = None; |
| 184 |
} |
| 185 |
|
| 186 |
self.name.clone_from(&project.name); |
| 187 |
|
| 188 |
let version_ok = pyproject_toml.warn_invalid_version_info(); |
| 189 |
if !version_ok { |
| 190 |
// This is a hard error for maturin>=2.0, see https://github.com/PyO3/maturin/issues/2416. |
| 191 |
let current_major = env!("CARGO_PKG_VERSION_MAJOR").parse::<usize>().unwrap(); |
| 192 |
if current_major > 1 { |
| 193 |
bail!("Invalid version information in pyproject.toml."); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
if let Some(version) = &project.version { |
| 198 |
self.version = version.clone(); |
| 199 |
} |
| 200 |
|
| 201 |
if let Some(description) = &project.description { |
| 202 |
self.summary = Some(description.clone()); |
| 203 |
} |
| 204 |
|
| 205 |
match &project.readme { |
| 206 |
Some(pyproject_toml::ReadMe::RelativePath(readme_path)) => { |
| 207 |
let readme_path = pyproject_dir.join(readme_path); |
| 208 |
let description = Some(fs::read_to_string(&readme_path).context(format!( |
| 209 |
"Failed to read readme specified in pyproject.toml, which should be at {}", |
| 210 |
readme_path.display() |
| 211 |
))?); |
| 212 |
self.description = description; |
| 213 |
self.description_content_type = Some(path_to_content_type(&readme_path)); |
| 214 |
} |
| 215 |
Some(pyproject_toml::ReadMe::Table { |
| 216 |
file, |
| 217 |
text, |
| 218 |
content_type, |
| 219 |
}) => { |
| 220 |
if file.is_some() && text.is_some() { |
| 221 |
bail!("file and text fields of 'project.readme' are mutually-exclusive, only one of them should be specified"); |
| 222 |
} |
| 223 |
if let Some(readme_path) = file { |
| 224 |
let readme_path = pyproject_dir.join(readme_path); |
| 225 |
let description = Some(fs::read_to_string(&readme_path).context(format!( |
| 226 |
"Failed to read readme specified in pyproject.toml, which should be at {}", |
| 227 |
readme_path.display() |
| 228 |
))?); |
| 229 |
self.description = description; |
| 230 |
} |
| 231 |
if let Some(description) = text { |
| 232 |
self.description = Some(description.clone()); |
| 233 |
} |
| 234 |
self.description_content_type.clone_from(content_type); |
| 235 |
} |
| 236 |
None => {} |
| 237 |
} |
| 238 |
|
| 239 |
// Make sure existing license files from `Cargo.toml` are relative to the project root |
| 240 |
let license_files = std::mem::take(&mut self.license_files); |
| 241 |
for license_file in license_files { |
| 242 |
let license_path = license_file |
| 243 |
.strip_prefix(pyproject_dir) |
| 244 |
.with_context(|| { |
| 245 |
format!( |
| 246 |
"license file `{}` exists outside of the project root `{}`", |
| 247 |
license_file.display(), |
| 248 |
pyproject_dir.display() |
| 249 |
) |
| 250 |
})? |
| 251 |
.to_path_buf(); |
| 252 |
self.license_files.push(license_path); |
| 253 |
} |
| 254 |
|
| 255 |
if let Some(requires_python) = &project.requires_python { |
| 256 |
self.requires_python = Some(requires_python.clone()); |
| 257 |
} |
| 258 |
|
| 259 |
if let Some(license) = &project.license { |
| 260 |
match license { |
| 261 |
// PEP 639 |
| 262 |
License::Spdx(license_expr) => { |
| 263 |
self.license_expression = Some(license_expr.clone()) |
| 264 |
} |
| 265 |
// Deprecated by PEP 639 |
| 266 |
License::File { file } => { |
| 267 |
self.license_files.push(file.to_path_buf()); |
| 268 |
} |
| 269 |
License::Text { text } => self.license = Some(text.clone()), |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
// Handle PEP 639 license-files field |
| 274 |
if let Some(license_files) = &project.license_files { |
| 275 |
// Safe on Windows and Unix as neither forward nor backwards slashes are escaped. |
| 276 |
let escaped_pyproject_dir = |
| 277 |
PathBuf::from(glob::Pattern::escape(pyproject_dir.to_str().unwrap())); |
| 278 |
for license_glob in license_files { |
| 279 |
check_pep639_glob(license_glob)?; |
| 280 |
for license_path in |
| 281 |
glob::glob(&escaped_pyproject_dir.join(license_glob).to_string_lossy())? |
| 282 |
{ |
| 283 |
let license_path = license_path?; |
| 284 |
if !license_path.is_file() { |
| 285 |
continue; |
| 286 |
} |
| 287 |
let license_path = license_path |
| 288 |
.strip_prefix(pyproject_dir) |
| 289 |
.expect("matched path starts with glob root") |
| 290 |
.to_path_buf(); |
| 291 |
if !self.license_files.contains(&license_path) { |
| 292 |
debug!("Including license file `{}`", license_path.display()); |
| 293 |
self.license_files.push(license_path); |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
} else { |
| 298 |
// Auto-discovery of license files for backwards compatibility |
| 299 |
// license-files.globs = ["LICEN[CS]E*", "COPYING*", "NOTICE*", "AUTHORS*"] |
| 300 |
let license_include_targets = ["LICEN[CS]E*", "COPYING*", "NOTICE*", "AUTHORS*"]; |
| 301 |
let escaped_manifest_string = |
| 302 |
glob::Pattern::escape(pyproject_dir.to_str().unwrap()); |
| 303 |
let escaped_manifest_path = Path::new(&escaped_manifest_string); |
| 304 |
for pattern in license_include_targets.iter() { |
| 305 |
for license_path in |
| 306 |
glob::glob(&escaped_manifest_path.join(pattern).to_string_lossy())? |
| 307 |
.filter_map(Result::ok) |
| 308 |
{ |
| 309 |
if !license_path.is_file() { |
| 310 |
continue; |
| 311 |
} |
| 312 |
let license_path = license_path |
| 313 |
.strip_prefix(pyproject_dir) |
| 314 |
.expect("matched path starts with glob root") |
| 315 |
.to_path_buf(); |
| 316 |
// if the pyproject.toml specified the license file, |
| 317 |
// then we won't list it as automatically included |
| 318 |
if !self.license_files.contains(&license_path) { |
| 319 |
eprintln!("📦 Including license file `{}`", license_path.display()); |
| 320 |
self.license_files.push(license_path); |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
if let Some(authors) = &project.authors { |
| 327 |
let mut names = Vec::with_capacity(authors.len()); |
| 328 |
let mut emails = Vec::with_capacity(authors.len()); |
| 329 |
for author in authors { |
| 330 |
match (author.name(), author.email()) { |
| 331 |
(Some(name), Some(email)) => { |
| 332 |
emails.push(escape_email_with_display_name(name, email)); |
| 333 |
} |
| 334 |
(Some(name), None) => { |
| 335 |
names.push(name); |
| 336 |
} |
| 337 |
(None, Some(email)) => { |
| 338 |
emails.push(email.to_string()); |
| 339 |
} |
| 340 |
(None, None) => {} |
| 341 |
} |
| 342 |
} |
| 343 |
if !names.is_empty() { |
| 344 |
self.author = Some(names.join(", ")); |
| 345 |
} |
| 346 |
if !emails.is_empty() { |
| 347 |
self.author_email = Some(emails.join(", ")); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
if let Some(maintainers) = &project.maintainers { |
| 352 |
let mut names = Vec::with_capacity(maintainers.len()); |
| 353 |
let mut emails = Vec::with_capacity(maintainers.len()); |
| 354 |
for maintainer in maintainers { |
| 355 |
match (maintainer.name(), maintainer.email()) { |
| 356 |
(Some(name), Some(email)) => { |
| 357 |
emails.push(escape_email_with_display_name(name, email)); |
| 358 |
} |
| 359 |
(Some(name), None) => { |
| 360 |
names.push(name); |
| 361 |
} |
| 362 |
(None, Some(email)) => { |
| 363 |
emails.push(email.to_string()); |
| 364 |
} |
| 365 |
(None, None) => {} |
| 366 |
} |
| 367 |
} |
| 368 |
if !names.is_empty() { |
| 369 |
self.maintainer = Some(names.join(", ")); |
| 370 |
} |
| 371 |
if !emails.is_empty() { |
| 372 |
self.maintainer_email = Some(emails.join(", ")); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
if let Some(keywords) = &project.keywords { |
| 377 |
self.keywords = Some(keywords.join(",")); |
| 378 |
} |
| 379 |
|
| 380 |
if let Some(classifiers) = &project.classifiers { |
| 381 |
self.classifiers.clone_from(classifiers); |
| 382 |
} |
| 383 |
|
| 384 |
if let Some(urls) = &project.urls { |
| 385 |
self.project_url.clone_from(urls); |
| 386 |
} |
| 387 |
|
| 388 |
if let Some(dependencies) = &project.dependencies { |
| 389 |
self.requires_dist.clone_from(dependencies); |
| 390 |
} |
| 391 |
|
| 392 |
if let Some(dependencies) = &project.optional_dependencies { |
| 393 |
// Transform the extra -> deps map into the PEP 508 style `dep ; extras = ...` style |
| 394 |
for (extra, deps) in dependencies { |
| 395 |
self.provides_extra.push(extra.clone()); |
| 396 |
for dep in deps { |
| 397 |
let mut dep = dep.clone(); |
| 398 |
// Keep in sync with `develop()`! |
| 399 |
let new_extra = MarkerExpression::Extra { |
| 400 |
operator: ExtraOperator::Equal, |
| 401 |
name: MarkerValueExtra::Extra( |
| 402 |
ExtraName::new(extra.clone()) |
| 403 |
.with_context(|| format!("invalid extra name: {extra}"))?, |
| 404 |
), |
| 405 |
}; |
| 406 |
dep.marker.and(MarkerTree::expression(new_extra)); |
| 407 |
self.requires_dist.push(dep); |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
if let Some(scripts) = &project.scripts { |
| 413 |
self.scripts.clone_from(scripts); |
| 414 |
} |
| 415 |
if let Some(gui_scripts) = &project.gui_scripts { |
| 416 |
self.gui_scripts.clone_from(gui_scripts); |
| 417 |
} |
| 418 |
if let Some(entry_points) = &project.entry_points { |
| 419 |
// Raise error on ambiguous entry points: https://www.python.org/dev/peps/pep-0621/#entry-points |
| 420 |
if entry_points.contains_key("console_scripts") { |
| 421 |
bail!("console_scripts is not allowed in project.entry-points table"); |
| 422 |
} |
| 423 |
if entry_points.contains_key("gui_scripts") { |
| 424 |
bail!("gui_scripts is not allowed in project.entry-points table"); |
| 425 |
} |
| 426 |
self.entry_points.clone_from(entry_points); |
| 427 |
} |
| 428 |
} |
| 429 |
Ok(()) |
| 430 |
} |
| 431 |
|
| 432 |
/// Uses a Cargo.toml to create the metadata for python packages |
| 433 |
/// |
| 434 |
/// manifest_path must be the directory, not the file |
| 435 |
pub fn from_cargo_toml( |
| 436 |
manifest_path: impl AsRef<Path>, |
| 437 |
cargo_metadata: &cargo_metadata::Metadata, |
| 438 |
) -> Result<Metadata24> { |
| 439 |
let package = cargo_metadata |
| 440 |
.root_package() |
| 441 |
.context("Expected cargo to return metadata with root_package")?; |
| 442 |
let authors = package.authors.join(", "); |
| 443 |
let author_email = if authors.contains('@') { |
| 444 |
Some(authors.clone()) |
| 445 |
} else { |
| 446 |
None |
| 447 |
}; |
| 448 |
|
| 449 |
let mut description: Option<String> = None; |
| 450 |
let mut description_content_type: Option<String> = None; |
| 451 |
// See https://packaging.python.org/specifications/core-metadata/#description |
| 452 |
// and https://doc.rust-lang.org/cargo/reference/manifest.html#the-readme-field |
| 453 |
if package.readme == Some("false".into()) { |
| 454 |
// > You can suppress this behavior by setting this field to false |
| 455 |
} else if let Some(ref readme) = package.readme { |
| 456 |
let readme_path = manifest_path.as_ref().join(readme); |
| 457 |
description = Some(fs::read_to_string(&readme_path).context(format!( |
| 458 |
"Failed to read Readme specified in Cargo.toml, which should be at {}", |
| 459 |
readme_path.display() |
| 460 |
))?); |
| 461 |
|
| 462 |
description_content_type = Some(path_to_content_type(&readme_path)); |
| 463 |
} else { |
| 464 |
// > If no value is specified for this field, and a file named |
| 465 |
// > README.md, README.txt or README exists in the package root |
| 466 |
// Even though it's not what cargo does, we also search for README.rst |
| 467 |
// since it's still popular in the python world |
| 468 |
for readme_guess in ["README.md", "README.txt", "README.rst", "README"] { |
| 469 |
let guessed_readme = manifest_path.as_ref().join(readme_guess); |
| 470 |
if guessed_readme.exists() { |
| 471 |
let context = format!( |
| 472 |
"Readme at {} exists, but can't be read", |
| 473 |
guessed_readme.display() |
| 474 |
); |
| 475 |
description = Some(fs::read_to_string(&guessed_readme).context(context)?); |
| 476 |
description_content_type = Some(path_to_content_type(&guessed_readme)); |
| 477 |
break; |
| 478 |
} |
| 479 |
} |
| 480 |
}; |
| •481 |
let name = package.name.to_string(); |
| 482 |
let mut project_url = IndexMap::new(); |
| 483 |
if let Some(repository) = package.repository.as_ref() { |
| 484 |
project_url.insert("Source Code".to_string(), repository.clone()); |
| 485 |
} |
| 486 |
let license_files = if let Some(license_file) = package.license_file.as_ref() { |
| 487 |
let license_path = manifest_path.as_ref().join(license_file).normalize()?; |
| 488 |
if !license_path.is_file() { |
| 489 |
bail!( |
| 490 |
"license file `{license_file}` specified in `{}` is not a file", |
| 491 |
manifest_path.as_ref().display() |
| 492 |
); |
| 493 |
} |
| 494 |
vec![license_path.into_path_buf()] |
| 495 |
} else { |
| 496 |
Vec::new() |
| 497 |
}; |
| 498 |
|
| 499 |
let version = Version::from_str(&package.version.to_string()).map_err(|err| { |
| 500 |
format_err!( |
| 501 |
"Rust version used in Cargo.toml is not a valid python version: {}. \ |
| 502 |
Note that rust uses [SemVer](https://semver.org/) while python uses \ |
| 503 |
[PEP 440](https://peps.python.org/pep-0440/), which have e.g. some differences \ |
| 504 |
when declaring prereleases.", |
| 505 |
err |
| 506 |
) |
| 507 |
})?; |
| 508 |
let metadata = Metadata24 { |
| 509 |
// name, version and metadata_version are added through metadata24::new() |
| 510 |
// Mapped from cargo metadata |
| 511 |
summary: package.description.as_ref().map(|d| d.trim().into()), |
| 512 |
description, |
| 513 |
description_content_type, |
| 514 |
keywords: if package.keywords.is_empty() { |
| 515 |
None |
| 516 |
} else { |
| 517 |
Some(package.keywords.join(",")) |
| 518 |
}, |
| 519 |
home_page: package.homepage.clone(), |
| 520 |
download_url: None, |
| 521 |
// Cargo.toml has no distinction between author and author email |
| 522 |
author: if package.authors.is_empty() { |
| 523 |
None |
| 524 |
} else { |
| 525 |
Some(authors) |
| 526 |
}, |
| 527 |
author_email, |
| 528 |
license: package.license.clone(), |
| 529 |
license_files, |
| 530 |
project_url, |
| 531 |
..Metadata24::new(name, version) |
| 532 |
}; |
| 533 |
Ok(metadata) |
| 534 |
} |
| 535 |
|
| 536 |
/// Formats the metadata into a list where keys with multiple values |
| 537 |
/// become multiple single-valued key-value pairs. This format is needed for the pypi |
| 538 |
/// uploader and for the METADATA file inside wheels |
| 539 |
pub fn to_vec(&self) -> Vec<(String, String)> { |
| 540 |
let mut fields = vec![ |
| 541 |
("Metadata-Version", self.metadata_version.clone()), |
| 542 |
("Name", self.name.clone()), |
| 543 |
("Version", self.version.to_string()), |
| 544 |
]; |
| 545 |
|
| 546 |
let mut add_vec = |name, values: &[String]| { |
| 547 |
for i in values { |
| 548 |
fields.push((name, i.clone())); |
| 549 |
} |
| 550 |
}; |
| 551 |
|
| 552 |
add_vec("Platform", &self.platform); |
| 553 |
add_vec("Supported-Platform", &self.supported_platform); |
| 554 |
add_vec("Classifier", &self.classifiers); |
| 555 |
add_vec( |
| 556 |
"Requires-Dist", |
| 557 |
&self |
| 558 |
.requires_dist |
| 559 |
.iter() |
| 560 |
.map(ToString::to_string) |
| 561 |
.collect::<Vec<String>>(), |
| 562 |
); |
| 563 |
add_vec("Provides-Dist", &self.provides_dist); |
| 564 |
add_vec("Obsoletes-Dist", &self.obsoletes_dist); |
| 565 |
add_vec("Requires-External", &self.requires_external); |
| 566 |
add_vec("Provides-Extra", &self.provides_extra); |
| 567 |
|
| 568 |
let license_files: Vec<String> = self |
| 569 |
.license_files |
| 570 |
.iter() |
| 571 |
// Use a portable path to ensure the metadata is consistent between Unix and Windows. |
| 572 |
.map(|path| path.display().to_string().replace("\\", "/")) |
| 573 |
.collect(); |
| 574 |
add_vec("License-File", &license_files); |
| 575 |
|
| 576 |
let mut add_option = |name, value: &Option<String>| { |
| 577 |
if let Some(some) = value.clone() { |
| 578 |
fields.push((name, some)); |
| 579 |
} |
| 580 |
}; |
| 581 |
|
| 582 |
add_option("Summary", &self.summary); |
| 583 |
add_option("Keywords", &self.keywords); |
| 584 |
add_option("Home-Page", &self.home_page); |
| 585 |
add_option("Download-URL", &self.download_url); |
| 586 |
add_option("Author", &self.author); |
| 587 |
add_option("Author-email", &self.author_email); |
| 588 |
add_option("Maintainer", &self.maintainer); |
| 589 |
add_option("Maintainer-email", &self.maintainer_email); |
| 590 |
// PEP 639 |
| 591 |
add_option("License-Expression", &self.license_expression); |
| 592 |
// Deprecated by PEP 639 |
| 593 |
add_option("License", &self.license.as_deref().map(fold_header)); |
| 594 |
add_option( |
| 595 |
"Requires-Python", |
| 596 |
&self |
| 597 |
.requires_python |
| 598 |
.as_ref() |
| 599 |
.map(|requires_python| requires_python.to_string()), |
| 600 |
); |
| 601 |
add_option("Description-Content-Type", &self.description_content_type); |
| 602 |
// Project-URL is special |
| 603 |
// "A string containing a browsable URL for the project and a label for it, separated by a comma." |
| 604 |
// `Project-URL: Bug Tracker, http://bitbucket.org/tarek/distribute/issues/` |
| 605 |
for (key, value) in self.project_url.iter() { |
| 606 |
fields.push(("Project-URL", format!("{key}, {value}"))) |
| 607 |
} |
| 608 |
|
| 609 |
// Description shall be last, so we can ignore RFC822 and just put the description |
| 610 |
// in the body |
| 611 |
// The borrow checker doesn't like us using add_option here |
| 612 |
if let Some(description) = &self.description { |
| 613 |
fields.push(("Description", description.clone())); |
| 614 |
} |
| 615 |
|
| 616 |
fields |
| 617 |
.into_iter() |
| 618 |
.map(|(k, v)| (k.to_string(), v)) |
| 619 |
.collect() |
| 620 |
} |
| 621 |
|
| 622 |
/// Writes the format for the metadata file inside wheels |
| 623 |
pub fn to_file_contents(&self) -> Result<String> { |
| 624 |
let mut fields = self.to_vec(); |
| 625 |
let mut out = "".to_string(); |
| 626 |
let body = match fields.last() { |
| 627 |
Some((key, description)) if key == "Description" => { |
| 628 |
let desc = description.clone(); |
| 629 |
fields.pop().unwrap(); |
| 630 |
Some(desc) |
| 631 |
} |
| 632 |
Some((_, _)) => None, |
| 633 |
None => None, |
| 634 |
}; |
| 635 |
|
| 636 |
for (key, value) in fields { |
| 637 |
writeln!(out, "{key}: {value}")?; |
| 638 |
} |
| 639 |
|
| 640 |
if let Some(body) = body { |
| 641 |
writeln!(out, "\n{body}")?; |
| 642 |
} |
| 643 |
|
| 644 |
Ok(out) |
| 645 |
} |
| 646 |
|
| 647 |
/// Returns the distribution name according to PEP 427, Section "Escaping |
| 648 |
/// and Unicode" |
| 649 |
pub fn get_distribution_escaped(&self) -> String { |
| 650 |
let re = Regex::new(r"[^\w\d.]+").unwrap(); |
| 651 |
re.replace_all(&self.name, "_").to_string() |
| 652 |
} |
| 653 |
|
| 654 |
/// Returns the version encoded according to PEP 427, Section "Escaping |
| 655 |
/// and Unicode" |
| 656 |
pub fn get_version_escaped(&self) -> String { |
| 657 |
self.version.to_string().replace('-', "_") |
| 658 |
} |
| 659 |
|
| 660 |
/// Returns the name of the .dist-info directory as defined in the wheel specification |
| 661 |
pub fn get_dist_info_dir(&self) -> PathBuf { |
| 662 |
PathBuf::from(format!( |
| 663 |
"{}-{}.dist-info", |
| 664 |
&self.get_distribution_escaped(), |
| 665 |
&self.get_version_escaped() |
| 666 |
)) |
| 667 |
} |
| 668 |
|
| 669 |
/// Returns the name of the .data directory as defined in the wheel specification |
| 670 |
pub fn get_data_dir(&self) -> PathBuf { |
| 671 |
PathBuf::from(format!( |
| 672 |
"{}-{}.data", |
| 673 |
&self.get_distribution_escaped(), |
| 674 |
&self.get_version_escaped() |
| 675 |
)) |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
/// Escape email addresses with display name if necessary |
| 680 |
/// according to RFC 822 Section 3.3. "specials". |
| 681 |
fn escape_email_with_display_name(display_name: &str, email: &str) -> String { |
| 682 |
if display_name.chars().any(|c| { |
| 683 |
matches!( |
| 684 |
c, |
| 685 |
'(' | ')' | '<' | '>' | '@' | ',' | ';' | ':' | '\\' | '"' | '.' | '[' | ']' |
| 686 |
) |
| 687 |
}) { |
| 688 |
return format!( |
| 689 |
"\"{}\" <{email}>", |
| 690 |
display_name.replace('\\', "\\\\").replace('\"', "\\\"") |
| 691 |
); |
| 692 |
} |
| 693 |
format!("{display_name} <{email}>") |
| 694 |
} |
| 695 |
|
| 696 |
/// Fold long header field according to RFC 5322 section 2.2.3 |
| 697 |
/// https://datatracker.ietf.org/doc/html/rfc5322#section-2.2.3 |
| 698 |
fn fold_header(text: &str) -> String { |
| 699 |
let mut result = String::with_capacity(text.len()); |
| 700 |
|
| 701 |
let options = textwrap::Options::new(78) |
| 702 |
.initial_indent("") |
| 703 |
.subsequent_indent("\t"); |
| 704 |
for (i, line) in textwrap::wrap(text, options).iter().enumerate() { |
| 705 |
if i > 0 { |
| 706 |
result.push_str("\r\n"); |
| 707 |
} |
| 708 |
let line = line.trim_end(); |
| 709 |
if line.is_empty() { |
| 710 |
result.push('\t'); |
| 711 |
} else { |
| 712 |
result.push_str(line); |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
result |
| 717 |
} |
| 718 |
|
| 719 |
#[cfg(test)] |
| 720 |
mod test { |
| 721 |
use super::*; |
| 722 |
use cargo_metadata::MetadataCommand; |
| 723 |
use expect_test::{expect, Expect}; |
| 724 |
use indoc::indoc; |
| 725 |
use pretty_assertions::assert_eq; |
| 726 |
use tempfile::TempDir; |
| 727 |
|
| 728 |
fn assert_metadata_from_cargo_toml( |
| 729 |
readme: &str, |
| 730 |
cargo_toml: &str, |
| 731 |
expected: Expect, |
| 732 |
) -> Metadata24 { |
| 733 |
let crate_dir = tempfile::tempdir().unwrap(); |
| 734 |
let crate_path = crate_dir.path(); |
| 735 |
let manifest_path = crate_path.join("Cargo.toml"); |
| 736 |
fs::create_dir(crate_path.join("src")).unwrap(); |
| 737 |
fs::write(crate_path.join("src/lib.rs"), "").unwrap(); |
| 738 |
|
| 739 |
let readme_path = crate_path.join("README.md"); |
| 740 |
fs::write(&readme_path, readme.as_bytes()).unwrap(); |
| 741 |
|
| 742 |
let readme_path = if cfg!(windows) { |
| 743 |
readme_path.to_str().unwrap().replace('\\', "/") |
| 744 |
} else { |
| 745 |
readme_path.to_str().unwrap().to_string() |
| 746 |
}; |
| 747 |
|
| 748 |
let toml_with_path = cargo_toml.replace("REPLACE_README_PATH", &readme_path); |
| 749 |
fs::write(&manifest_path, toml_with_path).unwrap(); |
| 750 |
|
| 751 |
let cargo_metadata = MetadataCommand::new() |
| 752 |
.manifest_path(manifest_path) |
| 753 |
.exec() |
| 754 |
.unwrap(); |
| 755 |
|
| 756 |
let metadata = Metadata24::from_cargo_toml(crate_path, &cargo_metadata).unwrap(); |
| 757 |
|
| 758 |
let actual = metadata.to_file_contents().unwrap(); |
| 759 |
|
| 760 |
expected.assert_eq(&actual); |
| 761 |
|
| 762 |
// get_dist_info_dir test checks against hard-coded values - check that they are as expected in the source first |
| 763 |
assert!( |
| 764 |
cargo_toml.contains("name = \"info-project\"") |
| 765 |
&& cargo_toml.contains("version = \"0.1.0\""), |
| 766 |
"cargo_toml name and version string do not match hardcoded values, test will fail", |
| 767 |
); |
| 768 |
|
| 769 |
metadata |
| 770 |
} |
| 771 |
|
| 772 |
#[test] |
| 773 |
fn test_metadata_from_cargo_toml() { |
| 774 |
let readme = indoc!( |
| 775 |
r#" |
| 776 |
# Some test package |
| 777 |
|
| 778 |
This is the readme for a test package |
| 779 |
"# |
| 780 |
); |
| 781 |
|
| 782 |
let cargo_toml = indoc!( |
| 783 |
r#" |
| 784 |
[package] |
| 785 |
authors = ["konstin <[email protected]>"] |
| 786 |
name = "info-project" |
| 787 |
version = "0.1.0" |
| 788 |
description = """ |
| 789 |
A test project |
| 790 |
""" |
| 791 |
homepage = "https://example.org" |
| 792 |
readme = "REPLACE_README_PATH" |
| 793 |
keywords = ["ffi", "test"] |
| 794 |
|
| 795 |
[lib] |
| 796 |
crate-type = ["cdylib"] |
| 797 |
name = "pyo3_pure" |
| 798 |
"# |
| 799 |
); |
| 800 |
|
| 801 |
let expected = expect![[r#" |
| 802 |
Metadata-Version: 2.4 |
| 803 |
Name: info-project |
| 804 |
Version: 0.1.0 |
| 805 |
Summary: A test project |
| 806 |
Keywords: ffi,test |
| 807 |
Home-Page: https://example.org |
| 808 |
Author: konstin <[email protected]> |
| 809 |
Author-email: konstin <[email protected]> |
| 810 |
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM |
| 811 |
|
| 812 |
# Some test package |
| 813 |
|
| 814 |
This is the readme for a test package |
| 815 |
|
| 816 |
"#]]; |
| 817 |
|
| 818 |
assert_metadata_from_cargo_toml(readme, cargo_toml, expected); |
| 819 |
} |
| 820 |
|
| 821 |
#[test] |
| 822 |
fn test_path_to_content_type() { |
| 823 |
for (filename, expected) in &[ |
| 824 |
("r.md", GFM_CONTENT_TYPE), |
| 825 |
("r.markdown", GFM_CONTENT_TYPE), |
| 826 |
("r.mArKdOwN", GFM_CONTENT_TYPE), |
| 827 |
("r.rst", "text/x-rst; charset=UTF-8"), |
| 828 |
("r.somethingelse", PLAINTEXT_CONTENT_TYPE), |
| 829 |
("r", PLAINTEXT_CONTENT_TYPE), |
| 830 |
] { |
| 831 |
let result = path_to_content_type(&PathBuf::from(filename)); |
| 832 |
assert_eq!( |
| 833 |
&result.as_str(), |
| 834 |
expected, |
| 835 |
"Wrong content type for file '{}'. Expected '{}', got '{}'", |
| 836 |
filename, |
| 837 |
expected, |
| 838 |
result |
| 839 |
); |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
#[test] |
| 844 |
fn test_merge_metadata_from_pyproject_toml() { |
| 845 |
let manifest_dir = PathBuf::from("test-crates").join("pyo3-pure"); |
| 846 |
let cargo_metadata = MetadataCommand::new() |
| 847 |
.manifest_path(manifest_dir.join("Cargo.toml")) |
| 848 |
.exec() |
| 849 |
.unwrap(); |
| 850 |
let mut metadata = Metadata24::from_cargo_toml(&manifest_dir, &cargo_metadata).unwrap(); |
| 851 |
let pyproject_toml = PyProjectToml::new(manifest_dir.join("pyproject.toml")).unwrap(); |
| 852 |
metadata |
| 853 |
.merge_pyproject_toml(&manifest_dir, &pyproject_toml) |
| 854 |
.unwrap(); |
| 855 |
assert_eq!( |
| 856 |
metadata.summary, |
| 857 |
Some("Implements a dummy function in Rust".to_string()) |
| 858 |
); |
| 859 |
assert_eq!( |
| 860 |
metadata.description, |
| 861 |
Some(fs_err::read_to_string("test-crates/pyo3-pure/README.md").unwrap()) |
| 862 |
); |
| 863 |
assert_eq!(metadata.classifiers, &["Programming Language :: Rust"]); |
| 864 |
assert_eq!( |
| 865 |
metadata.maintainer_email, |
| 866 |
Some("messense <[email protected]>".to_string()) |
| 867 |
); |
| 868 |
assert_eq!(metadata.scripts["get_42"], "pyo3_pure:DummyClass.get_42"); |
| 869 |
assert_eq!( |
| 870 |
metadata.gui_scripts["get_42_gui"], |
| 871 |
"pyo3_pure:DummyClass.get_42" |
| 872 |
); |
| 873 |
assert_eq!(metadata.provides_extra, &["test"]); |
| 874 |
assert_eq!( |
| 875 |
metadata.requires_dist, |
| 876 |
&[ |
| 877 |
Requirement::from_str("attrs; extra == 'test'",).unwrap(), |
| 878 |
Requirement::from_str("boltons; (sys_platform == 'win32') and extra == 'test'") |
| 879 |
.unwrap(), |
| 880 |
] |
| 881 |
); |
| 882 |
assert_eq!(metadata.license.as_ref().unwrap(), "MIT"); |
| 883 |
|
| 884 |
let license_file = &metadata.license_files[0]; |
| 885 |
assert_eq!(license_file.file_name().unwrap(), "LICENSE"); |
| 886 |
|
| 887 |
let content = metadata.to_file_contents().unwrap(); |
| 888 |
let pkginfo: Result<python_pkginfo::Metadata, _> = content.parse(); |
| 889 |
assert!(pkginfo.is_ok()); |
| 890 |
} |
| 891 |
|
| 892 |
#[test] |
| 893 |
fn test_merge_metadata_from_pyproject_toml_with_customized_python_source_dir() { |
| 894 |
let manifest_dir = PathBuf::from("test-crates").join("pyo3-mixed-py-subdir"); |
| 895 |
let cargo_metadata = MetadataCommand::new() |
| 896 |
.manifest_path(manifest_dir.join("Cargo.toml")) |
| 897 |
.exec() |
| 898 |
.unwrap(); |
| 899 |
let mut metadata = Metadata24::from_cargo_toml(&manifest_dir, &cargo_metadata).unwrap(); |
| 900 |
let pyproject_toml = PyProjectToml::new(manifest_dir.join("pyproject.toml")).unwrap(); |
| 901 |
metadata |
| 902 |
.merge_pyproject_toml(&manifest_dir, &pyproject_toml) |
| 903 |
.unwrap(); |
| 904 |
// defined in Cargo.toml |
| 905 |
assert_eq!( |
| 906 |
metadata.summary, |
| 907 |
Some("Implements a dummy function combining rust and python".to_string()) |
| 908 |
); |
| 909 |
// defined in pyproject.toml |
| 910 |
assert_eq!(metadata.scripts["get_42"], "pyo3_mixed_py_subdir:get_42"); |
| 911 |
} |
| 912 |
|
| 913 |
#[test] |
| 914 |
fn test_implicit_readme() { |
| 915 |
let manifest_dir = PathBuf::from("test-crates").join("pyo3-mixed"); |
| 916 |
let cargo_metadata = MetadataCommand::new() |
| 917 |
.manifest_path(manifest_dir.join("Cargo.toml")) |
| 918 |
.exec() |
| 919 |
.unwrap(); |
| 920 |
let metadata = Metadata24::from_cargo_toml(&manifest_dir, &cargo_metadata).unwrap(); |
| 921 |
assert!(metadata.description.unwrap().starts_with("# pyo3-mixed")); |
| 922 |
assert_eq!( |
| 923 |
metadata.description_content_type.unwrap(), |
| 924 |
"text/markdown; charset=UTF-8; variant=GFM" |
| 925 |
); |
| 926 |
} |
| 927 |
|
| 928 |
#[test] |
| 929 |
fn test_pep639() { |
| 930 |
let manifest_dir = PathBuf::from("test-crates").join("pyo3-mixed"); |
| 931 |
let cargo_metadata = MetadataCommand::new() |
| 932 |
.manifest_path(manifest_dir.join("Cargo.toml")) |
| 933 |
.exec() |
| 934 |
.unwrap(); |
| 935 |
let mut metadata = Metadata24::from_cargo_toml(&manifest_dir, &cargo_metadata).unwrap(); |
| 936 |
let pyproject_toml = PyProjectToml::new(manifest_dir.join("pyproject.toml")).unwrap(); |
| 937 |
metadata |
| 938 |
.merge_pyproject_toml(&manifest_dir, &pyproject_toml) |
| 939 |
.unwrap(); |
| 940 |
|
| 941 |
assert_eq!(metadata.license_expression.as_ref().unwrap(), "MIT"); |
| 942 |
assert_eq!(metadata.license.as_ref(), None); |
| 943 |
} |
| 944 |
|
| 945 |
#[test] |
| 946 |
fn test_merge_metadata_from_pyproject_dynamic_license_test() { |
| 947 |
let manifest_dir = PathBuf::from("test-crates").join("license-test"); |
| 948 |
let cargo_metadata = MetadataCommand::new() |
| 949 |
.manifest_path(manifest_dir.join("Cargo.toml")) |
| 950 |
.exec() |
| 951 |
.unwrap(); |
| 952 |
let mut metadata = Metadata24::from_cargo_toml(&manifest_dir, &cargo_metadata).unwrap(); |
| 953 |
let pyproject_toml = PyProjectToml::new(manifest_dir.join("pyproject.toml")).unwrap(); |
| 954 |
metadata |
| 955 |
.merge_pyproject_toml(&manifest_dir, &pyproject_toml) |
| 956 |
.unwrap(); |
| 957 |
|
| 958 |
// verify Cargo.toml value came through |
| 959 |
assert_eq!(metadata.license.as_ref().unwrap(), "MIT"); |
| 960 |
|
| 961 |
// verify we have the total number of expected licenses |
| 962 |
assert_eq!(4, metadata.license_files.len()); |
| 963 |
|
| 964 |
// Verify pyproject.toml license = {file = ...} worked |
| 965 |
assert_eq!(metadata.license_files[0], PathBuf::from("LICENCE.txt")); |
| 966 |
|
| 967 |
// Verify the default licenses were included |
| 968 |
assert_eq!(metadata.license_files[1], PathBuf::from("LICENSE")); |
| 969 |
assert_eq!(metadata.license_files[2], PathBuf::from("NOTICE.md")); |
| 970 |
assert_eq!(metadata.license_files[3], PathBuf::from("AUTHORS.txt")); |
| 971 |
} |
| 972 |
|
| 973 |
#[test] |
| 974 |
fn test_escape_email_with_display_name_without_special_characters() { |
| 975 |
let display_name = "Foo Bar !#$%&'*+-/=?^_`{|}~ 123"; |
| 976 |
let email = "[email protected]"; |
| 977 |
let result = escape_email_with_display_name(display_name, email); |
| 978 |
assert_eq!( |
| 979 |
result, |
| 980 |
"Foo Bar !#$%&'*+-/=?^_`{|}~ 123 <[email protected]>" |
| 981 |
); |
| 982 |
} |
| 983 |
|
| 984 |
#[test] |
| 985 |
fn test_escape_email_with_display_name_with_special_characters() { |
| 986 |
let tests = [ |
| 987 |
("Foo ( Bar", "\"Foo ( Bar\""), |
| 988 |
("Foo ) Bar", "\"Foo ) Bar\""), |
| 989 |
("Foo < Bar", "\"Foo < Bar\""), |
| 990 |
("Foo > Bar", "\"Foo > Bar\""), |
| 991 |
("Foo @ Bar", "\"Foo @ Bar\""), |
| 992 |
("Foo , Bar", "\"Foo , Bar\""), |
| 993 |
("Foo ; Bar", "\"Foo ; Bar\""), |
| 994 |
("Foo : Bar", "\"Foo : Bar\""), |
| 995 |
("Foo \\ Bar", "\"Foo \\\\ Bar\""), |
| 996 |
("Foo \" Bar", "\"Foo \\\" Bar\""), |
| 997 |
("Foo . Bar", "\"Foo . Bar\""), |
| 998 |
("Foo [ Bar", "\"Foo [ Bar\""), |
| 999 |
("Foo ] Bar", "\"Foo ] Bar\""), |
| 1000 |
("Foo ) Bar", "\"Foo ) Bar\""), |
| 1001 |
("Foo ) Bar", "\"Foo ) Bar\""), |
| 1002 |
("Foo, Bar", "\"Foo, Bar\""), |
| 1003 |
]; |
| 1004 |
for (display_name, expected_name) in tests { |
| 1005 |
let email = "[email protected]"; |
| 1006 |
let result = escape_email_with_display_name(display_name, email); |
| 1007 |
let expected = format!("{expected_name} <{email}>"); |
| 1008 |
assert_eq!(result, expected); |
| 1009 |
} |
| 1010 |
} |
| 1011 |
|
| 1012 |
#[test] |
| 1013 |
fn test_issue_2544_respect_pyproject_dynamic_without_dynamic_fields() { |
| 1014 |
let temp_dir = TempDir::new().unwrap(); |
| 1015 |
let crate_path = temp_dir.path(); |
| 1016 |
let manifest_path = crate_path.join("Cargo.toml"); |
| 1017 |
let pyproject_path = crate_path.join("pyproject.toml"); |
| 1018 |
|
| 1019 |
// Create basic src structure |
| 1020 |
fs::create_dir(crate_path.join("src")).unwrap(); |
| 1021 |
fs::write(crate_path.join("src/lib.rs"), "").unwrap(); |
| 1022 |
|
| 1023 |
// Write Cargo.toml with metadata that should NOT be included |
| 1024 |
// because pyproject.toml doesn't declare them as dynamic |
| 1025 |
let cargo_toml = indoc!( |
| 1026 |
r#" |
| 1027 |
[package] |
| 1028 |
name = "test-package" |
| 1029 |
version = "0.1.0" |
| 1030 |
description = "Description from Cargo.toml - should not appear" |
| 1031 |
authors = ["author from cargo.toml <[email protected]>"] |
| 1032 |
keywords = ["cargo", "toml", "keyword"] |
| 1033 |
repository = "https://github.com/example/repo" |
| 1034 |
|
| 1035 |
[lib] |
| 1036 |
crate-type = ["cdylib"] |
| 1037 |
"# |
| 1038 |
); |
| 1039 |
fs::write(&manifest_path, cargo_toml).unwrap(); |
| 1040 |
|
| 1041 |
// Write pyproject.toml WITHOUT declaring the fields as dynamic |
| 1042 |
let pyproject_toml_content = indoc!( |
| 1043 |
r#" |
| 1044 |
[build-system] |
| 1045 |
requires = ["maturin>=1.0,<2.0"] |
| 1046 |
build-backend = "maturin" |
| 1047 |
|
| 1048 |
[project] |
| 1049 |
name = "test-package" |
| 1050 |
version = "0.1.0" |
| 1051 |
# Note: no description, authors, keywords, urls in dynamic list |
| 1052 |
# dynamic = [] # Not specified, so defaults to empty |
| 1053 |
"# |
| 1054 |
); |
| 1055 |
fs::write(&pyproject_path, pyproject_toml_content).unwrap(); |
| 1056 |
|
| 1057 |
// Load metadata as maturin does |
| 1058 |
let cargo_metadata = MetadataCommand::new() |
| 1059 |
.manifest_path(&manifest_path) |
| 1060 |
.exec() |
| 1061 |
.unwrap(); |
| 1062 |
let mut metadata = Metadata24::from_cargo_toml(crate_path, &cargo_metadata).unwrap(); |
| 1063 |
let pyproject_toml = PyProjectToml::new(&pyproject_path).unwrap(); |
| 1064 |
metadata |
| 1065 |
.merge_pyproject_toml(crate_path, &pyproject_toml) |
| 1066 |
.unwrap(); |
| 1067 |
|
| 1068 |
assert_eq!( |
| 1069 |
metadata.summary, None, |
| 1070 |
"summary should be None when not in dynamic list" |
| 1071 |
); |
| 1072 |
assert_eq!( |
| 1073 |
metadata.author, None, |
| 1074 |
"author should be None when not in dynamic list" |
| 1075 |
); |
| 1076 |
assert_eq!( |
| 1077 |
metadata.keywords, None, |
| 1078 |
"keywords should be None when not in dynamic list" |
| 1079 |
); |
| 1080 |
assert!( |
| 1081 |
metadata.project_url.is_empty(), |
| 1082 |
"project_url should be empty when not in dynamic list" |
| 1083 |
); |
| 1084 |
} |
| 1085 |
|
| 1086 |
#[test] |
| 1087 |
fn test_issue_2544_respect_pyproject_dynamic_with_dynamic_fields() { |
| 1088 |
let temp_dir = TempDir::new().unwrap(); |
| 1089 |
let crate_path = temp_dir.path(); |
| 1090 |
let manifest_path = crate_path.join("Cargo.toml"); |
| 1091 |
let pyproject_path = crate_path.join("pyproject.toml"); |
| 1092 |
|
| 1093 |
// Create basic src structure |
| 1094 |
fs::create_dir(crate_path.join("src")).unwrap(); |
| 1095 |
fs::write(crate_path.join("src/lib.rs"), "").unwrap(); |
| 1096 |
|
| 1097 |
// Write Cargo.toml with metadata |
| 1098 |
let cargo_toml = indoc!( |
| 1099 |
r#" |
| 1100 |
[package] |
| 1101 |
name = "test-package" |
| 1102 |
version = "0.1.0" |
| 1103 |
description = "Description from Cargo.toml - should appear" |
| 1104 |
authors = ["author from cargo.toml <[email protected]>"] |
| 1105 |
keywords = ["cargo", "toml", "keyword"] |
| 1106 |
repository = "https://github.com/example/repo" |
| 1107 |
|
| 1108 |
[lib] |
| 1109 |
crate-type = ["cdylib"] |
| 1110 |
"# |
| 1111 |
); |
| 1112 |
fs::write(&manifest_path, cargo_toml).unwrap(); |
| 1113 |
|
| 1114 |
// Write pyproject.toml WITH fields declared as dynamic |
| 1115 |
let pyproject_toml_content = indoc!( |
| 1116 |
r#" |
| 1117 |
[build-system] |
| 1118 |
requires = ["maturin>=1.0,<2.0"] |
| 1119 |
build-backend = "maturin" |
| 1120 |
|
| 1121 |
[project] |
| 1122 |
name = "test-package" |
| 1123 |
version = "0.1.0" |
| 1124 |
# Fields declared as dynamic - should come from Cargo.toml |
| 1125 |
dynamic = ["description", "authors", "keywords", "urls"] |
| 1126 |
"# |
| 1127 |
); |
| 1128 |
fs::write(&pyproject_path, pyproject_toml_content).unwrap(); |
| 1129 |
|
| 1130 |
// Load metadata as maturin does |
| 1131 |
let cargo_metadata = MetadataCommand::new() |
| 1132 |
.manifest_path(&manifest_path) |
| 1133 |
.exec() |
| 1134 |
.unwrap(); |
| 1135 |
let mut metadata = Metadata24::from_cargo_toml(crate_path, &cargo_metadata).unwrap(); |
| 1136 |
let pyproject_toml = PyProjectToml::new(&pyproject_path).unwrap(); |
| 1137 |
metadata |
| 1138 |
.merge_pyproject_toml(crate_path, &pyproject_toml) |
| 1139 |
.unwrap(); |
| 1140 |
|
| 1141 |
// These fields SHOULD be set because they are in dynamic list |
| 1142 |
assert_eq!( |
| 1143 |
metadata.summary, |
| 1144 |
Some("Description from Cargo.toml - should appear".to_string()) |
| 1145 |
); |
| 1146 |
assert_eq!( |
| 1147 |
metadata.author, |
| 1148 |
Some("author from cargo.toml <[email protected]>".to_string()) |
| 1149 |
); |
| 1150 |
assert_eq!(metadata.keywords, Some("cargo,toml,keyword".to_string())); |
| 1151 |
assert_eq!( |
| 1152 |
metadata.project_url.get("Source Code"), |
| 1153 |
Some(&"https://github.com/example/repo".to_string()) |
| 1154 |
); |
| 1155 |
} |
| 1156 |
} |