From c37b7e0d78ee39118f47c99ddefee9a637433387 Mon Sep 17 00:00:00 2001 From: "Sean P. Kelly" Date: Fri, 3 Jan 2025 23:44:23 +0000 Subject: [PATCH] license-scan: allow skipping directories in scanned sources This is useful for scanning the source directory filled with sample license texts in the spdx crate itself. --- license-scan/src/main.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/license-scan/src/main.rs b/license-scan/src/main.rs index 6121da1..5403a0f 100644 --- a/license-scan/src/main.rs +++ b/license-scan/src/main.rs @@ -206,6 +206,10 @@ struct InnerClarification { /// List of files that should be skipped as they don't contain license information. #[serde(default)] skip_files: Vec, + + /// List of source directories which should not be scanned for license information. + #[serde(default)] + skip_dirs: Vec, } impl InnerClarification { @@ -232,6 +236,7 @@ struct LicenseFile { struct Clarified<'a> { expression: &'a Expression, skip_files: &'a Vec, + skip_dirs: &'a Vec, } impl Clarifications { @@ -258,6 +263,20 @@ impl Clarifications { files.remove(file.as_path()); } + let skipped_dir_files = files + .keys() + .filter(|input_file| { + clarification + .skip_dirs + .iter() + .any(|skipped_dir| input_file.starts_with(skipped_dir)) + }) + .copied() + .collect::>(); + for skipped_file in skipped_dir_files { + files.remove(skipped_file); + } + // convert `clarification.license_files` into a struct we can compare with `files` let clarify_files = clarification .license_files @@ -274,6 +293,7 @@ impl Clarifications { Ok(Some(Clarified { expression: &clarification.expression, skip_files: &clarification.skip_files, + skip_dirs: &clarification.skip_dirs, })) } else { Ok(None) @@ -568,6 +588,7 @@ mod test { Some(Clarified { expression: &spdx::Expression::parse("Apache-2.0").unwrap(), skip_files: &vec![], + skip_dirs: &vec![], }) ); @@ -587,6 +608,7 @@ mod test { Some(Clarified { expression: &spdx::Expression::parse("Apache-2.0").unwrap(), skip_files: &vec![], + skip_dirs: &vec![], }) ); } @@ -612,6 +634,7 @@ mod test { Some(Clarified { expression: &spdx::Expression::parse("MIT").unwrap(), skip_files: &vec![], + skip_dirs: &vec![], }) ); @@ -675,6 +698,7 @@ mod test { Some(Clarified { expression: &spdx::Expression::parse("Apache-2.0 OR BSD-3-Clause").unwrap(), skip_files: &vec![], + skip_dirs: &vec![], }) ); assert_eq!( @@ -691,6 +715,7 @@ mod test { Some(Clarified { expression: &spdx::Expression::parse("Apache-2.0 OR MIT").unwrap(), skip_files: &vec![], + skip_dirs: &vec![], }) ); } @@ -716,6 +741,7 @@ mod test { Some(Clarified { expression: &spdx::Expression::parse("BSD-3-Clause").unwrap(), skip_files: &vec![], + skip_dirs: &vec![], }) ); assert_eq!( @@ -732,6 +758,7 @@ mod test { Some(Clarified { expression: &spdx::Expression::parse("BSD-3-Clause AND Apache-2.0").unwrap(), skip_files: &vec![], + skip_dirs: &vec![], }) ); }