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