Show progress bars

Summary: Add progress bars. The previous change broke the display as we'd write to the console in parallel, too.

Reviewed By: jknoxville

Differential Revision: D21405230

fbshipit-source-id: 3a60690c216fd2f44816ef1b01917f66ebe804df
This commit is contained in:
Pascal Hartig
2020-05-07 07:20:29 -07:00
committed by Facebook GitHub Bot
parent 59aa7decc4
commit dc783bcb19
2 changed files with 35 additions and 13 deletions

View File

@@ -15,6 +15,7 @@ sha2 = "0.8.1"
data-encoding = "2.2.0" data-encoding = "2.2.0"
serde_json = "1.0.52" serde_json = "1.0.52"
rayon = "1.3.0" rayon = "1.3.0"
indicatif = "0.14.0"
[dev-dependencies] [dev-dependencies]
tempdir = "0.3.7" tempdir = "0.3.7"

View File

@@ -13,11 +13,13 @@ use clap::value_t_or_exit;
use rayon::prelude::{IntoParallelIterator, ParallelIterator}; use rayon::prelude::{IntoParallelIterator, ParallelIterator};
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::fs::File; use std::fs::File;
use std::io::{stdout, BufReader, Read, Write}; use std::io::{BufReader, Read, Write};
use std::path; use std::path;
use types::{PackType, Platform}; use types::{PackType, Platform};
const DEFAULT_PACKLIST: &str = include_str!("packlist.yaml"); const DEFAULT_PACKLIST: &str = include_str!("packlist.yaml");
// This is to ensure that all progress bar prefixes are aligned.
const PROGRESS_PREFIX_LEN: usize = 24;
type PackListPlatform = BTreeMap<PackType, Vec<path::PathBuf>>; type PackListPlatform = BTreeMap<PackType, Vec<path::PathBuf>>;
@@ -32,24 +34,35 @@ struct PackManifest {
files: BTreeMap<PackType, HashSum>, files: BTreeMap<PackType, HashSum>,
} }
fn default_progress_bar(len: u64) -> indicatif::ProgressBar {
let pb = indicatif::ProgressBar::new(len as u64 * 2);
pb.set_style(
indicatif::ProgressStyle::default_bar()
.template("{prefix:.bold}▕{bar:.magenta}▏{msg}")
.progress_chars("█▓▒░ "),
);
pb
}
fn pack( fn pack(
platform: &Platform, platform: &Platform,
dist_dir: &std::path::PathBuf, dist_dir: &std::path::PathBuf,
pack_list: &PackList, pack_list: &PackList,
output_directory: &std::path::PathBuf, output_directory: &std::path::PathBuf,
) -> Result<Vec<(PackType, path::PathBuf)>> { ) -> Result<Vec<(PackType, path::PathBuf)>> {
let pb = default_progress_bar(pack_list.0.len() as u64 * 2 - 1);
pb.set_prefix(&format!(
"{:width$}",
"Packing archives",
width = PROGRESS_PREFIX_LEN
));
let packtype_paths = pack_list let packtype_paths = pack_list
.0 .0
.get(platform) .get(platform)
.ok_or_else(|| error::Error::MissingPlatformDefinition(platform.clone()))?; .ok_or_else(|| error::Error::MissingPlatformDefinition(platform.clone()))?;
packtype_paths let res = packtype_paths
.into_par_iter() .into_par_iter()
.map(|(pack_type, pack_files)| { .map(|(pack_type, pack_files)| {
print!(
"Packing for platform {:?} type {:?} ... ",
platform, pack_type
);
let _ = stdout().flush();
let output_path = path::Path::new(output_directory).join(format!("{}.tar", pack_type)); let output_path = path::Path::new(output_directory).join(format!("{}.tar", pack_type));
let mut tar = tar::Builder::new(File::create(&output_path).with_context(|| { let mut tar = tar::Builder::new(File::create(&output_path).with_context(|| {
format!( format!(
@@ -61,12 +74,16 @@ fn pack(
// to the "Current" one. // to the "Current" one.
tar.follow_symlinks(false); tar.follow_symlinks(false);
pack_platform(platform, dist_dir, pack_files, pack_type, &mut tar)?; pack_platform(platform, dist_dir, pack_files, pack_type, &mut tar)?;
pb.inc(1);
tar.finish()?; tar.finish()?;
println!("done."); pb.inc(1);
Ok((*pack_type, output_path)) Ok((*pack_type, output_path))
}) })
.collect() .collect();
pb.finish();
res
} }
fn pack_platform( fn pack_platform(
@@ -165,11 +182,7 @@ fn manifest(
archive_paths: &[(PackType, path::PathBuf)], archive_paths: &[(PackType, path::PathBuf)],
output_directory: &path::PathBuf, output_directory: &path::PathBuf,
) -> Result<path::PathBuf> { ) -> Result<path::PathBuf> {
print!("Generating manifest ... ");
let _ = stdout().flush();
// TODO: This could easily be parallelised.
let archive_manifest = gen_manifest(&archive_paths)?; let archive_manifest = gen_manifest(&archive_paths)?;
println!("done.");
write_manifest(&output_directory, &archive_manifest) write_manifest(&output_directory, &archive_manifest)
} }
@@ -193,11 +206,18 @@ fn gen_manifest(archive_paths: &[(PackType, path::PathBuf)]) -> Result<PackManif
fn gen_manifest_files( fn gen_manifest_files(
archive_paths: &[(PackType, path::PathBuf)], archive_paths: &[(PackType, path::PathBuf)],
) -> Result<BTreeMap<PackType, HashSum>> { ) -> Result<BTreeMap<PackType, HashSum>> {
let pb = default_progress_bar(archive_paths.len() as u64 - 1);
pb.set_prefix(&format!(
"{:width$}",
"Computing manifest",
width = PROGRESS_PREFIX_LEN
));
let res = archive_paths let res = archive_paths
.into_par_iter() .into_par_iter()
.map(|(pack_type, path)| { .map(|(pack_type, path)| {
let reader = BufReader::new(File::open(path)?); let reader = BufReader::new(File::open(path)?);
let hash = sha256_digest(reader)?; let hash = sha256_digest(reader)?;
pb.inc(1);
Ok((*pack_type, hash)) Ok((*pack_type, hash))
}) })
.collect::<Result<Vec<_>>>()? .collect::<Result<Vec<_>>>()?
@@ -209,6 +229,7 @@ fn gen_manifest_files(
acc acc
}, },
); );
pb.finish();
Ok(res) Ok(res)
} }