Skip to content

Commit c37b7e0

Browse files
committed
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.
1 parent af659f1 commit c37b7e0

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

license-scan/src/main.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ struct InnerClarification {
206206
/// List of files that should be skipped as they don't contain license information.
207207
#[serde(default)]
208208
skip_files: Vec<PathBuf>,
209+
210+
/// List of source directories which should not be scanned for license information.
211+
#[serde(default)]
212+
skip_dirs: Vec<PathBuf>,
209213
}
210214

211215
impl InnerClarification {
@@ -232,6 +236,7 @@ struct LicenseFile {
232236
struct Clarified<'a> {
233237
expression: &'a Expression,
234238
skip_files: &'a Vec<PathBuf>,
239+
skip_dirs: &'a Vec<PathBuf>,
235240
}
236241

237242
impl Clarifications {
@@ -258,6 +263,20 @@ impl Clarifications {
258263
files.remove(file.as_path());
259264
}
260265

266+
let skipped_dir_files = files
267+
.keys()
268+
.filter(|input_file| {
269+
clarification
270+
.skip_dirs
271+
.iter()
272+
.any(|skipped_dir| input_file.starts_with(skipped_dir))
273+
})
274+
.copied()
275+
.collect::<Vec<_>>();
276+
for skipped_file in skipped_dir_files {
277+
files.remove(skipped_file);
278+
}
279+
261280
// convert `clarification.license_files` into a struct we can compare with `files`
262281
let clarify_files = clarification
263282
.license_files
@@ -274,6 +293,7 @@ impl Clarifications {
274293
Ok(Some(Clarified {
275294
expression: &clarification.expression,
276295
skip_files: &clarification.skip_files,
296+
skip_dirs: &clarification.skip_dirs,
277297
}))
278298
} else {
279299
Ok(None)
@@ -568,6 +588,7 @@ mod test {
568588
Some(Clarified {
569589
expression: &spdx::Expression::parse("Apache-2.0").unwrap(),
570590
skip_files: &vec![],
591+
skip_dirs: &vec![],
571592
})
572593
);
573594

@@ -587,6 +608,7 @@ mod test {
587608
Some(Clarified {
588609
expression: &spdx::Expression::parse("Apache-2.0").unwrap(),
589610
skip_files: &vec![],
611+
skip_dirs: &vec![],
590612
})
591613
);
592614
}
@@ -612,6 +634,7 @@ mod test {
612634
Some(Clarified {
613635
expression: &spdx::Expression::parse("MIT").unwrap(),
614636
skip_files: &vec![],
637+
skip_dirs: &vec![],
615638
})
616639
);
617640

@@ -675,6 +698,7 @@ mod test {
675698
Some(Clarified {
676699
expression: &spdx::Expression::parse("Apache-2.0 OR BSD-3-Clause").unwrap(),
677700
skip_files: &vec![],
701+
skip_dirs: &vec![],
678702
})
679703
);
680704
assert_eq!(
@@ -691,6 +715,7 @@ mod test {
691715
Some(Clarified {
692716
expression: &spdx::Expression::parse("Apache-2.0 OR MIT").unwrap(),
693717
skip_files: &vec![],
718+
skip_dirs: &vec![],
694719
})
695720
);
696721
}
@@ -716,6 +741,7 @@ mod test {
716741
Some(Clarified {
717742
expression: &spdx::Expression::parse("BSD-3-Clause").unwrap(),
718743
skip_files: &vec![],
744+
skip_dirs: &vec![],
719745
})
720746
);
721747
assert_eq!(
@@ -732,6 +758,7 @@ mod test {
732758
Some(Clarified {
733759
expression: &spdx::Expression::parse("BSD-3-Clause AND Apache-2.0").unwrap(),
734760
skip_files: &vec![],
761+
skip_dirs: &vec![],
735762
})
736763
);
737764
}

0 commit comments

Comments
 (0)