bintools/evalid/evalid.pl
changeset 0 044383f39525
child 676 b5e6747818a9
equal deleted inserted replaced
-1:000000000000 0:044383f39525
       
     1 #
       
     2 # Copyright (c) 1999-2009 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 the License "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 # Validate releasables
       
    16 # See below for usage
       
    17 #
       
    18 
       
    19 use Getopt::Long;
       
    20 use Cwd;
       
    21 use FindBin;
       
    22 use File::Path;
       
    23 use lib "$FindBin::Bin";
       
    24 use EvalidMD5 0.01;
       
    25 use EvalidCompare;
       
    26 
       
    27 my $passed=0;
       
    28 my $failed=0;
       
    29 
       
    30 GetOptions("c", "v", "l=s", "g", "f", "m", "u", "x=s@", "i=s@", "d=s");
       
    31 $opt_v = $opt_v; # To keep -w quiet.
       
    32 $opt_g = $opt_g; # To keep -w quiet.
       
    33 $opt_f = $opt_f; # To keep -w quiet.
       
    34 $opt_m = $opt_m; # To keep -w quiet.
       
    35 $opt_u = $opt_u; # To keep -w quiet.
       
    36 $opt_d = $opt_d; # To keep -w quiet.
       
    37 
       
    38 unless ((@ARGV > 1) && (@ARGV < 4))
       
    39 	{
       
    40 #........1.........2.........3.........4.........5.........6.........7.....
       
    41 	print <<USAGE_EOF;
       
    42 
       
    43 Usage:
       
    44   evalid [opts]    file1    file2       -- compare two files
       
    45   evalid [opts]    dir1     dir2        -- recursively compare two trees
       
    46   
       
    47 MD5 Usage: 
       
    48   evalid [opts] -g dir1     file1       -- recursively generate MD5 checksums
       
    49   evalid [opts] -f listfile dir1  file1 -- create MD5 checksums of listed files
       
    50   evalid [opts] -m file1    file2       -- compare two MD5 datafiles
       
    51 
       
    52 The recursive form will take each file in the tree rooted at dir1, 
       
    53 determine its type, and using the appropriate comparison attempt to
       
    54 compare it with the corresponding file in the tree rooted at dir2.
       
    55 
       
    56   evalid file1 dir2   =>  evalid file1      dir2\\file1
       
    57   evalid dir1  file2  =>  evalid dir1\\file1 file2
       
    58   
       
    59 The opts parameter controls where the output goes:
       
    60 
       
    61 	-v            -- verbose information about failed comparisons
       
    62 	-c            -- print results to standard output
       
    63 	-l <logfile>  -- append results to <logfile>
       
    64 
       
    65 The default is equivalent to "-l evalid.lis"
       
    66 
       
    67 
       
    68 MD5 Options in addition to standard options:
       
    69 
       
    70 MD5 Generation options
       
    71   -g            -- generate MD5 of the dir1 and write to file1
       
    72   -f            -- generate MD5 of the files listed files and write to file1
       
    73                    The listfile must contain a list of files, one per line.
       
    74                    They should be specified relative to dir1 but should not
       
    75                    start in a directory seperator (\\)
       
    76 
       
    77 Sub options for -g and -f
       
    78   -x            -- exclude a reqular expression from the list of files
       
    79   -i            -- include a regular expression in to the list of files
       
    80   -d dump_dir   -- generate the output used in the processing of dir1 content
       
    81                    into files of the same name, within the same directory
       
    82                    structure, under dump_dir.  This option permits the manual
       
    83                    examination of the data EVALID uses for its comparison work. 
       
    84 
       
    85 MD5 Comparison option
       
    86   -m            -- compare the MD5's of the file1 against to file2
       
    87   -u            -- alternate output formats for comparison
       
    88                    suitable for upgrading the directory defined by file1
       
    89                    to have the same contents as the directory defined by file2
       
    90 
       
    91 Note:  The inclusion takes precedence over the exclusion.
       
    92 Note:  Standard option (-v) has no effect on MD5 operations.
       
    93 Note:  Standard option (-l) has no effect on the -u option.
       
    94 
       
    95 USAGE_EOF
       
    96 	exit 1;
       
    97 	}
       
    98 
       
    99 # Generate checksum option
       
   100 if ($opt_g)
       
   101 {
       
   102   # Check $ARGV[0] is a directory
       
   103   die "$ARGV[0] is not a directory" unless (-d $ARGV[0]);
       
   104   # Check $ARGV[1] does not exist
       
   105   die "$ARGV[1] already exisits" if (-e $ARGV[1]);
       
   106   &EvalidMD5::MD5Generate($ARGV[0], $ARGV[1], \@opt_x, \@opt_i, undef, $opt_d);
       
   107   exit (0);
       
   108 }
       
   109 
       
   110 if ($opt_f)
       
   111 {
       
   112   # Check $ARGV[0] is a directory
       
   113   die "$ARGV[0] does not exist" unless (-e $ARGV[0]);
       
   114   # Check $ARGV[1] is a directory
       
   115   die "$ARGV[1] is not a directory" unless (-d $ARGV[1]);
       
   116   # Check $ARGV[2] does not exist
       
   117   die "$ARGV[2] already exists" if (-e $ARGV[2]);
       
   118   &EvalidMD5::MD5Generate($ARGV[1], $ARGV[2], \@opt_x, \@opt_i, $ARGV[0], $opt_d);
       
   119   exit (0);
       
   120 }
       
   121 
       
   122 # Compare checksum file option (alternate format for upgrades)
       
   123 if ($opt_u)
       
   124 {
       
   125   my ($iCommon, $iLeftHeaders, $iRightHeaders, $iDiff);
       
   126   # Check $ARGV[0] file exists
       
   127   die "$ARGV[0] is not a file" unless (-f $ARGV[0]);
       
   128   # Check $ARGV[1] does not exist
       
   129   die "$ARGV[1] is not a file" unless (-f $ARGV[1]);
       
   130   ($iCommon, $iLeftHeaders, $iRightHeaders, $iDiff) = &EvalidMD5::MD5Compare($ARGV[0], $ARGV[1]);
       
   131   $failed = &EvalidMD5::MD5CompareZipDel($iCommon, $iDiff, $ARGV[0], $ARGV[1]);
       
   132   exit ($failed);
       
   133 }
       
   134 
       
   135 # Redirect output
       
   136 if ($opt_c)
       
   137 	{
       
   138 	$log = \*STDOUT;
       
   139 	}
       
   140 else
       
   141 	{
       
   142 	if (!$opt_l)
       
   143 		{
       
   144 		$opt_l = "evalid.lis";
       
   145 		}
       
   146 	open LOG, ">>$opt_l" or die "Cannot open logfile $opt_l\n";
       
   147 	$log = \*LOG;
       
   148 	}
       
   149 
       
   150 # Compare checksum file option
       
   151 if ($opt_m)
       
   152 {
       
   153   my ($iCommon, $iLeftHeaders, $iRightHeaders, $iDiff);
       
   154   # Check $ARGV[0] file exists
       
   155   die "$ARGV[0] is not a file" unless (-f $ARGV[0]);
       
   156   # Check $ARGV[1] does not exist
       
   157   die "$ARGV[1] is not a file" unless (-f $ARGV[1]);
       
   158   ($iCommon, $iLeftHeaders, $iRightHeaders, $iDiff) = &EvalidMD5::MD5Compare($ARGV[0], $ARGV[1]);
       
   159   $failed = &EvalidMD5::MD5ComparePrint($iCommon, $iLeftHeaders, $iRightHeaders, $iDiff, $log);
       
   160   exit ($failed);
       
   161 }
       
   162 
       
   163 
       
   164 # Make the comparison(s) Old Style
       
   165 
       
   166 compare($ARGV[0], $ARGV[1]);
       
   167 
       
   168 # Summarise the results
       
   169 
       
   170 my $total=$passed+$failed;
       
   171 
       
   172 if ($total > 1)
       
   173 	{
       
   174 	print $log "\nResults of evalid  $ARGV[0]  $ARGV[1]\n";
       
   175 	}
       
   176 
       
   177 if (@missing)
       
   178 	{
       
   179 	if ($total>1)
       
   180 		{
       
   181 		printf $log "\n%d missing files\n\n", scalar @missing;
       
   182 		}
       
   183 	map {
       
   184 		print $log "MISSING: $_\n";
       
   185 		} @missing;
       
   186 	}
       
   187 
       
   188 if (@failures)
       
   189 	{
       
   190 	if ($total>1)
       
   191 		{
       
   192 		printf $log "\n%d failed comparisons\n\n", scalar @failures;
       
   193 		}
       
   194 	map {
       
   195 		my ($left, $right, $type) = @{$_};
       
   196 		print $log "FAILED: $left\tand $right  ($type)\n";
       
   197 		} @failures;
       
   198 	}
       
   199 
       
   200 if ($total>1)
       
   201 	{
       
   202 	my ($sec, $min, $hour, $mday, $mon, $year) = localtime(time);
       
   203 	printf $log "\n----------------\n%02d:%02d %02d/%02d/%04d\n", $hour, $min, $mday, $mon+1, $year+1900;
       
   204 	print $log "evalid  $ARGV[0]  $ARGV[1]\n";
       
   205 	if ($failed==0)
       
   206 		{
       
   207 		print $log "Passed all $total comparisons\n";
       
   208 		}
       
   209 	else
       
   210 		{
       
   211 		print $log "Failed $failed of $total comparisons\n";
       
   212 		}
       
   213 	
       
   214 	print $log "----------------\n\n";
       
   215 	}
       
   216 
       
   217 exit($failed==0);
       
   218 
       
   219 #---------------------------------
       
   220 sub identical
       
   221 	{
       
   222 	my ($left, $right, $type)=@_;
       
   223 	print $log "OK: $left\tand $right  ($type)\n";
       
   224 	$passed+=1;
       
   225 	}
       
   226 
       
   227 sub different
       
   228 	{
       
   229 	my ($left, $right, $type)=@_;
       
   230 	push @failures, [$left, $right, $type];
       
   231 	$failed+=1;
       
   232 	}
       
   233 
       
   234 sub warning
       
   235 	{
       
   236 	my ($message)=@_;
       
   237 	print $log "WARNING: $message\n";
       
   238 	if (!$opt_c)
       
   239 		{
       
   240 		print "WARNING: $message\n";
       
   241 		}
       
   242 	# not a test failure as such
       
   243 	}
       
   244 
       
   245 sub problem
       
   246 	{
       
   247 	my ($message)=@_;
       
   248 	print $log "PROBLEM: $message\n";
       
   249 	$failed+=1;
       
   250 	}
       
   251 
       
   252 sub missing
       
   253 	{
       
   254 	my ($left) = @_;
       
   255 	push @missing, $left;
       
   256 	$failed+=1;
       
   257 	}
       
   258 
       
   259 sub compare
       
   260 	{
       
   261 	my ($left, $right, $recursing) = @_;
       
   262 	if (-d $left && -d $right)
       
   263 		{
       
   264 		# Read all of the directory entries except . and ..
       
   265 		# in to a local list.
       
   266 		opendir LEFTDIR, $left or print "Cannot read directory $left\n" and return;
       
   267 		my @list = grep !/^\.\.?$/, readdir LEFTDIR;
       
   268 		closedir LEFTDIR;
       
   269 		# recurse
       
   270 		map { 
       
   271 			compare($left."\\".$_, $right."\\".$_, 1);
       
   272 			} @list;
       
   273 		return;
       
   274 		}
       
   275 	if (-d $left)
       
   276 		{
       
   277 		if ($recursing)
       
   278 			{
       
   279 			if (-e $right)
       
   280 				{
       
   281 				problem("File $right should be a directory");
       
   282 				}
       
   283 			else
       
   284 				{
       
   285 				problem("Directory $right does not exist");
       
   286 				}
       
   287 			return;
       
   288 			}
       
   289 		compare($left."\\".$right,$right);
       
   290 		return;
       
   291 		}
       
   292 	if (-d $right)
       
   293 		{
       
   294 		if ($recursing)
       
   295 			{
       
   296 			problem("Directory $right should be a file");
       
   297 			return;
       
   298 			}
       
   299 		compare($left,$right."\\".$left);
       
   300 		return;
       
   301 		}
       
   302 	# comparing files
       
   303 	if (-e $left && !-e $right)
       
   304 		{
       
   305 		missing($right);
       
   306 		return;
       
   307 		}
       
   308 	if (!-e $left && -e $right)
       
   309 		{
       
   310 		problem("File $left does not exist");
       
   311 		return;
       
   312 		}
       
   313 
       
   314 	my ($same, $type) = EvalidCompare::CompareFiles($left, $right, $opt_v, $log);
       
   315 	if ($same)
       
   316 		{
       
   317 		identical($left, $right, $type);
       
   318 		}
       
   319 	else
       
   320 		{
       
   321 		different($left, $right, $type);
       
   322 		}
       
   323 }