2020-05-06 05:24:25 +00:00
|
|
|
//! Downloads a testnet configuration from Github.
|
|
|
|
|
2020-09-11 01:43:13 +00:00
|
|
|
use eth2_config::{altona, medalla, Eth2NetArchiveAndDirectory};
|
|
|
|
use std::fs;
|
2020-03-03 23:02:44 +00:00
|
|
|
use std::fs::File;
|
2020-09-11 01:43:13 +00:00
|
|
|
use std::io;
|
|
|
|
use zip::ZipArchive;
|
2020-03-03 23:02:44 +00:00
|
|
|
|
2020-09-11 01:43:13 +00:00
|
|
|
const ETH2_NET_DIRS: &[Eth2NetArchiveAndDirectory<'static>] =
|
|
|
|
&[altona::ETH2_NET_DIR, medalla::ETH2_NET_DIR];
|
2020-03-03 23:02:44 +00:00
|
|
|
|
|
|
|
fn main() {
|
2020-07-29 06:39:29 +00:00
|
|
|
for testnet in ETH2_NET_DIRS {
|
|
|
|
let testnet_dir = testnet.dir();
|
2020-09-11 01:43:13 +00:00
|
|
|
let archive_fullpath = testnet.archive_fullpath();
|
|
|
|
//no need to do anything if archives have already been uncompressed before
|
2020-07-29 06:39:29 +00:00
|
|
|
if !testnet_dir.exists() {
|
2020-09-11 01:43:13 +00:00
|
|
|
if archive_fullpath.exists() {
|
|
|
|
//uncompress archive and continue
|
|
|
|
let archive_file = match File::open(&archive_fullpath) {
|
|
|
|
Ok(f) => f,
|
|
|
|
Err(e) => panic!("Problem opening archive file: {}", e),
|
|
|
|
};
|
2020-07-29 06:39:29 +00:00
|
|
|
|
2020-09-11 01:43:13 +00:00
|
|
|
match uncompress(archive_file) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) => panic!(e),
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
panic!(
|
|
|
|
"Couldn't find testnet archive at this location: {:?}",
|
|
|
|
archive_fullpath
|
|
|
|
);
|
2020-03-19 00:22:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-03 23:02:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 01:43:13 +00:00
|
|
|
fn uncompress(archive_file: File) -> Result<(), String> {
|
|
|
|
let mut archive =
|
|
|
|
ZipArchive::new(archive_file).map_err(|e| format!("Error with zip file: {}", e))?;
|
|
|
|
for i in 0..archive.len() {
|
|
|
|
let mut file = archive
|
|
|
|
.by_index(i)
|
|
|
|
.map_err(|e| format!("Error retrieving file {} inside zip: {}", i, e))?;
|
2020-03-03 23:02:44 +00:00
|
|
|
|
2020-09-11 01:43:13 +00:00
|
|
|
let outpath = file.sanitized_name();
|
2020-03-03 23:02:44 +00:00
|
|
|
|
2020-09-11 01:43:13 +00:00
|
|
|
if file.name().ends_with('/') {
|
|
|
|
fs::create_dir_all(&outpath)
|
|
|
|
.map_err(|e| format!("Error creating testnet directories: {}", e))?;
|
|
|
|
} else {
|
|
|
|
if let Some(p) = outpath.parent() {
|
|
|
|
if !p.exists() {
|
|
|
|
fs::create_dir_all(&p)
|
|
|
|
.map_err(|e| format!("Error creating testnet directories: {}", e))?;
|
|
|
|
}
|
|
|
|
}
|
2020-03-03 23:02:44 +00:00
|
|
|
|
2020-09-11 01:43:13 +00:00
|
|
|
let mut outfile = File::create(&outpath)
|
|
|
|
.map_err(|e| format!("Error while creating file {:?}: {}", outpath, e))?;
|
|
|
|
io::copy(&mut file, &mut outfile)
|
|
|
|
.map_err(|e| format!("Error writing file {:?}: {}", outpath, e))?;
|
|
|
|
}
|
|
|
|
}
|
2020-03-03 23:02:44 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|