build/tools/compress_zips.pl
branchRCL_3
changeset 18 ea3e26ea6629
parent 6 c8ecf89eb77f
equal deleted inserted replaced
6:c8ecf89eb77f 18:ea3e26ea6629
     1 #
       
     2 # Copyright (c) 2006 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 use strict;
       
    18 use File::Find;     # for finding
       
    19 use File::Basename; # for fileparse
       
    20 use File::Path;    
       
    21 use File::Copy;    
       
    22 use Getopt::Long;
       
    23 use Cwd;
       
    24 
       
    25 my ($back_up);
       
    26 my (@zips, @temp_paths);
       
    27 my (%zips_files, %zips_paths);
       
    28 
       
    29 my $temp_dir = "\\compress_temp\\"; 
       
    30 my $version = "1.0";
       
    31 
       
    32 info();
       
    33 
       
    34 if ( !GetOptions(
       
    35 	'backup' => \$back_up,
       
    36 	'z=s' => \@zips))
       
    37 {
       
    38 	&error_msg("Invalid arguments!\n");
       
    39 }
       
    40 
       
    41 if (! scalar (@zips)) {
       
    42 	&error_msg("Invalid arguments!\n");
       
    43 }
       
    44 
       
    45 if (scalar (@zips) == 1) {
       
    46 	&error_msg("More that one input file is needed!\n");
       
    47 }
       
    48 
       
    49 foreach (@zips) {
       
    50 	if (! -e $_) {
       
    51 		&error_msg("File not found : $_\n");
       
    52 	}
       
    53 }
       
    54 
       
    55 foreach (@zips) {
       
    56 	unzip_temp($_);
       
    57 }
       
    58 
       
    59 rezip();
       
    60 
       
    61 rmtree($temp_dir);
       
    62 
       
    63 sub unzip_temp {
       
    64 	my $zip_file = shift;
       
    65 
       
    66 	my($n, $d, $ext) = fileparse($zip_file, '\..*');
       
    67 	
       
    68 	mkpath ($temp_dir.$n);
       
    69 	push @temp_paths, $n; 
       
    70 	$zips_paths{$n}=$d;
       
    71 
       
    72 	my $extrCmd = "7za.exe x -y ${zip_file} -o${temp_dir}${n}";
       
    73 	print "$extrCmd";
       
    74 	system ("$extrCmd");
       
    75 	
       
    76 	if ($back_up) { 
       
    77 		 	print "rename ${zip_file} to ${d}${n}_orig${ext}\n";
       
    78 		 	my $backup_name = ${d}.${n}."_orig".${ext};
       
    79 		 	rename (${zip_file}, ${backup_name});
       
    80 	}
       
    81 
       
    82 		unlink (${zip_file});
       
    83 
       
    84 	
       
    85 	if (-e "${temp_dir}${n}\\checksum.md5") {
       
    86 		unlink ("${temp_dir}${n}\\checksum.md5");
       
    87 	}
       
    88 	
       
    89 	print "\\epoc32\\tools\\evalid.pl -g ${temp_dir}${n} ${temp_dir}${n}\\checksum.md5";	
       
    90 	system ("\\epoc32\\tools\\evalid.pl -g ${temp_dir}${n} ${temp_dir}${n}\\checksum.md5");	
       
    91 		
       
    92 	open (MD5_FILE,"${temp_dir}${n}\\checksum.md5") or &error_msg("Cannot open ${temp_dir}${n}\\checksum.md5");
       
    93 	
       
    94 	while (<MD5_FILE>) {
       
    95  		chomp;
       
    96 	  next if (/^\/\//); # Skip comment lines
       
    97 	  next if (/^#/); # Skip comment lines
       
    98 	  next if (/^ *$/); # Skip empty lines 
       
    99   	$_ =~ s/\//\\/g;    #Change / marks to \
       
   100 
       
   101 		next if !/(TYPE=)/i;
       
   102 
       
   103 		my $file_name = $_;
       
   104 		$file_name =~ s/( TYPE=).*//i;
       
   105 
       
   106 		$_ =~ s/.*MD5=//i;
       
   107 
       
   108 	 	$file_name = lc $file_name;
       
   109 	 	$file_name =~ s/\\/\//g;    #Change / marks to \
       
   110 	
       
   111 		my %temp;
       
   112 		$temp{$_}=1;
       
   113 		if (!exists $zips_files{$file_name}) {
       
   114 			$zips_files{$file_name}={%temp};
       
   115 		}
       
   116 		else {
       
   117 #				print "$zips_files{$file_name}{$_}\t $_ \n";
       
   118 			if (exists $zips_files{$file_name}{$_}) {
       
   119 				$zips_files{$file_name}->{$_}=$zips_files{$file_name}->{$_}+1;
       
   120 			}
       
   121 		}
       
   122 	}
       
   123 	
       
   124 	close MD5_FILE;
       
   125 }
       
   126 
       
   127 sub rezip {
       
   128 	my $given_files = scalar (@zips);
       
   129 	my @zip_list;
       
   130 	print "\nrezip $given_files \n";
       
   131 	 
       
   132 	 foreach my $key (keys %zips_files) {
       
   133 		 foreach my $key2 (keys %{$zips_files{$key}}) {
       
   134 	 		if ($zips_files{$key}{$key2} != $given_files) {
       
   135 		  	$key =~ s/\//\\/g;    #Change / marks to \
       
   136 	 			push @zip_list, $key."\n"; 
       
   137 	 		}
       
   138 		}
       
   139 	}
       
   140 
       
   141   my $current_dir = cwd();
       
   142 	foreach my $zip_name (keys %zips_paths) {
       
   143 		my $name=$zips_paths{$zip_name}.$zip_name.".zip";
       
   144 	  chdir(${temp_dir}.${zip_name});
       
   145 		open (ZIP,"| zip -r $name -@");
       
   146 		print ZIP @zip_list;
       
   147 		close ZIP;
       
   148   	chdir($current_dir);
       
   149 	}
       
   150 }
       
   151 
       
   152 
       
   153 sub info {
       
   154   print "compress_zips.pl   version $version \n";
       
   155   print "Remove files that are same in each given zipfile.\n";
       
   156   print "Uses evalid to compare files inside zips.\n";
       
   157 }
       
   158 
       
   159 sub error_msg ($){
       
   160   my($ErrorMsg);
       
   161   ($ErrorMsg)=@_;
       
   162   my $given_command=$0;
       
   163   $given_command =~ s/.*\\(\w+\.\w+)$/$1/;
       
   164   print "Error: $ErrorMsg \n";
       
   165 	print "Usage: \n$given_command -z \\zips\\delta_western.zip -z \\zips\\delta_china.zip -z \\zips\\delta_japan.zip -backup\n";
       
   166 	print "           -z <zipfile>  zipfiles that should compress, two or more are needed\n";
       
   167 	print "           -backup       rename original zip files adding '_orig' end of the filename \n";
       
   168 	print "Example: \n$given_command -z \\zips\\delta_western.zip -z \\zips\\delta_china.zip -z \\zips\\delta_japan.zip -backup\n";
       
   169 	print "\n";  
       
   170 	die "\n";  
       
   171 }