]> git.lizzy.rs Git - rust.git/blob - src/tools/collect-license-metadata/src/reuse.rs
Auto merge of #105416 - nnethercote:more-linting-tweaks, r=cjgillot
[rust.git] / src / tools / collect-license-metadata / src / reuse.rs
1 use crate::licenses::{License, LicenseId, LicensesInterner};
2 use anyhow::Error;
3 use std::path::{Path, PathBuf};
4 use std::process::{Command, Stdio};
5 use std::time::Instant;
6
7 pub(crate) fn collect(
8     reuse_exe: &Path,
9     interner: &mut LicensesInterner,
10 ) -> Result<Vec<(PathBuf, LicenseId)>, Error> {
11     eprintln!("gathering license information from REUSE");
12     let start = Instant::now();
13     let raw = &obtain_spdx_document(reuse_exe)?;
14     eprintln!("finished gathering the license information from REUSE in {:.2?}", start.elapsed());
15
16     let document = spdx_rs::parsers::spdx_from_tag_value(&raw)?;
17
18     let mut result = Vec::new();
19     for file in document.file_information {
20         let license = interner.intern(License {
21             spdx: file.concluded_license.to_string(),
22             copyright: file.copyright_text.split('\n').map(|s| s.into()).collect(),
23         });
24
25         result.push((file.file_name.into(), license));
26     }
27
28     Ok(result)
29 }
30
31 fn obtain_spdx_document(reuse_exe: &Path) -> Result<String, Error> {
32     let output = Command::new(reuse_exe)
33         .args(&["spdx", "--add-license-concluded", "--creator-person=bors"])
34         .stdout(Stdio::piped())
35         .spawn()?
36         .wait_with_output()?;
37
38     if !output.status.success() {
39         eprintln!();
40         eprintln!("Note that Rust requires some REUSE features that might not be present in the");
41         eprintln!("release you're using. Make sure your REUSE release includes these PRs:");
42         eprintln!();
43         eprintln!(" - https://github.com/fsfe/reuse-tool/pull/623");
44         eprintln!();
45         anyhow::bail!("collecting licensing information with REUSE failed");
46     }
47
48     Ok(String::from_utf8(output.stdout)?)
49 }