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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
use failure::{Error, ResultExt};
use regex::Regex;
use std::collections::HashMap;
use std::fs::read_to_string;
use std::path::Path;
use std::str;
use CargoToml;

/// The metadata required to generate the .dist-info directory
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct WheelMetadata {
    /// Python Package Metadata 2.1
    pub metadata21: Metadata21,
    /// The `[console_scripts]` for the entry_points.txt
    pub scripts: HashMap<String, String>,
    /// The name of the module can be distinct from the package name, mostly
    /// because package names normally contain minuses while module names
    /// have underscores. The package name is part of metadata21
    pub module_name: String,
}

/// Python Package Metadata 2.1 as specified in
/// https://packaging.python.org/specifications/core-metadata/
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
#[serde(rename_all = "kebab-case")]
#[allow(missing_docs)]
pub struct Metadata21 {
    // Mandatory fields
    pub metadata_version: String,
    pub name: String,
    pub version: String,
    // Optional fields
    pub platform: Vec<String>,
    pub supported_platform: Vec<String>,
    pub summary: Option<String>,
    pub description: Option<String>,
    pub description_content_type: Option<String>,
    pub keywords: Option<String>,
    pub home_page: Option<String>,
    pub download_url: Option<String>,
    pub author: Option<String>,
    pub author_email: Option<String>,
    pub maintainer: Option<String>,
    pub maintainer_email: Option<String>,
    pub license: Option<String>,
    pub classifier: Vec<String>,
    pub requires_dist: Vec<String>,
    pub provides_dist: Vec<String>,
    pub obsoletes_dist: Vec<String>,
    pub requires_python: Option<String>,
    pub requires_external: Vec<String>,
    pub project_url: Vec<String>,
    pub provides_extra: Vec<String>,
}

impl Metadata21 {
    /// Uses a Cargo.Toml to create the metadata for python packages
    ///
    /// manifest_path must be the directory, not the file
    pub fn from_cargo_toml(
        cargo_toml: &CargoToml,
        manifest_path: &Path,
    ) -> Result<Metadata21, Error> {
        let authors = cargo_toml.package.authors.join(", ");

        // See https://packaging.python.org/specifications/core-metadata/#description
        let description = if let Some(ref readme) = cargo_toml.package.readme {
            Some(read_to_string(manifest_path.join(readme)).context(format!(
                "Failed to read readme specified in Cargo.toml, which should be at {}",
                manifest_path.join(readme).display()
            ))?)
        } else {
            None
        };

        let description_content_type = if description.is_some() {
            // I'm not hundred percent sure if that's the best preset
            Some("text/markdown; charset=UTF-8; variant=GFM".to_owned())
        } else {
            None
        };

        Ok(Metadata21 {
            metadata_version: "2.1".to_owned(),
            name: cargo_toml.package.name.to_owned(),
            version: cargo_toml.package.version.to_owned(),
            platform: Vec::new(),
            supported_platform: Vec::new(),
            summary: cargo_toml.package.description.clone(),
            description,
            description_content_type,
            keywords: cargo_toml
                .package
                .keywords
                .clone()
                .map(|keywords| keywords.join(" ")),
            home_page: cargo_toml.package.homepage.clone(),
            download_url: None,
            // Cargo.toml has no distinction between author and author email
            author: Some(authors.to_owned()),
            author_email: Some(authors.to_owned()),
            maintainer: None,
            maintainer_email: None,
            license: cargo_toml.package.license.clone(),
            classifier: Vec::new(),
            requires_dist: Vec::new(),
            provides_dist: Vec::new(),
            obsoletes_dist: Vec::new(),
            requires_python: None,
            requires_external: Vec::new(),
            project_url: Vec::new(),
            provides_extra: Vec::new(),
        })
    }

