Skip to content

Commit

Permalink
During the cache update a progress bar can now be displayed. To have …
Browse files Browse the repository at this point in the history
…a progress bar use the new command 'update_modules_cache'.
  • Loading branch information
Frederik Delaere committed Oct 15, 2018
1 parent 7024473 commit 5ad76b1
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 8 deletions.
51 changes: 51 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions setup_rsmodules.csh
Original file line number Diff line number Diff line change
@@ -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 ""
Expand Down
5 changes: 5 additions & 0 deletions setup_rsmodules.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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=""
Expand Down
52 changes: 51 additions & 1 deletion src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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();

Expand All @@ -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 { "" };

Expand Down Expand Up @@ -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(_) => {
Expand All @@ -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();

Expand All @@ -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<Module> = 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,
Expand Down
10 changes: 6 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/rsmodules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 5ad76b1

Please sign in to comment.