use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command as Process;
use anyhow::{Context, Result, bail};
use comply::spdx;
use crate::cli::commands::lint::lint_project;
use crate::cli::shared::{load_config, resolve_root};
const DEFAULT_SOURCE: &str =
"https://raw.githubusercontent.com/spdx/license-list-data/main/text/{}.txt";
pub(crate) struct Request<'a> {
pub(crate) ids: &'a [String],
pub(crate) all: bool,
pub(crate) output: Option<&'a String>,
pub(crate) source: Option<&'a String>,
pub(crate) force: bool,
}
pub(crate) fn run(path: Option<&String>, request: &Request<'_>) -> Result<()> {
let root = resolve_root(path)?;
let target = request
.output
.map_or_else(|| root.join("LICENSES"), PathBuf::from);
let template = request.source.map_or(DEFAULT_SOURCE, String::as_str);
let ids = resolve_ids(&root, request)?;
if ids.is_empty() {
println!("Nothing to download: every referenced licence already has a file.");
return Ok(());
}
fs::create_dir_all(&target)
.with_context(|| format!("failed to create {}", target.display()))?;
for id in &ids {
let id = id.strip_suffix('+').unwrap_or(id);
let destination = target.join(format!("{id}.txt"));
let project_local = is_license_ref(id);
if !project_local && !spdx::is_known_license(id) && !spdx::is_known_exception(id) {
bail!("'{id}' is not a known SPDX identifier; nothing to download");
}
if destination.exists() && !request.force {
println!("{id}: already present, skipping");
continue;
}
if project_local {
fs::write(&destination, "")
.with_context(|| format!("failed to write {}", destination.display()))?;
println!(
"{id}: empty placeholder written to {}",
destination.display()
);
continue;
}
let source_id = if spdx::deprecated_licenses().contains(&id) {
format!("deprecated_{id}")
} else {
id.to_string()
};
let text = fetch(template, &source_id)?;
if text.trim().is_empty() {
bail!("'{id}': the source returned an empty document");
}
fs::write(&destination, text)
.with_context(|| format!("failed to write {}", destination.display()))?;
println!("{id}: written to {}", destination.display());
}
Ok(())
}
fn is_license_ref(id: &str) -> bool {
id.len() > "LicenseRef-".len()
&& id
.get(.."LicenseRef-".len())
.is_some_and(|prefix| prefix.eq_ignore_ascii_case("LicenseRef-"))
}
fn resolve_ids(root: &Path, request: &Request<'_>) -> Result<Vec<String>> {
if !request.all {
return Ok(request.ids.to_vec());
}
let config = load_config(root)?;
let report = lint_project(root, &config)?;
let mut ids = report.licenses().missing.clone();
ids.extend(request.ids.iter().cloned());
ids.sort_unstable();
ids.dedup();
Ok(ids)
}
fn fetch(template: &str, id: &str) -> Result<String> {
let location = template.replace("{}", id);
if let Some(local) = location.strip_prefix("file://") {
return fs::read_to_string(local)
.with_context(|| format!("failed to read {local} for '{id}'"));
}
if !location.starts_with("http://") && !location.starts_with("https://") {
return fs::read_to_string(&location)
.with_context(|| format!("failed to read {location} for '{id}'"));
}
let output = Process::new("curl")
.args(["-sfL", "--max-time", "30", &location])
.output()
.context("could not run `curl`; install it or pass --source with a local path")?;
if !output.status.success() {
bail!("failed to download '{id}' from {location}");
}
Ok(String::from_utf8_lossy(&output.stdout).into_owned())
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
fn source_dir() -> (TempDir, String) {
let tmp = TempDir::new().unwrap();
fs::write(tmp.path().join("MIT.txt"), "MIT License text\n").unwrap();
fs::write(tmp.path().join("Apache-2.0.txt"), "Apache text\n").unwrap();
fs::write(tmp.path().join("Empty.txt"), " \n").unwrap();
let template = format!("{}/{{}}.txt", tmp.path().display());
(tmp, template)
}
fn project() -> (TempDir, PathBuf, String) {
let tmp = TempDir::new().unwrap();
let root = tmp.path().canonicalize().unwrap();
fs::write(root.join("REUSE.toml"), "version = 1\n").unwrap();
let arg = root.to_str().unwrap().to_string();
(tmp, root, arg)
}
fn request<'a>(ids: &'a [String], source: &'a String, all: bool, force: bool) -> Request<'a> {
Request {
ids,
all,
output: None,
source: Some(source),
force,
}
}
mod reference_parity {
use super::*;
#[test]
fn a_plus_suffix_resolves_to_the_base_licence() {
let (_src, template) = source_dir();
let (_tmp, root, arg) = project();
let ids = vec!["MIT+".to_string()];
run(Some(&arg), &request(&ids, &template, false, false)).unwrap();
assert_eq!(
fs::read_to_string(root.join("LICENSES/MIT.txt")).unwrap(),
"MIT License text\n",
"the `+` belongs to the expression, so the file is the base id"
);
assert!(
!root.join("LICENSES/MIT+.txt").exists(),
"nothing should be stored under the suffixed name"
);
}
#[test]
fn a_deprecated_identifier_falls_back_to_the_deprecated_path() {
let tmp = TempDir::new().unwrap();
fs::write(tmp.path().join("deprecated_GPL-3.0.txt"), "old GPL\n").unwrap();
let template = format!("{}/{{}}.txt", tmp.path().display());
let (_tmp, root, arg) = project();
let ids = vec!["GPL-3.0".to_string()];
run(Some(&arg), &request(&ids, &template, false, false)).unwrap();
assert_eq!(
fs::read_to_string(root.join("LICENSES/GPL-3.0.txt")).unwrap(),
"old GPL\n",
"fetched from the deprecated path, stored under the plain id"
);
}
#[test]
fn a_license_ref_gets_an_empty_placeholder() {
let (_src, template) = source_dir();
let (_tmp, root, arg) = project();
let ids = vec!["LicenseRef-Custom".to_string()];
run(Some(&arg), &request(&ids, &template, false, false)).unwrap();
let written = root.join("LICENSES/LicenseRef-Custom.txt");
assert!(written.exists(), "placeholder must be created");
assert_eq!(
fs::read_to_string(&written).unwrap(),
"",
"nothing to download, so the placeholder is empty"
);
}
#[test]
fn an_unknown_identifier_is_still_refused() {
let (_src, template) = source_dir();
let (_tmp, _root, arg) = project();
let ids = vec!["Not-A-Licence".to_string()];
let err = run(Some(&arg), &request(&ids, &template, false, false)).unwrap_err();
assert!(
err.to_string().contains("not a known SPDX identifier"),
"got: {err}"
);
}
}
#[test]
fn a_named_licence_lands_in_the_licenses_directory() {
let (_src, template) = source_dir();
let (_tmp, root, arg) = project();
let ids = vec!["MIT".to_string()];
run(Some(&arg), &request(&ids, &template, false, false)).unwrap();
assert_eq!(
fs::read_to_string(root.join("LICENSES/MIT.txt")).unwrap(),
"MIT License text\n"
);
}
#[test]
fn an_existing_file_is_kept_unless_force_is_given() {
let (_src, template) = source_dir();
let (_tmp, root, arg) = project();
fs::create_dir_all(root.join("LICENSES")).unwrap();
fs::write(root.join("LICENSES/MIT.txt"), "hand-edited\n").unwrap();
let ids = vec!["MIT".to_string()];
run(Some(&arg), &request(&ids, &template, false, false)).unwrap();
assert_eq!(
fs::read_to_string(root.join("LICENSES/MIT.txt")).unwrap(),
"hand-edited\n",
"a local edit must not be clobbered silently"
);
run(Some(&arg), &request(&ids, &template, false, true)).unwrap();
assert_eq!(
fs::read_to_string(root.join("LICENSES/MIT.txt")).unwrap(),
"MIT License text\n",
"--force is the way to replace it"
);
}
#[test]
fn all_fetches_exactly_what_the_lint_reports_missing() {
let (_src, template) = source_dir();
let (_tmp, root, arg) = project();
fs::write(
root.join("a.py"),
"# SPDX-FileCopyrightText: 2026 A\n# SPDX-License-Identifier: Apache-2.0\nx = 1\n",
)
.unwrap();
run(Some(&arg), &request(&[], &template, true, false)).unwrap();
assert!(root.join("LICENSES/Apache-2.0.txt").exists());
assert!(
!root.join("LICENSES/MIT.txt").exists(),
"--all must not pull licences the project never references"
);
}
#[test]
fn an_unknown_identifier_is_refused_before_any_transfer() {
let (_src, template) = source_dir();
let (_tmp, _root, arg) = project();
let ids = vec!["Not-A-Licence".to_string()];
let err = run(Some(&arg), &request(&ids, &template, false, false)).unwrap_err();
assert!(
format!("{err:#}").contains("not a known SPDX identifier"),
"got: {err:#}"
);
}
#[test]
fn an_empty_document_is_an_error_rather_than_an_empty_licence_file() {
let (_src, template) = source_dir();
let (_tmp, root, arg) = project();
fs::write(PathBuf::from(template.replace("{}", "0BSD")), " \n").unwrap();
let ids = vec!["0BSD".to_string()];
let err = run(Some(&arg), &request(&ids, &template, false, false)).unwrap_err();
assert!(
format!("{err:#}").contains("empty document"),
"got: {err:#}"
);
assert!(
!root.join("LICENSES/0BSD.txt").exists(),
"nothing should be written when the source is blank"
);
}
#[test]
fn a_missing_source_file_names_what_could_not_be_read() {
let (_src, template) = source_dir();
let (_tmp, _root, arg) = project();
let ids = vec!["0BSD".to_string()];
let err = run(Some(&arg), &request(&ids, &template, false, false)).unwrap_err();
assert!(format!("{err:#}").contains("0BSD"), "got: {err:#}");
}
fn serve_once(body: &'static str) -> (String, std::thread::JoinHandle<()>) {
use std::io::{Read as _, Write as _};
use std::net::TcpListener;
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
let handle = std::thread::spawn(move || {
if let Ok((mut stream, _)) = listener.accept() {
let mut discard = [0_u8; 1024];
let _ = stream.read(&mut discard);
let response = format!(
"HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
body.len()
);
let _ = stream.write_all(response.as_bytes());
}
});
(format!("http://127.0.0.1:{port}/{{}}.txt"), handle)
}
#[test]
fn a_successful_transfer_writes_exactly_what_the_server_returned() {
let (template, server) = serve_once("MIT text off the wire\n");
let (_tmp, root, arg) = project();
let ids = vec!["MIT".to_string()];
run(Some(&arg), &request(&ids, &template, false, false)).unwrap();
server.join().unwrap();
assert_eq!(
fs::read_to_string(root.join("LICENSES/MIT.txt")).unwrap(),
"MIT text off the wire\n"
);
}
#[test]
fn a_transfer_that_cannot_connect_reports_the_identifier_and_location() {
let (_tmp, root, arg) = project();
let template = "http://127.0.0.1:1/{}.txt".to_string();
let ids = vec!["MIT".to_string()];
let err = run(Some(&arg), &request(&ids, &template, false, false)).unwrap_err();
let message = format!("{err:#}");
assert!(message.contains("MIT"), "got: {message}");
assert!(message.contains("127.0.0.1"), "got: {message}");
assert!(
!root.join("LICENSES/MIT.txt").exists(),
"a failed transfer must leave nothing behind"
);
}
#[test]
fn a_file_url_template_is_read_from_disk() {
let (src, _template) = source_dir();
let (_tmp, root, arg) = project();
let template = format!("file://{}/{{}}.txt", src.path().display());
let ids = vec!["MIT".to_string()];
run(Some(&arg), &request(&ids, &template, false, false)).unwrap();
assert!(root.join("LICENSES/MIT.txt").exists());
}
#[test]
fn an_output_directory_overrides_the_default() {
let (_src, template) = source_dir();
let (_tmp, root, arg) = project();
let elsewhere = root.join("vendor/licences");
let ids = vec!["MIT".to_string()];
run(
Some(&arg),
&Request {
ids: &ids,
all: false,
output: Some(&elsewhere.to_str().unwrap().to_string()),
source: Some(&template),
force: false,
},
)
.unwrap();
assert!(elsewhere.join("MIT.txt").exists());
}
#[test]
fn a_project_with_nothing_missing_reports_that_and_writes_nothing() {
let (_src, template) = source_dir();
let (_tmp, root, arg) = project();
run(Some(&arg), &request(&[], &template, true, false)).unwrap();
assert!(
!root.join("LICENSES").exists(),
"no directory should be created when there is nothing to fetch"
);
}
#[test]
fn an_exception_identifier_is_accepted_as_well_as_a_licence() {
let (src, template) = source_dir();
let (_tmp, root, arg) = project();
fs::write(
src.path().join("Classpath-exception-2.0.txt"),
"exception text\n",
)
.unwrap();
let ids = vec!["Classpath-exception-2.0".to_string()];
run(Some(&arg), &request(&ids, &template, false, false)).unwrap();
assert!(root.join("LICENSES/Classpath-exception-2.0.txt").exists());
}
}