Skip to content

Commit 81fb650

Browse files
committed
Auto merge of #38291 - brson:licenseck, r=alexcrichton
Check the license of vendored deps r? @alexcrichton
2 parents 5e2f37f + 5e3be09 commit 81fb650

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

src/tools/tidy/src/deps.rs

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! Check license of third-party deps by inspecting src/vendor
12+
13+
use std::fs::File;
14+
use std::io::Read;
15+
use std::path::Path;
16+
17+
static LICENSES: &'static [&'static str] = &[
18+
"MIT/Apache-2.0"
19+
];
20+
21+
pub fn check(path: &Path, bad: &mut bool) {
22+
let path = path.join("vendor");
23+
assert!(path.exists(), "vendor directory missing");
24+
let mut saw_dir = false;
25+
for dir in t!(path.read_dir()) {
26+
saw_dir = true;
27+
let dir = t!(dir);
28+
let toml = dir.path().join("Cargo.toml");
29+
if !check_license(&toml) {
30+
*bad = true;
31+
}
32+
}
33+
assert!(saw_dir, "no vendored source");
34+
}
35+
36+
fn check_license(path: &Path) -> bool {
37+
if !path.exists() {
38+
panic!("{} does not exist", path.display());
39+
}
40+
let mut contents = String::new();
41+
t!(t!(File::open(path)).read_to_string(&mut contents));
42+
43+
let mut found_license = false;
44+
for line in contents.lines() {
45+
if !line.starts_with("license") {
46+
continue;
47+
}
48+
let license = extract_license(line);
49+
if !LICENSES.contains(&&*license) {
50+
println!("invalid license {} in {}", license, path.display());
51+
return false;
52+
}
53+
found_license = true;
54+
break;
55+
}
56+
if !found_license {
57+
println!("no license in {}", path.display());
58+
return false;
59+
}
60+
61+
true
62+
}
63+
64+
fn extract_license(line: &str) -> String {
65+
let first_quote = line.find('"');
66+
let last_quote = line.rfind('"');
67+
if let (Some(f), Some(l)) = (first_quote, last_quote) {
68+
let license = &line[f + 1 .. l];
69+
license.into()
70+
} else {
71+
"bad-license-parse".into()
72+
}
73+
}

src/tools/tidy/src/main.rs

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ mod errors;
3636
mod features;
3737
mod cargo;
3838
mod pal;
39+
mod deps;
3940

4041
fn main() {
4142
let path = env::args_os().skip(1).next().expect("need an argument");
@@ -48,6 +49,7 @@ fn main() {
4849
cargo::check(&path, &mut bad);
4950
features::check(&path, &mut bad);
5051
pal::check(&path, &mut bad);
52+
deps::check(&path, &mut bad);
5153

5254
if bad {
5355
panic!("some tidy checks failed");

0 commit comments

Comments
 (0)