tsrc/testing/tools/updateHeliumCfgs.pl
changeset 71 60b4b6493d7b
equal deleted inserted replaced
-1:000000000000 71:60b4b6493d7b
       
     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 . "utils.pl" );
       
    64 }
       
    65 
       
    66 #------------------------------------------------------------------------------------
       
    67 # GLOBAL CODE
       
    68 #------------------------------------------------------------------------------------
       
    69 
       
    70 my $argcount = scalar(@ARGV);
       
    71 
       
    72 while(scalar(@ARGV) >= 1)
       
    73 {
       
    74 	my $argument = shift(@ARGV);
       
    75 
       
    76 	if($argument eq "-h")
       
    77 	{
       
    78 		Help();
       
    79 		die;
       
    80 	}
       
    81 	else  {
       
    82         Help();
       
    83 		die;
       
    84 	}
       
    85 }
       
    86 
       
    87 sub Help
       
    88 {
       
    89     print("\nThis script updates test cases in *Helium.cfg files. First it reads all the CFGs from the same folder and then writes new *Helium.CFG\n");
       
    90     exit;
       
    91 }
       
    92 
       
    93 
       
    94 UpdateHeliumCfgs();
       
    95 
       
    96 exit;
       
    97 
       
    98 sub UpdateHeliumCfgs
       
    99 {
       
   100     my @cfgFiles;
       
   101     
       
   102     FindFiles( ".", "Helium.cfg", 0, \@cfgFiles );
       
   103     
       
   104     foreach my $file ( @cfgFiles )
       
   105     {
       
   106         print("Updating $file.\n");
       
   107         UpdateHeliumCfg( $file );
       
   108     }
       
   109 }
       
   110 
       
   111 sub UpdateHeliumCfg
       
   112 {
       
   113     my ( $fileName ) = @_;
       
   114     
       
   115     my $file = GetPathFileName( $fileName );
       
   116     my $path = GetPathDir( $fileName );
       
   117     
       
   118     # Read the cfg.
       
   119 	open(FILE_HANDLE, $fileName) or die ("ERROR! Could not open file $fileName\n");
       
   120 	my @heliumCfgLines = <FILE_HANDLE>;
       
   121 	close(FILE_HANDLE);    
       
   122 
       
   123     # Find all cases in the cfg.
       
   124     my @heliumCases;
       
   125 
       
   126     foreach my $line ( @heliumCfgLines )
       
   127     {
       
   128         if( $line =~ m/^title /i )
       
   129         {
       
   130             push @heliumCases, ( $line );
       
   131         }
       
   132     }
       
   133     
       
   134     # Get all cfgs in same dir as the Helium cfg.
       
   135     my @cfgs;
       
   136     FindFiles( $path, ".cfg", 0, \@cfgs );
       
   137     
       
   138     # Get all cfgs that have cases in Helium cfg.
       
   139     my @cfgsWithHeliumCases;
       
   140     
       
   141     foreach my $cfg ( @cfgs )
       
   142     {
       
   143         # Do not read cases again from helium cfg.
       
   144         next if( $cfg =~ /helium\.cfg/i );
       
   145         
       
   146         my $cfgHasCasesForHelium = 0;
       
   147         
       
   148         foreach my $heliumCase ( @heliumCases )
       
   149         {
       
   150             if( CfgHasCase( $cfg, $heliumCase ) )
       
   151             {
       
   152                 $cfgHasCasesForHelium = 1;
       
   153                 last;
       
   154             }
       
   155         }
       
   156         
       
   157         if( $cfgHasCasesForHelium )
       
   158         {
       
   159             push @cfgsWithHeliumCases, ( $cfg );
       
   160         }
       
   161     }
       
   162     
       
   163     # Read cases, defs and subs 
       
   164     my @cfgDefsAndIncludes;
       
   165     my @cfgSubs;
       
   166     my @cfgCases;
       
   167 
       
   168     foreach my $cfg ( @cfgsWithHeliumCases )
       
   169     {
       
   170         print("Reading: " . GetPathFileName($cfg) . "\n");
       
   171         ReadCfgDefs( $cfg, \@cfgDefsAndIncludes );
       
   172         ReadCfgSubs( $cfg, \@cfgSubs );
       
   173         ReadCfgCases( $cfg, \@cfgCases, \@heliumCases );
       
   174     }
       
   175     
       
   176     # Write the helium CFG.
       
   177     open(FILE_HANDLE, ">$fileName") or die("Could not write into file $fileName\n");
       
   178     
       
   179     # Write everything from helium CFG before the first defs.
       
   180     foreach my $line ( @heliumCfgLines )
       
   181     {
       
   182         if( $line =~ m/\[Define\]/ )
       
   183         {
       
   184             last; 
       
   185         }
       
   186         print FILE_HANDLE ( $line );
       
   187     }
       
   188     
       
   189     # Write defs, includes and subs.
       
   190     print FILE_HANDLE ( @cfgDefsAndIncludes );
       
   191     print FILE_HANDLE ( "\n" );
       
   192     print FILE_HANDLE ( @cfgSubs );
       
   193     print FILE_HANDLE ( "\n" );
       
   194     
       
   195     # Write cases, in same order as they were.
       
   196     foreach my $heliumCase ( @heliumCases )
       
   197     {
       
   198         my $caseFound = 0;
       
   199         
       
   200         foreach my $refCase ( @cfgCases )
       
   201         {
       
   202             if( @$refCase[0] eq $heliumCase )
       
   203             {
       
   204                 my $refCaseLines = @$refCase[1];
       
   205                 print FILE_HANDLE ( @$refCaseLines );
       
   206                 $caseFound = 1;
       
   207                 last;
       
   208             }
       
   209         }
       
   210         
       
   211         if( $caseFound )
       
   212         {
       
   213             print FILE_HANDLE ( "\n" );
       
   214         }
       
   215         else
       
   216         {
       
   217             print(" - the cfg has following case but it was not found from other CFGs: $heliumCase\n");
       
   218         }
       
   219     }
       
   220     
       
   221     close( FILE_HANDLE );
       
   222 }
       
   223 
       
   224 sub CfgHasCase
       
   225 {
       
   226     my ( $cfgFileName, $case ) = @_;
       
   227     
       
   228 	open(FILE_HANDLE, $cfgFileName) or die ("ERROR! Could not open file $cfgFileName\n");
       
   229 	my @lines = <FILE_HANDLE>;
       
   230 	close(FILE_HANDLE);    
       
   231 	
       
   232 	foreach my $line ( @lines )
       
   233 	{
       
   234 	    if( $line =~ m/$case/i )
       
   235 	    {
       
   236 	        return 1;
       
   237 	    }
       
   238 	}	
       
   239 	return 0;
       
   240 }
       
   241 
       
   242 sub ReadCfgDefs
       
   243 {
       
   244     my ( $cfgFileName, $refDefsAndIncludes ) = @_;
       
   245     
       
   246 	open(FILE_HANDLE, $cfgFileName) or die ("ERROR! Could not open file $cfgFileName\n");
       
   247 	my @lines = <FILE_HANDLE>;
       
   248 	close(FILE_HANDLE);    
       
   249 
       
   250 	my $readLines = 0;
       
   251 	foreach my $line ( @lines )
       
   252 	{
       
   253 	    # Defs starts here.
       
   254 	    if( $line =~ m/\[Define\]/ )
       
   255 	    {
       
   256 	        $readLines = 1;
       
   257 	    }
       
   258 	    
       
   259 	    # Reading the defs.
       
   260 	    if( $readLines )
       
   261 	    {
       
   262 	        my $alreadyHaveThisLine = 0;
       
   263 	        
       
   264 	        # Check if we got the line already.
       
   265 	        foreach my $defLine ( @$refDefsAndIncludes )
       
   266 	        {
       
   267 	            if( $defLine eq $line )
       
   268 	            {
       
   269 	                $alreadyHaveThisLine = 1;
       
   270 	            }
       
   271 	        }
       
   272 	        
       
   273 	        # If not, save it.
       
   274 	        if( !$alreadyHaveThisLine )
       
   275 	        {
       
   276 	            print(" - Def: $line");
       
   277 	            push @$refDefsAndIncludes, ( $line );
       
   278 	        }
       
   279 	    }
       
   280 	    
       
   281 	    # End of defs.
       
   282 	    if( $line =~ m/\[Enddefine\]/ )
       
   283 	    {
       
   284 	        return;
       
   285 	    }
       
   286 	}
       
   287 }
       
   288 
       
   289 sub ReadCfgSubs
       
   290 {
       
   291     my ( $cfgFileName, $refSubs ) = @_;
       
   292     
       
   293 	open(FILE_HANDLE, $cfgFileName) or die ("ERROR! Could not open file $cfgFileName\n");
       
   294 	my @lines = <FILE_HANDLE>;
       
   295 	close(FILE_HANDLE);
       
   296 	
       
   297 	my $inSub = 0;
       
   298 	foreach my $line ( @lines )
       
   299 	{
       
   300 	    # A sub function starts here.
       
   301 	    if( $line =~ m/\[Sub/ )
       
   302 	    {
       
   303 	        $inSub = 1;
       
   304             # Check if we got it already.
       
   305 	        foreach my $subLine ( @$refSubs )
       
   306 	        {
       
   307 	            if( $subLine =~ m/\[Sub/ && $subLine eq $line )
       
   308 	            {
       
   309 	                $inSub = 0; # Sub with same name has been already read.
       
   310 	                last; # Breakout from checking.
       
   311 	            }
       
   312 	        }
       
   313 	        if( $inSub )
       
   314 	        {
       
   315 	            print(" - Sub: $line");
       
   316 	        }
       
   317 	    }
       
   318 	    
       
   319 	    # Reading sub.
       
   320 	    if( $inSub )
       
   321 	    {
       
   322 	        push @$refSubs, ( $line );
       
   323 	    }
       
   324 	    
       
   325 	    # Sub ends here.
       
   326 	    if( $line =~ m/\[EndSub\]/ )
       
   327 	    {
       
   328 	        $inSub = 0;
       
   329 	    }
       
   330 	}
       
   331 }
       
   332 
       
   333 sub ReadCfgCases
       
   334 {
       
   335     my ( $cfgFileName, $refCases, $refCasesToRead ) = @_;
       
   336     
       
   337 	open(FILE_HANDLE, $cfgFileName) or die ("ERROR! Could not open file $cfgFileName\n");
       
   338 	my @lines = <FILE_HANDLE>;
       
   339 	close(FILE_HANDLE);
       
   340 	
       
   341 	my $inCase = 0;
       
   342 	my @caseLines;
       
   343 	my $caseTitle;
       
   344 
       
   345 	foreach my $line ( @lines )
       
   346 	{
       
   347 	    # Case starts here.
       
   348 	    if( $line =~ m/\[Test\]/ )
       
   349 	    {
       
   350 	        undef( @caseLines );
       
   351 	        $caseTitle = "";
       
   352 	        $inCase = 1;
       
   353 	    }
       
   354 	    
       
   355 	    # Found title for the case.
       
   356 	    if( $line =~ m/^title / )
       
   357 	    {
       
   358 	        $caseTitle = $line;
       
   359 	    }
       
   360 	    
       
   361 	    # Save case lines.
       
   362 	    if( $inCase )
       
   363 	    {
       
   364 	        push @caseLines, ( $line );
       
   365 	    }
       
   366 	    
       
   367 	    # End of test case.
       
   368 	    if( $line =~ m/\[Endtest\]/ )
       
   369 	    {
       
   370 	        $inCase = 0;
       
   371 	        
       
   372 	        # Was the case in list of cases to read.
       
   373 	        foreach my $heliumCases ( @$refCasesToRead )
       
   374 	        {
       
   375 	            if( $caseTitle eq $heliumCases )
       
   376 	            {
       
   377     	            print(" - Case: $caseTitle");
       
   378 
       
   379 	                # Yes, save it.
       
   380 	                my @case;
       
   381 	                $case[0] = $caseTitle;
       
   382 	                $case[1] = [ @caseLines ];
       
   383 	                push @$refCases, [ @case ];
       
   384 	            }
       
   385 	        }
       
   386 	    }
       
   387 	}
       
   388 }
       
   389 
       
   390 sub FindFiles
       
   391 {
       
   392 	my ($godir, $fileSearch, $searchType, $refIncfiles) = @_;
       
   393 
       
   394 	my $startDir = cwd;
       
   395 
       
   396 	chdir($godir);
       
   397 
       
   398 	#print("Now in: " . cwd . "\n");
       
   399 
       
   400 	opendir(DIR, ".");
       
   401 	my @filelist = sort(readdir(DIR));
       
   402 	closedir(DIR);
       
   403 
       
   404 	foreach my $file(@filelist)
       
   405 	{
       
   406 		if($file eq "." or $file eq "..") {next};
       
   407 
       
   408 		if (-d $file)
       
   409 		{
       
   410 		 	FindFiles( $file, $fileSearch, $searchType, $refIncfiles);
       
   411 		} else
       
   412 		{
       
   413 			if( ($file =~ m/$fileSearch/i and $searchType == 0 ) or ($file =~ m/$fileSearch$/i and $searchType == 1 ) )
       
   414 			{
       
   415                 $file = cwd . "/" . $file;
       
   416 				push @$refIncfiles, $file;
       
   417 				#print("$file\n");
       
   418 			}
       
   419 		}
       
   420 	}
       
   421 
       
   422 	chdir ($startDir);
       
   423 }