Speech:Train 01.pl

Summary
Title: train_01.pl

Author: Eric Beikman

location: mnt/main/scripts/user/train_01.pl

Date: June, 2013

Revisions:
 * Version: 1.1
 * Fixed a bug which was created by $0's inconsistent output.
 * Version 1.0:
 * Initial release.

Usage:

Description
This Script is designed to automate the first series of steps in running a train

Normal operation is indicated with no given arguments, the script will automatically determine the lowest available experiment number to use based on the contents of /mnt/main/Exp directory. Normally, it will choose the next number in the experiment number sequence (E.G. it will choose experiment# 0071 if the last experiment number used was #0070); however, in the event there is a gap in between two experiment numbers, it will choose the lowest available experiment number within that gap. For example: If there is an experiment# 0095 and experiment #0098 with no experiments 0096-0097, the script will choose experiment# 0096.


 * Options:
 * -a:
 * Once finished with creating the files, the script will give the created base experiment folder and everything contained within it (recursively), group write access to the user's default group.


 * -n:
 * Specify an experiment number. Specifies an experiment number to utilize, bypassing the behavior detailed above. That being said, it will check to see if the experiment has been created, if it has, the script will error out; this is intentional to protect the integrity of older experiments.


 * -h:
 * Usage help, this text. Does not execute the script.


 * -s:
 * Specify an alternate SphinxTrain setup script. By default, this script will use '"/mnt/main/root/tools/SphinxTrain-1.0/scripts_pl/setup_SphinxTrain.pl'. If you desire to utilize a different script, specifiy it here, flags may not work. Keep in mind that this script will append a '-task ' to the end of the supplied script before executing it; do not add it on within this argument or the script will error out. The type of script given does not matter, keep in mind that if it can't be executed directly in the shell (using ./ ), it can't be executed here; so make sure that the user has filesystem permissions to execute it, and that it has a '#!/' to specify what type of script it is.

Once the script is finished successfully. It will generate a log located at logdir/aes/train_01.pl.log. The contents of this log include the arguments passed to the script, the calling process, and the contents of the setup_SphinxTrain.pl script. It will then print out the experiment number of the experiment which was created, then exiting with a '0'. This script does not print anything but that number during the normal operation; as such, it will work well if called by another script. In case of failure, the script will 'die', returning a '1' to the calling process and prints out the error to standard error.

Code
use Getopt::Std;#Simplifies CMD line Options use File::Path; use File::Basename; #Allows for a more consistant way of getting the script's name without resorting to $0, the output of which varies.
 * 1) !/usr/bin/perl
 * 2)   Title: train_001.pl
 * 3)   Author: Eric Beikman
 * 4)       Date : June, 2013
 * 5) 	Version: 1.1
 * 6) 		Fixed a bug which was created by $0's inconsistent output.
 * 7) 	Version 1.0:
 * 8) 		Initial release.
 * 9) Experiment directory setup script:
 * 10) Accomplishes the following tasks:
 * 11) 1) Creates a new Experiment directory in the root experiment directory.
 * 12) 2) Runs the setup_SphinxTrain.pl script with the appropriate arguments.
 * 13) 3) Gives that base experiment directory group read/write permissions.
 * 1) 2) Runs the setup_SphinxTrain.pl script with the appropriate arguments.
 * 2) 3) Gives that base experiment directory group read/write permissions.

$group = `groups | awk '{print \$1}'`; chop $group; $setupScript = "/mnt/main/root/tools/SphinxTrain-1.0/scripts_pl/setup_SphinxTrain.pl";#default setup_sphinxTrain.pl script to use if none are specified.

$scriptName = basename($0);

$help = "$scriptName: The 0Experiment Directory Setup script.

This script will do the following: 1) Creates a new Experiment directory (specify with -n flag) in the root experiment directory.	2) Runs the setup_SphinxTrain.pl (customize with -s flag) script with the appropriate arguments. 3)(with -a flag) Gives the user's default group (Yours is: '$group') read/write permissions to both the newly created base experiment directory, but also everything within that directory.

Usage: $scriptName [-hw] [-n ] [-s ]

