Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
This perl script generate a platform with n peer (arg0)
[simgrid.git] / buildtools / Cmake / tesh.pl
1 #! /usr/bin/perl -w
2
3 use strict;
4 use Term::ANSIColor;
5 use IPC::Open3;
6
7 if($#ARGV < 0)
8 {
9         die "Usage: tesh.pl <options> <teshfile.tesh>\n";
10 }
11
12 my($line1);
13 my($line2);
14 my($execline);
15 my($command);
16 my($command_tesh);
17 my($command_executed)=0;
18 my($expected_result_line)=0;
19 my($sort)=0;
20 my($nb_arg)=0;
21 my(@list1)=();
22 my(@list2)=();
23 my(@list3)=();
24 my(@list_of_commands)=();
25 my(@buffer)=();
26 my($timeout)=0;
27 my($encore)=0;
28 my($old_buffer);
29 my($linebis);
30 my($SIGABRT)=0;
31 my($no_output_ignore)=1;
32 my($verbose)=0;
33 my($return)=-1;
34 my($pid);
35 my($result);
36 my($result_err);
37 my($fich_name);
38 my($forked);
39 my($config)="";
40
41 my($tesh_command)=0;
42 my(@buffer_tesh)=();
43
44 #options
45 do{
46         if($ARGV[$nb_arg] =~ /^--cd$/)
47         {
48                 $nb_arg++;
49                 if(!$ARGV[$nb_arg] or $ARGV[$nb_arg] =~ /^--/){die "Usage: tesh.pl --cd <directory>\n";}
50                 my($directory)=$ARGV[$nb_arg];
51                 if( -e $directory) 
52                 {
53                         chdir("$directory");
54                         print "[Tesh/INFO] --cd \"$directory\"\n";
55                 }
56                 else
57                 {
58                         die "[Tesh/CRITICAL] Directory not found : \"$directory\"\n";
59                 }
60                 $nb_arg++;      
61         }
62         elsif($ARGV[$nb_arg] =~ /^--setenv$/)
63         {
64                 $nb_arg++;
65                 if(!$ARGV[$nb_arg] or $ARGV[$nb_arg] =~ /^--/){die "Usage: tesh.pl --setenv environment_variable\n";}
66                 $ARGV[$nb_arg] =~ /^(.*)=(.*)$/;
67                 my($var)=$1;
68                 my($content)=$2;
69                 $ENV{$var} = $content;
70                 print "[Tesh/INFO] --setenv $var=$content\n";
71                 $nb_arg++;
72         }
73         elsif($ARGV[$nb_arg] =~ /^--cfg$/)
74         {
75                 $nb_arg++;
76                 if(!$ARGV[$nb_arg] or $ARGV[$nb_arg] =~ /^--/){die "Usage: tesh.pl --setenv environment_variable\n";}
77                 $config = "$config--cfg=$ARGV[$nb_arg] ";
78                 print "[Tesh/INFO] $config\n";
79                 $nb_arg++;
80         }
81         elsif($ARGV[$nb_arg] =~ /^--verbose$/)
82         {
83                 $verbose=1;$nb_arg++;
84         }
85         else
86         {
87                 print "[Tesh/CRITICAL] Unrecognized option : $ARGV[$nb_arg]\n";
88                 $nb_arg++;
89         }
90 }while(($nb_arg) < $#ARGV);
91
92 #Add current directory to path
93 $ENV{PATH} = "$ENV{PATH}:.";
94
95 #tesh file
96 if(!$ARGV[$nb_arg]){die "tesh.pl <options> <teshfile.tesh>\n";}
97 print "[Tesh/INFO] load file : $ARGV[$nb_arg]\n";
98 my($file)=$ARGV[$nb_arg];
99 open SH_LIGNE, $file or die "[Tesh/CRITICAL] Unable to open $file. $!\n";
100
101 while(defined($line1=<SH_LIGNE>))
102 {
103         if($line1 =~ /^\< \$ /){        # arg command line
104                 $line1 =~ s/\${EXEEXT:=}//g;
105                 $line1 =~ s/^\< \$\ *//g;
106                 $line1 =~ s/^.\/lua/lua/g;
107                 $line1 =~ s/^.\/ruby/ruby/g;
108                 $line1 =~ s/^.\///g;
109                 $line1 =~ s/^tesh/.\/tesh/g;
110                 $line1 =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
111                 chomp $line1;
112                 $command_tesh = $line1;
113                 print "[Tesh/INFO] arg_exec_line   : $command_tesh\n";
114         }
115         elsif($line1 =~ /^\< \< /){  # arg buffer line
116                 $line1 =~ s/^\< \<\ *//g;
117                 chomp $line1;
118                 print "[Tesh/INFO] arg_buffer_line : $line1\n";
119                 push @buffer_tesh, "$line1\n";
120         }
121         elsif($line1 =~ /^\< \> /){  # arg output line
122                 $line1 =~ s/^\< \>\ *//g;
123                 $line1 =~ s/\r//g;
124                 chomp $line1;
125                 push @list2, $line1;
126                 print "[Tesh/INFO] arg_output_line : $line1\n";
127                 $expected_result_line = 1;
128         }
129         elsif($line1 =~ /^\$ mkfile/){          # "mkfile" command line
130                 $line1 =~ s/\$ //g;
131                 $line1 =~ s/mkfile//g;
132                 chomp $line1;
133                 $fich_name = $line1;
134                 $line1 =();
135                 print "[Tesh/INFO] exec_line : mkfile $fich_name\n";
136                 `rm -f $fich_name`;
137                 open(FILE,">$fich_name") or die "[Tesh/CRITICAL] Unable to make file : $fich_name. $!\n";
138                 print FILE @buffer;
139                 close(FILE);
140                 @buffer = ();   
141         }
142         elsif($line1 =~ /^\$ cd/){      # "cd" command line
143                 $line1 =~ s/\$ //g;
144                 chomp $line1;
145                 print "[Tesh/INFO] exec_line : $line1\n";
146                 $line1 =~ s/cd //g;
147                 chdir("$line1") or die "[Tesh/CRITICAL] Unable to open $line1. $!\n";   
148         }
149         elsif($line1 =~ /^\$ /){        #command line
150                 if($line1 =~ /^\$ .\/tesh/){    # tesh command line
151                         $tesh_command = 1;
152                         @buffer = @buffer_tesh;
153                         @buffer_tesh=();
154                 }
155                 $line1 =~ s/\${EXEEXT:=}//g;
156                 $line1 =~ s/^\$\ *//g;
157                 $line1 =~ s/^.\/lua/lua/g;
158                 $line1 =~ s/^.\/ruby/ruby/g;
159                 $line1 =~ s/^.\///g;
160                 $line1 =~ s/^tesh/.\/tesh/g;
161                 $line1 =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
162                 chomp $line1;
163                 $line1 = "$line1 $config";
164                 
165                 if(@list1){
166                         print color("red");
167                         print "[Tesh/CRITICAL] -\n";
168                         print "[Tesh/CRITICAL] + @list1";
169                         print color("reset"), "\n";
170                         die;}           
171                 if(@list_of_commands){ # need parallel execution
172                         push @list_of_commands, $line1;
173                         print "[Tesh/INFO] exec_line : $line1\n";
174                 }
175                 else{
176                         print "[Tesh/INFO] exec_line : $line1\n";
177                         if($tesh_command)
178                         {       $execline = $command_tesh;
179                                 $tesh_command=0;}
180                         else
181                         {       $execline = $line1;}
182                         $pid = open3(\*IN, \*OUT, \*OUT, $execline );
183                         if( $timeout){
184                                 $forked = fork();die "fork() failed: $!" unless defined $forked;
185                                 if ( $forked == 0 )
186                                 {
187                                 sleep $timeout;
188                                 kill(9, $pid);
189                                 exit;
190                                 }
191                         }
192                         
193                         while(@buffer)
194                         {
195                                 $line1 = shift (@buffer);
196                                 print IN $line1;
197                         }
198                         close IN ;
199                         
200                         waitpid( $pid, 0 );
201                         if($timeout){kill(9, $forked);$timeout=0;}
202                         $timeout = 0;
203                         
204                         while(defined($linebis=<OUT>))
205                         {
206                                 $linebis =~ s/\r//g;
207                                 $linebis =~ s/^( )*//g;
208                                 chomp $linebis;
209                                 push @list1,"$linebis";
210                         }       
211                         close OUT;
212                         $command_executed = 1;
213                 }
214         }
215         elsif($line1 =~ /^\& /){        # parallel command line
216                 $command_executed = 0;
217                 $expected_result_line = 0;
218                 $line1 =~ s/\${EXEEXT:=}//g;
219                 $line1 =~ s/^\& //g;
220                 $line1 =~ s/^.\/lua/lua/g;
221                 $line1 =~ s/^.\/ruby/ruby/g;
222                 $line1 =~ s/^.\///g;
223                 $line1 =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
224                 chomp $line1;
225                 $line1 = "$line1 $config";
226                 
227                 $execline = "$line1";
228                 print "[Tesh/INFO] exec_line : $execline\n";
229                 push @list_of_commands, $execline;      
230         }       
231         elsif($line1 =~ /^\>/){ #expected result line
232                 $line1 =~ s/^\>( )*//g;
233                 $line1 =~ s/\r//g;
234                 chomp $line1;
235                 push @list2, $line1;
236                 $expected_result_line = 1;
237         }
238         elsif($line1 =~ /^\</ or $encore==1){   #need to buffer
239                 $line1 =~ s/^\<( )*//g; #delete < with space or tab after
240                 $line1 =~ s/\r//g;
241                 chomp $line1;
242                 if($line1 =~ /\\$/) # need to store this line into old_buff
243                 {
244                         $encore=1;
245                         $line1 =~ s/\\$//g;
246                         $old_buffer = "$old_buffer$line1";
247                 }
248                 else
249                 {
250                         if($encore == 1){push @buffer, "$old_buffer$line1";}
251                         else{push @buffer, "$line1\n";}
252                         $old_buffer = ();
253                         $encore = 0;    
254                 }
255         }
256         elsif($line1 =~ /^p/){  #comment
257                 $line1 =~ s/^p //g;
258                 $line1 =~ s/\r//g;
259                 chomp $line1;
260                 print "[Tesh/INFO] comment_line :$line1\n";
261         }
262         elsif($line1 =~ /^! output sort/){      #output sort
263                 $sort=1;
264         }
265         elsif($line1 =~ /^! output ignore/){    #output ignore
266                 $no_output_ignore=0;
267         }
268         elsif($line1 =~ /^! expect signal SIGABRT$/) #expect signal SIGABRT
269         {
270                 $SIGABRT=1;
271         }
272         elsif($line1 =~ /^! expect return/){    #expect return
273                 $line1 =~ s/^! expect return //g;
274                 $line1 =~ s/\r//g;
275                 chomp $line1;
276                 $return = $line1;
277                 print color("red"), "[Tesh/INFO] expect return $return";
278                 print color("reset"), "\n";
279                 die;
280         }
281         elsif($line1 =~ /^! setenv/){   #setenv
282                 $line1 =~ s/^! setenv //g;
283                 $line1 =~ s/\r//g;
284                 chomp $line1;
285                 $line1 =~ /^(.*)=(.*)$/;
286                 $ENV{$1} = $2;
287                 print "[Tesh/INFO] setenv: $1=$2\n";
288         }
289         elsif($line1 =~ /^! include/){  #output sort
290                 print color("red"), "[Tesh/CRITICAL] need include";
291                 print color("reset"), "\n";
292                 die;
293         }
294         elsif($line1 =~ /^! timeout/){  #timeout
295                 $line1 =~ s/^! timeout //g;
296                 $line1 =~ s/\r//g;
297                 chomp $line1;
298                 if($timeout != $line1){
299                 $timeout = $line1;
300                 print "[Tesh/INFO] timeout   : $timeout\n";}
301         }
302         elsif($line1 =~ /^! need execute/){     #need execute // commands
303                 $execline = ();
304                 $sort = 1; # need sort output
305                 while(@list_of_commands)
306                 {
307                         $command = shift (@list_of_commands);
308                         if($execline){$execline = "$command & $execline";}
309                         else{$execline = "$command";}
310                 }
311                 $pid = open3(\*IN, \*OUT, \*OUT, $execline);
312                 if( $timeout){
313                         $forked = fork();die "fork() failed: $!" unless defined $forked;
314                         if ( $forked == 0 )
315                         {
316                         sleep $timeout;
317                         kill(9, $pid);
318                         exit;
319                         }
320                 }
321                 
322                 while(@buffer)
323                 {
324                         $line1 = shift (@buffer);
325                         print IN $line1;
326                 }
327                 close IN ;
328                 waitpid( $pid, 0 );
329                 if($timeout){kill(9, $forked);$timeout=0;}
330                 $timeout = 0;
331
332                 @list1=();
333                 while(defined($linebis=<OUT>))
334                 {
335                         $linebis =~ s/\r//g;
336                         $linebis =~ s/^( )*//g;
337                         chomp $linebis;
338                         push @list1,"$linebis";
339                 }       
340                 close OUT;
341                 $command_executed = 1;
342         }
343         elsif($command_executed and $expected_result_line)
344         {
345                 if($no_output_ignore){
346                 @buffer = ();
347                 if($sort == 1)
348                 {
349                         @list3 = sort @list1;
350                         @list1=();
351                         @list1=@list3;
352                         @list3=();
353                         
354                         @list3 = sort @list2;
355                         @list2=();
356                         @list2=@list3;
357                         @list3=();
358                         
359                         $sort=0;
360                 }
361                 if($SIGABRT)
362                 {
363                         push @list2,"Aborted";
364                         $SIGABRT = 0;
365                 }
366                                 
367                 while(@list1 or @list2)
368                 {
369                         if(@list1){$line1 = shift (@list1);$expected_result_line = "x$line1";}
370                         if(@list2){$line2 = shift (@list2);$command_executed = "x$line2";}
371                         if($command_executed and $expected_result_line)
372                         {
373                                 
374                                 if($line1 eq $line2){
375                                         if($verbose == 1){print color("green"),"[Tesh/VERB] $line1\n",color("reset");}
376                                         else{push @buffer, "[Tesh/CRITICAL]   $line1\n";}
377                                         
378                                 }
379                                 else
380                                 {       if($verbose == 0){print color("green"),@buffer,color("reset");}
381                                         if($line2) {print color("red"), "[Tesh/CRITICAL] - $line2",color("reset"),"\n";}
382                                         if($line1) {print color("red"), "[Tesh/CRITICAL] + $line1",color("reset"),"\n";}
383                                         die;
384                                 }
385                         }
386                         else
387                         {       if($verbose == 0){print color("green"),@buffer,color("reset");}
388                                 if($line2) {print color("red"), "[Tesh/CRITICAL] - $line2",color("reset"),"\n";}
389                                 if($line1) {print color("red"), "[Tesh/CRITICAL] + $line1",color("reset"),"\n";}
390                                 die;
391                         }
392                 }
393                 }else{$no_output_ignore = 1;}
394                 $command_executed = 0;
395                 $expected_result_line = 0;
396                 @list1=();
397                 @list2=();
398                 @buffer = ();
399                 $tesh_command=0;
400                 @buffer_tesh=();
401         }
402
403 }
404
405 if(@list_of_commands){ # need parallel execution
406         $execline = ();
407         $sort = 1; # need sort output
408         while(@list_of_commands)
409         {
410                 $command = shift (@list_of_commands);
411                 if($execline){$execline = "$command & $execline";}
412                 else{$execline = "$command";}
413         }
414         print "[Tesh/INFO] exec_line : $execline\n";
415         $pid = open3(\*IN, \*OUT, \*OUT,"$execline");
416
417         if( $timeout){
418                 $forked = fork();die "fork() failed: $!" unless defined $forked;
419                 if ( $forked == 0 )
420                 {
421                 sleep $timeout;
422                 kill(9, $pid);
423                 exit;
424                 }
425         }
426         
427         while(@buffer)
428         {
429                 $line1 = shift (@buffer);
430                 print IN $line1;
431         }
432         close IN ;
433         waitpid( $pid, 0 );
434         if($timeout){kill(9, $forked);$timeout=0;}
435         $timeout = 0;
436
437         @list1=();
438         while(defined($linebis=<OUT>))
439         {
440                 $linebis =~ s/\r//g;
441                 $linebis =~ s/^( )*//g;
442                 chomp $linebis;
443                 push @list1,"$linebis";
444         }       
445         close OUT;
446         $command_executed = 1;
447 }
448
449 if($command_executed and $expected_result_line ){
450                         if($no_output_ignore){
451                         @buffer = ();
452                         if($sort == 1)
453                         {
454                                 @list3 = sort @list1;
455                                 @list1=();
456                                 @list1=@list3;
457                                 @list3=();
458                                 
459                                 @list3 = sort @list2;
460                                 @list2=();
461                                 @list2=@list3;
462                                 @list3=();
463                                 
464                                 $sort=0;
465                         }
466                         if($SIGABRT)
467                         {
468                                 push @list2,"Aborted";
469                                 $SIGABRT = 0;
470                         }
471                         
472                         while(@list1 or @list2)
473                         {
474                                 if(@list1){$line1 = shift (@list1);$expected_result_line = "x$line1";}
475                                 if(@list2){$line2 = shift (@list2);$command_executed = "x$line2";}
476                                 if($command_executed and $expected_result_line)
477                                 {
478                                         if($line1 eq $line2){
479                                                 if($verbose == 1){print color("green"),"[Tesh/VERB] $line1\n",color("reset");}
480                                                 else{push @buffer, "[Tesh/CRITICAL]   $line1\n";}
481                                                 
482                                         }
483                                         else
484                                         {       if($verbose == 0){print color("green"),@buffer,color("reset");}
485                                                 if($line2) {print color("red"), "[Tesh/CRITICAL] - $line2",color("reset"),"\n";}
486                                                 if($line1) {print color("red"), "[Tesh/CRITICAL] + $line1",color("reset"),"\n";}
487                                                 die;
488                                         }
489                                 }
490                                 else
491                                 {       if($verbose == 0){print color("green"),@buffer,color("reset");}
492                                         if($line2) {print color("red"), "[Tesh/CRITICAL] - $line2",color("reset"),"\n";}
493                                         if($line1) {print color("red"), "[Tesh/CRITICAL] + $line1",color("reset"),"\n";}
494                                         die;
495                                 }
496                         }
497                         }else{$no_output_ignore = 1;}
498                         $command_executed = 0;
499                         $expected_result_line= 0;
500                         @list1=();
501                         @list2=();
502                         @buffer = ();
503 }