Skip to content

Commit

Permalink
Fold long header fields in Python metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Jul 14, 2021
1 parent 61f2ff3 commit 091a5d0
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 8 deletions.
29 changes: 28 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ scroll = "0.10.2"
target-lexicon = "0.12.0"
pyproject-toml = "0.1.0"
python-pkginfo = "0.4.0"
textwrap = "0.14.2"

[dev-dependencies]
indoc = "1.0.3"
Expand Down
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Use UTF-8 encoding when reading `pyproject.toml` by domdfcoding in [#588](https://github.com/PyO3/maturin/pull/588)
* Use Cargo's `repository` field as `Source Code` in project URL in [#590](https://github.com/PyO3/maturin/pull/590)
* Fold long header fields in Python metadata in [#594](https://github.com/PyO3/maturin/pull/594)

## [0.11.1] - 2021-07-10

Expand Down
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ my-project
## Python metadata

maturin supports [PEP 621](https://www.python.org/dev/peps/pep-0621/), you can specify python package metadata in `pyproject.toml`.
maturin merges metadata from `Cargo.toml` and `pyproject.toml`, `pyproject.toml` take precedence over `Cargo.toml`.

To specify python dependencies, add a list `dependencies` in a `[project]` section in the `pyproject.toml`. This list is equivalent to `install_requires` in setuptools:

Expand Down
44 changes: 37 additions & 7 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,12 @@ impl Metadata21 {
(None, None) => {}
}
}
self.author = Some(names.join(", "));
self.author_email = Some(emails.join(", "));
if !names.is_empty() {
self.author = Some(names.join(", "));
}
if !emails.is_empty() {
self.author_email = Some(emails.join(", "));
}
}

if let Some(maintainers) = &project.maintainers {
Expand All @@ -192,8 +196,12 @@ impl Metadata21 {
(None, None) => {}
}
}
self.maintainer = Some(names.join(", "));
self.maintainer_email = Some(emails.join(", "));
if !names.is_empty() {
self.maintainer = Some(names.join(", "));
}
if !emails.is_empty() {
self.maintainer_email = Some(emails.join(", "));
}
}

if let Some(keywords) = &project.keywords {
Expand Down Expand Up @@ -383,15 +391,15 @@ impl Metadata21 {
}
};

add_option("Summary", &self.summary);
add_option("Summary", &self.summary.as_deref().map(fold_header));
add_option("Keywords", &self.keywords);
add_option("Home-Page", &self.home_page);
add_option("Download-URL", &self.download_url);
add_option("Author", &self.author);
add_option("Author-email", &self.author_email);
add_option("Maintainer", &self.maintainer);
add_option("Maintainer-email", &self.maintainer_email);
add_option("License", &self.license);
add_option("License", &self.license.as_deref().map(fold_header));
add_option("Requires-Python", &self.requires_python);
add_option("Description-Content-Type", &self.description_content_type);
// Project-URL is special
Expand Down Expand Up @@ -463,6 +471,28 @@ impl Metadata21 {
}
}

/// Fold long header field according to RFC 5322 section 2.2.3
/// https://datatracker.ietf.org/doc/html/rfc5322#section-2.2.3
fn fold_header(text: &str) -> String {
let mut result = String::with_capacity(text.len());

let options = textwrap::Options::new(78)
.initial_indent("")
.subsequent_indent("\t");
for (i, line) in textwrap::wrap(text, options).iter().enumerate() {
if i > 0 {
result.push_str("\r\n");
}
if line.is_empty() {
result.push('\t');
} else {
result.push_str(&line);
}
}

result
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -740,6 +770,6 @@ mod test {
"attrs; extra == 'test'",
"boltons; (sys_platform == 'win32') and extra == 'test'"
]
)
);
}
}
25 changes: 25 additions & 0 deletions test-crates/pyo3-pure/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright (c) 2018-present konstin

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions test-crates/pyo3-pure/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ readme = "Readme.md"
maintainers = [
{name = "messense", email = "[email protected]"}
]
license = { file = "LICENSE" }

[project.optional-dependencies]
test = [
Expand Down

0 comments on commit 091a5d0

Please sign in to comment.