Normal operation is indicated with no given arguments, the script will automatically determine the lowest available experiment number to use based on the previous experiments within the /mnt/main/Exp directory. Normally, it will choose the next number in the experiment number sequence (E.G. it will choose experiment# 0071 if the last experiment number used was #0070); however, in the event there is a gap in between two experiment numbers, it will choose the lowest available experiment number within that gap. For example: If there is an experiment# 0095 and experiment #0098 with no experiments 0096-0097, the script will choose experiment# 0096.\n Options: -a: Once finished with creating the files, the script will give the created base experiment folder and everything contained within it (recursively), group write access to the user's default group (in your case it's: '$group').

-n: Specify an experiment number. Specifies an experiment number to utilize, bypassing the behavior detailed above. That being said, it will check to see if the experiment has been created, if it has, the script will error out; this is intentional.

-h: Usage help, this text. Does not execute script. -s: Specify an alternate SphinxTrain setup script. By default, this script will use '$setupScript'. If you desire to utilize a different script, specifiy it here, flags may not work. Keep in mind that this script will append a '-task ' to the end of the supplied script before executing it; do not add it on within this argument or the script will error out. The type of script given does not matter, keep in mind that if it can't be executed directly in the shell (using ./ ), it can't be executed here; so make sure that the user has filesystem permissions to execute it, and that it has a '#!/' to specify what type of script it is.\n";

$expNumber = 0000; #Experiment number to use. $rootExpDir = "/mnt/main/Exp/";

getopts('ahn:s:', \%flags)	or die "$help" ; #Get option flags.

if($flags{s}){ $setupScript = $flags{s}; if(!(-e $setupScript)){ die "Error: Alternate SphinxTrain setup script (as defined by -s) is invalid, or otherwise does not exist!"; } }

if($flags{h}){ print "$help"; exit; } elsif(defined $flags{n}){ $expNumber = $flags{n}; &checkValidExpNum($expNumber); $expNumber = &expandExpNumber($expNumber); } else { opendir(EXPDIR, $rootExpDir) or die "Cannot open experiment directory!"; @contents = readdir(EXPDIR); closedir(EXPDIR); @contents = sort(@contents); #We need to strip out non-experiement directories. for($i = 0; $i < scalar(@contents); $i++){#Remove non-experiment directories. $temp = $contents[$i]; if( $temp =~ m/\D/g || $temp =~ m/^0+$/){ splice(@contents, $i,1); $i--; }	}	#Now, we need to find the lowest available experiment number. for($i = 0; $i < scalar(@contents); $i++){ if($i == scalar(@contents) - 1){ #The next available Exp number is the next number in the sequence.

$expNumber = $contents[$i]; $expNumber++; last; }		elsif(!(&isInArray(\@contents, $contents[$i] + 1))){ $expNumber = $contents[$i]; $expNumber++; last; }		}	 $expNumber = &expandExpNumber($expNumber); #Perl probably doesn't need this, but just in case... }

if (-d $rootExpDir . $expNumber){#if the experiment directory already exists. die "Error: Experiment $expNumber already exists!"; }
 * 1) Error checking, to determine if the experiment directory chosen exists:

mkdir $rootExpDir. $expNumber or die "Error: Cannot Create Experiment Directory!";
 * 1) Create the experiment directory:

chdir $rootExpDir. $expNumber or die "Error: Cannot access the newly created Experiment Directory!";
 * 1) move to that directory:

$results = `$setupScript -task $expNumber 2>&1`;

if($? == -1){ die "Error executing $setupScript: $!\n\n$results"; }

mkdir $rootExpDir. $expNumber. "/logdir". "/aes" or die "Error: Cannot Create Log Directory!"; open(LOGFILE, ">", $rootExpDir . $expNumber . "/logdir" . "/aes/" . $scriptName . ".log") or die "Cannot create new log file!" ;
 * 1) Create a logfile of what happened.

print LOGFILE "$scriptName: Log contents.\nChose next available experiment number: $expNumber\n Created experiment directory and executed '$setupScript' to populate base experiment directory. Results from the script are as follows:\n

$results" or die "Cannot write to logfile!";

if( $flags{a}){ $dir = $rootExpDir. $expNumber; $results = `chmod -R g+w $dir 2>&1`; if($? == -1){ close LOGFILE; print "Error: cannot change permissions to $dir due to $!!\n Undoing experiment." ;			remove_tree($rootExpDir . $expNumber) or die "Error: Cannot delete experiment!"; die "Undid experiment!"; }	print LOGFILE "\n --- Gave the user's group ($group) full R-W privleges to all files within the base experiment directory, including the base experiment directory itself.";

} close LOGFILE;

print $expNumber. "\n"; exit 0;


 * 1) SUBROUTINES###########################


 * 1) Confirms that a given number is a valid experiment number.
 * 2) Meaning that it isn't all zeros, or has non-numerical characters in it.
 * 3) Returns:
 * 4) 	Nothing if true.
 * 5) 	Errors out if the supplied number is invalid.

sub checkValidExpNum {	$temp = "$_[0]";

if($temp =~ m/\D/g){ die "Error: The submitted experiment number ($temp) has non-numeric characters!\n"; }	if(($temp =~ m/^0+$/)|| $temp eq ""){ die "Error: An expeirment number cannot consist of only zeroes!\n"; } }

sub expandExpNumber{ $temp = "$_[0]"; while(1){ if(length($temp) >= 4){ last; } 		else{ $temp = '0'. $temp; }	}	return $temp; } sub isInArray{ @arr = @{$_[0]}; $exp = $_[1]; my $i = 0; foreach(@arr){ if($_ == $exp ){ return $i; }		$i++; }	return "";
 * 1) If the experiment number is less than 4 characters long, this subroutine will
 * 2) Stuff the left of the number with zeroes until it reaches four characters long.
 * 1) Determines if a given input is contained within an entry in a given array.
 * 2) Usage: inInArray(\, )
 * 3) Returns: The element where the first instance of the given item is. "" otherwise.

}