tsrc/testing/tools/genreruncfgs.pl
changeset 0 96612d01cf9f
equal deleted inserted replaced
-1:000000000000 0:96612d01cf9f
       
     1 #
       
     2 # Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description: 
       
    15 #
       
    16 
       
    17 
       
    18 #------------------------------------------------------------------------------------
       
    19 # Includes
       
    20 #------------------------------------------------------------------------------------
       
    21 #use strict;
       
    22 use warnings;
       
    23 use Cwd; # for cwd
       
    24 use FindBin; # for FindBin:Bin
       
    25 use File::Path; # for mkpath
       
    26 use Date::Calc;
       
    27 
       
    28 { # No globals.
       
    29     # Solve where the script is located.
       
    30     $0=~/^(.+[\\\/])[^\\\/]+[\\\/]*$/;
       
    31     my $cgidir= $1 || "./";
       
    32     
       
    33     # And try to solve relative path.
       
    34     if( index( $cgidir, "..\\" ) != -1 )
       
    35     {
       
    36     	my $p = cwd;
       
    37     	$p =~ s/\//\\/g;
       
    38     	$cgidir =~ s/\//\\/g;
       
    39     	while(1) 
       
    40     	{
       
    41     		my $pos = index( $cgidir, "..\\" );
       
    42     		last if( $pos == -1 );
       
    43     		$cgidir = substr( $cgidir, $pos+3 );
       
    44     		
       
    45     		$pos = rindex( $p, "\\" );
       
    46     		last if( $pos == -1 );
       
    47     		$p = substr( $p, 0, $pos);
       
    48     	}
       
    49     	$cgidir = $p . "\\" . $cgidir;
       
    50     }
       
    51     
       
    52     if ( ! -e( $cgidir . "rerunsubs.pl" ) )
       
    53     {
       
    54         $cgidir = cwd;
       
    55         my $domain = "VideoApp_Domain";
       
    56         my $pos = index( $cgidir, $domain );
       
    57         if( $pos != -1 )
       
    58         {
       
    59             $cgidir = substr( $cgidir, 0, $pos + length( $domain ) ) . "\\videoplayer\\tsrc\\testing\\tools\\";
       
    60             
       
    61         }
       
    62     }
       
    63     require( $cgidir . "rerunsubs.pl" );
       
    64     require( $cgidir . "utils.pl" );
       
    65 }
       
    66 
       
    67 #------------------------------------------------------------------------------------
       
    68 # GLOBAL CODE
       
    69 #------------------------------------------------------------------------------------
       
    70 
       
    71 my $lineFeed = "\n";
       
    72 
       
    73 my $regexpCaseName = '[<>\x3C\x3E\(\)\w.,\/:_\-&\' ]+';
       
    74 
       
    75 my $argcount = scalar(@ARGV);
       
    76 my $paramStartDir = "..";
       
    77 my $paramQuiet = 0;
       
    78 my $paramFilename = "";
       
    79 my $paramMakeSis = 1;
       
    80 my $paramGenCaseList = 0;
       
    81 
       
    82 if(scalar(@ARGV) <= 0)
       
    83 {
       
    84     my @xmlFiles;
       
    85     FindFiles(".", "xml", 1, \@xmlFiles, 0);
       
    86     
       
    87     if( scalar(@xmlFiles) > 1 )
       
    88     {
       
    89     	print("Multiple xml files. Can run without arguments only if there's one testreport xml.\n");
       
    90     	foreach my $file ( @xmlFiles ) 
       
    91     	{
       
    92     	    print("$file\n");
       
    93     	}
       
    94     	ShowHelp();
       
    95     }
       
    96 
       
    97     if( scalar(@xmlFiles) <= 0 )
       
    98     {
       
    99     	print("No testreport xml in current directory, cannot run without arguments.\n");
       
   100     	ShowHelp();
       
   101     }
       
   102     
       
   103     $paramFilename = $xmlFiles[0];
       
   104     print("No arguments given. Picked file $paramFilename.\n");
       
   105 }
       
   106 else
       
   107 	{
       
   108 	while(scalar(@ARGV) >= 1)
       
   109 	{
       
   110 		my $argument = shift(@ARGV);
       
   111 	
       
   112 		if($argument eq "-f")
       
   113 		{
       
   114 			$paramFilename = shift(@ARGV);
       
   115 		}
       
   116 		elsif($argument eq "-h")
       
   117 		{
       
   118 			ShowHelp();
       
   119 		}
       
   120 		elsif($argument eq "-q")
       
   121 		{
       
   122 			$paramQuiet = 1;
       
   123 		}
       
   124 		elsif($argument eq "-sis")
       
   125 		{
       
   126 			$paramMakeSis = 1;
       
   127 		}
       
   128 		elsif($argument eq "-caselist")
       
   129 		{
       
   130 			$paramGenCaseList = 1;
       
   131 		}
       
   132 		else  {
       
   133 			print "arg: $argument\n";
       
   134 			ShowHelp();
       
   135 		}
       
   136 	}
       
   137 }
       
   138 
       
   139 my @incfiles;
       
   140 my $startDir = cwd;
       
   141 my @failedCfgs;
       
   142 
       
   143 if( ! -e ".\\init\\testframework.ini" )
       
   144 {
       
   145 	die("Must be run from tsrc and there must be .\\init\\\testframework.ini!\n");
       
   146 }
       
   147 
       
   148 if( Rerun_ReadFailedCases( $paramFilename, \@failedCfgs ) )
       
   149 {
       
   150     die("Reading the Excel file failed!\n");
       
   151 }
       
   152 
       
   153 if( $paramGenCaseList )
       
   154 {
       
   155     if(!open(FILE_HANDLE, ">" . "caselist.txt") )
       
   156     {
       
   157         print("ERROR! Could not open file for writing '" . "caselist.txt" . "'\n");
       
   158         return 0;
       
   159     }
       
   160 
       
   161     my $currCfg = "";
       
   162     foreach my $cfg( @failedCfgs )
       
   163     {
       
   164         $refCases = @$cfg[1];
       
   165         $file = @$cfg[0];
       
   166 
       
   167         if($file ne $currCfg)
       
   168         {
       
   169             print FILE_HANDLE ("cfg $file\n");
       
   170         }
       
   171 
       
   172         foreach my $failedCaseName (@$refCases)
       
   173         {
       
   174             print FILE_HANDLE ("case $failedCaseName\n");
       
   175         }
       
   176     }
       
   177 
       
   178     close(FILE_HANDLE);
       
   179 
       
   180     exit();
       
   181 }
       
   182 else
       
   183 {
       
   184     if(scalar(@failedCfgs) <= 0)
       
   185     {
       
   186     	print("No fails!\n");
       
   187     	exit();
       
   188     }
       
   189     
       
   190     system("rmdir /s /q 1rerun\\\n") if( -e "1rerun" );
       
   191     mkdir("1rerun");
       
   192 
       
   193     foreach my $cfg( @failedCfgs )
       
   194     {
       
   195         my $file = @$cfg[0];
       
   196         Rerun_GenerateCfg( ".\\conf\\" . $file, $file, \@failedCfgs );
       
   197     }
       
   198 
       
   199     MakeSISX( \@failedCfgs ) if( $paramMakeSis );
       
   200 
       
   201     foreach my $cfg( @failedCfgs )
       
   202     {
       
   203         my $file = @$cfg[0];
       
   204         if( -e ".\\conf\\$file" ) 
       
   205         {
       
   206             AddCfgToRerunIni( ".\\init\\testframework.ini", $file );
       
   207             system("move $file 1rerun\\$file");
       
   208         }
       
   209     }
       
   210 
       
   211 }
       
   212 chdir($startDir);
       
   213 
       
   214 chdir("1rerun");
       
   215 system("dir");
       
   216 
       
   217 exit();
       
   218 
       
   219 #------------------------------------------------------------------------------------
       
   220 # ShowHelp
       
   221 #------------------------------------------------------------------------------------
       
   222 sub ShowHelp {
       
   223 
       
   224 print <<USAGE_EOF;
       
   225 genreruncfgs.pl
       
   226 
       
   227 parameter -f <Excel xml testreport> (-sis)
       
   228 
       
   229 Creates cfg files into the run directory, the cfgs will have cases which have failed or are for setup.
       
   230 
       
   231 The current directory must be tsrc, there must be testframework.ini in ./init and STIF cfg files in ./cfg.
       
   232 
       
   233 The testreport can be in current directory and then no parameters are needed.
       
   234 
       
   235 -sis Creates STIF PU sis of the rerun CFGs.
       
   236 -caselist Creates caselist.txt which lists the failed cases
       
   237 
       
   238 USAGE_EOF
       
   239 
       
   240 	exit();
       
   241 
       
   242 };
       
   243 
       
   244 #------------------------------------------------------------------------------------
       
   245 # IniHasCfg
       
   246 #------------------------------------------------------------------------------------
       
   247 sub IniHasCfg
       
   248 {
       
   249     my ($iniFile, $cfgFile) = @_;
       
   250 
       
   251 	if( !open(FILE_HANDLE, $iniFile) )
       
   252 	{
       
   253 		print("ERROR! Could not open file '" . $iniFile . "'\n");
       
   254 		return 0;
       
   255 	}
       
   256 	my @array = <FILE_HANDLE>;
       
   257 	close(FILE_HANDLE);
       
   258 
       
   259 	my $inNew_Module = 0; # We are parsing [New_Module] section from INI
       
   260 	my $lineCount = scalar(@array);
       
   261 	for(my $i=0; $i<$lineCount; $i++ )
       
   262 	{
       
   263 	    $line = $array[$i];
       
   264 
       
   265         $inNew_Module = 1 if($line =~ m/\[New_Module\]/);
       
   266         $inNew_Module = 0 if($line =~ m/\[End_Module\]/);
       
   267 
       
   268         if($inNew_Module)
       
   269         {
       
   270             if($line =~ m/$cfgFile/i)
       
   271             {
       
   272                 return 1;
       
   273             }
       
   274         }
       
   275     }
       
   276 
       
   277     return 0;
       
   278 }
       
   279 
       
   280 sub AddCfgToRerunIni
       
   281 {
       
   282     my ($iniFile, $cfgFile) = @_;
       
   283     
       
   284 	my $destIniFile = "./1rerun/TestFramework.ini";
       
   285 
       
   286     # Create directory for rerun ini
       
   287     mkdir( substr($destIniFile, 0, rindex($destIniFile, "/")) );
       
   288 
       
   289     # Rerun ini does not exist. Create one.
       
   290     if( ! (-e $destIniFile) )
       
   291     {
       
   292     	if( !open(FILE_HANDLE, $iniFile) )
       
   293     	{
       
   294     		print("ERROR! Could not open file '" . $iniFile . "'\n");
       
   295     		return 0;
       
   296     	}
       
   297     	my @array = <FILE_HANDLE>;
       
   298     	close(FILE_HANDLE);
       
   299 
       
   300     	if( !open(FILE_HANDLE, ">$destIniFile") )
       
   301     	{
       
   302     		print("ERROR! Could not open file '" . $destIniFile . "'\n");
       
   303     		return 0;
       
   304     	}
       
   305 
       
   306         my $inNew_Module = 0;
       
   307 
       
   308         foreach $line ( @array )
       
   309         {
       
   310             if($line =~ m/\[New_Module\]/)
       
   311             {
       
   312                 $inNew_Module = 1;
       
   313                 print FILE_HANDLE ($line);
       
   314                 print FILE_HANDLE ("ModuleName= TestScripter\n");
       
   315             }
       
   316             if($line =~ m/\[End_Module\]/)
       
   317             {
       
   318                 $inNew_Module = 0;
       
   319             }
       
   320 
       
   321             if($inNew_Module)
       
   322             {
       
   323                 # Copy only setup cfgs if they are found.
       
   324                 if($line =~ m/setvalidservices/i)
       
   325                 {
       
   326                     print FILE_HANDLE ($line);
       
   327                 }
       
   328                 if($line =~ m/testprovisioning/i)
       
   329                 {
       
   330                     print FILE_HANDLE ($line);
       
   331                 }
       
   332             }
       
   333             else
       
   334             {
       
   335                 print FILE_HANDLE ($line);
       
   336             }
       
   337         }
       
   338 
       
   339     	close(FILE_HANDLE);
       
   340     }
       
   341 
       
   342     # Read the existing file.
       
   343 
       
   344     if( !open(FILE_HANDLE, $destIniFile) )
       
   345 	{
       
   346 		print("ERROR! Could not open file '" . $destIniFile . "'\n");
       
   347 		return 0;
       
   348 	}
       
   349 	my @array = <FILE_HANDLE>;
       
   350 	close(FILE_HANDLE);
       
   351 
       
   352     # Check if CFG is already there.
       
   353 
       
   354     my $inNew_Module = 0;
       
   355 
       
   356     foreach $line ( @array )
       
   357     {
       
   358         if($line =~ m/\[New_Module\]/)
       
   359         {
       
   360             $inNew_Module = 1;
       
   361         }
       
   362         if($line =~ m/\[End_Module\]/)
       
   363         {
       
   364             $inNew_Module = 0;
       
   365         }
       
   366 
       
   367         if($inNew_Module)
       
   368         {
       
   369             if($line =~ m/$cfgFile/i)
       
   370             {
       
   371                 return 1;
       
   372             }
       
   373         }
       
   374     }
       
   375 
       
   376     # Add the cfg to it.
       
   377 
       
   378 	if( !open(FILE_HANDLE, ">$destIniFile") )
       
   379 	{
       
   380 		print("ERROR! Could not open file '" . $destIniFile . "'\n");
       
   381 		return 0;
       
   382 	}
       
   383 
       
   384     $inNew_Module = 0;
       
   385 
       
   386     foreach $line ( @array )
       
   387     {
       
   388         if($line =~ m/\[End_Module\]/)
       
   389         {
       
   390             $inNew_Module = 0;
       
   391             print FILE_HANDLE ("TestCaseFile= c:\\TestFramework\\$cfgFile\n");
       
   392         }
       
   393         print FILE_HANDLE ($line);
       
   394     }
       
   395 
       
   396 	close(FILE_HANDLE);
       
   397 
       
   398     return 1;
       
   399 }
       
   400 
       
   401 #------------------------------------------------------------------------------------
       
   402 # MakeSISX
       
   403 #
       
   404 # Parameters:
       
   405 # 	$refCfgs
       
   406 #------------------------------------------------------------------------------------
       
   407 sub MakeSISX
       
   408 {
       
   409     my ($refCfgs) = @_;
       
   410 
       
   411     my $pkgname = "STIF_failedcases.pkg";
       
   412     my $sisname = "STIF_failedcases.sis";
       
   413     my $sisnamex = "STIF_failedcases.sisx";
       
   414 
       
   415     print("Writing pkg $pkgname\n");
       
   416 	if( !open(FILE_HANDLE, ">$pkgname") )
       
   417 	{
       
   418 		print("ERROR: could not write to file: $pkgname\n");
       
   419 		return 0;
       
   420 	}
       
   421 
       
   422     print FILE_HANDLE (
       
   423     "&EN" . $lineFeed .
       
   424     "#{\"RERUN\"},(0x00000000),0,1,0,TYPE=PU" . $lineFeed .
       
   425     "%{\"Nokia EN\"}" . $lineFeed .
       
   426     ":\"Nokia\"" . $lineFeed
       
   427     );
       
   428 
       
   429     foreach my $cfg (@$refCfgs)
       
   430     {
       
   431         my $file = @$cfg[0];
       
   432         if( -e $file )
       
   433         {
       
   434             print FILE_HANDLE ("\"$file\" - \"c:\\TestFramework\\$file\"" . $lineFeed );
       
   435         }
       
   436     }
       
   437 
       
   438     close(FILE_HANDLE);
       
   439 
       
   440     # Check that we have signing keys.
       
   441     
       
   442     my $derFile = "RDTest_02.der";
       
   443     my $keyFile = "RDTest_02.key";
       
   444 
       
   445     if (-e $derFile and -e $keyFile)
       
   446     {
       
   447         # Keys are in current dir
       
   448     }
       
   449     elsif(-e "sis\\$derFile" and -e "sis\\$keyFile")
       
   450     {
       
   451         # Keys are in internal/sis dir
       
   452         $derFile = "sis\\$derFile";
       
   453         $keyFile = "sis\\$keyFile";
       
   454     }
       
   455     elsif(-e "group\\$derFile" and -e "group\\$keyFile")
       
   456     {
       
   457         # Keys are in internal/group dir
       
   458         $derFile = "group\\$derFile";
       
   459         $keyFile = "group\\$keyFile";
       
   460     }    
       
   461     else
       
   462     {
       
   463         # Find em.
       
   464         my @derFiles;
       
   465         my @keyFiles;
       
   466         FindFiles( ".", "$derFile", 0, \@derFiles );
       
   467         FindFiles( ".", "$keyFile", 0, \@keyFiles );
       
   468         $derFile = $derFiles[0] if( scalar(@derFiles) > 0 );
       
   469         $keyFile = $keyFiles[0] if( scalar(@keyFiles) > 0 );
       
   470     }
       
   471     
       
   472     if (!-e $derFile or !-e $keyFile)
       
   473     {
       
   474         print("\nNeed signing keys: $derFile and $keyFile in current or subdirs.\n");
       
   475         return;
       
   476     }
       
   477     
       
   478     # Create SISX
       
   479 
       
   480     my $cmd = "makesis $pkgname $sisname";
       
   481     print("$cmd\n");
       
   482     system $cmd;
       
   483 
       
   484     $cmd = "signsis $sisname $sisnamex $derFile $keyFile";
       
   485     print("$cmd\n");
       
   486     system $cmd;
       
   487 
       
   488     system("move $sisnamex 1rerun\\$sisname");
       
   489 
       
   490     system("del $pkgname");
       
   491     system("del $sisname");
       
   492 }
       
   493 
       
   494 #------------------------------------------------------------------------------------
       
   495 # FindFiles
       
   496 # Parameters:
       
   497 #	$goDir, where to start finding
       
   498 #	$fileSearch, filename search
       
   499 #	$searchType, 0 = fullname search, 1 = filetype search
       
   500 #	$refIncfiles, reference to array which will hold found files
       
   501 #------------------------------------------------------------------------------------
       
   502 sub FindFiles
       
   503 {
       
   504 	my ($godir, $fileSearch, $searchType, $refIncfiles, $recurse) = @_;
       
   505 
       
   506 	my $startDir = cwd;
       
   507 
       
   508 	chdir($godir);
       
   509 
       
   510 	#print("Now in: " . cwd . "\n");
       
   511 
       
   512 	opendir(DIR, ".");
       
   513 	my @filelist = sort(readdir(DIR));
       
   514 	closedir(DIR);
       
   515 
       
   516 	foreach my $file(@filelist)
       
   517 	{
       
   518 		if($file eq "." or $file eq "..") {next};
       
   519 
       
   520 		if (-d $file)
       
   521 		{
       
   522 		 	FindFiles( $file, $fileSearch, $searchType, $refIncfiles) if( $recurse );
       
   523 		} else
       
   524 		{
       
   525 			if( ($file =~ m/$fileSearch/i and $searchType == 0 ) or ($file =~ m/$fileSearch$/i and $searchType == 1 ) )
       
   526 			{
       
   527                 $file = cwd . "/" . $file;
       
   528 				push @$refIncfiles, $file;
       
   529 				#print("$file\n");
       
   530 			}
       
   531 		}
       
   532 	}
       
   533 
       
   534 	chdir ($startDir);
       
   535 }