xref: /aoo41x/main/solenv/bin/zipdep.pl (revision cdf0e10c)
1*cdf0e10cSrcweir:
2*cdf0e10cSrcweireval 'exec perl -wS $0 ${1+"$@"}'
3*cdf0e10cSrcweir    if 0;
4*cdf0e10cSrcweir#*************************************************************************
5*cdf0e10cSrcweir#
6*cdf0e10cSrcweir# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7*cdf0e10cSrcweir#
8*cdf0e10cSrcweir# Copyright 2000, 2010 Oracle and/or its affiliates.
9*cdf0e10cSrcweir#
10*cdf0e10cSrcweir# OpenOffice.org - a multi-platform office productivity suite
11*cdf0e10cSrcweir#
12*cdf0e10cSrcweir# This file is part of OpenOffice.org.
13*cdf0e10cSrcweir#
14*cdf0e10cSrcweir# OpenOffice.org is free software: you can redistribute it and/or modify
15*cdf0e10cSrcweir# it under the terms of the GNU Lesser General Public License version 3
16*cdf0e10cSrcweir# only, as published by the Free Software Foundation.
17*cdf0e10cSrcweir#
18*cdf0e10cSrcweir# OpenOffice.org is distributed in the hope that it will be useful,
19*cdf0e10cSrcweir# but WITHOUT ANY WARRANTY; without even the implied warranty of
20*cdf0e10cSrcweir# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21*cdf0e10cSrcweir# GNU Lesser General Public License version 3 for more details
22*cdf0e10cSrcweir# (a copy is included in the LICENSE file that accompanied this code).
23*cdf0e10cSrcweir#
24*cdf0e10cSrcweir# You should have received a copy of the GNU Lesser General Public License
25*cdf0e10cSrcweir# version 3 along with OpenOffice.org.  If not, see
26*cdf0e10cSrcweir# <http://www.openoffice.org/license.html>
27*cdf0e10cSrcweir# for a copy of the LGPLv3 License.
28*cdf0e10cSrcweir#
29*cdf0e10cSrcweir#*************************************************************************
30*cdf0e10cSrcweir
31*cdf0e10cSrcweir#
32*cdf0e10cSrcweir# mapgen  - generate a dependencies file for zip commando
33*cdf0e10cSrcweir#
34*cdf0e10cSrcweiruse Cwd;
35*cdf0e10cSrcweir
36*cdf0e10cSrcweir#### script id #####
37*cdf0e10cSrcweir
38*cdf0e10cSrcweir( $script_name = $0 ) =~ s/^.*\b(\w+)\.pl$/$1/;
39*cdf0e10cSrcweir
40*cdf0e10cSrcweir$id_str = ' $Revision: 1.12 $ ';
41*cdf0e10cSrcweir$id_str =~ /Revision:\s+(\S+)\s+\$/
42*cdf0e10cSrcweir  ? ($script_rev = $1) : ($script_rev = "-");
43*cdf0e10cSrcweir
44*cdf0e10cSrcweirprint STDERR "$script_name -- version: $script_rev\n";
45*cdf0e10cSrcweirprint STDERR "Multi Platform Enabled Edition\n";
46*cdf0e10cSrcweir
47*cdf0e10cSrcweir#########################
48*cdf0e10cSrcweir#                       #
49*cdf0e10cSrcweir#   Globale Variablen   #
50*cdf0e10cSrcweir#                       #
51*cdf0e10cSrcweir#########################
52*cdf0e10cSrcweir
53*cdf0e10cSrcweir$zip_file = '';
54*cdf0e10cSrcweir$R = '';
55*cdf0e10cSrcweir$r = '';
56*cdf0e10cSrcweir$exclude = '';
57*cdf0e10cSrcweir$include = '';
58*cdf0e10cSrcweir@given_patterns = ();	# patterns(files) list from command line
59*cdf0e10cSrcweir%files_in_arch = ();
60*cdf0e10cSrcweir@exc_patterns = (); 	# array of all patterns for files to be excluded
61*cdf0e10cSrcweir@inc_patterns = (); 	# array of all patterns for files to be included
62*cdf0e10cSrcweir%exc_files_hash = ();	# hash of files to be excluded (according to @exc_patterns)
63*cdf0e10cSrcweir%inc_files_hash = ();	# hash of files to be included (according to @inc_patterns)
64*cdf0e10cSrcweir$prefix = '';
65*cdf0e10cSrcweir
66*cdf0e10cSrcweir#### main ####
67*cdf0e10cSrcweir
68*cdf0e10cSrcweir&get_options;
69*cdf0e10cSrcweir&get_zip_content;
70*cdf0e10cSrcweir&write_zip_file;
71*cdf0e10cSrcweir
72*cdf0e10cSrcweir#### end of main procedure ####
73*cdf0e10cSrcweir
74*cdf0e10cSrcweir#########################
75*cdf0e10cSrcweir#                       #
76*cdf0e10cSrcweir#      Procedures       #
77*cdf0e10cSrcweir#                       #
78*cdf0e10cSrcweir#########################
79*cdf0e10cSrcweir
80*cdf0e10cSrcweir#
81*cdf0e10cSrcweir# procedure writes zipdep file
82*cdf0e10cSrcweir#
83*cdf0e10cSrcweirsub write_zip_file {
84*cdf0e10cSrcweir	my @dependencies = keys %files_in_arch;
85*cdf0e10cSrcweir	if ($#dependencies != -1) {
86*cdf0e10cSrcweir		print "\n". &convert_slashes($zip_file) . ' :';
87*cdf0e10cSrcweir		foreach (@dependencies) {
88*cdf0e10cSrcweir			next if (-d);
89*cdf0e10cSrcweir			print " \\\n\t" . $prefix . &convert_slashes($_);
90*cdf0e10cSrcweir		};
91*cdf0e10cSrcweir		print "\n\n";
92*cdf0e10cSrcweir	};
93*cdf0e10cSrcweir};
94*cdf0e10cSrcweir
95*cdf0e10cSrcweir#
96*cdf0e10cSrcweir# convert slashes
97*cdf0e10cSrcweir#
98*cdf0e10cSrcweirsub convert_slashes {
99*cdf0e10cSrcweir	my $path = shift;
100*cdf0e10cSrcweir	$path =~ s/\//\$\//g;
101*cdf0e10cSrcweir	$path =~ s/\\/\$\//g;
102*cdf0e10cSrcweir	return $path;
103*cdf0e10cSrcweir};
104*cdf0e10cSrcweir
105*cdf0e10cSrcweir#
106*cdf0e10cSrcweir# convert slashes to internal perl representation
107*cdf0e10cSrcweir#
108*cdf0e10cSrcweirsub perled_slashes {
109*cdf0e10cSrcweir	my $path = shift;
110*cdf0e10cSrcweir	$path =~ s/\\/\//g;
111*cdf0e10cSrcweir	$path =~ s/\/+/\//g;
112*cdf0e10cSrcweir	return $path;
113*cdf0e10cSrcweir};
114*cdf0e10cSrcweir
115*cdf0e10cSrcweir#
116*cdf0e10cSrcweir# Collect all files to zip in @patterns_array array
117*cdf0e10cSrcweir#
118*cdf0e10cSrcweirsub get_zip_content {
119*cdf0e10cSrcweir	&get_zip_entries(\@given_patterns);
120*cdf0e10cSrcweir	my $file_name = '';
121*cdf0e10cSrcweir	foreach $file_name (keys %files_in_arch) {
122*cdf0e10cSrcweir		if (-d $file_name) {
123*cdf0e10cSrcweir			&get_dir_content($file_name, \%files_in_arch) if ($r || $R);
124*cdf0e10cSrcweir			undef $files_in_arch{$file_name};
125*cdf0e10cSrcweir		};
126*cdf0e10cSrcweir	};
127*cdf0e10cSrcweir	&remove_uncompliant(\@given_patterns) if ($R);
128*cdf0e10cSrcweir	&get_patterns_files(\@exc_patterns, \%exc_files_hash) if ($exclude);
129*cdf0e10cSrcweir	&get_patterns_files(\@inc_patterns, \%inc_files_hash) if ($include);
130*cdf0e10cSrcweir	foreach my $file_name (keys %exc_files_hash) {
131*cdf0e10cSrcweir		if (defined $files_in_arch{$file_name}) {
132*cdf0e10cSrcweir			delete $files_in_arch{$file_name};
133*cdf0e10cSrcweir			#print STDERR "excluded $file_name\n";
134*cdf0e10cSrcweir		};
135*cdf0e10cSrcweir	};
136*cdf0e10cSrcweir	if ($include) {
137*cdf0e10cSrcweir		foreach my $file_name (keys %files_in_arch) {
138*cdf0e10cSrcweir			if (!(defined $inc_files_hash{$file_name})) {
139*cdf0e10cSrcweir				delete $files_in_arch{$file_name};
140*cdf0e10cSrcweir			};
141*cdf0e10cSrcweir		};
142*cdf0e10cSrcweir	}
143*cdf0e10cSrcweir};
144*cdf0e10cSrcweir
145*cdf0e10cSrcweir#
146*cdf0e10cSrcweir# Procedure removes from %files_in_arch all files which
147*cdf0e10cSrcweir# are not compliant to patterns in @given_patterns
148*cdf0e10cSrcweir#
149*cdf0e10cSrcweirsub remove_uncompliant {
150*cdf0e10cSrcweir	my $given_patterns = shift;
151*cdf0e10cSrcweir	my @reg_exps = ();
152*cdf0e10cSrcweir	my $pattern = '';
153*cdf0e10cSrcweir	foreach $pattern (@$given_patterns) {
154*cdf0e10cSrcweir		push(@reg_exps, &make_reg_exp($pattern));
155*cdf0e10cSrcweir	};
156*cdf0e10cSrcweir	# write file name as a value for the path(key)
157*cdf0e10cSrcweir	foreach my $file (keys %files_in_arch) {
158*cdf0e10cSrcweir		next if (-d $file);
159*cdf0e10cSrcweir		#print "$file\n";
160*cdf0e10cSrcweir		if ($file =~ /[\\ | \/](.+)$/) {
161*cdf0e10cSrcweir			$files_in_arch{$file} = $1;
162*cdf0e10cSrcweir		} else {
163*cdf0e10cSrcweir			$files_in_arch{$file} = $file;
164*cdf0e10cSrcweir		};
165*cdf0e10cSrcweir	};
166*cdf0e10cSrcweir	foreach $pattern (@reg_exps) {
167*cdf0e10cSrcweir		foreach my $file (keys %files_in_arch) {
168*cdf0e10cSrcweir			if (!($files_in_arch{$file} =~ /$pattern/)) {
169*cdf0e10cSrcweir				delete $files_in_arch{$file};
170*cdf0e10cSrcweir			#} else {
171*cdf0e10cSrcweir			#	print "Complient: $file\n";
172*cdf0e10cSrcweir			};
173*cdf0e10cSrcweir		};
174*cdf0e10cSrcweir	};
175*cdf0e10cSrcweir};
176*cdf0e10cSrcweir
177*cdf0e10cSrcweir#
178*cdf0e10cSrcweir# Procedure adds/removes to/from %files_in_arch all files, that are
179*cdf0e10cSrcweir# compliant to the patterns in array passed
180*cdf0e10cSrcweir#
181*cdf0e10cSrcweirsub get_zip_entries {
182*cdf0e10cSrcweir	if ($R) {
183*cdf0e10cSrcweir		opendir DIR, '.';
184*cdf0e10cSrcweir		my @dir_content = readdir(DIR);
185*cdf0e10cSrcweir		close DIR;
186*cdf0e10cSrcweir		foreach my $file_name(@dir_content) {
187*cdf0e10cSrcweir			$file_name =~ /^\.$/ and next;
188*cdf0e10cSrcweir			$file_name =~ /^\.\.$/ and next;
189*cdf0e10cSrcweir			$files_in_arch{$file_name}++;
190*cdf0e10cSrcweir			#print "included $file_name\n";
191*cdf0e10cSrcweir		};
192*cdf0e10cSrcweir	} else {
193*cdf0e10cSrcweir		my $patterns_array = shift;
194*cdf0e10cSrcweir		my $pattern = '';
195*cdf0e10cSrcweir		foreach $pattern (@$patterns_array) {
196*cdf0e10cSrcweir			if ((-d $pattern) || (-f $pattern)) {
197*cdf0e10cSrcweir				$files_in_arch{$pattern}++;
198*cdf0e10cSrcweir				next;
199*cdf0e10cSrcweir			}
200*cdf0e10cSrcweir			my $file_name = '';
201*cdf0e10cSrcweir			foreach $file_name (glob $pattern) {
202*cdf0e10cSrcweir				#next if (!(-d $file_name) || !(-f $file_name));
203*cdf0e10cSrcweir				$files_in_arch{$file_name}++;
204*cdf0e10cSrcweir			};
205*cdf0e10cSrcweir		};
206*cdf0e10cSrcweir	}
207*cdf0e10cSrcweir};
208*cdf0e10cSrcweir
209*cdf0e10cSrcweir#
210*cdf0e10cSrcweir# Procedure converts given parameter to a regular expression
211*cdf0e10cSrcweir#
212*cdf0e10cSrcweirsub make_reg_exp {
213*cdf0e10cSrcweir	my $arg = shift;
214*cdf0e10cSrcweir	$arg =~ s/\\/\\\\/g;
215*cdf0e10cSrcweir	$arg =~ s/\//\\\//g;
216*cdf0e10cSrcweir	$arg =~ s/\./\\\./g;
217*cdf0e10cSrcweir	$arg =~ s/\+/\\\+/g;
218*cdf0e10cSrcweir	$arg =~ s/\{/\\\{/g;
219*cdf0e10cSrcweir	$arg =~ s/\}/\\\}/g;
220*cdf0e10cSrcweir	$arg =~ s/\*/\.\*/g;
221*cdf0e10cSrcweir	$arg =~ s/\?/\./g;
222*cdf0e10cSrcweir	#$arg = '/'.$arg.'/';
223*cdf0e10cSrcweir	#print "Regular expression:	$arg\n";
224*cdf0e10cSrcweir	return $arg;
225*cdf0e10cSrcweir};
226*cdf0e10cSrcweir
227*cdf0e10cSrcweir#
228*cdf0e10cSrcweir# Procedure retrieves shell pattern and converts them into regular expressions
229*cdf0e10cSrcweir#
230*cdf0e10cSrcweirsub get_patterns {
231*cdf0e10cSrcweir	my $patterns = shift;
232*cdf0e10cSrcweir	my $arg = '';
233*cdf0e10cSrcweir	while ($arg = shift @ARGV) {
234*cdf0e10cSrcweir		$arg =~ /^-/	and unshift(@ARGV, $arg) and return;
235*cdf0e10cSrcweir		if (!$zip_file) {
236*cdf0e10cSrcweir			$zip_file = $arg;
237*cdf0e10cSrcweir			next;
238*cdf0e10cSrcweir		};
239*cdf0e10cSrcweir		$arg = &make_reg_exp($arg);
240*cdf0e10cSrcweir		push(@$patterns, $arg);
241*cdf0e10cSrcweir	};
242*cdf0e10cSrcweir};
243*cdf0e10cSrcweir
244*cdf0e10cSrcweir#
245*cdf0e10cSrcweir# Get all options passed
246*cdf0e10cSrcweir#
247*cdf0e10cSrcweirsub get_options {
248*cdf0e10cSrcweir	my ($arg);
249*cdf0e10cSrcweir	&usage() && exit(0) if ($#ARGV == -1);
250*cdf0e10cSrcweir	while ($arg = shift @ARGV) {
251*cdf0e10cSrcweir		$arg = &perled_slashes($arg);
252*cdf0e10cSrcweir		#print STDERR "$arg\n";
253*cdf0e10cSrcweir		$arg =~ /^-R$/			and $R = 1	and next;
254*cdf0e10cSrcweir		$arg =~ /^-r$/			and $r = 1	and next;
255*cdf0e10cSrcweir		$arg =~ /^-x$/			and $exclude = 1 and &get_patterns(\@exc_patterns) and next;
256*cdf0e10cSrcweir		$arg =~ /^-i$/			and $include = 1 and &get_patterns(\@inc_patterns) and next;
257*cdf0e10cSrcweir		$arg =~ /^-prefix$/		and $prefix = shift @ARGV					and next;
258*cdf0e10cSrcweir		$arg =~ /^-b$/			and shift @ARGV					and next;
259*cdf0e10cSrcweir		$arg =~ /^-n$/			and shift @ARGV					and next;
260*cdf0e10cSrcweir		$arg =~ /^-t$/			and shift @ARGV					and next;
261*cdf0e10cSrcweir		$arg =~ /^-tt$/			and shift @ARGV					and next;
262*cdf0e10cSrcweir		$arg =~ /^-h$/			and &usage						and exit(0);
263*cdf0e10cSrcweir		$arg =~ /^--help$/		and &usage						and exit(0);
264*cdf0e10cSrcweir		$arg =~ /^-?$/			and &usage						and exit(0);
265*cdf0e10cSrcweir		if ($arg =~ /^-(\w)(\w+)$/) {
266*cdf0e10cSrcweir			unshift (@ARGV, '-'.$1);
267*cdf0e10cSrcweir			unshift (@ARGV, '-'.$2);
268*cdf0e10cSrcweir			next;
269*cdf0e10cSrcweir		};
270*cdf0e10cSrcweir# just ignore other switches...
271*cdf0e10cSrcweir		$arg =~ /^-(\w+)$/		and	next;
272*cdf0e10cSrcweir		$arg =~ /^\/\?$/			and &usage						and exit(0);
273*cdf0e10cSrcweir		$zip_file = $arg		and next if (!$zip_file);
274*cdf0e10cSrcweir		push(@given_patterns, $arg);
275*cdf0e10cSrcweir	};
276*cdf0e10cSrcweir	&print_error('error: Invalid command arguments (do not specify both -r and -R)') if ($r && $R);
277*cdf0e10cSrcweir	if ($r && ($#given_patterns == -1)) {
278*cdf0e10cSrcweir		&print_error('no list specified');
279*cdf0e10cSrcweir	};
280*cdf0e10cSrcweir};
281*cdf0e10cSrcweir
282*cdf0e10cSrcweir#
283*cdf0e10cSrcweir# Procedure fills out passed hash with files from passed dir
284*cdf0e10cSrcweir# compliant to the pattern from @$patterns
285*cdf0e10cSrcweir#
286*cdf0e10cSrcweirsub get_patterns_files {
287*cdf0e10cSrcweir	my $patterns_array = shift;
288*cdf0e10cSrcweir	my $files_hash = shift;
289*cdf0e10cSrcweir	my @zip_files = keys %files_in_arch;
290*cdf0e10cSrcweir	foreach my $pattern (@$patterns_array) {
291*cdf0e10cSrcweir		my @fit_pattern = grep /$pattern/, @zip_files;
292*cdf0e10cSrcweir		foreach my $entry (@fit_pattern) {
293*cdf0e10cSrcweir			$$files_hash{$entry}++;
294*cdf0e10cSrcweir			#print "$entry\n";
295*cdf0e10cSrcweir		};
296*cdf0e10cSrcweir	};
297*cdf0e10cSrcweir};
298*cdf0e10cSrcweir
299*cdf0e10cSrcweir#
300*cdf0e10cSrcweir# Get dir stuff to pack
301*cdf0e10cSrcweir#
302*cdf0e10cSrcweirsub get_dir_content {
303*cdf0e10cSrcweir	my $dir = shift;
304*cdf0e10cSrcweir	my $dir_hash_ref = shift;
305*cdf0e10cSrcweir	my $entry = '';
306*cdf0e10cSrcweir	if (opendir(DIR, $dir)) {
307*cdf0e10cSrcweir		my @prj_dir_list = readdir(DIR);
308*cdf0e10cSrcweir		closedir (DIR);
309*cdf0e10cSrcweir		foreach $entry (@prj_dir_list) {
310*cdf0e10cSrcweir			$entry =~ /^\.$/ and next;
311*cdf0e10cSrcweir			$entry =~ /^\.\.$/ and next;
312*cdf0e10cSrcweir
313*cdf0e10cSrcweir			$entry = $dir . '/' . $entry;
314*cdf0e10cSrcweir			# if $enry is a dir - read all its files,
315*cdf0e10cSrcweir			# otherwise store $entry itself
316*cdf0e10cSrcweir			if (-d $entry) {
317*cdf0e10cSrcweir				&get_dir_content($entry, $dir_hash_ref);
318*cdf0e10cSrcweir			} else {
319*cdf0e10cSrcweir				$$dir_hash_ref{$entry}++;
320*cdf0e10cSrcweir			};
321*cdf0e10cSrcweir		};
322*cdf0e10cSrcweir	};
323*cdf0e10cSrcweir	return '1';
324*cdf0e10cSrcweir};
325*cdf0e10cSrcweir
326*cdf0e10cSrcweirsub print_error {
327*cdf0e10cSrcweir    my $message = shift;
328*cdf0e10cSrcweir    print STDERR "\nERROR: $message\n";
329*cdf0e10cSrcweir	exit (1);
330*cdf0e10cSrcweir};
331*cdf0e10cSrcweir
332*cdf0e10cSrcweirsub usage {
333*cdf0e10cSrcweir	print STDERR "      zipdep  [-aABcdDeEfFghjklLmoqrRSTuvVwXyz]     [-b path]\n";
334*cdf0e10cSrcweir	print STDERR "      [-n suffixes]  [-t mmddyyyy]  [-tt mmddyyyy]  [  zipfile [\n";
335*cdf0e10cSrcweir	print STDERR "      file1 file2 ...]] [-xi list]\n";
336*cdf0e10cSrcweir}
337*cdf0e10cSrcweir
338