]> AND Public Git Repository - simgrid.git/blobdiff - buildtools/Cmake/tesh.pl
Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
modernize a bit the tesh.pl by using POD and Getopt. TBC
[simgrid.git] / buildtools / Cmake / tesh.pl
index f4338f942f90d5954df4ef2dc7a37b2f3baa2649..bb2db788efd1aaf9a92321f13fa68f3716f4b46c 100755 (executable)
@@ -1,27 +1,30 @@
 #! /usr/bin/perl -w
 
+=encoding UTF-8
+
+=head1 NAME
+
+tesh -- testing shell
+
+=head1 SYNOPSIS
+
+B<tesh> [I<options>] I<tesh_file>
+
+=cut
+
+use Pod::Usage qw(pod2usage);
+use Getopt::Long qw(GetOptions);
 use strict;
 use Term::ANSIColor;
 use IPC::Open3;
 
-if($#ARGV < 0)
-{
-       die "Usage: tesh.pl <options> <teshfile.tesh>\n";
-}
 
-my($line1);
-my($line2);
-my($execline);
-my($command);
-my($command_tesh);
+my($line1,$line2,$execline,$command,$command_tesh);
 my($command_executed)=0;
 my($expected_result_line)=0;
 my($sort)=0;
 my($nb_arg)=0;
-my(@list1)=();
-my(@list2)=();
-my(@list3)=();
-my(@list_of_commands)=();
+my(@list1,@list2,@list3,@list_of_commands)=();
 my(@buffer)=();
 my($timeout)=0;
 my($encore)=0;
@@ -36,62 +39,83 @@ my($result);
 my($result_err);
 my($fich_name);
 my($forked);
+my($config)="";
 
 my($tesh_command)=0;
 my(@buffer_tesh)=();
 
-#options
-do{
-       if($ARGV[$nb_arg] =~ /^--cd$/)
-       {
-               $nb_arg++;
-               if(!$ARGV[$nb_arg] or $ARGV[$nb_arg] =~ /^--/){die "Usage: tesh.pl --cd <directory>\n";}
-               my($directory)=$ARGV[$nb_arg];
-               if( -e $directory) 
-               {
-                       chdir("$directory");
-                       print "[Tesh/INFO] Change directory to \"$directory\"\n";
-               }
-               else
-               {
-                       die "[Tesh/CRITICAL] Directory not found : \"$directory\"\n";
-               }
-               $nb_arg++;      
-       }
-       elsif($ARGV[$nb_arg] =~ /^--setenv$/)
-       {
-               $nb_arg++;
-               if(!$ARGV[$nb_arg] or $ARGV[$nb_arg] =~ /^--/){die "Usage: tesh.pl --setenv environment_variable\n";}
-               if(!$ARGV[$nb_arg+1] or $ARGV[$nb_arg+1] =~ /^--/){die "Usage: tesh.pl --setenv environment_variable\n";}
-               $ENV{$ARGV[$nb_arg]} = "$ENV{$ARGV[$nb_arg]}:$ARGV[$nb_arg+1]";
-               print "[Tesh/INFO] export $ARGV[$nb_arg]=\"$ENV{$ARGV[$nb_arg]}\"\n";
-               $nb_arg++;
-               $nb_arg++;
-       }
-       elsif($ARGV[$nb_arg] =~ /^--verbose$/)
-       {
-               $verbose=1;$nb_arg++;
-       }
-       else
-       {
-               print "[Tesh/CRITICAL] Unrecognized option : $ARGV[$nb_arg]\n";
-               $nb_arg++;
-       }
-}while(($nb_arg) < $#ARGV);
+# make sure we received a tesh file
+scalar @ARGV > 0 || pod2usage(-exitval => 1);
 
 #Add current directory to path
 $ENV{PATH} = "$ENV{PATH}:.";
 
-#tesh file
-if(!$ARGV[$nb_arg]){die "tesh.pl <options> <teshfile.tesh>\n";}
-print "[Tesh/INFO] load file : $ARGV[$nb_arg]\n";
-my($file)=$ARGV[$nb_arg];
-open SH_LIGNE, $file or die "[Tesh/CRITICAL] Unable to open $file. $!\n";
+
+#options
+sub cd_cmd {
+    my $directory=$_[1];
+    if (-e $directory) {
+       chdir("$directory");
+       print "[Tesh/INFO] change directory to $directory\n";
+    } else {
+       die "[Tesh/CRITICAL] Directory not found : \"$directory\"\n";
+    }
+}
+
+sub setenv_cmd {
+    if ($_[1] =~ /^(.*)=(.*)$/) {
+       my($var,$ctn)=($1,$2);
+       $ENV{$var} = $ctn;
+       print "[Tesh/INFO] setenv $var=$ctn\n";
+    } else { 
+       die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
+    }
+}
+
+my $tesh_file;
+sub get_options {
+    # remove the tesh file from the ARGV used
+    my @ARGV = @_;
+    $tesh_file = pop @ARGV;
+
+    # temporary arrays for GetOption
+    my @verbose = ();
+    my @cfg;
+
+    my %opt = (
+       "help"    => 0,
+       "debug"   => 0,
+       "verbose" => 0
+       );
+
+    Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
+    
+    GetOptions(
+       'help|h'     => \$opt{'help'},
+
+        'verbose|v'  => \@verbose,
+        'debug|d'    => \$opt{"debug"},
+
+       'cd=s'       => \&cd_cmd,
+       'setenv=s'   => \&setenv_cmd,
+       'cfg=s'      => \@cfg,
+       );
+
+    $opt{'verbose'} = scalar @verbose;
+    foreach (@cfg) {
+       $opt{'cfg'} .= " --cfg=$_";
+    }
+    return %opt;
+}
+
+my %opts = get_options(@ARGV);
+
+# parse tesh file
+open SH_LIGNE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file: $!\n";
 
 while(defined($line1=<SH_LIGNE>))
 {
        if($line1 =~ /^\< \$ /){        # arg command line
-               $line1 =~ s/\$\{srcdir\:\=\.\}/./g;
                $line1 =~ s/\${EXEEXT:=}//g;
                $line1 =~ s/^\< \$\ *//g;
                $line1 =~ s/^.\/lua/lua/g;
@@ -143,7 +167,6 @@ while(defined($line1=<SH_LIGNE>))
                        @buffer = @buffer_tesh;
                        @buffer_tesh=();
                }
-               $line1 =~ s/\$\{srcdir\:\=\.\}/./g;
                $line1 =~ s/\${EXEEXT:=}//g;
                $line1 =~ s/^\$\ *//g;
                $line1 =~ s/^.\/lua/lua/g;
@@ -152,11 +175,13 @@ while(defined($line1=<SH_LIGNE>))
                $line1 =~ s/^tesh/.\/tesh/g;
                $line1 =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
                chomp $line1;
+               $line1 = "$line1 $config";
+               
                if(@list1){
                        print color("red");
                        print "[Tesh/CRITICAL] -\n";
-                       print "[Tesh/CRITICAL] + @list1\n";
-                       print color("reset");
+                       print "[Tesh/CRITICAL] + @list1";
+                       print color("reset"), "\n";
                        die;}           
                if(@list_of_commands){ # need parallel execution
                        push @list_of_commands, $line1;
@@ -205,7 +230,6 @@ while(defined($line1=<SH_LIGNE>))
        elsif($line1 =~ /^\& /){        # parallel command line
                $command_executed = 0;
                $expected_result_line = 0;
-               $line1 =~ s/\$\{srcdir\:\=\.\}/./g;
                $line1 =~ s/\${EXEEXT:=}//g;
                $line1 =~ s/^\& //g;
                $line1 =~ s/^.\/lua/lua/g;
@@ -213,6 +237,8 @@ while(defined($line1=<SH_LIGNE>))
                $line1 =~ s/^.\///g;
                $line1 =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
                chomp $line1;
+               $line1 = "$line1 $config";
+               
                $execline = "$line1";
                print "[Tesh/INFO] exec_line : $execline\n";
                push @list_of_commands, $execline;      
@@ -263,7 +289,8 @@ while(defined($line1=<SH_LIGNE>))
                $line1 =~ s/\r//g;
                chomp $line1;
                $return = $line1;
-               print color("red"), "[Tesh/INFO] expect return $return\n";
+               print color("red"), "[Tesh/INFO] expect return $return";
+               print color("reset"), "\n";
                die;
        }
        elsif($line1 =~ /^! setenv/){   #setenv
@@ -275,7 +302,9 @@ while(defined($line1=<SH_LIGNE>))
                print "[Tesh/INFO] setenv: $1=$2\n";
        }
        elsif($line1 =~ /^! include/){  #output sort
-               color("red");die "[Tesh/CRITICAL] need include\n";
+               print color("red"), "[Tesh/CRITICAL] need include";
+               print color("reset"), "\n";
+               die;
        }
        elsif($line1 =~ /^! timeout/){  #timeout
                $line1 =~ s/^! timeout //g;
@@ -364,15 +393,15 @@ while(defined($line1=<SH_LIGNE>))
                                }
                                else
                                {       if($verbose == 0){print color("green"),@buffer,color("reset");}
-                                       if($line2) {print color("red"), "[Tesh/CRITICAL] - $line2\n",color("reset");}
-                                       if($line1) {print color("red"), "[Tesh/CRITICAL] + $line1\n",color("reset");}
+                                       if($line2) {print color("red"), "[Tesh/CRITICAL] - $line2",color("reset"),"\n";}
+                                       if($line1) {print color("red"), "[Tesh/CRITICAL] + $line1",color("reset"),"\n";}
                                        die;
                                }
                        }
                        else
                        {       if($verbose == 0){print color("green"),@buffer,color("reset");}
-                               if($line2) {print color("red"), "[Tesh/CRITICAL] - $line2\n",color("reset");}
-                               if($line1) {print color("red"), "[Tesh/CRITICAL] + $line1\n",color("reset");}
+                               if($line2) {print color("red"), "[Tesh/CRITICAL] - $line2",color("reset"),"\n";}
+                               if($line1) {print color("red"), "[Tesh/CRITICAL] + $line1",color("reset"),"\n";}
                                die;
                        }
                }
@@ -468,15 +497,15 @@ if($command_executed and $expected_result_line ){
                                        }
                                        else
                                        {       if($verbose == 0){print color("green"),@buffer,color("reset");}
-                                               if($line2) {print color("red"), "[Tesh/CRITICAL] - $line2\n",color("reset");}
-                                               if($line1) {print color("red"), "[Tesh/CRITICAL] + $line1\n",color("reset");}
+                                               if($line2) {print color("red"), "[Tesh/CRITICAL] - $line2",color("reset"),"\n";}
+                                               if($line1) {print color("red"), "[Tesh/CRITICAL] + $line1",color("reset"),"\n";}
                                                die;
                                        }
                                }
                                else
                                {       if($verbose == 0){print color("green"),@buffer,color("reset");}
-                                       if($line2) {print color("red"), "[Tesh/CRITICAL] - $line2\n",color("reset");}
-                                       if($line1) {print color("red"), "[Tesh/CRITICAL] + $line1\n",color("reset");}
+                                       if($line2) {print color("red"), "[Tesh/CRITICAL] - $line2",color("reset"),"\n";}
+                                       if($line1) {print color("red"), "[Tesh/CRITICAL] + $line1",color("reset"),"\n";}
                                        die;
                                }
                        }
@@ -486,4 +515,4 @@ if($command_executed and $expected_result_line ){
                        @list1=();
                        @list2=();
                        @buffer = ();
-}
\ No newline at end of file
+}