Rewrite imag binary without parallelization stuff

This commit is contained in:
Matthias Beyer 2017-08-26 20:47:23 +02:00
parent befff96220
commit 09fbf08747
2 changed files with 54 additions and 72 deletions

View File

@ -23,7 +23,6 @@ libimagutil = { path = "../libimagutil" }
[dependencies] [dependencies]
version = "2.0" version = "2.0"
walkdir = "0.1" walkdir = "0.1"
crossbeam = "0.2"
clap = "2.*" clap = "2.*"
log = "0.3" log = "0.3"

View File

@ -17,7 +17,6 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
extern crate crossbeam;
extern crate clap; extern crate clap;
#[macro_use] extern crate version; #[macro_use] extern crate version;
#[macro_use] extern crate log; #[macro_use] extern crate log;
@ -33,7 +32,6 @@ use std::process::Stdio;
use std::io::ErrorKind; use std::io::ErrorKind;
use walkdir::WalkDir; use walkdir::WalkDir;
use crossbeam::*;
use clap::{Arg, AppSettings, SubCommand}; use clap::{Arg, AppSettings, SubCommand};
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
@ -42,7 +40,7 @@ use libimagerror::trace::trace_error;
/// Returns the helptext, putting the Strings in cmds as possible /// Returns the helptext, putting the Strings in cmds as possible
/// subcommands into it /// subcommands into it
fn help_text(cmds: Vec<String>) -> String { fn help_text(cmds: Vec<String>) -> String {
let text = format!(r#" format!(r#"
_ _
(_)_ __ ___ __ _ __ _ (_)_ __ ___ __ _ __ _
@ -72,59 +70,43 @@ fn help_text(cmds: Vec<String>) -> String {
imag is free software. It is released under the terms of LGPLv2.1 imag is free software. It is released under the terms of LGPLv2.1
(c) 2016 Matthias Beyer and contributors"#, imagbins = cmds.into_iter() (c) 2016 Matthias Beyer and contributors"#,
imagbins = cmds
.into_iter()
.map(|cmd| format!("\t{}\n", cmd)) .map(|cmd| format!("\t{}\n", cmd))
.fold(String::new(), |s, c| { .fold(String::new(), |s, c| {
let s = s + c.as_str(); let s = s + c.as_str();
s s
})); }))
text
} }
/// Returns the list of imag-* executables found in $PATH /// Returns the list of imag-* executables found in $PATH
fn get_commands() -> Vec<String> { fn get_commands() -> Vec<String> {
let path = env::var("PATH"); match env::var("PATH") {
if path.is_err() { Err(e) => {
println!("PATH error: {:?}", path); println!("PATH error: {:?}", e);
exit(1); exit(1);
} },
let pathelements = path.unwrap();
let pathelements = pathelements.split(":");
let joinhandles : Vec<ScopedJoinHandle<Vec<String>>> = pathelements Ok(path) => path
.map(|elem| { .split(":")
crossbeam::scope(|scope| { .flat_map(|elem| {
scope.spawn(|| {
WalkDir::new(elem) WalkDir::new(elem)
.max_depth(1) .max_depth(1)
.into_iter() .into_iter()
.filter(|path| { .filter(|path| match *path {
match path { Ok(ref p) => p.file_name().to_str().map_or(false, |f| f.starts_with("imag-")),
&Ok(ref p) => p.file_name() Err(_) => false,
.to_str()
.map_or(false, |filename| filename.starts_with("imag-")),
&Err(_) => false,
}
}) })
.filter_map(|x| x.ok()) .filter_map(Result::ok)
.filter_map(|path| { .filter_map(|path| path
path.file_name() .file_name()
.to_str() .to_str()
.and_then(|s| s.splitn(2, "-").nth(1).map(String::from)) .and_then(|s| s.splitn(2, "-").nth(1).map(String::from))
)
}) })
.collect() .collect()
})
})
})
.collect();
let mut execs = vec![];
for joinhandle in joinhandles.into_iter() {
let mut v = joinhandle.join();
execs.append(&mut v);
} }
execs
} }
@ -171,26 +153,27 @@ fn main() {
if matches.is_present("versions") { if matches.is_present("versions") {
debug!("Showing versions"); debug!("Showing versions");
let mut result = vec![]; commands
for command in commands.iter() { .iter()
result.push(crossbeam::scope(|scope| { .map(|command| {
scope.spawn(|| { match Command::new(format!("imag-{}", command))
let v = Command::new(format!("imag-{}",command)).arg("--version").output(); .arg("--version")
match v { .output()
Ok(v) => match String::from_utf8(v.stdout) { .map(|v| v.stdout)
{
Ok(s) => match String::from_utf8(s) {
Ok(s) => format!("{:10} -> {}", command, s), Ok(s) => format!("{:10} -> {}", command, s),
Err(e) => format!("Failed calling {} -> {:?}", command, e), Err(e) => format!("UTF8 Error while working with output of imag{}: {:?}", command, e),
}, },
Err(e) => format!("Failed calling {} -> {:?}", command, e), Err(e) => format!("Failed calling imag-{} -> {:?}", command, e),
} }
}) })
})) .fold((), |_, line| {
}
for versionstring in result.into_iter().map(|handle| handle.join()) {
// The amount of newlines may differ depending on the subprocess // The amount of newlines may differ depending on the subprocess
println!("{}", versionstring.trim()); println!("{}", line.trim());
} });
exit(0);
} }
// Matches any subcommand given // Matches any subcommand given
@ -213,7 +196,7 @@ fn main() {
.stderr(Stdio::inherit()) .stderr(Stdio::inherit())
.args(&subcommand_args[..]) .args(&subcommand_args[..])
.spawn() .spawn()
.and_then(|mut handle| handle.wait()) .and_then(|mut c| c.wait())
{ {
Ok(exit_status) => { Ok(exit_status) => {
if !exit_status.success() { if !exit_status.success() {
@ -230,7 +213,7 @@ fn main() {
ErrorKind::NotFound => { ErrorKind::NotFound => {
println!("No such command: 'imag-{}'", subcommand); println!("No such command: 'imag-{}'", subcommand);
println!("See 'imag --help' for available subcommands"); println!("See 'imag --help' for available subcommands");
exit(2); exit(1);
}, },
ErrorKind::PermissionDenied => { ErrorKind::PermissionDenied => {
println!("No permission to execute: 'imag-{}'", subcommand); println!("No permission to execute: 'imag-{}'", subcommand);