Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add functions to tesh.pl
[simgrid.git] / buildtools / Cmake / tesh.pl
1 #! /usr/bin/perl -w
2
3 use strict;
4
5 if($#ARGV<0){die "Usage: tesh.pl <options> <teshfile.tesh>\n";}
6 my($line1);
7 my($line2);
8 my($execline);
9 my($ok)=0;
10 my($ok1)=0;
11 my($sort)=0;
12 my($nb_arg)=0;
13 my($result);
14 my(@list1)=();
15 my(@list2)=();
16 my(@list3)=();
17 my(@buffer)=();
18 my($timeout)=-1;
19 my($parallel)=0;
20 my($verbose)=0;
21 my($encore)=0;
22 my($old_buffer);
23
24 #options
25 do{
26         if($ARGV[$nb_arg] =~ /^--cd$/)
27         {
28                 $nb_arg++;
29                 if(!$ARGV[$nb_arg] or $ARGV[$nb_arg] =~ /^--/){die "Usage: tesh.pl --cd <directory>\n";}
30                 my($directory)=$ARGV[$nb_arg];
31                 if( -e $directory) 
32                 {
33                         chdir("$directory");
34                         print "[Tesh/INFO] Change directory to \"$directory\"\n";
35                 }
36                 else
37                 {
38                         die "[Tesh/CRITICAL] Directory not found : \"$directory\"\n";
39                 }
40                 $nb_arg++;      
41         }
42         elsif($ARGV[$nb_arg] =~ /^--setenv$/)
43         {
44                 $nb_arg++;
45                 if(!$ARGV[$nb_arg] or $ARGV[$nb_arg] =~ /^--/){die "Usage: tesh.pl --setenv environment_variable\n";}
46                 if(!$ARGV[$nb_arg+1] or $ARGV[$nb_arg+1] =~ /^--/){die "Usage: tesh.pl --setenv environment_variable\n";}
47                 $ENV{$ARGV[$nb_arg]} = "$ENV{$ARGV[$nb_arg]}:$ARGV[$nb_arg+1]";
48                 print "[Tesh/INFO] export $ARGV[$nb_arg]=\"$ENV{$ARGV[$nb_arg]}\"\n";
49                 $nb_arg++;
50                 $nb_arg++;
51         }
52         elsif($ARGV[$nb_arg] =~ /^--verbose$/)
53         {
54                 $verbose=1;
55         }
56         else
57         {
58                 print "[Tesh/CRITICAL] Unrecognized option : $ARGV[$nb_arg]\n";
59                 $nb_arg++;
60         }
61 }while(($nb_arg) < $#ARGV);
62
63 #Add current directory to path
64 $ENV{PATH} = "$ENV{PATH}:.";
65
66 #tesh file
67 if(!$ARGV[$nb_arg]){die "tesh.pl <options> <teshfile.tesh>\n";}
68 print "[Tesh/INFO] load file : $ARGV[$nb_arg]\n";
69 my($file)=$ARGV[$nb_arg];
70 open SH_LIGNE, $file or die "[Tesh/CRITICAL] Unable to open $file. $!\n";
71
72 while(defined($line1=<SH_LIGNE>))
73 {
74         if($line1 =~ /^\$ /){   #command line
75                 $line1 =~ s/\$\{srcdir\:\=\.\}/./g;
76                 $line1 =~ s/\${EXEEXT:=}//g;
77                 $line1 =~ s/^\$\ *//g;
78                 $line1 =~ s/^.\/lua/lua/g;
79                 $line1 =~ s/^.\/ruby/ruby/g;
80                 $line1 =~ s/^.\///g;
81                 $line1 =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
82                 chomp $line1;
83                 if(@list1){
84                         print "[Tesh/CRITICAL] Old result : @list1";
85                         print "[Tesh/CRITICAL] Previous result not check!";
86                         die;}
87                 if($parallel == 1)
88                 {
89                         $execline = "$execline \& $line1 2>&1";
90                         print "[Tesh/INFO] exec_line // : $execline\n";
91                 }
92                 else
93                 {
94                         $ok = 1;
95                         $execline = "$line1  2>&1";
96                         print "[Tesh/INFO] exec_line : $execline\n";
97                         $result=`$execline`;
98                         @list1 = split(/\n/,$result);
99                 }
100         }
101         elsif($line1 =~ /^\& /){        # parallel command line
102                 $line1 =~ s/\$\{srcdir\:\=\.\}/./g;
103                 $line1 =~ s/\${EXEEXT:=}//g;
104                 $line1 =~ s/^\& //g;
105                 $line1 =~ s/^.\/lua/lua/g;
106                 $line1 =~ s/^.\/ruby/ruby/g;
107                 $line1 =~ s/^.\///g;
108                 $line1 =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
109                 chomp $line1;
110                 if(@list1){
111                         print "Old result : @list1";
112                         die "Previous result not check!";}
113                 if($parallel == 1)
114                 {
115                         $execline = "$execline \& $line1  2>&1";
116                         print "[Tesh/INFO] exec_line // : $execline\n";
117                 }
118                 else
119                 {
120                         $parallel = 1;
121                         $execline = "$line1 2>&1";
122                         print "[Tesh/INFO] exec_line // : $execline\n";
123                 }
124                 
125         }       
126         elsif($line1 =~ /^\>/){ #expected result line
127                 if($ok == 0 and $parallel== 0){die "No command line\n";}
128                 $ok1 = 1;
129                 $line1 =~ s/^\> //g;
130                 $line1 =~ s/\r//g;
131                 chomp $line1;
132                 push @list2, $line1;
133         }
134         elsif($line1 =~ /^\</ or $encore==1){   #need to buffer
135                 $line1 =~ s/^\< //g;
136                 $line1 =~ s/\r//g;
137                 chomp $line1;
138                 if($line1 =~ /\\$/) # need to store this line into old_buff
139                 {
140                         $encore=1;
141                         $line1 =~ s/\\$//g;
142                         $old_buffer = "$old_buffer$line1";
143                 }
144                 else
145                 {
146                         if($encore == 1)
147                         {       
148                                 push @buffer, "$old_buffer$line1";
149                                 $old_buffer = ();
150                                 $encore = 0;                            
151                         }
152                         else
153                         {
154                                 push @buffer, "$line1\n";
155                                 $old_buffer = ();
156                                 $encore = 0;                                    
157                         }
158                 }
159         }
160         elsif($line1 =~ /^p/){  #comment
161                 $line1 =~ s/^p //g;
162                 $line1 =~ s/\r//g;
163                 chomp $line1;
164                 print "[Tesh/INFO] comment_line :$line1\n";
165         }
166         elsif($line1 =~ /^! output sort/){      #output sort
167                 print "[Tesh/INFO] output sort\n";
168                 $sort=1;
169         }
170         elsif($line1 =~ /^! expect return/){    #expect sort
171                 print "[Tesh/CRITICAL] expect return\n";
172                 die;
173         }
174         elsif($line1 =~ /^! setenv/){   #setenv
175                 $line1 =~ s/^! setenv //g;
176                 $line1 =~ s/\r//g;
177                 chomp $line1;
178                 $line1 =~ /^(.*)=(.*)$/;
179                 $ENV{$1} = $2;
180                 print "[Tesh/INFO] setenv: $1=$2\n";
181         }
182         elsif($line1 =~ /^! include/){  #output sort
183                 die "[Tesh/CRITICAL] need include\n";
184         }
185         elsif($line1 =~ /^! timeout/){  #timeout
186                 $line1 =~ s/^! timeout //g;
187                 $line1 =~ s/\r//g;
188                 chomp $line1;
189                 $timeout = $line1;
190                 print "[Tesh/INFO] timeout   : $timeout\n";
191         }
192         elsif($ok == 1 and $ok1 == 1)
193         {
194                 @buffer = ();
195                 if($sort == 1)
196                 {
197                         @list3 = sort @list1;
198                         @list1=();
199                         @list1=@list3;
200                         @list3=();
201                         
202                         @list3 = sort @list2;
203                         @list2=();
204                         @list2=@list3;
205                         @list3=();
206                         
207                         $sort=0;
208                 }
209                 while(@list1 or @list2)
210                 {
211                         $line1 = shift (@list1);
212                         $line2 = shift (@list2);
213                         if($line2 and $line1)
214                         {
215                                 if($line1 eq $line2){
216                                         if($verbose == 1){print "$line1\n";}
217                                         else{push @buffer, "$line1\n";}
218                                         
219                                 }
220                                 else
221                                 {       if($verbose == 0){print @buffer};
222                                         if($line2) {print "- $line2\n"};
223                                         if($line1) {print "+ $line1\n"};
224                                         die;
225                                 }
226                         }
227                         else
228                         {       if($verbose == 0){print @buffer};
229                                 if($line2) {print "- $line2\n"};
230                                 if($line1) {print "+ $line1\n"};
231                                 die;
232                         }
233                 }
234                 $ok = 0;
235                 $ok1= 0;
236                 @list1=();
237                 @list2=();
238                 @buffer = ();
239                 $result = ();
240         }
241 }
242
243 if($parallel == 1)
244 {
245         $ok = 1;
246         $result=`$execline`;
247         @list1 = split(/\n/,$result);
248 }
249 elsif($ok == 1 and $ok1 == 1)
250                 {
251                         @buffer = ();
252                         if($sort == 1)
253                         {
254                                 @list3 = sort @list1;
255                                 @list1=();
256                                 @list1=@list3;
257                                 @list3=();
258                                 
259                                 @list3 = sort @list2;
260                                 @list2=();
261                                 @list2=@list3;
262                                 @list3=();
263                                 
264                                 $sort=0;
265                         }
266                         while(@list1 or @list2)
267                         {
268                                 $line1 = shift (@list1);
269                                 $line2 = shift (@list2);
270                                 if($line2 and $line1)
271                                 {
272                                         if($line1 eq $line2){
273                                                 if($verbose == 1){print "$line1\n";}
274                                                 else{push @buffer, "$line1\n";}
275                                                 
276                                         }
277                                         else
278                                         {       if($verbose == 0){print @buffer};
279                                                 if($line2) {print "- $line2\n"};
280                                                 if($line1) {print "+ $line1\n"};
281                                                 die;
282                                         }
283                                 }
284                                 else
285                                 {       if($verbose == 0){print @buffer};
286                                         if($line2) {print "- $line2\n"};
287                                         if($line1) {print "+ $line1\n"};
288                                         die;
289                                 }
290                         }
291                         $ok = 0;
292                         $ok1= 0;
293                         @list1=();
294                         @list2=();
295                         @buffer = ();
296 }