-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add prototype to generate COPYRIGHT
from REUSE metadata
#104439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
17ee25d
add the build.reuse config option to choose the reuse binary
pietroalbini 13efb20
add tool to collect license metadata from REUSE
pietroalbini 4af7de1
initial prototype of the tool to generate copyright notices
pietroalbini 51d5ae6
merge together similar copyright statements
pietroalbini f78e646
make comment more clear
pietroalbini f8a7123
address review feedback
pietroalbini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "collect-license-metadata" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
anyhow = "1.0.65" | ||
serde = { version = "1.0.147", features = ["derive"] } | ||
serde_json = "1.0.85" | ||
spdx-rs = "0.5.1" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::collections::HashMap; | ||
|
||
const COPYRIGHT_PREFIXES: &[&str] = &["SPDX-FileCopyrightText:", "Copyright", "(c)", "(C)", "©"]; | ||
|
||
pub(crate) struct LicensesInterner { | ||
by_id: Vec<License>, | ||
by_struct: HashMap<License, usize>, | ||
} | ||
|
||
impl LicensesInterner { | ||
pub(crate) fn new() -> Self { | ||
LicensesInterner { by_id: Vec::new(), by_struct: HashMap::new() } | ||
} | ||
|
||
pub(crate) fn intern(&mut self, mut license: License) -> LicenseId { | ||
license.simplify(); | ||
if let Some(id) = self.by_struct.get(&license) { | ||
LicenseId(*id) | ||
} else { | ||
let id = self.by_id.len(); | ||
self.by_id.push(license.clone()); | ||
self.by_struct.insert(license, id); | ||
LicenseId(id) | ||
} | ||
} | ||
|
||
pub(crate) fn resolve(&self, id: LicenseId) -> &License { | ||
&self.by_id[id.0] | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, serde::Serialize)] | ||
#[serde(transparent)] | ||
pub(crate) struct LicenseId(usize); | ||
|
||
#[derive(Clone, Hash, PartialEq, Eq, serde::Serialize)] | ||
pub(crate) struct License { | ||
pub(crate) spdx: String, | ||
pub(crate) copyright: Vec<String>, | ||
} | ||
|
||
impl License { | ||
fn simplify(&mut self) { | ||
self.remove_copyright_prefixes(); | ||
self.copyright.sort(); | ||
self.copyright.dedup(); | ||
} | ||
|
||
fn remove_copyright_prefixes(&mut self) { | ||
for copyright in &mut self.copyright { | ||
let mut stripped = copyright.trim(); | ||
let mut previous_stripped; | ||
loop { | ||
previous_stripped = stripped; | ||
for pattern in COPYRIGHT_PREFIXES { | ||
stripped = stripped.trim_start_matches(pattern).trim_start(); | ||
} | ||
if stripped == previous_stripped { | ||
break; | ||
} | ||
} | ||
*copyright = stripped.into(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
mod licenses; | ||
mod path_tree; | ||
mod reuse; | ||
|
||
use crate::licenses::LicensesInterner; | ||
use anyhow::Error; | ||
use std::path::PathBuf; | ||
|
||
fn main() -> Result<(), Error> { | ||
let reuse_exe: PathBuf = std::env::var_os("REUSE_EXE").expect("Missing REUSE_EXE").into(); | ||
let dest: PathBuf = std::env::var_os("DEST").expect("Missing DEST").into(); | ||
|
||
let mut interner = LicensesInterner::new(); | ||
let paths = crate::reuse::collect(&reuse_exe, &mut interner)?; | ||
|
||
let mut tree = crate::path_tree::build(paths); | ||
tree.simplify(); | ||
|
||
if let Some(parent) = dest.parent() { | ||
std::fs::create_dir_all(parent)?; | ||
} | ||
std::fs::write( | ||
&dest, | ||
&serde_json::to_vec_pretty(&serde_json::json!({ | ||
"files": crate::path_tree::strip_interning(tree, &interner), | ||
}))?, | ||
)?; | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.