Skip to content

Commit c9c391c

Browse files
pierwilljwilliams-mongo
authored andcommitted
DOCSP-29009 Tooling to create RST content for MEKO Golang licenses (#1279)
1 parent 1022b27 commit c9c391c

File tree

7 files changed

+249
-0
lines changed

7 files changed

+249
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ source/figures/
2424
giza.log
2525
backups/*
2626
.vscode/*
27+
tools/license-tool/target/
28+
tools/license-tool/licenses.csv

tools/license-tool/Cargo.lock

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

tools/license-tool/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "license-tool"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
csv = "1.2.1"
10+
serde = { version = "1.0.159", features = ["derive"] }

tools/license-tool/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Requirements
2+
3+
Install [Rust](https://www.rust-lang.org/learn/get-started).
4+
5+
# Instructions
6+
7+
1) Download the license file for the desired version (for example, https://github.com/10gen/ops-manager-kubernetes/blob/1.19.1/licenses.csv).
8+
2) Place the licenses.csv file in `/docs-k8s-operator/tools/license-tool/΄`.
9+
3) Change to the tool directory: `cd tools/license-tool/`.
10+
4) Run the tool: `cargo run`.
11+
5) Paste the output into the docs at `source/third-party-licenses.txt`.

tools/license-tool/src/constants.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/// Pairs of license strings (as they appear in the csv file) and the headers
2+
/// we use in the RST file.
3+
pub const LICENSES: &[(&str, &str)] = &[
4+
("Apache-2.0", APACHE_HEADER),
5+
("BSD-2-Clause", BSD2_HEADER),
6+
("BSD-3-Clause", BSD3_HEADER),
7+
("ISC", ISC_HEADER),
8+
("MIT", MIT_HEADER),
9+
("MPL-2.0", MPL_HEADER),
10+
];
11+
12+
pub const TABLE_HEADER: &str = ".. list-table::
13+
:widths: 70 30
14+
:header-rows: 1
15+
16+
* - Package
17+
- Version
18+
";
19+
20+
const APACHE_HEADER: &str = "Apache License 2.0
21+
------------------
22+
23+
License: :tldrl:`TL;DR <apache2>` | :osil:`Full Text <Apache-2.0>`";
24+
25+
const BSD2_HEADER: &str = ":abbr:`BSD (Berkeley Software Distribution)` 2-Clause
26+
-----------------------------------------------------
27+
28+
License: :tldrl:`TL;DR <freebsd>` | :osil:`Full Text <BSD-2-Clause>`";
29+
30+
const BSD3_HEADER: &str = ":abbr:`BSD (Berkeley Software Distribution)` 3-Clause
31+
-----------------------------------------------------
32+
33+
License: :tldrl:`TL;DR <bsd3>` | :osil:`Full Text <BSD-3-Clause>`";
34+
35+
const ISC_HEADER: &str = ":abbr:`ISC (Internet Systems Consortium)` License
36+
-------------------------------------------------
37+
38+
License: :tldrl:`TL;DR <isc>` | :osil:`Full Text <ISC>`";
39+
40+
const MIT_HEADER: &str = ":abbr:`MIT (Massachusetts Institute of Technology)` License
41+
-----------------------------------------------------------
42+
43+
License: :tldrl:`TL;DR <mit>` | :osil:`Full Text <MIT>`";
44+
45+
const MPL_HEADER: &str = ":abbr:`MPL (Mozilla Public License)` 2.0
46+
----------------------------------------
47+
48+
License: :tldrl:`TL;DR <mpl-2.0>` | :osil:`Full Text <MPL-2.0>`";
49+
50+
pub const UNKNOWN_LICENSE_ERR: &str =
51+
"\n\nUnknown license!\n\nIf there is a new license, add it to the `LICENSES`
52+
constant and add a new license header.\n\n";

tools/license-tool/src/entry.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use std::fmt;
2+
3+
use serde::Deserialize;
4+
5+
// Each row in the csv will become an Entry.
6+
#[derive(Clone, Debug, Deserialize)]
7+
pub struct Entry {
8+
pub path: String,
9+
pub version: String,
10+
_license_url: String,
11+
pub license: String,
12+
}
13+
14+
impl fmt::Display for Entry {
15+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16+
write!(
17+
f,
18+
" * - :gopkg:`{} </{}>`\n - {}\n",
19+
self.path, self.path, self.version
20+
)
21+
}
22+
}

tools/license-tool/src/main.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use std::fs;
2+
3+
mod constants;
4+
mod entry;
5+
6+
use crate::constants::{LICENSES, TABLE_HEADER, UNKNOWN_LICENSE_ERR};
7+
use crate::entry::Entry;
8+
9+
fn main() {
10+
let file = fs::read_to_string("licenses.csv").expect("licenses file missing");
11+
12+
// Build the CSV reader
13+
let mut rdr = csv::ReaderBuilder::new()
14+
.has_headers(false) // don't skip first row
15+
.from_reader(file.as_bytes());
16+
17+
// Create a vector of entries.
18+
let mut entries: Vec<Entry> = vec![];
19+
// Deserialize csv records and add to vector.
20+
for result in rdr.deserialize() {
21+
let entry: Entry = match result {
22+
Ok(s) => s,
23+
Err(_) => panic!(),
24+
};
25+
entries.push(entry);
26+
}
27+
28+
// Check for unknown licenses
29+
let existing_licenses: Vec<String> = LICENSES.iter().map(|l| l.0.to_string()).collect();
30+
for entry in &entries {
31+
if !existing_licenses.contains(&entry.license) {
32+
panic!("{}", UNKNOWN_LICENSE_ERR);
33+
}
34+
}
35+
36+
// For each license, output the corresponding header and list of entries.
37+
for (license, header) in LICENSES {
38+
println!("{header}\n");
39+
println!("{TABLE_HEADER}\n");
40+
41+
for entry in &entries {
42+
if entry.license == license.to_string() {
43+
println!("{entry}");
44+
}
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)