bldsystemtools/commonbldutils/delta_zip.pl
changeset 0 83f4b4db085c
child 1 d4b442d23379
equal deleted inserted replaced
-1:000000000000 0:83f4b4db085c
       
     1 # Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 # All rights reserved.
       
     3 # This component and the accompanying materials are made available
       
     4 # under the terms of "Eclipse Public License v1.0"
       
     5 # which accompanies this distribution, and is available
       
     6 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 #
       
     8 # Initial Contributors:
       
     9 # Nokia Corporation - initial contribution.
       
    10 #
       
    11 # Contributors:
       
    12 #
       
    13 # Description:
       
    14 #
       
    15 
       
    16 use Getopt::Long;
       
    17 GetOptions("v", "x=s@");
       
    18 
       
    19 if (@ARGV<2)
       
    20 	{
       
    21 #........1.........2.........3.........4.........5.........6.........7.....
       
    22 	print <<USAGE_EOF;
       
    23 
       
    24 Usage:
       
    25   delta_zip zipfile dir1 [dir2 ...] [-x excludezip ] [-x pattern]
       
    26 
       
    27 Create <zipfile> which contains the tree rooted at <dir1>,
       
    28 excluding all files listed in <excludezip>, and optionally
       
    29 all files which match the specified Perl patterns.
       
    30 
       
    31 This is done with "zip zipfile -@", with the appropriately 
       
    32 filtered list of files supplied as standard input.
       
    33  
       
    34 USAGE_EOF
       
    35 	exit 1;
       
    36 	}
       
    37 
       
    38 my $zipfile = shift;
       
    39 die "$zipfile already exists\n" if (-e $zipfile);
       
    40 
       
    41 #--------
       
    42 # Find the basic list of files
       
    43 #
       
    44 
       
    45 my %filelist;
       
    46 my $filecount = 0;
       
    47 my $arg = shift;
       
    48 
       
    49 while ($arg ne "")
       
    50 	{
       
    51 	if (-d $arg)
       
    52 		{
       
    53 		add_dir($arg);
       
    54 		}
       
    55 	elsif (-e $arg)
       
    56 		{
       
    57 		add_file($arg);
       
    58 		}
       
    59 	else
       
    60 		{
       
    61 		print "Cannot find $arg - ignored\n";
       
    62 		}
       
    63 	print "$filecount files after processing $arg\n";
       
    64 
       
    65 	$arg = shift;
       
    66 	next;
       
    67 	}
       
    68 
       
    69 #--------
       
    70 # Remove excluded files
       
    71 #
       
    72 
       
    73 foreach $arg (@opt_x)
       
    74 	{
       
    75 	if (-e $arg)
       
    76 		{
       
    77 		exclude_zip($arg);
       
    78 		}
       
    79 	else
       
    80 		{
       
    81 		exclude_pattern($arg);
       
    82 		}
       
    83 	print "$filecount files after excluding $arg\n";
       
    84 	$arg = shift;
       
    85 	}
       
    86 
       
    87 print "Invoking \"zip $zipfile -@\"\n";
       
    88 
       
    89 open ZIPOUT, "| zip $zipfile -@";
       
    90 foreach $arg ( sort keys %filelist)
       
    91 	{
       
    92 	if ($filelist{$arg} ne "")
       
    93 		{
       
    94 		print ZIPOUT "$filelist{$arg}\n";
       
    95 		}
       
    96 	}
       
    97 
       
    98 close ZIPOUT;
       
    99 die "Problems creating zip file\n" if ($? != 0);
       
   100 
       
   101 exit 0;
       
   102 
       
   103 
       
   104 #------------------------------------------------------------
       
   105 
       
   106 sub add_file
       
   107 	{
       
   108 	my ($file) = @_;
       
   109 	my $key = lc $file;
       
   110 
       
   111 	$key =~ s-/-\\-g;	# convert / to \ for path separators
       
   112 	$key =~ s/^\\//;	# remove leading \ since it won't appear in zip files
       
   113 	if ($filelist{$key} ne "")
       
   114 		{
       
   115 		die "Duplicate file $file\n";
       
   116 		}
       
   117 	$filelist{$key} = $file;
       
   118 	$filecount += 1;
       
   119 	}
       
   120 
       
   121 sub exclude_file
       
   122 	{
       
   123 	my ($file) = @_;
       
   124 	my $key = lc $file;
       
   125 
       
   126 	$key =~ s-/-\\-g;	# convert / to \ for path separators
       
   127 	$key =~ s/^\\//;	# remove leading \ since it won't appear in zip files
       
   128 	if ($filelist{$key} ne "")
       
   129 		{
       
   130 		delete $filelist{$key};
       
   131 		$filecount -= 1;
       
   132 		}
       
   133 	}
       
   134 
       
   135 sub exclude_pattern
       
   136 	{
       
   137 	my ($pattern) = @_;
       
   138 	my $key;
       
   139 
       
   140 	foreach $key (keys %filelist)
       
   141 		{
       
   142 		if ($key =~ /$pattern/i && $filelist{$key} ne "")
       
   143 			{
       
   144 			delete $filelist{$key};
       
   145 			$filecount -= 1;
       
   146 			}
       
   147 		}
       
   148 	}
       
   149 
       
   150 sub add_dir
       
   151 	{
       
   152 	my ($dir) = @_;
       
   153 	opendir LISTDIR, $dir or print "Cannot read directory $dir\n" and return;
       
   154 	my @list = grep !/^\.\.?$/, readdir LISTDIR;
       
   155 	closedir LISTDIR;
       
   156 
       
   157 	if ($opt_v)
       
   158 		{
       
   159 		print "Scanning $dir...\n";
       
   160 		}
       
   161 	my $name;
       
   162 	foreach $name (@list)
       
   163 		{
       
   164 		my $filename = "$dir\\$name";
       
   165 		if (-d $filename)
       
   166 			{
       
   167 			add_dir($filename);	# recurse
       
   168 			}
       
   169 		else
       
   170 			{
       
   171 			add_file($filename);
       
   172 			}
       
   173 		}
       
   174 	}
       
   175 
       
   176 sub exclude_zip
       
   177 	{
       
   178 	my ($excludezip) = @_;
       
   179 
       
   180 	die "$excludezip does not exist\n" if (!-e $excludezip);
       
   181 	print "Reading exclusions from $excludezip...\n";
       
   182 
       
   183 	my $line;
       
   184 	open ZIPEX, "unzip -l $excludezip |";
       
   185 	while ($line=<ZIPEX>)
       
   186 		{
       
   187 		#    4492  10-12-99  17:31   epoc32/BLDMAKE/AGENDA/ARM4.MAKE
       
   188 		if ($line =~ /..-..-..\s+..:..\s+(.*)$/)
       
   189 			{
       
   190 			exclude_file($1);
       
   191 			}
       
   192 		}
       
   193 	close ZIPEX;
       
   194 	die "Problem reading $excludezip\n" if ($? != 0);
       
   195 	}
       
   196 
       
   197 
       
   198 
       
   199