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
|
#![feature(assoc_unix_epoch)]
extern crate serde;
extern crate serde_json;
#[macro_use] extern crate serde_derive;
extern crate glob;
extern crate pbr;
extern crate reqwest;
extern crate clap;
extern crate git2;
use glob::{glob_with, MatchOptions};
use pbr::ProgressBar;
use reqwest::Client;
use clap::{Arg, App};
use git2::Repository;
use std::time::SystemTime;
use std::fs::{File, create_dir_all};
use std::io::{BufReader, BufWriter, BufRead, Write};
use std::path::{Path, PathBuf};
use std::collections::HashMap;
use std::error::Error;
#[derive(Debug, Deserialize)]
struct CrateDep {
name: String,
req: String,
features: Vec<String>,
optional: bool,
default_features: bool,
target: Option<String>,
kind: Option<String>,
}
#[derive(Debug, Deserialize)]
struct Crate {
name: String,
vers: String,
deps: Vec<CrateDep>,
yanked: bool,
cksum: String,
features: HashMap<String, Vec<String>>,
}
fn read_crate_file<P>(path: P)
-> Result<Vec<Crate>, Box<Error>>
where P: AsRef<Path>
{
let file = BufReader::new(File::open(path)?);
let lines: Result<Vec<_>, _> = file.lines()
.map(|line| serde_json::from_str(&line.unwrap()))
.collect();
Ok(lines?)
}
fn mirror_path<P>(base: P, cr: Crate) -> PathBuf
where PathBuf: From<P>
{
let mut path = PathBuf::from(base);
path.push("api/v1/crates");
path.push(&cr.name);
path.push(&cr.vers);
if ! path.exists() {
create_dir_all(&path).unwrap();
}
path.push("download");
path
}
fn missing_path(path: &PathBuf) -> bool {
! path.exists()
}
fn download_crates(paths: &[PathBuf])
-> Result<(), Box<Error>>
{
let mut pb = ProgressBar::new(paths.len() as u64);
let client = Client::new();
for path in paths {
let mut file = BufWriter::new(File::create(&path)?);
let uri = format!("https://crates.io/{}", path.to_str().unwrap());
let mut resp = client.get(&uri).send()?;
if resp.status().is_success() {
resp.copy_to(&mut file)?;
} else {
panic!("failed to download {}", uri);
}
pb.inc();
}
Ok(())
}
fn clone_index<P>(path: P) -> Result<(), Box<Error>>
where P: AsRef<Path>
{
eprintln!("cloning index...");
let _repo = Repository::clone("https://github.com/rust-lang/crates.io-index.git", path)?;
eprintln!("done");
Ok(())
}
fn pull_index<P>(path: P) -> Result<(), Box<Error>>
where P: AsRef<Path>
{
let repo = Repository::open(path)?;
let mut remote = repo.find_remote("origin")?;
remote.fetch(&["master"], None, None)?;
let oid = repo.refname_to_id("refs/remotes/origin/master")?;
let object = repo.find_object(oid, None).unwrap();
repo.reset(&object, git2::ResetType::Hard, None)?;
Ok(())
}
fn main() {
let args = App::new("download-crates")
.arg(Arg::with_name("OUTPUT")
.help("Mirror output directory")
.index(1)
.takes_value(true))
.get_matches();
let mirror_base = PathBuf::from(args.value_of("OUTPUT").unwrap_or("."));
if ! mirror_base.exists() {
create_dir_all(&mirror_base).expect("unable to create output directory");
}
let index = {
let mut p = mirror_base.clone();
p.push("crates.io-index");
p
};
let indexglob = {
let mut p = index.clone();
p.push("*");
p.push("**");
p.push("*");
String::from(p.to_str().unwrap())
};
let newfilesfn = {
let epoch = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
let mut p = mirror_base.clone();
p.push(&format!("new-files-{}", epoch.as_secs()));
p
};
let mut newfiles = BufWriter::new(File::create(&newfilesfn).unwrap());
if ! index.exists() {
clone_index(&index).expect("unable to clone index");
} else {
pull_index(&index).expect("unable to pull index");
}
let opts = MatchOptions {
case_sensitive: false,
require_literal_separator: false,
require_literal_leading_dot: true
};
let paths: Vec<_> = glob_with(&indexglob, &opts).unwrap()
.filter_map(|p| p.ok())
.filter(|p| p.is_file())
.flat_map(|p| read_crate_file(p).unwrap())
.map(|p| mirror_path(&mirror_base, p))
.filter(missing_path)
.collect();
download_crates(&paths).expect("downloading crates failed");
for path in paths {
writeln!(&mut newfiles, "{}", path.to_str().unwrap()).unwrap();
}
println!("List of new files written to {}", newfilesfn.to_str().unwrap());
}
|