1cdf0e10cSrcweir: 2cdf0e10cSrcweireval 'exec perl -wS $0 ${1+"$@"}' 3cdf0e10cSrcweir if 0; 47e90fac2SAndrew Rist#************************************************************** 57e90fac2SAndrew Rist# 67e90fac2SAndrew Rist# Licensed to the Apache Software Foundation (ASF) under one 77e90fac2SAndrew Rist# or more contributor license agreements. See the NOTICE file 87e90fac2SAndrew Rist# distributed with this work for additional information 97e90fac2SAndrew Rist# regarding copyright ownership. The ASF licenses this file 107e90fac2SAndrew Rist# to you under the Apache License, Version 2.0 (the 117e90fac2SAndrew Rist# "License"); you may not use this file except in compliance 127e90fac2SAndrew Rist# with the License. You may obtain a copy of the License at 137e90fac2SAndrew Rist# 147e90fac2SAndrew Rist# http://www.apache.org/licenses/LICENSE-2.0 157e90fac2SAndrew Rist# 167e90fac2SAndrew Rist# Unless required by applicable law or agreed to in writing, 177e90fac2SAndrew Rist# software distributed under the License is distributed on an 187e90fac2SAndrew Rist# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 197e90fac2SAndrew Rist# KIND, either express or implied. See the License for the 207e90fac2SAndrew Rist# specific language governing permissions and limitations 217e90fac2SAndrew Rist# under the License. 227e90fac2SAndrew Rist# 237e90fac2SAndrew Rist#************************************************************** 247e90fac2SAndrew Rist 257e90fac2SAndrew Rist 26cdf0e10cSrcweir 27cdf0e10cSrcweir# 28cdf0e10cSrcweir# mapgen - generate a dependencies file for zip commando 29cdf0e10cSrcweir# 30cdf0e10cSrcweiruse Cwd; 31cdf0e10cSrcweir 32cdf0e10cSrcweir#### script id ##### 33cdf0e10cSrcweir 34cdf0e10cSrcweir( $script_name = $0 ) =~ s/^.*\b(\w+)\.pl$/$1/; 35cdf0e10cSrcweir 36cdf0e10cSrcweir$id_str = ' $Revision: 1.12 $ '; 37cdf0e10cSrcweir$id_str =~ /Revision:\s+(\S+)\s+\$/ 38cdf0e10cSrcweir ? ($script_rev = $1) : ($script_rev = "-"); 39cdf0e10cSrcweir 40cdf0e10cSrcweirprint STDERR "$script_name -- version: $script_rev\n"; 41cdf0e10cSrcweirprint STDERR "Multi Platform Enabled Edition\n"; 42cdf0e10cSrcweir 43cdf0e10cSrcweir######################### 44cdf0e10cSrcweir# # 45cdf0e10cSrcweir# Globale Variablen # 46cdf0e10cSrcweir# # 47cdf0e10cSrcweir######################### 48cdf0e10cSrcweir 49cdf0e10cSrcweir$zip_file = ''; 50cdf0e10cSrcweir$R = ''; 51cdf0e10cSrcweir$r = ''; 52cdf0e10cSrcweir$exclude = ''; 53cdf0e10cSrcweir$include = ''; 54cdf0e10cSrcweir@given_patterns = (); # patterns(files) list from command line 55cdf0e10cSrcweir%files_in_arch = (); 56cdf0e10cSrcweir@exc_patterns = (); # array of all patterns for files to be excluded 57cdf0e10cSrcweir@inc_patterns = (); # array of all patterns for files to be included 58cdf0e10cSrcweir%exc_files_hash = (); # hash of files to be excluded (according to @exc_patterns) 59cdf0e10cSrcweir%inc_files_hash = (); # hash of files to be included (according to @inc_patterns) 60cdf0e10cSrcweir$prefix = ''; 61cdf0e10cSrcweir 62cdf0e10cSrcweir#### main #### 63cdf0e10cSrcweir 64cdf0e10cSrcweir&get_options; 65cdf0e10cSrcweir&get_zip_content; 66cdf0e10cSrcweir&write_zip_file; 67cdf0e10cSrcweir 68cdf0e10cSrcweir#### end of main procedure #### 69cdf0e10cSrcweir 70cdf0e10cSrcweir######################### 71cdf0e10cSrcweir# # 72cdf0e10cSrcweir# Procedures # 73cdf0e10cSrcweir# # 74cdf0e10cSrcweir######################### 75cdf0e10cSrcweir 76cdf0e10cSrcweir# 77cdf0e10cSrcweir# procedure writes zipdep file 78cdf0e10cSrcweir# 79cdf0e10cSrcweirsub write_zip_file { 80cdf0e10cSrcweir my @dependencies = keys %files_in_arch; 81cdf0e10cSrcweir if ($#dependencies != -1) { 82cdf0e10cSrcweir print "\n". &convert_slashes($zip_file) . ' :'; 83cdf0e10cSrcweir foreach (@dependencies) { 84cdf0e10cSrcweir next if (-d); 85cdf0e10cSrcweir print " \\\n\t" . $prefix . &convert_slashes($_); 86cdf0e10cSrcweir }; 87cdf0e10cSrcweir print "\n\n"; 88cdf0e10cSrcweir }; 89cdf0e10cSrcweir}; 90cdf0e10cSrcweir 91cdf0e10cSrcweir# 92cdf0e10cSrcweir# convert slashes 93cdf0e10cSrcweir# 94cdf0e10cSrcweirsub convert_slashes { 95cdf0e10cSrcweir my $path = shift; 96cdf0e10cSrcweir $path =~ s/\//\$\//g; 97cdf0e10cSrcweir $path =~ s/\\/\$\//g; 98*8746300dSYuri Dario if ( $^O eq 'os2' ) 99*8746300dSYuri Dario { 100*8746300dSYuri Dario # remove also quotes surrounding name, thus writing buggy paths 101*8746300dSYuri Dario $path =~ s/\"//g; 102*8746300dSYuri Dario } 103cdf0e10cSrcweir return $path; 104cdf0e10cSrcweir}; 105cdf0e10cSrcweir 106cdf0e10cSrcweir# 107cdf0e10cSrcweir# convert slashes to internal perl representation 108cdf0e10cSrcweir# 109cdf0e10cSrcweirsub perled_slashes { 110cdf0e10cSrcweir my $path = shift; 111cdf0e10cSrcweir $path =~ s/\\/\//g; 112cdf0e10cSrcweir $path =~ s/\/+/\//g; 113cdf0e10cSrcweir return $path; 114cdf0e10cSrcweir}; 115cdf0e10cSrcweir 116cdf0e10cSrcweir# 117cdf0e10cSrcweir# Collect all files to zip in @patterns_array array 118cdf0e10cSrcweir# 119cdf0e10cSrcweirsub get_zip_content { 120cdf0e10cSrcweir &get_zip_entries(\@given_patterns); 121cdf0e10cSrcweir my $file_name = ''; 122cdf0e10cSrcweir foreach $file_name (keys %files_in_arch) { 123cdf0e10cSrcweir if (-d $file_name) { 124cdf0e10cSrcweir &get_dir_content($file_name, \%files_in_arch) if ($r || $R); 125cdf0e10cSrcweir undef $files_in_arch{$file_name}; 126cdf0e10cSrcweir }; 127cdf0e10cSrcweir }; 128cdf0e10cSrcweir &remove_uncompliant(\@given_patterns) if ($R); 129cdf0e10cSrcweir &get_patterns_files(\@exc_patterns, \%exc_files_hash) if ($exclude); 130cdf0e10cSrcweir &get_patterns_files(\@inc_patterns, \%inc_files_hash) if ($include); 131cdf0e10cSrcweir foreach my $file_name (keys %exc_files_hash) { 132cdf0e10cSrcweir if (defined $files_in_arch{$file_name}) { 133cdf0e10cSrcweir delete $files_in_arch{$file_name}; 134cdf0e10cSrcweir #print STDERR "excluded $file_name\n"; 135cdf0e10cSrcweir }; 136cdf0e10cSrcweir }; 137cdf0e10cSrcweir if ($include) { 138cdf0e10cSrcweir foreach my $file_name (keys %files_in_arch) { 139cdf0e10cSrcweir if (!(defined $inc_files_hash{$file_name})) { 140cdf0e10cSrcweir delete $files_in_arch{$file_name}; 141cdf0e10cSrcweir }; 142cdf0e10cSrcweir }; 143cdf0e10cSrcweir } 144cdf0e10cSrcweir}; 145cdf0e10cSrcweir 146cdf0e10cSrcweir# 147cdf0e10cSrcweir# Procedure removes from %files_in_arch all files which 148cdf0e10cSrcweir# are not compliant to patterns in @given_patterns 149cdf0e10cSrcweir# 150cdf0e10cSrcweirsub remove_uncompliant { 151cdf0e10cSrcweir my $given_patterns = shift; 152cdf0e10cSrcweir my @reg_exps = (); 153cdf0e10cSrcweir my $pattern = ''; 154cdf0e10cSrcweir foreach $pattern (@$given_patterns) { 155cdf0e10cSrcweir push(@reg_exps, &make_reg_exp($pattern)); 156cdf0e10cSrcweir }; 157cdf0e10cSrcweir # write file name as a value for the path(key) 158cdf0e10cSrcweir foreach my $file (keys %files_in_arch) { 159cdf0e10cSrcweir next if (-d $file); 160cdf0e10cSrcweir #print "$file\n"; 161cdf0e10cSrcweir if ($file =~ /[\\ | \/](.+)$/) { 162cdf0e10cSrcweir $files_in_arch{$file} = $1; 163cdf0e10cSrcweir } else { 164cdf0e10cSrcweir $files_in_arch{$file} = $file; 165cdf0e10cSrcweir }; 166cdf0e10cSrcweir }; 167cdf0e10cSrcweir foreach $pattern (@reg_exps) { 168cdf0e10cSrcweir foreach my $file (keys %files_in_arch) { 169cdf0e10cSrcweir if (!($files_in_arch{$file} =~ /$pattern/)) { 170cdf0e10cSrcweir delete $files_in_arch{$file}; 171cdf0e10cSrcweir #} else { 172cdf0e10cSrcweir # print "Complient: $file\n"; 173cdf0e10cSrcweir }; 174cdf0e10cSrcweir }; 175cdf0e10cSrcweir }; 176cdf0e10cSrcweir}; 177cdf0e10cSrcweir 178cdf0e10cSrcweir# 179cdf0e10cSrcweir# Procedure adds/removes to/from %files_in_arch all files, that are 180cdf0e10cSrcweir# compliant to the patterns in array passed 181cdf0e10cSrcweir# 182cdf0e10cSrcweirsub get_zip_entries { 183cdf0e10cSrcweir if ($R) { 184cdf0e10cSrcweir opendir DIR, '.'; 185cdf0e10cSrcweir my @dir_content = readdir(DIR); 186cdf0e10cSrcweir close DIR; 187cdf0e10cSrcweir foreach my $file_name(@dir_content) { 188cdf0e10cSrcweir $file_name =~ /^\.$/ and next; 189cdf0e10cSrcweir $file_name =~ /^\.\.$/ and next; 190cdf0e10cSrcweir $files_in_arch{$file_name}++; 191cdf0e10cSrcweir #print "included $file_name\n"; 192cdf0e10cSrcweir }; 193cdf0e10cSrcweir } else { 194cdf0e10cSrcweir my $patterns_array = shift; 195cdf0e10cSrcweir my $pattern = ''; 196cdf0e10cSrcweir foreach $pattern (@$patterns_array) { 197cdf0e10cSrcweir if ((-d $pattern) || (-f $pattern)) { 198cdf0e10cSrcweir $files_in_arch{$pattern}++; 199cdf0e10cSrcweir next; 200cdf0e10cSrcweir } 201cdf0e10cSrcweir my $file_name = ''; 202cdf0e10cSrcweir foreach $file_name (glob $pattern) { 203cdf0e10cSrcweir #next if (!(-d $file_name) || !(-f $file_name)); 204cdf0e10cSrcweir $files_in_arch{$file_name}++; 205cdf0e10cSrcweir }; 206cdf0e10cSrcweir }; 207cdf0e10cSrcweir } 208cdf0e10cSrcweir}; 209cdf0e10cSrcweir 210cdf0e10cSrcweir# 211cdf0e10cSrcweir# Procedure converts given parameter to a regular expression 212cdf0e10cSrcweir# 213cdf0e10cSrcweirsub make_reg_exp { 214cdf0e10cSrcweir my $arg = shift; 215cdf0e10cSrcweir $arg =~ s/\\/\\\\/g; 216cdf0e10cSrcweir $arg =~ s/\//\\\//g; 217cdf0e10cSrcweir $arg =~ s/\./\\\./g; 218cdf0e10cSrcweir $arg =~ s/\+/\\\+/g; 219cdf0e10cSrcweir $arg =~ s/\{/\\\{/g; 220cdf0e10cSrcweir $arg =~ s/\}/\\\}/g; 221cdf0e10cSrcweir $arg =~ s/\*/\.\*/g; 222cdf0e10cSrcweir $arg =~ s/\?/\./g; 223cdf0e10cSrcweir #$arg = '/'.$arg.'/'; 224cdf0e10cSrcweir #print "Regular expression: $arg\n"; 225cdf0e10cSrcweir return $arg; 226cdf0e10cSrcweir}; 227cdf0e10cSrcweir 228cdf0e10cSrcweir# 229cdf0e10cSrcweir# Procedure retrieves shell pattern and converts them into regular expressions 230cdf0e10cSrcweir# 231cdf0e10cSrcweirsub get_patterns { 232cdf0e10cSrcweir my $patterns = shift; 233cdf0e10cSrcweir my $arg = ''; 234cdf0e10cSrcweir while ($arg = shift @ARGV) { 235cdf0e10cSrcweir $arg =~ /^-/ and unshift(@ARGV, $arg) and return; 236cdf0e10cSrcweir if (!$zip_file) { 237cdf0e10cSrcweir $zip_file = $arg; 238cdf0e10cSrcweir next; 239cdf0e10cSrcweir }; 240cdf0e10cSrcweir $arg = &make_reg_exp($arg); 241cdf0e10cSrcweir push(@$patterns, $arg); 242cdf0e10cSrcweir }; 243cdf0e10cSrcweir}; 244cdf0e10cSrcweir 245cdf0e10cSrcweir# 246cdf0e10cSrcweir# Get all options passed 247cdf0e10cSrcweir# 248cdf0e10cSrcweirsub get_options { 249cdf0e10cSrcweir my ($arg); 250cdf0e10cSrcweir &usage() && exit(0) if ($#ARGV == -1); 251cdf0e10cSrcweir while ($arg = shift @ARGV) { 252cdf0e10cSrcweir $arg = &perled_slashes($arg); 253cdf0e10cSrcweir #print STDERR "$arg\n"; 254cdf0e10cSrcweir $arg =~ /^-R$/ and $R = 1 and next; 255cdf0e10cSrcweir $arg =~ /^-r$/ and $r = 1 and next; 256cdf0e10cSrcweir $arg =~ /^-x$/ and $exclude = 1 and &get_patterns(\@exc_patterns) and next; 257cdf0e10cSrcweir $arg =~ /^-i$/ and $include = 1 and &get_patterns(\@inc_patterns) and next; 258cdf0e10cSrcweir $arg =~ /^-prefix$/ and $prefix = shift @ARGV and next; 259cdf0e10cSrcweir $arg =~ /^-b$/ and shift @ARGV and next; 260cdf0e10cSrcweir $arg =~ /^-n$/ and shift @ARGV and next; 261cdf0e10cSrcweir $arg =~ /^-t$/ and shift @ARGV and next; 262cdf0e10cSrcweir $arg =~ /^-tt$/ and shift @ARGV and next; 263cdf0e10cSrcweir $arg =~ /^-h$/ and &usage and exit(0); 264cdf0e10cSrcweir $arg =~ /^--help$/ and &usage and exit(0); 265cdf0e10cSrcweir $arg =~ /^-?$/ and &usage and exit(0); 266cdf0e10cSrcweir if ($arg =~ /^-(\w)(\w+)$/) { 267cdf0e10cSrcweir unshift (@ARGV, '-'.$1); 268cdf0e10cSrcweir unshift (@ARGV, '-'.$2); 269cdf0e10cSrcweir next; 270cdf0e10cSrcweir }; 271cdf0e10cSrcweir# just ignore other switches... 272cdf0e10cSrcweir $arg =~ /^-(\w+)$/ and next; 273cdf0e10cSrcweir $arg =~ /^\/\?$/ and &usage and exit(0); 274cdf0e10cSrcweir $zip_file = $arg and next if (!$zip_file); 275cdf0e10cSrcweir push(@given_patterns, $arg); 276cdf0e10cSrcweir }; 277cdf0e10cSrcweir &print_error('error: Invalid command arguments (do not specify both -r and -R)') if ($r && $R); 278cdf0e10cSrcweir if ($r && ($#given_patterns == -1)) { 279cdf0e10cSrcweir &print_error('no list specified'); 280cdf0e10cSrcweir }; 281cdf0e10cSrcweir}; 282cdf0e10cSrcweir 283cdf0e10cSrcweir# 284cdf0e10cSrcweir# Procedure fills out passed hash with files from passed dir 285cdf0e10cSrcweir# compliant to the pattern from @$patterns 286cdf0e10cSrcweir# 287cdf0e10cSrcweirsub get_patterns_files { 288cdf0e10cSrcweir my $patterns_array = shift; 289cdf0e10cSrcweir my $files_hash = shift; 290cdf0e10cSrcweir my @zip_files = keys %files_in_arch; 291cdf0e10cSrcweir foreach my $pattern (@$patterns_array) { 292cdf0e10cSrcweir my @fit_pattern = grep /$pattern/, @zip_files; 293cdf0e10cSrcweir foreach my $entry (@fit_pattern) { 294cdf0e10cSrcweir $$files_hash{$entry}++; 295cdf0e10cSrcweir #print "$entry\n"; 296cdf0e10cSrcweir }; 297cdf0e10cSrcweir }; 298cdf0e10cSrcweir}; 299cdf0e10cSrcweir 300cdf0e10cSrcweir# 301cdf0e10cSrcweir# Get dir stuff to pack 302cdf0e10cSrcweir# 303cdf0e10cSrcweirsub get_dir_content { 304cdf0e10cSrcweir my $dir = shift; 305cdf0e10cSrcweir my $dir_hash_ref = shift; 306cdf0e10cSrcweir my $entry = ''; 307cdf0e10cSrcweir if (opendir(DIR, $dir)) { 308cdf0e10cSrcweir my @prj_dir_list = readdir(DIR); 309cdf0e10cSrcweir closedir (DIR); 310cdf0e10cSrcweir foreach $entry (@prj_dir_list) { 311cdf0e10cSrcweir $entry =~ /^\.$/ and next; 312cdf0e10cSrcweir $entry =~ /^\.\.$/ and next; 313cdf0e10cSrcweir 314cdf0e10cSrcweir $entry = $dir . '/' . $entry; 315cdf0e10cSrcweir # if $enry is a dir - read all its files, 316cdf0e10cSrcweir # otherwise store $entry itself 317cdf0e10cSrcweir if (-d $entry) { 318cdf0e10cSrcweir &get_dir_content($entry, $dir_hash_ref); 319cdf0e10cSrcweir } else { 320cdf0e10cSrcweir $$dir_hash_ref{$entry}++; 321cdf0e10cSrcweir }; 322cdf0e10cSrcweir }; 323cdf0e10cSrcweir }; 324cdf0e10cSrcweir return '1'; 325cdf0e10cSrcweir}; 326cdf0e10cSrcweir 327cdf0e10cSrcweirsub print_error { 328cdf0e10cSrcweir my $message = shift; 329cdf0e10cSrcweir print STDERR "\nERROR: $message\n"; 330cdf0e10cSrcweir exit (1); 331cdf0e10cSrcweir}; 332cdf0e10cSrcweir 333cdf0e10cSrcweirsub usage { 334cdf0e10cSrcweir print STDERR " zipdep [-aABcdDeEfFghjklLmoqrRSTuvVwXyz] [-b path]\n"; 335cdf0e10cSrcweir print STDERR " [-n suffixes] [-t mmddyyyy] [-tt mmddyyyy] [ zipfile [\n"; 336cdf0e10cSrcweir print STDERR " file1 file2 ...]] [-xi list]\n"; 337cdf0e10cSrcweir} 338cdf0e10cSrcweir 339