10
10
11
11
//! Check license of third-party deps by inspecting src/vendor
12
12
13
+ use std:: collections:: HashSet ;
13
14
use std:: fs:: File ;
14
15
use std:: io:: Read ;
15
16
use std:: path:: Path ;
16
-
17
17
use std:: process:: Command ;
18
18
19
19
use serde_json;
@@ -56,22 +56,40 @@ static WHITELIST: &'static [(&'static str, &'static str)] = &[];
56
56
#[ derive( Deserialize ) ]
57
57
struct Output {
58
58
packages : Vec < Package > ,
59
- _resolve : String ,
59
+
60
+ // Not used, but needed to not confuse serde :P
61
+ #[ allow( dead_code) ] resolve : Resolve ,
60
62
}
61
63
62
64
#[ derive( Deserialize ) ]
63
65
struct Package {
64
- _id : String ,
65
66
name : String ,
66
67
version : String ,
67
- _source : Option < String > ,
68
- _manifest_path : String ,
68
+
69
+ // Not used, but needed to not confuse serde :P
70
+ #[ allow( dead_code) ] id : String ,
71
+ #[ allow( dead_code) ] source : Option < String > ,
72
+ #[ allow( dead_code) ] manifest_path : String ,
73
+ }
74
+
75
+ // Not used, but needed to not confuse serde :P
76
+ #[ allow( dead_code) ]
77
+ #[ derive( Deserialize ) ]
78
+ struct Resolve {
79
+ nodes : Vec < ResolveNode > ,
80
+ }
81
+
82
+ // Not used, but needed to not confuse serde :P
83
+ #[ allow( dead_code) ]
84
+ #[ derive( Deserialize ) ]
85
+ struct ResolveNode {
86
+ id : String ,
87
+ dependencies : Vec < String > ,
69
88
}
70
89
71
90
/// Checks the dependency at the given path. Changes `bad` to `true` if a check failed.
72
91
///
73
- /// Specifically, this checks that the license is correct and that the dependencies are on the
74
- /// whitelist.
92
+ /// Specifically, this checks that the license is correct.
75
93
pub fn check ( path : & Path , bad : & mut bool ) {
76
94
// Check licences
77
95
let path = path. join ( "vendor" ) ;
@@ -95,21 +113,35 @@ pub fn check(path: &Path, bad: &mut bool) {
95
113
* bad = * bad || !check_license ( & toml) ;
96
114
}
97
115
assert ! ( saw_dir, "no vendored source" ) ;
116
+ }
98
117
118
+ /// Checks the dependency at the given path. Changes `bad` to `true` if a check failed.
119
+ ///
120
+ /// Specifically, this checks that the dependencies are on the whitelist.
121
+ pub fn check_whitelist ( path : & Path , bad : & mut bool ) {
99
122
// Check dependencies
100
- let deps = get_deps ( & path) ;
101
- * bad = * bad
102
- || deps. iter ( ) . any (
103
- |& Package {
104
- ref name,
105
- ref version,
106
- ..
107
- } | {
108
- WHITELIST
109
- . iter ( )
110
- . all ( |& ( wname, wversion) | name != wname || version != wversion)
111
- } ,
112
- ) ;
123
+ let deps: HashSet < _ > = get_deps ( & path)
124
+ . into_iter ( )
125
+ . map ( |Package { name, version, .. } | ( name, version) )
126
+ . collect ( ) ;
127
+ let whitelist: HashSet < ( String , String ) > = WHITELIST
128
+ . iter ( )
129
+ . map ( |& ( n, v) | ( n. to_owned ( ) , v. to_owned ( ) ) )
130
+ . collect ( ) ;
131
+
132
+ // Dependencies not in the whitelist
133
+ let mut unapproved: Vec < _ > = deps. difference ( & whitelist) . collect ( ) ;
134
+
135
+ // For ease of reading
136
+ unapproved. sort ( ) ;
137
+
138
+ if unapproved. len ( ) > 0 {
139
+ println ! ( "Dependencies not on the whitelist:" ) ;
140
+ for dep in unapproved {
141
+ println ! ( "* {} {}" , dep. 0 , dep. 1 ) ; // name version
142
+ }
143
+ * bad = true ;
144
+ }
113
145
}
114
146
115
147
fn check_license ( path : & Path ) -> bool {
0 commit comments