1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#![feature(once_cell)]
use std::path::{PathBuf, Path};
use cargo_metadata::camino::Utf8PathBuf;
use walkdir::WalkDir;
use std::lazy::SyncOnceCell;
use regex::Regex;
use cargo_metadata::Package;
use std::fs::create_dir_all;
use std::io::{ErrorKind, BufReader, BufRead};
pub fn path(package: cargo_metadata::Package) -> Option<Utf8PathBuf> {
package.manifest_path.parent().map(|path| path.to_path_buf())
}
static LICENSE_REGEX: SyncOnceCell<Regex> = SyncOnceCell::new();
static CHECK_CONTENT_REGEX: SyncOnceCell<Regex> = SyncOnceCell::new();
static LICENSE_CONTENT_REGEX: SyncOnceCell<Regex> = SyncOnceCell::new();
pub fn search_for_licenses(package: &Package) -> Option<(PathBuf, Box<dyn Iterator<Item=PathBuf>>)> {
let license_regex = LICENSE_REGEX.get_or_init(|| {
Regex::new(r"(?i)LICENSE|COPYRIGHT|NOTICE|AUTHORS|CONTRIBUTORS|COPYING|PATENT").unwrap()
});
let license_content_regex = LICENSE_CONTENT_REGEX.get_or_init(|| {
Regex::new(r"(?i)LICENSE|COPYRIGHT|COPYING|PATENT").unwrap()
});
let check_content_regex = CHECK_CONTENT_REGEX.get_or_init(|| {
Regex::new(r"(?i)\.html|\.txt|\.md|README").unwrap()
});
let path = package.manifest_path.parent().map(|path| path.to_path_buf())?;
Some((path.canonicalize().unwrap(),
if let Some(license_file) = &package.license_file() {
Box::new(vec!(license_file.canonicalize().unwrap()).into_iter())
} else {
Box::new(WalkDir::new(path)
.into_iter()
.filter_map(|entry| entry.ok())
.filter(move |entry|
license_regex.is_match(&entry.path().to_string_lossy()) ||
(
check_content_regex.is_match(&entry.path().to_string_lossy()) &&
matches_any_line(&license_content_regex, entry.path()).unwrap_or_default()
)
)
.map(|entry| entry.path().to_path_buf()))
}
))
}
fn matches_any_line(regex: &Regex, file: &Path) -> Option<bool> {
let file = std::fs::File::open(file).ok()?;
let reader = BufReader::new(file);
Some(reader.lines()
.filter_map(|line| line.ok())
.any(|line| regex.is_match(&line)))
}
fn with_relative_paths<'a>(root_path: PathBuf, paths: Box<dyn Iterator<Item=PathBuf> + 'a>) -> Box<dyn Iterator<Item=(PathBuf, PathBuf)> + 'a> {
Box::new(paths.map(move |path| {
let stripped_path = (&path.canonicalize().unwrap()).strip_prefix(&root_path.canonicalize().unwrap()).map_err(|err| format!("Couldn't remove the prefix {:?} from {:?}: {}", &root_path, &path, err)).unwrap().to_path_buf();
(path, stripped_path)
}))
}
pub type Licenses = Box<dyn Iterator<Item=(String, PathBuf, Box<dyn Iterator<Item=PathBuf>>)>>;
pub fn search_for_all_licenses(metadata: cargo_metadata::Metadata) -> Licenses {
let packages = metadata.packages;
let workspace_members = metadata.workspace_members;
let external_packages: Vec<Package> = packages.into_iter()
.filter(|package| !(&workspace_members).contains(&package.id))
.collect();
Box::new(external_packages.into_iter()
.filter_map(|package| Some(package.name.clone()).zip(search_for_licenses(&package)))
.map(|(name, (root_path, license_paths))| (name, root_path, license_paths))
)
}
pub fn copy_licenses_to(destination: &Path, licenses: Licenses) -> std::io::Result<Vec<std::io::Result<()>>> {
create_dir_all(destination)?;
Ok(
licenses
.map(move |(name, root_path, license_paths)| {
(name, with_relative_paths(root_path, license_paths))
})
.map(|(name, license_pairs)|
license_pairs.map(move |(src, stem)| (src, destination.join(name.clone()).join(stem)))
)
.flatten()
.map(|(src, dst)| dst.parent().map(create_dir_all).unwrap_or(Ok(()))
.and(flatten_copy_dir_result(copy_dir::copy_dir(src, dst))))
.collect()
)
}
#[inline]
fn flatten_copy_dir_result(result: Result<Vec<std::io::Error>, std::io::Error>) -> Result<(), std::io::Error> {
match result {
Ok(multiple) => if multiple.is_empty() {
Ok(())
} else {
Err(std::io::Error::new(ErrorKind::Other, format!("{:?}", multiple)))
},
Err(err) => Err(err)
}
}