diff --git a/Cargo.lock b/Cargo.lock index 35ba3e3..2bc3f55 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -121,6 +121,18 @@ name = "num-traits" version = "0.1.40" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "pbr" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)", + "termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "proc-macro2" version = "0.2.3" @@ -146,6 +158,19 @@ dependencies = [ "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "redox_syscall" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "redox_termios" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "regex" version = "0.2.3" @@ -181,6 +206,7 @@ dependencies = [ "gumdrop_derive 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "is_executable 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "pbr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "rhai 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -224,6 +250,16 @@ dependencies = [ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "termion" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "thread_local" version = "0.3.4" @@ -233,6 +269,16 @@ dependencies = [ "unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "time" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "unicode-xid" version = "0.1.0" @@ -322,9 +368,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)" = "36fbc8a8929c632868295d0178dd8f63fc423fd7537ad0738372bd010b3ac9b0" "checksum memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "796fba70e76612589ed2ce7f45282f5af869e0fdd7cc6199fa1aa1f1d591ba9d" "checksum num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "99843c856d68d8b4313b03a17e33c4bb42ae8f6610ea81b28abe076ac721b9b0" +"checksum pbr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "deb73390ab68d81992bd994d145f697451bb0b54fd39738e72eef32458ad6907" "checksum proc-macro2 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cd07deb3c6d1d9ff827999c7f9b04cdfd66b1b17ae508e14fe47b620f2282ae0" "checksum quote 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1eca14c727ad12702eb4b6bfb5a232287dcf8385cb8ca83a3eeaf6519c44c408" "checksum rand 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)" = "6475140dfd8655aeb72e1fd4b7a1cc1c202be65d71669476e392fe62532b9edd" +"checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1" +"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" "checksum regex 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ac6ab4e9218ade5b423358bbd2567d1617418403c7a512603630181813316322" "checksum regex-syntax 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad890a5eef7953f55427c50575c680c42841653abd2b028b68cd223d157f62db" "checksum rhai 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7d8dd1945cb3f6c777074814bb2856a8fce36af4d6aa6f479c6a3029869e865b" @@ -333,7 +382,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum serde 0.8.23 (registry+https://github.com/rust-lang/crates.io-index)" = "9dad3f759919b92c3068c696c15c3d17238234498bbdcc80f2c469606f948ac8" "checksum shellexpand 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de7a5b5a9142fd278a10e0209b021a1b85849352e6951f4f914735c976737564" "checksum syn 0.12.14 (registry+https://github.com/rust-lang/crates.io-index)" = "8c5bc2d6ff27891209efa5f63e9de78648d7801f085e4653701a692ce938d6fd" +"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum thread_local 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "1697c4b57aeeb7a536b647165a2825faddffb1d3bad386d507709bd51a90bb14" +"checksum time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "d825be0eb33fda1a7e68012d51e9c7f451dc1a69391e7fdc197060bb8c56667b" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" "checksum users 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e7d8fb16f17ce0e6a18a25ce39f08edb5fbe9a25f3f346c9dca5e6ffc0485cdf" diff --git a/Cargo.toml b/Cargo.toml index 9ca9856..06961f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ gumdrop_derive = "0.4.0" ansi_term = "0.9" glob = "0.2" dirs = "1.0" +pbr = "1.0.1" [[bin]] name = "rsmodules" diff --git a/README.md b/README.md index 796920c..14a31bd 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ RSModules has a few configuration options which are located in the setup scripts The scripts ```setup_rsmodules.sh``` and ```setup_rsmodules.sh``` need to be sourced when the user logs in. You either symlink these in /etc/profile.d/ or source them in the users init scripts (.bashrc, .cshrc, .zshrc, ...) -After you have added modulefiles, don't forget to update the module cache by running the command: ```module makecache``` +After you have added modulefiles, don't forget to update the module cache by running the command: ```update_modules_cache``` In case you want to use RSModules inside your python or perl scripts, add the ```$RSMODULES_INSTALL_DIR``` to the ```$PYTHONPAH``` and/or ```$PERL5LIB``` environment variable(s). But this shouldn't be needed as the ```setup_rsmodules.(c)sh``` scripts do this for you. @@ -110,7 +110,7 @@ On the next login the ```module``` command will be available A dummy module will be created to help you around. -After you have added modulefiles, don't forget to update the module cache by running the command: ```module makecache``` +After you have added modulefiles, don't forget to update the module cache by running the command: ```update_modules_cache``` ### Compiling from source First you'll have to install Rust on your system: @@ -200,6 +200,11 @@ After you have created new modulefiles, don't forget to update the module cache [user@awesome ~]$ module makecache ``` +You can also have a progress bar while updating the cache. +```bash +[user@awesome ~] update_modules_cache +``` + ### Using from perl When you source the file ```setup_rsmodules.(c)sh``` there is a path added to your ```$PERL5LIB``` environment diff --git a/setup_rsmodules.csh b/setup_rsmodules.csh index 4d510b5..d34dd01 100644 --- a/setup_rsmodules.csh +++ b/setup_rsmodules.csh @@ -1,6 +1,8 @@ #!/bin/csh alias module 'setenv TERMWIDTH `stty size |& tee /dev/null | cut -d" " -f2` ; eval `$RSMODULES_INSTALL_DIR/rsmodules csh,$TERMWIDTH ' \!'*` ' +alias update_modules_cache 'setenv TERMWIDTH `stty size |& tee /dev/null | cut -d" " -f2` ; $RSMODULES_INSTALL_DIR/rsmodules progressbar,$TERMWIDTH makecache ' + setenv MODULEPATH "" setenv RSMODULES_INSTALL_DIR "" #setenv LOADEDMODULES "" diff --git a/setup_rsmodules.sh b/setup_rsmodules.sh index 16b75b5..0e1cff1 100644 --- a/setup_rsmodules.sh +++ b/setup_rsmodules.sh @@ -5,6 +5,11 @@ module() { eval `$RSMODULES_INSTALL_DIR/rsmodules bash,$TERMWIDTH $*`; } +update_modules_cache() { + export TERMWIDTH=`/bin/stty size 2>&1 | cut -d" " -f2`; + $RSMODULES_INSTALL_DIR/rsmodules progressbar,$TERMWIDTH makecache; +} + export MODULEPATH="" export RSMODULES_INSTALL_DIR="" #export LOADEDMODULES="" diff --git a/src/cache.rs b/src/cache.rs index 1f40cff..277a8d1 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -32,6 +32,8 @@ extern crate bincode; use bincode::rustc_serialize::{decode_from, encode_into}; use super::{crash, echo, get_module_description, get_module_paths, is_module_loaded}; +use pbr::ProgressBar; + pub static MODULESINDEX: &str = ".modulesindex"; #[derive(RustcEncodable, RustcDecodable, Clone, Eq)] @@ -117,6 +119,20 @@ pub fn update(modulepath: &str, shell: &str) -> bool { let mut index_succes: i32 = 0; let mut index_default: i32 = 0; + let file_str = format!("{}/{}", modulepath, MODULESINDEX); + let num_modules = count_modules_in_cache(&PathBuf::from(&file_str)) * 2; + + if shell == "progressbar" { + echo("", shell); + echo(&format!(" Indexing {}", modulepath), shell); + echo("", shell); + } + let mut pb = ProgressBar::new(0); + + if num_modules != 0 && shell == "progressbar" { + pb = ProgressBar::new(num_modules as u64); + } + for entry in WalkDir::new(module_path).into_iter().filter_map(|e| e.ok()) { let str_path: &str = entry.path().to_str().unwrap(); @@ -125,6 +141,9 @@ pub fn update(modulepath: &str, shell: &str) -> bool { if !entry.path().is_dir() { for mut modulename in part { if modulename != "" { + if shell == "progressbar" { + pb.inc(); + } let first = modulename.chars().next().unwrap(); let second = if modulename.len() >= 2 { &modulename[1..2] } else { "" }; @@ -177,9 +196,11 @@ pub fn update(modulepath: &str, shell: &str) -> bool { index_default += 1; } add_module(modulename, description, flags, &mut modules); + if shell == "progressbar" { + pb.inc(); + } } - let file_str = format!("{}/{}", modulepath, MODULESINDEX); let file: File = match File::create(&file_str) { Ok(file) => file, Err(_) => { @@ -199,6 +220,12 @@ pub fn update(modulepath: &str, shell: &str) -> bool { } }; + if shell == "progressbar" { + echo("", shell); + } + echo("", shell); + echo(&" Writing cache file.", shell); + let mut writer = BufWriter::new(file); encode_into(&modules, &mut writer, bincode::SizeLimit::Infinite).unwrap(); @@ -217,11 +244,34 @@ pub fn update(modulepath: &str, shell: &str) -> bool { } else { let msg: String = format!("{} success", modulepath); echo(&msg, shell); + let tmp = format!("{}", index_succes); + let msg: String = format!("Total number of modules: {}", &tmp); + echo(&msg, shell); + let tmp = format!("{}", index_default); + let msg: String = format!("Number of default (D) modules: {}", &tmp); + echo(&msg, shell); } true } +fn count_modules_in_cache(filename: &PathBuf) -> usize { + let file: File = match File::open(filename) { + Ok(file) => file, + Err(_) => { + crash( + super::super::CRASH_COULDNT_OPEN_CACHE_FILE, + "modules_cache_file: couldn't open the required index file", + ); + return 0; + } + }; + let mut reader = BufReader::new(file); + let decoded: Vec = decode_from(&mut reader, bincode::SizeLimit::Infinite).unwrap(); + + decoded.len() +} + pub fn parse_modules_cache_file(filename: &PathBuf, modules: &mut Vec<(String, i64)>) { let file: File = match File::open(filename) { Ok(file) => file, diff --git a/src/main.rs b/src/main.rs index 5ad0aa9..4c59a8e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,6 +51,7 @@ extern crate gumdrop; #[macro_use] extern crate gumdrop_derive; extern crate is_executable; +extern crate pbr; extern crate regex; extern crate shellexpand; @@ -152,6 +153,8 @@ static LONG_HELP: &str = " Updates the .modulesindex file in all the paths that are found in the $MODULEPATH variable. This ofcourse only works if you have the correct permissions. ;) + If you want a progress bar use the command: + update_modules_cache instead of module makecache * create [modulename] Starts a wizard to create a modulefile. @@ -169,12 +172,11 @@ static LONG_HELP: &str = " "; fn is_shell_supported(shell: &str) -> bool { - // when noshell is selected, all output is printed // to stdout instead of the temp file // noshell is also useful for debugging purposes - let shell_list = vec!["tcsh","csh","bash","zsh","noshell","python","perl"];//Vec::new(); + let shell_list = vec!["tcsh", "csh", "bash", "zsh", "noshell", "python", "perl", "progressbar"]; if shell_list.contains(&shell) { return true; @@ -312,6 +314,7 @@ fn run(args: &[String]) { }; let mut quoted_string: String; + let mut command_hit: &str = ""; if args.len() >= 3 { command = &args[2]; let matches: bool; @@ -373,7 +376,6 @@ fn run(args: &[String]) { } let mut num_hits: i32 = 0; - let mut command_hit: &str = ""; for cmd in command_list { if cmd.starts_with(command) { @@ -455,7 +457,7 @@ fn run(args: &[String]) { // this is used for scripts that want to parse the module av output // for example for tab completion - if shell != "noshell" && shell != "python" && shell != "perl" { + if shell != "noshell" && shell != "python" && shell != "perl" && shell != "progressbar" { // we want a self destructing tmpfile // so it must delete itself at the end of the run // if it crashes we still need to delete the file diff --git a/src/rsmodules.rs b/src/rsmodules.rs index d89d22c..14b6ef7 100644 --- a/src/rsmodules.rs +++ b/src/rsmodules.rs @@ -630,7 +630,7 @@ pub fn is_other_version_of_module_loaded(name: &str) -> bool { pub fn echo(line: &str, shell: &str) { //FIXME: if line contains \n and shell is csh or tcsh // escape it - if shell == "noshell" { + if shell == "noshell" || shell == "progressbar" { println!("{}", line); } else if shell == "python" { println!("print(\"{}\")", line);