|
| 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 | +} |
0 commit comments