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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use std::collections::HashMap;
use std::env;
use std::fs;
use std::io;
use std::path::Path;
use std::path::PathBuf;
use reqwest;
use serde_json;
use classic_sources_list::Entry;
use release;
use rfc822;
use lists;
use errors::*;
pub struct System {
lists_dir: PathBuf,
sources_entries: Vec<Entry>,
arches: Vec<String>,
keyring_paths: Vec<PathBuf>,
client: reqwest::Client,
}
impl System {
pub fn cache_dirs_only<P: AsRef<Path>>(lists_dir: P) -> Result<Self> {
fs::create_dir_all(lists_dir.as_ref())?;
let client = if let Ok(proxy) = env::var("http_proxy") {
reqwest::Client::builder()
.proxy(reqwest::Proxy::http(&proxy)?)
.build()?
} else {
reqwest::Client::new()
};
Ok(System {
lists_dir: lists_dir.as_ref().to_path_buf(),
sources_entries: Vec::new(),
arches: Vec::new(),
keyring_paths: Vec::new(),
client,
})
}
pub fn add_sources_entry_line(&mut self, src: &str) -> Result<()> {
self.add_sources_entries(::classic_sources_list::read(src)?);
Ok(())
}
pub fn add_sources_entries<I: IntoIterator<Item = Entry>>(&mut self, entries: I) {
self.sources_entries.extend(entries);
}
pub fn set_arches(&mut self, arches: &[&str]) {
self.arches = arches.iter().map(|x| x.to_string()).collect();
}
pub fn add_keyring_paths<P: AsRef<Path>, I: IntoIterator<Item = P>>(
&mut self,
keyrings: I,
) -> Result<()> {
self.keyring_paths
.extend(keyrings.into_iter().map(|x| x.as_ref().to_path_buf()));
Ok(())
}
pub fn update(&self) -> Result<()> {
let requested =
release::RequestedReleases::from_sources_lists(&self.sources_entries, &self.arches)
.chain_err(|| "parsing sources entries")?;
requested
.download(&self.lists_dir, &self.keyring_paths, &self.client)
.chain_err(|| "downloading releases")?;
let releases = requested
.parse(&self.lists_dir)
.chain_err(|| "parsing releases")?;
lists::download_files(&self.client, &self.lists_dir, &releases)
.chain_err(|| "downloading release content")?;
Ok(())
}
pub fn walk_sections<F>(&self, mut walker: F) -> Result<()>
where
F: FnMut(StringSection) -> Result<()>,
{
let releases =
release::RequestedReleases::from_sources_lists(&self.sources_entries, &self.arches)
.chain_err(|| "parsing sources entries")?
.parse(&self.lists_dir)
.chain_err(|| "parsing releases")?;
for release in releases {
for listing in lists::selected_listings(&release) {
for section in lists::sections_in(&release, &listing, &self.lists_dir)? {
let section = section?;
walker(StringSection {
inner: rfc822::map(§ion)
.chain_err(|| format!("loading section: {:?}", section))?,
}).chain_err(|| "processing section")?;
}
}
}
Ok(())
}
pub fn export(&self) -> Result<()> {
self.walk_sections(|section| {
serde_json::to_writer(io::stdout(), §ion.joined_lines())?;
println!();
Ok(())
})
}
pub fn source_ninja(&self) -> Result<()> {
self.walk_sections(|map| {
if map.as_ref().contains_key("Files") {
print_ninja_source(map.as_ref())
} else {
print_ninja_binary(map.as_ref())
}
})
}
}
fn one_line<'a>(lines: &[&'a str]) -> Result<&'a str> {
ensure!(1 == lines.len(), "{:?} isn't exactly one line", lines);
Ok(lines[0])
}
fn subdir(name: &str) -> &str {
if name.starts_with("lib") {
&name[..4]
} else {
&name[..1]
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StringSection<'s> {
inner: HashMap<&'s str, Vec<&'s str>>,
}
impl<'s> StringSection<'s> {
pub fn joined_lines(&self) -> HashMap<&str, String> {
self.inner.iter().map(|(&k, v)| (k, v.join("\n"))).collect()
}
pub fn get_if_one_line(&self, key: &str) -> Option<&str> {
match self.inner.get(key) {
Some(list) => match list.len() {
1 => Some(list[0]),
_ => None,
},
None => None,
}
}
}
impl<'s> AsRef<HashMap<&'s str, Vec<&'s str>>> for StringSection<'s> {
fn as_ref(&self) -> &HashMap<&'s str, Vec<&'s str>> {
&self.inner
}
}
#[cfg(never)]
struct Sections<'i> {
lists_dir: PathBuf,
releases: Box<Iterator<Item = release::Release> + 'i>,
release: release::Release,
listings: Box<Iterator<Item = lists::Listing> + 'i>,
sections: Box<Iterator<Item = Result<String>>>,
}
#[cfg(never)]
impl<'i> Iterator for Sections<'i> {
type Item = Result<String>;
fn next(&mut self) -> Option<Self::Item> {
loop {
if let Some(section) = self.sections.next() {
return Some(section);
}
if let Some(listing) = self.listings.next() {
self.sections = match lists::sections_in(&self.release, &listing, self.lists_dir) {
Ok(sections) => sections,
Err(e) => return Some(Err(e)),
};
continue;
}
}
}
}
fn print_ninja_source(map: &HashMap<&str, Vec<&str>>) -> Result<()> {
let pkg = one_line(&map["Package"])?;
let version = one_line(&map["Version"])?.replace(':', "$:");
let dir = one_line(&map["Directory"])?;
let dsc = map["Files"]
.iter()
.filter(|line| line.ends_with(".dsc"))
.next()
.unwrap()
.split_whitespace()
.nth(2)
.unwrap();
let size: u64 = map["Files"]
.iter()
.map(|line| {
let num: &str = line.split_whitespace().nth(1).unwrap();
let num: u64 = num.parse().unwrap();
num
})
.sum();
let prefix = format!("{}/{}_{}", subdir(pkg), pkg, version);
println!("build $dest/{}$suffix: process-source | $script", prefix);
println!(" description = PS {} {}", pkg, version);
println!(" pkg = {}", pkg);
println!(" version = {}", version);
println!(" url = $mirror/{}/{}", dir, dsc);
println!(" prefix = {}", prefix);
println!(" size = {}", size);
if size > 250 * 1024 * 1024 {
println!(" pool = massive")
} else if size > 100 * 1024 * 1024 {
println!(" pool = big")
}
Ok(())
}
fn print_ninja_binary(map: &HashMap<&str, Vec<&str>>) -> Result<()> {
let pkg = one_line(&map["Package"])?;
let source = one_line(&map.get("Source").unwrap_or_else(|| &map["Package"]))?
.split_whitespace()
.nth(0)
.unwrap();
let arch = one_line(&map["Architecture"])?;
let version = one_line(&map["Version"])?.replace(':', "$:");
let filename = one_line(&map["Filename"])?;
let size: u64 = one_line(&map["Size"])?.parse()?;
let prefix = format!("{}/{}/{}_{}_{}", subdir(source), source, pkg, version, arch);
println!("build $dest/{}$suffix: process-binary | $script", prefix);
println!(" description = PB {} {} {} {}", source, pkg, version, arch);
println!(" source = {}", source);
println!(" pkg = {}", pkg);
println!(" version = {}", version);
println!(" arch = {}", arch);
println!(" url = $mirror/{}", filename);
println!(" prefix = {}", prefix);
if size > 250 * 1024 * 1024 {
println!(" pool = massive")
} else if size > 100 * 1024 * 1024 {
println!(" pool = big")
}
Ok(())
}