Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 2bc303f

Browse files
Treehugger RobotGerrit Code Review
authored andcommitted
Merge "Refactor import logic to make missing license files recoverable." into main
2 parents 0cd7911 + 9c0adad commit 2bc303f

File tree

1 file changed

+39
-54
lines changed

1 file changed

+39
-54
lines changed

tools/external_crates/crate_tool/src/managed_repo.rs

Lines changed: 39 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ use std::{
1616
cell::OnceCell,
1717
collections::BTreeSet,
1818
env,
19-
fs::{create_dir, create_dir_all, read_dir, write},
19+
fs::{create_dir_all, read_dir, write},
2020
os::unix::fs::symlink,
2121
path::Path,
22-
process::Command,
2322
};
2423

2524
use anyhow::{anyhow, bail, Context, Result};
@@ -32,7 +31,6 @@ use repo_config::RepoConfig;
3231
use rooted_path::RootedPath;
3332
use semver::{Version, VersionReq};
3433
use serde::Serialize;
35-
use spdx::Licensee;
3634

3735
use crate::{
3836
android_bp::cargo_embargo_autoconfig,
@@ -268,52 +266,6 @@ impl ManagedRepo {
268266
println!(" Finding license files");
269267
let licenses = find_licenses(krate.path().abs(), krate.name(), krate.license())?;
270268

271-
if !licenses.unsatisfied.is_empty() && licenses.satisfied.is_empty() {
272-
let mut satisfied = false;
273-
// Sometimes multiple crates live in a single GitHub repo. A common case
274-
// is a crate with an associated proc_macro crate. In such cases, the individual
275-
// crates are in subdirectories with license files at root of the repo, and
276-
// the license files don't get distributed with the crates.
277-
// So, if we didn't find a license file, try to guess the URL of the appropriate
278-
// license file and download it. This is incredibly hacky, and only supports
279-
// the most common case, which is LICENSE-APACHE.
280-
if licenses.unsatisfied.len() == 1 {
281-
let req = licenses.unsatisfied.first().unwrap();
282-
if let Some(repository) = krate.repository() {
283-
if *req == Licensee::parse("Apache-2.0").unwrap().into_req() {
284-
let url = format!("{}/master/LICENSE-APACHE", repository);
285-
let body = reqwest::blocking::get(
286-
url.replace("github.com", "raw.githubusercontent.com"),
287-
)?
288-
.text()?;
289-
write(krate.path().abs().join("LICENSE"), body)?;
290-
let patch_dir = krate.path().abs().join("patches");
291-
create_dir(&patch_dir)?;
292-
let output = Command::new("diff")
293-
.args(["-u", "/dev/null", "LICENSE"])
294-
.current_dir(krate.path().abs())
295-
.output()?;
296-
write(patch_dir.join("LICENSE.patch"), output.stdout)?;
297-
satisfied = true;
298-
}
299-
}
300-
}
301-
if !satisfied {
302-
return Err(anyhow!(
303-
"Could not find license files for all licenses. Missing {}",
304-
licenses.unsatisfied.iter().join(", ")
305-
));
306-
}
307-
}
308-
309-
// If there's a single applicable license file, symlink it to LICENSE.
310-
if licenses.satisfied.len() == 1 && licenses.unsatisfied.is_empty() {
311-
let license_file = krate.path().join("LICENSE")?;
312-
if !license_file.abs().exists() {
313-
symlink(licenses.satisfied.iter().next().unwrap().1, license_file)?;
314-
}
315-
}
316-
317269
update_module_license_files(&krate.path().abs(), &licenses)?;
318270

319271
println!(" Creating METADATA");
@@ -335,12 +287,45 @@ impl ManagedRepo {
335287
} else {
336288
write(krate.path().abs().join("cargo_embargo.json"), "{}")?;
337289
}
338-
// Workaround. Our logic for crate health assumes the crate isn't healthy if there's
339-
// no Android.bp. So create an empty one.
340-
write(krate.path().abs().join("Android.bp"), "")?;
341290

342-
self.regenerate([&crate_name].iter())?;
343-
println!("Please edit {} and run 'regenerate' for this crate", managed_dir);
291+
// If there's a single applicable license file, symlink it to LICENSE.
292+
// TODO: Move this logic into regenerate, and improve it.
293+
if licenses.satisfied.len() == 1 && licenses.unsatisfied.is_empty() {
294+
let license_file = krate.path().join("LICENSE")?;
295+
if !license_file.abs().exists() {
296+
symlink(licenses.satisfied.iter().next().unwrap().1, license_file)?;
297+
}
298+
}
299+
300+
if !licenses.unsatisfied.is_empty() {
301+
println!(
302+
r#"
303+
Unable to find license files for the following license terms: {:?}
304+
305+
Please look in {managed_dir} and try to locate the license file
306+
307+
If you find the license file:
308+
* That means there's a bug in our detection logic.
309+
* Please file a bug at http://go/android-rust-crate-bug
310+
311+
If you can't find the license file:
312+
* This is usually because the source repo for the crate contains several crates in
313+
separate directories, with a license file at the root level that's not included in
314+
each crate
315+
* Please go to the upstream repo for the crate at {}
316+
* Download the license file and create a patch for it. Instructions for creating patches
317+
are at https://android.googlesource.com/platform/external/rust/android-crates-io/+/refs/heads/main/README.md#how-to-add-a-patch-file
318+
* Run `crate_tool regenerate {}` after you have added a patch for the license file
319+
320+
We apologize for the inconvenience."#,
321+
licenses.unsatisfied.iter().map(|u| u.to_string()).join(", "),
322+
krate.repository().unwrap_or("(Crate repository URL not found in Cargo.toml)"),
323+
krate.name()
324+
);
325+
} else {
326+
self.regenerate([&crate_name].iter())?;
327+
println!("Please edit {} and run 'regenerate' for this crate", managed_dir);
328+
}
344329

345330
Ok(())
346331
}

0 commit comments

Comments
 (0)