Skip to content

Commit 20a65ea

Browse files
committed
license-scan: add latest spdx license data
The latest SPDX data includes the Pixar license, which is a modified Apache-2.0 license. This rare license often confuses the scanner when it encounters the common Apache-2.0 license, so a new config option `spdx.ignore-licenses` has been added to ignore specific licenses from the input SPDX data. This change also adds the ability to skip directories when scanning sources. This is useful for scanning the source directory filled with sample license texts in the spdx crate itself.
1 parent 21883fd commit 20a65ea

File tree

7 files changed

+236
-14
lines changed

7 files changed

+236
-14
lines changed

configs/cargo-deny/clarify.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
[spdx]
2+
ignore-licenses = [
3+
# Apache-2.0 is often misclassified as Pixar, which is a significantly more rare
4+
# https://github.com/jpeddicord/askalono/issues/94
5+
"Pixar"
6+
]
7+
18
[clarify.askalono]
29
expression = "Apache-2.0"
310
license-files = [

configs/cargo-make/clarify.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
[spdx]
2+
ignore-licenses = [
3+
# Apache-2.0 is often misclassified as Pixar, which is a significantly more rare
4+
# https://github.com/jpeddicord/askalono/issues/94
5+
"Pixar"
6+
]
7+
18
[clarify.bstr]
29
expression = "(MIT OR Apache-2.0) AND Unicode-DFS-2016"
310
license-files = [

license-scan/Cargo.lock

Lines changed: 66 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

license-scan/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ ignore = "0.4"
1717
lazy_static = "1"
1818
semver = { version = "1", features = ["serde"] }
1919
serde = { version = "1", features = ["derive"] }
20-
spdx = "0.3"
20+
spdx = "0.10"
2121
structopt = { version = "0.3", default-features = false }
22+
tempfile = "3"
2223
toml = "0.8"
2324
twox-hash = "1"
2425
walkdir = "2"

license-scan/clarify.toml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
[spdx]
2+
ignore-licenses = [
3+
# Apache-2.0 is often misclassified as Pixar, which is a significantly more rare
4+
# https://github.com/jpeddicord/askalono/issues/94
5+
"Pixar"
6+
]
7+
18
[clarify.askalono]
29
expression = "Apache-2.0"
310
license-files = [
@@ -21,7 +28,6 @@ license-files = [
2128
{ path = "COPYING", hash = 0x278afbcf },
2229
{ path = "LICENSE-APACHE", hash = 0x24b54f4b },
2330
{ path = "LICENSE-MIT", hash = 0x462dee44 },
24-
{ path = "src/unicode/data/LICENSE-UNICODE", hash = 0x70f7339 },
2531
]
2632

2733
[clarify.crossbeam-channel]
@@ -58,12 +64,16 @@ license-files = [
5864
{ path = "src/unicode_tables/LICENSE-UNICODE", hash = 0xa7f28b93 },
5965
]
6066

61-
[clarify.unicode-ident]
62-
expression = "(MIT OR Apache-2.0) AND Unicode-DFS-2016"
67+
[clarify.spdx]
68+
expression = "MIT OR Apache-2.0"
6369
license-files = [
64-
{ path = "LICENSE-APACHE", hash = 0xb5518783 },
65-
{ path = "LICENSE-MIT", hash = 0x386ca1bc },
66-
{ path = "LICENSE-UNICODE", hash = 0x9698cbbe },
70+
{ path = "LICENSE-MIT", hash = 0xa502ee8a },
71+
{ path = "LICENSE-APACHE", hash = 0x4fccb6b7 },
72+
]
73+
skip-dirs = [
74+
# The spdx crate contains the full text of referred licenses
75+
"src/text/licenses",
76+
"src/text/exceptions",
6777
]
6878

6979
[clarify.zstd-safe]

license-scan/src/license_store.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
use anyhow::{ensure, Context, Result};
2+
use serde::Deserialize;
3+
use std::collections::HashSet;
4+
use std::path::Path;
5+
use walkdir::WalkDir;
6+
7+
#[derive(Debug, Deserialize, Default)]
8+
#[serde(rename_all = "kebab-case")]
9+
pub(crate) struct SPDXOptions {
10+
/// A set of licenses to ignore from the SPDX license data.
11+
#[serde(default)]
12+
pub(crate) ignore_licenses: HashSet<String>,
13+
}
14+
15+
impl SPDXOptions {
16+
/// Mogrifies SPDX licenses using a set of static rules.
17+
///
18+
/// This function is implemented to work around quirks in the SPDX data that cause unusual behavior
19+
/// in askalono during license identification.
20+
pub(crate) fn preprocess_licenses(
21+
&self,
22+
input_path: impl AsRef<Path>,
23+
output_path: impl AsRef<Path>,
24+
) -> Result<()> {
25+
let input_path = input_path.as_ref();
26+
let output_path = output_path.as_ref();
27+
ensure!(
28+
input_path.is_dir(),
29+
"License preprocessing input path is not a directory."
30+
);
31+
ensure!(
32+
output_path.is_dir(),
33+
"License preprocessing output path is not a directory."
34+
);
35+
ensure!(
36+
input_path.canonicalize()? != output_path.canonicalize()?,
37+
"License preprocessing must write to a new path"
38+
);
39+
40+
let license_iter = WalkDir::new(input_path)
41+
.min_depth(1)
42+
.max_depth(1)
43+
.into_iter();
44+
45+
for license_file in license_iter {
46+
let license_file = license_file?;
47+
48+
if !license_file.file_type().is_file() {
49+
continue;
50+
}
51+
let license_path = license_file.path();
52+
if license_file
53+
.file_name()
54+
.to_string_lossy()
55+
.ends_with(".json")
56+
&& self.should_include_license_file(license_path)?
57+
{
58+
let license_output_path = output_path.join(license_file.file_name());
59+
std::fs::copy(license_path, &license_output_path).with_context(|| {
60+
format!(
61+
"Failed to copy license from '{}' to '{}'",
62+
license_path.display(),
63+
license_output_path.display()
64+
)
65+
})?;
66+
}
67+
}
68+
69+
Ok(())
70+
}
71+
72+
fn should_include_license_file(&self, filepath: impl AsRef<Path>) -> Result<bool> {
73+
let filepath = filepath.as_ref();
74+
let license_name = filepath
75+
.file_name()
76+
.with_context(|| {
77+
format!(
78+
"License file '{}' seemingly has no filename",
79+
filepath.display()
80+
)
81+
})?
82+
.to_str()
83+
.with_context(|| {
84+
format!(
85+
"License filename '{}' not valid unicode",
86+
filepath.display()
87+
)
88+
})?
89+
.strip_suffix(".json")
90+
.with_context(|| {
91+
format!(
92+
"License filename '{}' does not end in '.json'",
93+
filepath.display()
94+
)
95+
})?;
96+
97+
Ok(!self.ignore_licenses.contains(license_name))
98+
}
99+
}

0 commit comments

Comments
 (0)