xref: /aoo41x/main/solenv/bin/zipdep.pl (revision 7e90fac2)
1cdf0e10cSrcweir:
2cdf0e10cSrcweireval 'exec perl -wS $0 ${1+"$@"}'
3cdf0e10cSrcweir    if 0;
4*7e90fac2SAndrew Rist#**************************************************************
5*7e90fac2SAndrew Rist#
6*7e90fac2SAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
7*7e90fac2SAndrew Rist#  or more contributor license agreements.  See the NOTICE file
8*7e90fac2SAndrew Rist#  distributed with this work for additional information
9*7e90fac2SAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
10*7e90fac2SAndrew Rist#  to you under the Apache License, Version 2.0 (the
11*7e90fac2SAndrew Rist#  "License"); you may not use this file except in compliance
12*7e90fac2SAndrew Rist#  with the License.  You may obtain a copy of the License at
13*7e90fac2SAndrew Rist#
14*7e90fac2SAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
15*7e90fac2SAndrew Rist#
16*7e90fac2SAndrew Rist#  Unless required by applicable law or agreed to in writing,
17*7e90fac2SAndrew Rist#  software distributed under the License is distributed on an
18*7e90fac2SAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19*7e90fac2SAndrew Rist#  KIND, either express or implied.  See the License for the
20*7e90fac2SAndrew Rist#  specific language governing permissions and limitations
21*7e90fac2SAndrew Rist#  under the License.
22*7e90fac2SAndrew Rist#
23*7e90fac2SAndrew Rist#**************************************************************
24*7e90fac2SAndrew Rist
25*7e90fac2SAndrew 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;
98cdf0e10cSrcweir	return $path;
99cdf0e10cSrcweir};
100cdf0e10cSrcweir
101cdf0e10cSrcweir#
102cdf0e10cSrcweir# convert slashes to internal perl representation
103cdf0e10cSrcweir#
104cdf0e10cSrcweirsub perled_slashes {
105cdf0e10cSrcweir	my $path = shift;
106cdf0e10cSrcweir	$path =~ s/\\/\//g;
107cdf0e10cSrcweir	$path =~ s/\/+/\//g;
108cdf0e10cSrcweir	return $path;
109cdf0e10cSrcweir};
110cdf0e10cSrcweir
111cdf0e10cSrcweir#
112cdf0e10cSrcweir# Collect all files to zip in @patterns_array array
113cdf0e10cSrcweir#
114cdf0e10cSrcweirsub get_zip_content {
115cdf0e10cSrcweir	&get_zip_entries(\@given_patterns);
116cdf0e10cSrcweir	my $file_name = '';
117cdf0e10cSrcweir	foreach $file_name (keys %files_in_arch) {
118cdf0e10cSrcweir		if (-d $file_name) {
119cdf0e10cSrcweir			&get_dir_content($file_name, \%files_in_arch) if ($r || $R);
120cdf0e10cSrcweir			undef $files_in_arch{$file_name};
121cdf0e10cSrcweir		};
122cdf0e10cSrcweir	};
123cdf0e10cSrcweir	&remove_uncompliant(\@given_patterns) if ($R);
124cdf0e10cSrcweir	&get_patterns_files(\@exc_patterns, \%exc_files_hash) if ($exclude);
125cdf0e10cSrcweir	&get_patterns_files(\@inc_patterns, \%inc_files_hash) if ($include);
126cdf0e10cSrcweir	foreach my $file_name (keys %exc_files_hash) {
127cdf0e10cSrcweir		if (defined $files_in_arch{$file_name}) {
128cdf0e10cSrcweir			delete $files_in_arch{$file_name};
129cdf0e10cSrcweir			#print STDERR "excluded $file_name\n";
130cdf0e10cSrcweir		};
131cdf0e10cSrcweir	};
132cdf0e10cSrcweir	if ($include) {
133cdf0e10cSrcweir		foreach my $file_name (keys %files_in_arch) {
134cdf0e10cSrcweir			if (!(defined $inc_files_hash{$file_name})) {
135cdf0e10cSrcweir				delete $files_in_arch{$file_name};
136cdf0e10cSrcweir			};
137cdf0e10cSrcweir		};
138cdf0e10cSrcweir	}
139cdf0e10cSrcweir};
140cdf0e10cSrcweir
141cdf0e10cSrcweir#
142cdf0e10cSrcweir# Procedure removes from %files_in_arch all files which
143cdf0e10cSrcweir# are not compliant to patterns in @given_patterns
144cdf0e10cSrcweir#
145cdf0e10cSrcweirsub remove_uncompliant {
146cdf0e10cSrcweir	my $given_patterns = shift;
147cdf0e10cSrcweir	my @reg_exps = ();
148cdf0e10cSrcweir	my $pattern = '';
149cdf0e10cSrcweir	foreach $pattern (@$given_patterns) {
150cdf0e10cSrcweir		push(@reg_exps, &make_reg_exp($pattern));
151cdf0e10cSrcweir	};
152cdf0e10cSrcweir	# write file name as a value for the path(key)
153cdf0e10cSrcweir	foreach my $file (keys %files_in_arch) {
154cdf0e10cSrcweir		next if (-d $file);
155cdf0e10cSrcweir		#print "$file\n";
156cdf0e10cSrcweir		if ($file =~ /[\\ | \/](.+)$/) {
157cdf0e10cSrcweir			$files_in_arch{$file} = $1;
158cdf0e10cSrcweir		} else {
159cdf0e10cSrcweir			$files_in_arch{$file} = $file;
160cdf0e10cSrcweir		};
161cdf0e10cSrcweir	};
162cdf0e10cSrcweir	foreach $pattern (@reg_exps) {
163cdf0e10cSrcweir		foreach my $file (keys %files_in_arch) {
164cdf0e10cSrcweir			if (!($files_in_arch{$file} =~ /$pattern/)) {
165cdf0e10cSrcweir				delete $files_in_arch{$file};
166cdf0e10cSrcweir			#} else {
167cdf0e10cSrcweir			#	print "Complient: $file\n";
168cdf0e10cSrcweir			};
169cdf0e10cSrcweir		};
170cdf0e10cSrcweir	};
171cdf0e10cSrcweir};
172cdf0e10cSrcweir
173cdf0e10cSrcweir#
174cdf0e10cSrcweir# Procedure adds/removes to/from %files_in_arch all files, that are
175cdf0e10cSrcweir# compliant to the patterns in array passed
176cdf0e10cSrcweir#
177cdf0e10cSrcweirsub get_zip_entries {
178cdf0e10cSrcweir	if ($R) {
179cdf0e10cSrcweir		opendir DIR, '.';
180cdf0e10cSrcweir		my @dir_content = readdir(DIR);
181cdf0e10cSrcweir		close DIR;
182cdf0e10cSrcweir		foreach my $file_name(@dir_content) {
183cdf0e10cSrcweir			$file_name =~ /^\.$/ and next;
184cdf0e10cSrcweir			$file_name =~ /^\.\.$/ and next;
185cdf0e10cSrcweir			$files_in_arch{$file_name}++;
186cdf0e10cSrcweir			#print "included $file_name\n";
187cdf0e10cSrcweir		};
188cdf0e10cSrcweir	} else {
189cdf0e10cSrcweir		my $patterns_array = shift;
190cdf0e10cSrcweir		my $pattern = '';
191cdf0e10cSrcweir		foreach $pattern (@$patterns_array) {
192cdf0e10cSrcweir			if ((-d $pattern) || (-f $pattern)) {
193cdf0e10cSrcweir				$files_in_arch{$pattern}++;
194cdf0e10cSrcweir				next;
195cdf0e10cSrcweir			}
196cdf0e10cSrcweir			my $file_name = '';
197cdf0e10cSrcweir			foreach $file_name (glob $pattern) {
198cdf0e10cSrcweir				#next if (!(-d $file_name) || !(-f $file_name));
199cdf0e10cSrcweir				$files_in_arch{$file_name}++;
200cdf0e10cSrcweir			};
201cdf0e10cSrcweir		};
202cdf0e10cSrcweir	}
203cdf0e10cSrcweir};
204cdf0e10cSrcweir
205cdf0e10cSrcweir#
206cdf0e10cSrcweir# Procedure converts given parameter to a regular expression
207cdf0e10cSrcweir#
208cdf0e10cSrcweirsub make_reg_exp {
209cdf0e10cSrcweir	my $arg = shift;
210cdf0e10cSrcweir	$arg =~ s/\\/\\\\/g;
211cdf0e10cSrcweir	$arg =~ s/\//\\\//g;
212cdf0e10cSrcweir	$arg =~ s/\./\\\./g;
213cdf0e10cSrcweir	$arg =~ s/\+/\\\+/g;
214cdf0e10cSrcweir	$arg =~ s/\{/\\\{/g;
215cdf0e10cSrcweir	$arg =~ s/\}/\\\}/g;
216cdf0e10cSrcweir	$arg =~ s/\*/\.\*/g;
217cdf0e10cSrcweir	$arg =~ s/\?/\./g;
218cdf0e10cSrcweir	#$arg = '/'.$arg.'/';
219cdf0e10cSrcweir	#print "Regular expression:	$arg\n";
220cdf0e10cSrcweir	return $arg;
221cdf0e10cSrcweir};
222cdf0e10cSrcweir
223cdf0e10cSrcweir#
224cdf0e10cSrcweir# Procedure retrieves shell pattern and converts them into regular expressions
225cdf0e10cSrcweir#
226cdf0e10cSrcweirsub get_patterns {
227cdf0e10cSrcweir	my $patterns = shift;
228cdf0e10cSrcweir	my $arg = '';
229cdf0e10cSrcweir	while ($arg = shift @ARGV) {
230cdf0e10cSrcweir		$arg =~ /^-/	and unshift(@ARGV, $arg) and return;
231cdf0e10cSrcweir		if (!$zip_file) {
232cdf0e10cSrcweir			$zip_file = $arg;
233cdf0e10cSrcweir			next;
234cdf0e10cSrcweir		};
235cdf0e10cSrcweir		$arg = &make_reg_exp($arg);
236cdf0e10cSrcweir		push(@$patterns, $arg);
237cdf0e10cSrcweir	};
238cdf0e10cSrcweir};
239cdf0e10cSrcweir
240cdf0e10cSrcweir#
241cdf0e10cSrcweir# Get all options passed
242cdf0e10cSrcweir#
243cdf0e10cSrcweirsub get_options {
244cdf0e10cSrcweir	my ($arg);
245cdf0e10cSrcweir	&usage() && exit(0) if ($#ARGV == -1);
246cdf0e10cSrcweir	while ($arg = shift @ARGV) {
247cdf0e10cSrcweir		$arg = &perled_slashes($arg);
248cdf0e10cSrcweir		#print STDERR "$arg\n";
249cdf0e10cSrcweir		$arg =~ /^-R$/			and $R = 1	and next;
250cdf0e10cSrcweir		$arg =~ /^-r$/			and $r = 1	and next;
251cdf0e10cSrcweir		$arg =~ /^-x$/			and $exclude = 1 and &get_patterns(\@exc_patterns) and next;
252cdf0e10cSrcweir		$arg =~ /^-i$/			and $include = 1 and &get_patterns(\@inc_patterns) and next;
253cdf0e10cSrcweir		$arg =~ /^-prefix$/		and $prefix = shift @ARGV					and next;
254cdf0e10cSrcweir		$arg =~ /^-b$/			and shift @ARGV					and next;
255cdf0e10cSrcweir		$arg =~ /^-n$/			and shift @ARGV					and next;
256cdf0e10cSrcweir		$arg =~ /^-t$/			and shift @ARGV					and next;
257cdf0e10cSrcweir		$arg =~ /^-tt$/			and shift @ARGV					and next;
258cdf0e10cSrcweir		$arg =~ /^-h$/			and &usage						and exit(0);
259cdf0e10cSrcweir		$arg =~ /^--help$/		and &usage						and exit(0);
260cdf0e10cSrcweir		$arg =~ /^-?$/			and &usage						and exit(0);
261cdf0e10cSrcweir		if ($arg =~ /^-(\w)(\w+)$/) {
262cdf0e10cSrcweir			unshift (@ARGV, '-'.$1);
263cdf0e10cSrcweir			unshift (@ARGV, '-'.$2);
264cdf0e10cSrcweir			next;
265cdf0e10cSrcweir		};
266cdf0e10cSrcweir# just ignore other switches...
267cdf0e10cSrcweir		$arg =~ /^-(\w+)$/		and	next;
268cdf0e10cSrcweir		$arg =~ /^\/\?$/			and &usage						and exit(0);
269cdf0e10cSrcweir		$zip_file = $arg		and next if (!$zip_file);
270cdf0e10cSrcweir		push(@given_patterns, $arg);
271cdf0e10cSrcweir	};
272cdf0e10cSrcweir	&print_error('error: Invalid command arguments (do not specify both -r and -R)') if ($r && $R);
273cdf0e10cSrcweir	if ($r && ($#given_patterns == -1)) {
274cdf0e10cSrcweir		&print_error('no list specified');
275cdf0e10cSrcweir	};
276cdf0e10cSrcweir};
277cdf0e10cSrcweir
278cdf0e10cSrcweir#
279cdf0e10cSrcweir# Procedure fills out passed hash with files from passed dir
280cdf0e10cSrcweir# compliant to the pattern from @$patterns
281cdf0e10cSrcweir#
282cdf0e10cSrcweirsub get_patterns_files {
283cdf0e10cSrcweir	my $patterns_array = shift;
284cdf0e10cSrcweir	my $files_hash = shift;
285cdf0e10cSrcweir	my @zip_files = keys %files_in_arch;
286cdf0e10cSrcweir	foreach my $pattern (@$patterns_array) {
287cdf0e10cSrcweir		my @fit_pattern = grep /$pattern/, @zip_files;
288cdf0e10cSrcweir		foreach my $entry (@fit_pattern) {
289cdf0e10cSrcweir			$$files_hash{$entry}++;
290cdf0e10cSrcweir			#print "$entry\n";
291cdf0e10cSrcweir		};
292cdf0e10cSrcweir	};
293cdf0e10cSrcweir};
294cdf0e10cSrcweir
295cdf0e10cSrcweir#
296cdf0e10cSrcweir# Get dir stuff to pack
297cdf0e10cSrcweir#
298cdf0e10cSrcweirsub get_dir_content {
299cdf0e10cSrcweir	my $dir = shift;
300cdf0e10cSrcweir	my $dir_hash_ref = shift;
301cdf0e10cSrcweir	my $entry = '';
302cdf0e10cSrcweir	if (opendir(DIR, $dir)) {
303cdf0e10cSrcweir		my @prj_dir_list = readdir(DIR);
304cdf0e10cSrcweir		closedir (DIR);
305cdf0e10cSrcweir		foreach $entry (@prj_dir_list) {
306cdf0e10cSrcweir			$entry =~ /^\.$/ and next;
307cdf0e10cSrcweir			$entry =~ /^\.\.$/ and next;
308cdf0e10cSrcweir
309cdf0e10cSrcweir			$entry = $dir . '/' . $entry;
310cdf0e10cSrcweir			# if $enry is a dir - read all its files,
311cdf0e10cSrcweir			# otherwise store $entry itself
312cdf0e10cSrcweir			if (-d $entry) {
313cdf0e10cSrcweir				&get_dir_content($entry, $dir_hash_ref);
314cdf0e10cSrcweir			} else {
315cdf0e10cSrcweir				$$dir_hash_ref{$entry}++;
316cdf0e10cSrcweir			};
317cdf0e10cSrcweir		};
318cdf0e10cSrcweir	};
319cdf0e10cSrcweir	return '1';
320cdf0e10cSrcweir};
321cdf0e10cSrcweir
322cdf0e10cSrcweirsub print_error {
323cdf0e10cSrcweir    my $message = shift;
324cdf0e10cSrcweir    print STDERR "\nERROR: $message\n";
325cdf0e10cSrcweir	exit (1);
326cdf0e10cSrcweir};
327cdf0e10cSrcweir
328cdf0e10cSrcweirsub usage {
329cdf0e10cSrcweir	print STDERR "      zipdep  [-aABcdDeEfFghjklLmoqrRSTuvVwXyz]     [-b path]\n";
330cdf0e10cSrcweir	print STDERR "      [-n suffixes]  [-t mmddyyyy]  [-tt mmddyyyy]  [  zipfile [\n";
331cdf0e10cSrcweir	print STDERR "      file1 file2 ...]] [-xi list]\n";
332cdf0e10cSrcweir}
333cdf0e10cSrcweir
334