diff options
| author | Brian Anderson <banderson@mozilla.com> | 2016-12-10 23:27:42 +0000 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2016-12-10 23:27:42 +0000 |
| commit | 5e3be09b58e5ef96573db8d412a8ed5146a47ff1 (patch) | |
| tree | ac68a845721a9fd28246fb2fe1e68804f1fbaa4f | |
| parent | c79d0b40a0e3e9f87ea022054577c5766b36e819 (diff) | |
| download | rust-5e3be09b58e5ef96573db8d412a8ed5146a47ff1.tar.gz rust-5e3be09b58e5ef96573db8d412a8ed5146a47ff1.zip | |
Check the license of vendored deps
| -rw-r--r-- | src/tools/tidy/src/deps.rs | 73 | ||||
| -rw-r--r-- | src/tools/tidy/src/main.rs | 2 |
2 files changed, 75 insertions, 0 deletions
diff --git a/src/tools/tidy/src/deps.rs b/src/tools/tidy/src/deps.rs new file mode 100644 index 00000000000..7592c09a913 --- /dev/null +++ b/src/tools/tidy/src/deps.rs @@ -0,0 +1,73 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Check license of third-party deps by inspecting src/vendor + +use std::fs::File; +use std::io::Read; +use std::path::Path; + +static LICENSES: &'static [&'static str] = &[ + "MIT/Apache-2.0" +]; + +pub fn check(path: &Path, bad: &mut bool) { + let path = path.join("vendor"); + assert!(path.exists(), "vendor directory missing"); + let mut saw_dir = false; + for dir in t!(path.read_dir()) { + saw_dir = true; + let dir = t!(dir); + let toml = dir.path().join("Cargo.toml"); + if !check_license(&toml) { + *bad = true; + } + } + assert!(saw_dir, "no vendored source"); +} + +fn check_license(path: &Path) -> bool { + if !path.exists() { + panic!("{} does not exist", path.display()); + } + let mut contents = String::new(); + t!(t!(File::open(path)).read_to_string(&mut contents)); + + let mut found_license = false; + for line in contents.lines() { + if !line.starts_with("license") { + continue; + } + let license = extract_license(line); + if !LICENSES.contains(&&*license) { + println!("invalid license {} in {}", license, path.display()); + return false; + } + found_license = true; + break; + } + if !found_license { + println!("no license in {}", path.display()); + return false; + } + + true +} + +fn extract_license(line: &str) -> String { + let first_quote = line.find('"'); + let last_quote = line.rfind('"'); + if let (Some(f), Some(l)) = (first_quote, last_quote) { + let license = &line[f + 1 .. l]; + license.into() + } else { + "bad-license-parse".into() + } +} diff --git a/src/tools/tidy/src/main.rs b/src/tools/tidy/src/main.rs index cb11fe261c4..7566580b1a5 100644 --- a/src/tools/tidy/src/main.rs +++ b/src/tools/tidy/src/main.rs @@ -36,6 +36,7 @@ mod errors; mod features; mod cargo; mod pal; +mod deps; fn main() { let path = env::args_os().skip(1).next().expect("need an argument"); @@ -48,6 +49,7 @@ fn main() { cargo::check(&path, &mut bad); features::check(&path, &mut bad); pal::check(&path, &mut bad); + deps::check(&path, &mut bad); if bad { panic!("some tidy checks failed"); |