    /// Formats the metadata into a list of key with multiple values have
    /// mutliple singel-valued pairs. This format is needed for the pypi
    /// uploader and for the metadata file inside wheels
    pub fn to_vec(&self) -> Vec<(String, String)> {
        let mut fields = vec![
            ("Metadata-Version", self.metadata_version.clone()),
            ("Name", self.name.clone()),
            ("Version", self.version.clone()),
        ];

        macro_rules! vec_types {
            ($(($name:tt : $value:ident)),*) => {
                $(
                    for i in &self.$value {
                        fields.push(($name, i.to_string()));
                    }
                )*
            }
        };

        macro_rules! option_types {
            ($(($name:tt : $value:ident)),*) => {
                $(
                    if let Some(some) = self.$value.clone() {
                        fields.push(($name, some));
                    }
                )*
            }
        }

        vec_types![
            ("Supported-Platform": supported_platform),
            ("Platform": platform),
            ("Supported-Platform": supported_platform),
            ("Classifier": classifier),
            ("Requires-Dist": requires_dist),
            ("Provides-Dist": provides_dist),
            ("Obsoletes-Dist": obsoletes_dist),
            ("Requires-External": requires_external),
            ("Project-Url": project_url),
            ("Provides-Extra": provides_extra)
        ];

        option_types![
            ("Summary": summary),
            ("Keywords": keywords),
            ("Home-Page": home_page),
            ("Download-Url": download_url),
            ("Author": author),
            ("Author-Email": author_email),
            ("Maintainer": maintainer),
            ("Maintainer-Email": maintainer_email),
            ("License": license),
            ("Requires-Python": requires_python),
            ("Description-Content-Type": description_content_type),
            // Description shall be last, so we can ignore RFC822 and just put the description
            // in the body
            ("Description": description)
        ];

        fields
            .into_iter()
            .map(|(k, v)| (k.to_string(), v))
            .collect()
    }

    /// Writes the format for the metadata file inside wheels
    pub fn to_file_contents(&self) -> String {
        let mut fields = self.to_vec();
        let mut out = "".to_string();

        let body = match fields.clone().last() {
            Some((key, description)) if key == "Description" => {
                fields.pop().unwrap();
                Some(description.clone())
            }
            Some((_, _)) => None,
            None => None,
        };

        for (key, value) in fields {
            out += &format!("{}: {}\n", key, value);
        }

        if let Some(body) = body {
            out += &format!("\n{}\n", body);
        }

        out
    }

    /// Returns the distribution name according to PEP 427, Section "Escaping
    /// and Unicode"
    pub fn get_distribution_escaped(&self) -> String {
        let re = Regex::new(r"[^\w\d.]+").unwrap();
        re.replace_all(&self.name, "_").to_string()
    }
}

#[cfg(test)]
mod test {
    extern crate indoc;
    extern crate tempfile;

    use cargo_toml::CargoTomlMetadata;
    use cargo_toml::CargoTomlMetadataPyo3Pack;
    // Macro scoping
    #[allow(unused_imports)]
    use self::indoc::*;
    use super::*;
    use std::io::Write;
    use toml;

    #[test]
    fn test_metadata_from_cargo_toml() {
        let readme = indoc!(
            r#"
            # Some test package

            This is the readme for a test pacakge
        "#
        );

        let mut readme_md = tempfile::NamedTempFile::new().unwrap();

        let readme_path = if cfg!(windows) {
            readme_md.path().to_str().unwrap().replace("\\", "/")
        } else {
            readme_md.path().to_str().unwrap().to_string()
        };

        readme_md.write_all(readme.as_bytes()).unwrap();

        let cargo_toml = indoc!(
            r#"
            [package]
            authors = ["konstin <konstin@mailbox.org>"]
            name = "info-project"
            version = "0.1.0"
            description = "A test project"
            homepage = "https://example.org"
            readme = "readme.md"
            keywords = ["ffi", "test"]

            [lib]
            crate-type = ["cdylib"]
            name = "get_fourtytwo"

            [package.metadata.pyo3-pack.scripts]
            ph = "pyo3_pack:print_hello"
        "#
        ).replace("readme.md", &readme_path);

        let cargo_toml: CargoToml = toml::from_str(&cargo_toml).unwrap();

        let metadata =
            Metadata21::from_cargo_toml(&cargo_toml, &readme_md.path().parent().unwrap()).unwrap();

        let expected = indoc!(
            r#"
            Metadata-Version: 2.1
            Name: info-project
            Version: 0.1.0
            Summary: A test project
            Keywords: ffi test
            Home-Page: https://example.org
            Author: konstin <konstin@mailbox.org>
            Author-Email: konstin <konstin@mailbox.org>
            Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM

            # Some test package

            This is the readme for a test pacakge
        "#
        );

        let actual = metadata.to_file_contents();

        assert_eq!(actual.trim(), expected.trim());

        let mut scripts = HashMap::new();
        scripts.insert("ph".to_string(), "pyo3_pack:print_hello".to_string());

        assert_eq!(
            cargo_toml.package.metadata,
            Some(CargoTomlMetadata {
                pyo3_pack: Some(CargoTomlMetadataPyo3Pack {
                    scripts: Some(scripts),
                }),
            })
        );
    }
}