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; 988746300dSYuri Dario if ( $^O eq 'os2' ) 998746300dSYuri Dario { 1008746300dSYuri Dario # remove also quotes surrounding name, thus writing buggy paths 1018746300dSYuri Dario $path =~ s/\"//g; 1028746300dSYuri 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; 113*810a4bfdSYuri Dario if ( $^O eq 'os2' ) 114*810a4bfdSYuri Dario { 115*810a4bfdSYuri Dario # remove also quotes surrounding name, thus writing buggy paths 116*810a4bfdSYuri Dario $path =~ s/\"//g; 117*810a4bfdSYuri Dario } 118cdf0e10cSrcweir return $path; 119cdf0e10cSrcweir}; 120cdf0e10cSrcweir 121cdf0e10cSrcweir# 122cdf0e10cSrcweir# Collect all files to zip in @patterns_array array 123cdf0e10cSrcweir# 124cdf0e10cSrcweirsub get_zip_content { 125cdf0e10cSrcweir &get_zip_entries(\@given_patterns); 126cdf0e10cSrcweir my $file_name = ''; 127cdf0e10cSrcweir foreach $file_name (keys %files_in_arch) { 128cdf0e10cSrcweir if (-d $file_name) { 129cdf0e10cSrcweir &get_dir_content($file_name, \%files_in_arch) if ($r || $R); 130cdf0e10cSrcweir undef $files_in_arch{$file_name}; 131cdf0e10cSrcweir }; 132cdf0e10cSrcweir }; 133cdf0e10cSrcweir &remove_uncompliant(\@given_patterns) if ($R); 134cdf0e10cSrcweir &get_patterns_files(\@exc_patterns, \%exc_files_hash) if ($exclude); 135cdf0e10cSrcweir &get_patterns_files(\@inc_patterns, \%inc_files_hash) if ($include); 136cdf0e10cSrcweir foreach my $file_name (keys %exc_files_hash) { 137cdf0e10cSrcweir if (defined $files_in_arch{$file_name}) { 138cdf0e10cSrcweir delete $files_in_arch{$file_name}; 139cdf0e10cSrcweir #print STDERR "excluded $file_name\n"; 140cdf0e10cSrcweir }; 141cdf0e10cSrcweir }; 142cdf0e10cSrcweir if ($include) { 143cdf0e10cSrcweir foreach my $file_name (keys %files_in_arch) { 144cdf0e10cSrcweir if (!(defined $inc_files_hash{$file_name})) { 145cdf0e10cSrcweir delete $files_in_arch{$file_name}; 146cdf0e10cSrcweir }; 147cdf0e10cSrcweir }; 148cdf0e10cSrcweir } 149cdf0e10cSrcweir}; 150cdf0e10cSrcweir 151cdf0e10cSrcweir# 152cdf0e10cSrcweir# Procedure removes from %files_in_arch all files which 153cdf0e10cSrcweir# are not compliant to patterns in @given_patterns 154cdf0e10cSrcweir# 155cdf0e10cSrcweirsub remove_uncompliant { 156cdf0e10cSrcweir my $given_patterns = shift; 157cdf0e10cSrcweir my @reg_exps = (); 158cdf0e10cSrcweir my $pattern = ''; 159cdf0e10cSrcweir foreach $pattern (@$given_patterns) { 160cdf0e10cSrcweir push(@reg_exps, &make_reg_exp($pattern)); 161cdf0e10cSrcweir }; 162cdf0e10cSrcweir # write file name as a value for the path(key) 163cdf0e10cSrcweir foreach my $file (keys %files_in_arch) { 164cdf0e10cSrcweir next if (-d $file); 165cdf0e10cSrcweir #print "$file\n"; 166cdf0e10cSrcweir if ($file =~ /[\\ | \/](.+)$/) { 167cdf0e10cSrcweir $files_in_arch{$file} = $1; 168cdf0e10cSrcweir } else { 169cdf0e10cSrcweir $files_in_arch{$file} = $file; 170cdf0e10cSrcweir }; 171cdf0e10cSrcweir }; 172cdf0e10cSrcweir foreach $pattern (@reg_exps) { 173cdf0e10cSrcweir foreach my $file (keys %files_in_arch) { 174cdf0e10cSrcweir if (!($files_in_arch{$file} =~ /$pattern/)) { 175cdf0e10cSrcweir delete $files_in_arch{$file}; 176cdf0e10cSrcweir #} else { 177cdf0e10cSrcweir # print "Complient: $file\n"; 178cdf0e10cSrcweir }; 179cdf0e10cSrcweir }; 180cdf0e10cSrcweir }; 181cdf0e10cSrcweir}; 182cdf0e10cSrcweir 183cdf0e10cSrcweir# 184cdf0e10cSrcweir# Procedure adds/removes to/from %files_in_arch all files, that are 185cdf0e10cSrcweir# compliant to the patterns in array passed 186cdf0e10cSrcweir# 187cdf0e10cSrcweirsub get_zip_entries { 188cdf0e10cSrcweir if ($R) { 189cdf0e10cSrcweir opendir DIR, '.'; 190cdf0e10cSrcweir my @dir_content = readdir(DIR); 191cdf0e10cSrcweir close DIR; 192cdf0e10cSrcweir foreach my $file_name(@dir_content) { 193cdf0e10cSrcweir $file_name =~ /^\.$/ and next; 194cdf0e10cSrcweir $file_name =~ /^\.\.$/ and next; 195cdf0e10cSrcweir $files_in_arch{$file_name}++; 196cdf0e10cSrcweir #print "included $file_name\n"; 197cdf0e10cSrcweir }; 198cdf0e10cSrcweir } else { 199cdf0e10cSrcweir my $patterns_array = shift; 200cdf0e10cSrcweir my $pattern = ''; 201cdf0e10cSrcweir foreach $pattern (@$patterns_array) { 202cdf0e10cSrcweir if ((-d $pattern) || (-f $pattern)) { 203cdf0e10cSrcweir $files_in_arch{$pattern}++; 204cdf0e10cSrcweir next; 205cdf0e10cSrcweir } 206cdf0e10cSrcweir my $file_name = ''; 207cdf0e10cSrcweir foreach $file_name (glob $pattern) { 208cdf0e10cSrcweir #next if (!(-d $file_name) || !(-f $file_name)); 209cdf0e10cSrcweir $files_in_arch{$file_name}++; 210cdf0e10cSrcweir }; 211cdf0e10cSrcweir }; 212cdf0e10cSrcweir } 213cdf0e10cSrcweir}; 214cdf0e10cSrcweir 215cdf0e10cSrcweir# 216cdf0e10cSrcweir# Procedure converts given parameter to a regular expression 217cdf0e10cSrcweir# 218cdf0e10cSrcweirsub make_reg_exp { 219cdf0e10cSrcweir my $arg = shift; 220cdf0e10cSrcweir $arg =~ s/\\/\\\\/g; 221cdf0e10cSrcweir $arg =~ s/\//\\\//g; 222cdf0e10cSrcweir $arg =~ s/\./\\\./g; 223cdf0e10cSrcweir $arg =~ s/\+/\\\+/g; 224cdf0e10cSrcweir $arg =~ s/\{/\\\{/g; 225cdf0e10cSrcweir $arg =~ s/\}/\\\}/g; 226cdf0e10cSrcweir $arg =~ s/\*/\.\*/g; 227cdf0e10cSrcweir $arg =~ s/\?/\./g; 228cdf0e10cSrcweir #$arg = '/'.$arg.'/'; 229cdf0e10cSrcweir #print "Regular expression: $arg\n"; 230cdf0e10cSrcweir return $arg; 231cdf0e10cSrcweir}; 232cdf0e10cSrcweir 233cdf0e10cSrcweir# 234cdf0e10cSrcweir# Procedure retrieves shell pattern and converts them into regular expressions 235cdf0e10cSrcweir# 236cdf0e10cSrcweirsub get_patterns { 237cdf0e10cSrcweir my $patterns = shift; 238cdf0e10cSrcweir my $arg = ''; 239cdf0e10cSrcweir while ($arg = shift @ARGV) { 240cdf0e10cSrcweir $arg =~ /^-/ and unshift(@ARGV, $arg) and return; 241cdf0e10cSrcweir if (!$zip_file) { 242cdf0e10cSrcweir $zip_file = $arg; 243cdf0e10cSrcweir next; 244cdf0e10cSrcweir }; 245cdf0e10cSrcweir $arg = &make_reg_exp($arg); 246cdf0e10cSrcweir push(@$patterns, $arg); 247cdf0e10cSrcweir }; 248cdf0e10cSrcweir}; 249cdf0e10cSrcweir 250cdf0e10cSrcweir# 251cdf0e10cSrcweir# Get all options passed 252cdf0e10cSrcweir# 253cdf0e10cSrcweirsub get_options { 254cdf0e10cSrcweir my ($arg); 255cdf0e10cSrcweir &usage() && exit(0) if ($#ARGV == -1); 256cdf0e10cSrcweir while ($arg = shift @ARGV) { 257cdf0e10cSrcweir $arg = &perled_slashes($arg); 258cdf0e10cSrcweir #print STDERR "$arg\n"; 259cdf0e10cSrcweir $arg =~ /^-R$/ and $R = 1 and next; 260cdf0e10cSrcweir $arg =~ /^-r$/ and $r = 1 and next; 261cdf0e10cSrcweir $arg =~ /^-x$/ and $exclude = 1 and &get_patterns(\@exc_patterns) and next; 262cdf0e10cSrcweir $arg =~ /^-i$/ and $include = 1 and &get_patterns(\@inc_patterns) and next; 263cdf0e10cSrcweir $arg =~ /^-prefix$/ and $prefix = shift @ARGV and next; 264cdf0e10cSrcweir $arg =~ /^-b$/ and shift @ARGV and next; 265cdf0e10cSrcweir $arg =~ /^-n$/ and shift @ARGV and next; 266cdf0e10cSrcweir $arg =~ /^-t$/ and shift @ARGV and next; 267cdf0e10cSrcweir $arg =~ /^-tt$/ and shift @ARGV and next; 268cdf0e10cSrcweir $arg =~ /^-h$/ and &usage and exit(0); 269cdf0e10cSrcweir $arg =~ /^--help$/ and &usage and exit(0); 270cdf0e10cSrcweir $arg =~ /^-?$/ and &usage and exit(0); 271cdf0e10cSrcweir if ($arg =~ /^-(\w)(\w+)$/) { 272cdf0e10cSrcweir unshift (@ARGV, '-'.$1); 273cdf0e10cSrcweir unshift (@ARGV, '-'.$2); 274cdf0e10cSrcweir next; 275cdf0e10cSrcweir }; 276cdf0e10cSrcweir# just ignore other switches... 277cdf0e10cSrcweir $arg =~ /^-(\w+)$/ and next; 278cdf0e10cSrcweir $arg =~ /^\/\?$/ and &usage and exit(0); 279cdf0e10cSrcweir $zip_file = $arg and next if (!$zip_file); 280cdf0e10cSrcweir push(@given_patterns, $arg); 281cdf0e10cSrcweir }; 282cdf0e10cSrcweir &print_error('error: Invalid command arguments (do not specify both -r and -R)') if ($r && $R); 283cdf0e10cSrcweir if ($r && ($#given_patterns == -1)) { 284cdf0e10cSrcweir &print_error('no list specified'); 285cdf0e10cSrcweir }; 286cdf0e10cSrcweir}; 287cdf0e10cSrcweir 288cdf0e10cSrcweir# 289cdf0e10cSrcweir# Procedure fills out passed hash with files from passed dir 290cdf0e10cSrcweir# compliant to the pattern from @$patterns 291cdf0e10cSrcweir# 292cdf0e10cSrcweirsub get_patterns_files { 293cdf0e10cSrcweir my $patterns_array = shift; 294cdf0e10cSrcweir my $files_hash = shift; 295cdf0e10cSrcweir my @zip_files = keys %files_in_arch; 296cdf0e10cSrcweir foreach my $pattern (@$patterns_array) { 297cdf0e10cSrcweir my @fit_pattern = grep /$pattern/, @zip_files; 298cdf0e10cSrcweir foreach my $entry (@fit_pattern) { 299cdf0e10cSrcweir $$files_hash{$entry}++; 300cdf0e10cSrcweir #print "$entry\n"; 301cdf0e10cSrcweir }; 302cdf0e10cSrcweir }; 303cdf0e10cSrcweir}; 304cdf0e10cSrcweir 305cdf0e10cSrcweir# 306cdf0e10cSrcweir# Get dir stuff to pack 307cdf0e10cSrcweir# 308cdf0e10cSrcweirsub get_dir_content { 309cdf0e10cSrcweir my $dir = shift; 310cdf0e10cSrcweir my $dir_hash_ref = shift; 311cdf0e10cSrcweir my $entry = ''; 312cdf0e10cSrcweir if (opendir(DIR, $dir)) { 313cdf0e10cSrcweir my @prj_dir_list = readdir(DIR); 314cdf0e10cSrcweir closedir (DIR); 315cdf0e10cSrcweir foreach $entry (@prj_dir_list) { 316cdf0e10cSrcweir $entry =~ /^\.$/ and next; 317cdf0e10cSrcweir $entry =~ /^\.\.$/ and next; 318cdf0e10cSrcweir 319cdf0e10cSrcweir $entry = $dir . '/' . $entry; 320cdf0e10cSrcweir # if $enry is a dir - read all its files, 321cdf0e10cSrcweir # otherwise store $entry itself 322cdf0e10cSrcweir if (-d $entry) { 323cdf0e10cSrcweir &get_dir_content($entry, $dir_hash_ref); 324cdf0e10cSrcweir } else { 325cdf0e10cSrcweir $$dir_hash_ref{$entry}++; 326cdf0e10cSrcweir }; 327cdf0e10cSrcweir }; 328cdf0e10cSrcweir }; 329cdf0e10cSrcweir return '1'; 330cdf0e10cSrcweir}; 331cdf0e10cSrcweir 332cdf0e10cSrcweirsub print_error { 333cdf0e10cSrcweir my $message = shift; 334cdf0e10cSrcweir print STDERR "\nERROR: $message\n"; 335cdf0e10cSrcweir exit (1); 336cdf0e10cSrcweir}; 337cdf0e10cSrcweir 338cdf0e10cSrcweirsub usage { 339cdf0e10cSrcweir print STDERR " zipdep [-aABcdDeEfFghjklLmoqrRSTuvVwXyz] [-b path]\n"; 340cdf0e10cSrcweir print STDERR " [-n suffixes] [-t mmddyyyy] [-tt mmddyyyy] [ zipfile [\n"; 341cdf0e10cSrcweir print STDERR " file1 file2 ...]] [-xi list]\n"; 342cdf0e10cSrcweir} 343cdf0e10cSrcweir 344