]> git.lizzy.rs Git - rust.git/blob - src/tools/tidy/src/deps.rs
Annotate the license exceptions
[rust.git] / src / tools / tidy / src / deps.rs
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     "MIT / Apache-2.0",
20     "Apache-2.0/MIT",
21     "MIT OR Apache-2.0",
22     "MIT",
23     "Unlicense/MIT",
24 ];
25
26 // These are exceptions to Rust's permissive licensing policy, and
27 // should be considered bugs. Exceptions are only allowed in Rust
28 // tooling. It is _crucial_ that no exception crates be dependencies
29 // of the Rust runtime (std / test).
30 static EXCEPTIONS: &'static [&'static str] = &[
31     "mdbook", // MPL2, mdbook
32     "openssl", // BSD+advertising clause, cargo, mdbook
33     "pest", // MPL2, mdbook via handlebars
34     "thread-id", // Apache-2.0, mdbook
35 ];
36
37 pub fn check(path: &Path, bad: &mut bool) {
38     let path = path.join("vendor");
39     assert!(path.exists(), "vendor directory missing");
40     let mut saw_dir = false;
41     'next_path: for dir in t!(path.read_dir()) {
42         saw_dir = true;
43         let dir = t!(dir);
44
45         // skip our exceptions
46         for exception in EXCEPTIONS {
47             if dir.path()
48                 .to_str()
49                 .unwrap()
50                 .contains(&format!("src/vendor/{}", exception)) {
51                 continue 'next_path;
52             }
53         }
54
55         let toml = dir.path().join("Cargo.toml");
56         if !check_license(&toml) {
57             *bad = true;
58         }
59     }
60     assert!(saw_dir, "no vendored source");
61 }
62
63 fn check_license(path: &Path) -> bool {
64     if !path.exists() {
65         panic!("{} does not exist", path.display());
66     }
67     let mut contents = String::new();
68     t!(t!(File::open(path)).read_to_string(&mut contents));
69
70     let mut found_license = false;
71     for line in contents.lines() {
72         if !line.starts_with("license") {
73             continue;
74         }
75         let license = extract_license(line);
76         if !LICENSES.contains(&&*license) {
77             println!("invalid license {} in {}", license, path.display());
78             return false;
79         }
80         found_license = true;
81         break;
82     }
83     if !found_license {
84         println!("no license in {}", path.display());
85         return false;
86     }
87
88     true
89 }
90
91 fn extract_license(line: &str) -> String {
92     let first_quote = line.find('"');
93     let last_quote = line.rfind('"');
94     if let (Some(f), Some(l)) = (first_quote, last_quote) {
95         let license = &line[f + 1 .. l];
96         license.into()
97     } else {
98         "bad-license-parse".into()
99     }
100 }